Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14 | #ifdef __APPLE__ |
| 15 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 16 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 17 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 18 | * at the end of this file for more information. |
| 19 | */ |
| 20 | # pragma weak lchown |
| 21 | # pragma weak statvfs |
| 22 | # pragma weak fstatvfs |
| 23 | |
| 24 | #endif /* __APPLE__ */ |
| 25 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 26 | #define PY_SSIZE_T_CLEAN |
| 27 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 28 | #include "Python.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 30 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 31 | # error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #endif /* defined(__VMS) */ |
| 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 39 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 40 | "This module provides access to operating system functionality that is\n\ |
| 41 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 42 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 43 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 46 | #if defined(PYOS_OS2) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 47 | #error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #define INCL_DOS |
| 49 | #define INCL_DOSERRORS |
| 50 | #define INCL_DOSPROCESS |
| 51 | #define INCL_NOPMAPI |
| 52 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 53 | #if defined(PYCC_GCC) |
| 54 | #include <ctype.h> |
| 55 | #include <io.h> |
| 56 | #include <stdio.h> |
| 57 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 58 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 59 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 62 | #ifdef HAVE_SYS_UIO_H |
| 63 | #include <sys/uio.h> |
| 64 | #endif |
| 65 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #endif /* HAVE_SYS_TYPES_H */ |
| 69 | |
| 70 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 75 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 79 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | #ifdef HAVE_FCNTL_H |
| 83 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 84 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 86 | #ifdef HAVE_GRP_H |
| 87 | #include <grp.h> |
| 88 | #endif |
| 89 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 90 | #ifdef HAVE_SYSEXITS_H |
| 91 | #include <sysexits.h> |
| 92 | #endif /* HAVE_SYSEXITS_H */ |
| 93 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 94 | #ifdef HAVE_SYS_LOADAVG_H |
| 95 | #include <sys/loadavg.h> |
| 96 | #endif |
| 97 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 98 | #ifdef HAVE_LANGINFO_H |
| 99 | #include <langinfo.h> |
| 100 | #endif |
| 101 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 102 | #ifdef HAVE_SYS_SENDFILE_H |
| 103 | #include <sys/sendfile.h> |
| 104 | #endif |
| 105 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 106 | #ifdef HAVE_SCHED_H |
| 107 | #include <sched.h> |
| 108 | #endif |
| 109 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 110 | #ifdef HAVE_ATTR_XATTR_H |
| 111 | #include <attr/xattr.h> |
| 112 | #endif |
| 113 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 114 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 115 | #ifdef HAVE_SYS_SOCKET_H |
| 116 | #include <sys/socket.h> |
| 117 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 118 | #endif |
| 119 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 120 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 121 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 122 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 123 | #include <process.h> |
| 124 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 125 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 126 | #define HAVE_GETCWD 1 |
| 127 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 128 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 129 | #if defined(__OS2__) |
| 130 | #define HAVE_EXECV 1 |
| 131 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 132 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 133 | #include <process.h> |
| 134 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 135 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 136 | #define HAVE_EXECV 1 |
| 137 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 138 | #define HAVE_OPENDIR 1 |
| 139 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 140 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #define HAVE_WAIT 1 |
| 142 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 143 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 144 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 145 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 146 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 147 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 148 | #define HAVE_EXECV 1 |
| 149 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
| 151 | #define HAVE_CWAIT 1 |
| 152 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 153 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 154 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 155 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 156 | /* 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] | 157 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 158 | /* Unix functions that the configure script doesn't check for */ |
| 159 | #define HAVE_EXECV 1 |
| 160 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 161 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 162 | #define HAVE_FORK1 1 |
| 163 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 164 | #define HAVE_GETCWD 1 |
| 165 | #define HAVE_GETEGID 1 |
| 166 | #define HAVE_GETEUID 1 |
| 167 | #define HAVE_GETGID 1 |
| 168 | #define HAVE_GETPPID 1 |
| 169 | #define HAVE_GETUID 1 |
| 170 | #define HAVE_KILL 1 |
| 171 | #define HAVE_OPENDIR 1 |
| 172 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 173 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 174 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 175 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 176 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 177 | #endif /* _MSC_VER */ |
| 178 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 179 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 180 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 182 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 183 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 184 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 185 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 186 | (default) */ |
| 187 | extern char *ctermid_r(char *); |
| 188 | #endif |
| 189 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 190 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 191 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 192 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 193 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 194 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 196 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 197 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 198 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 199 | #endif |
| 200 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 201 | extern int chdir(char *); |
| 202 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 203 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int chdir(const char *); |
| 205 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 207 | #ifdef __BORLANDC__ |
| 208 | extern int chmod(const char *, int); |
| 209 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 211 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 212 | /*#ifdef HAVE_FCHMOD |
| 213 | extern int fchmod(int, mode_t); |
| 214 | #endif*/ |
| 215 | /*#ifdef HAVE_LCHMOD |
| 216 | extern int lchmod(const char *, mode_t); |
| 217 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 218 | extern int chown(const char *, uid_t, gid_t); |
| 219 | extern char *getcwd(char *, int); |
| 220 | extern char *strerror(int); |
| 221 | extern int link(const char *, const char *); |
| 222 | extern int rename(const char *, const char *); |
| 223 | extern int stat(const char *, struct stat *); |
| 224 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 226 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 227 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 229 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 230 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 233 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #ifdef HAVE_UTIME_H |
| 236 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 237 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 239 | #ifdef HAVE_SYS_UTIME_H |
| 240 | #include <sys/utime.h> |
| 241 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 242 | #endif /* HAVE_SYS_UTIME_H */ |
| 243 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | #ifdef HAVE_SYS_TIMES_H |
| 245 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 246 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
| 248 | #ifdef HAVE_SYS_PARAM_H |
| 249 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 250 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | |
| 252 | #ifdef HAVE_SYS_UTSNAME_H |
| 253 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 254 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 256 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 258 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 259 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 260 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 261 | #include <direct.h> |
| 262 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 263 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 265 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 266 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 267 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 269 | #endif |
| 270 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 272 | #endif |
| 273 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 275 | #endif |
| 276 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 277 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 278 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 279 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 281 | #endif |
| 282 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 283 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 284 | #endif |
| 285 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 287 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 288 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 289 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 290 | #endif |
| 291 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 292 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 293 | #endif |
| 294 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 295 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 296 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 297 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 298 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 299 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 300 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 301 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 302 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 303 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 304 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 305 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 306 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 308 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 309 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 310 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 312 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 313 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 314 | #define MAXPATHLEN PATH_MAX |
| 315 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 317 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 318 | #endif /* MAXPATHLEN */ |
| 319 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 320 | #ifdef UNION_WAIT |
| 321 | /* Emulate some macros on systems that have a union instead of macros */ |
| 322 | |
| 323 | #ifndef WIFEXITED |
| 324 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 325 | #endif |
| 326 | |
| 327 | #ifndef WEXITSTATUS |
| 328 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 329 | #endif |
| 330 | |
| 331 | #ifndef WTERMSIG |
| 332 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 333 | #endif |
| 334 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 335 | #define WAIT_TYPE union wait |
| 336 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 337 | |
| 338 | #else /* !UNION_WAIT */ |
| 339 | #define WAIT_TYPE int |
| 340 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 341 | #endif /* UNION_WAIT */ |
| 342 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 343 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 344 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 345 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 346 | #define USE_CTERMID_R |
| 347 | #endif |
| 348 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 349 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 350 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 351 | #undef FSTAT |
| 352 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 353 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 354 | # define STAT win32_stat |
| 355 | # define FSTAT win32_fstat |
| 356 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 357 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 358 | # define STAT stat |
| 359 | # define FSTAT fstat |
| 360 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | #endif |
| 362 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 363 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 364 | #include <sys/mkdev.h> |
| 365 | #else |
| 366 | #if defined(MAJOR_IN_SYSMACROS) |
| 367 | #include <sys/sysmacros.h> |
| 368 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 369 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 370 | #include <sys/mkdev.h> |
| 371 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 372 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 373 | |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 374 | static int |
| 375 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 376 | { |
| 377 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 378 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 379 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 380 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 381 | #endif |
| 382 | if (PyErr_Occurred()) |
| 383 | return 0; |
| 384 | return 1; |
| 385 | } |
| 386 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 387 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 388 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 389 | * valid and throw an assertion if it isn't. |
| 390 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 391 | * an assertion can be useful, but it does contradict the POSIX standard |
| 392 | * which for write(2) states: |
| 393 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 394 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 395 | * writing." |
| 396 | * Furthermore, python allows the user to enter any old integer |
| 397 | * as a fd and should merely raise a python exception on error. |
| 398 | * The Microsoft CRT doesn't provide an official way to check for the |
| 399 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 400 | * 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] | 401 | * internal structures involved. |
| 402 | * The structures below must be updated for each version of visual studio |
| 403 | * according to the file internal.h in the CRT source, until MS comes |
| 404 | * up with a less hacky way to do this. |
| 405 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 406 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 407 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 408 | /* The actual size of the structure is determined at runtime. |
| 409 | * Only the first items must be present. |
| 410 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 411 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 412 | intptr_t osfhnd; |
| 413 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 414 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 415 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 416 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 417 | #define IOINFO_L2E 5 |
| 418 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 419 | #define IOINFO_ARRAYS 64 |
| 420 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 421 | #define FOPEN 0x01 |
| 422 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 423 | |
| 424 | /* This function emulates what the windows CRT does to validate file handles */ |
| 425 | int |
| 426 | _PyVerify_fd(int fd) |
| 427 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 428 | const int i1 = fd >> IOINFO_L2E; |
| 429 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 430 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 431 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 432 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 433 | /* Determine the actual size of the ioinfo structure, |
| 434 | * as used by the CRT loaded in memory |
| 435 | */ |
| 436 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 437 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 438 | } |
| 439 | if (sizeof_ioinfo == 0) { |
| 440 | /* This should not happen... */ |
| 441 | goto fail; |
| 442 | } |
| 443 | |
| 444 | /* See that it isn't a special CLEAR fileno */ |
| 445 | if (fd != _NO_CONSOLE_FILENO) { |
| 446 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 447 | * we check pointer validity and other info |
| 448 | */ |
| 449 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 450 | /* finally, check that the file is open */ |
| 451 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 452 | if (info->osfile & FOPEN) { |
| 453 | return 1; |
| 454 | } |
| 455 | } |
| 456 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 457 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 458 | errno = EBADF; |
| 459 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 463 | static int |
| 464 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 465 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 466 | if (!_PyVerify_fd(fd1)) |
| 467 | return 0; |
| 468 | if (fd2 == _NO_CONSOLE_FILENO) |
| 469 | return 0; |
| 470 | if ((unsigned)fd2 < _NHANDLE_) |
| 471 | return 1; |
| 472 | else |
| 473 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 474 | } |
| 475 | #else |
| 476 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 477 | #define _PyVerify_fd_dup2(A, B) (1) |
| 478 | #endif |
| 479 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 480 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 481 | /* The following structure was copied from |
| 482 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 483 | include doesn't seem to be present in the Windows SDK (at least as included |
| 484 | with Visual Studio Express). */ |
| 485 | typedef struct _REPARSE_DATA_BUFFER { |
| 486 | ULONG ReparseTag; |
| 487 | USHORT ReparseDataLength; |
| 488 | USHORT Reserved; |
| 489 | union { |
| 490 | struct { |
| 491 | USHORT SubstituteNameOffset; |
| 492 | USHORT SubstituteNameLength; |
| 493 | USHORT PrintNameOffset; |
| 494 | USHORT PrintNameLength; |
| 495 | ULONG Flags; |
| 496 | WCHAR PathBuffer[1]; |
| 497 | } SymbolicLinkReparseBuffer; |
| 498 | |
| 499 | struct { |
| 500 | USHORT SubstituteNameOffset; |
| 501 | USHORT SubstituteNameLength; |
| 502 | USHORT PrintNameOffset; |
| 503 | USHORT PrintNameLength; |
| 504 | WCHAR PathBuffer[1]; |
| 505 | } MountPointReparseBuffer; |
| 506 | |
| 507 | struct { |
| 508 | UCHAR DataBuffer[1]; |
| 509 | } GenericReparseBuffer; |
| 510 | }; |
| 511 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 512 | |
| 513 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 514 | GenericReparseBuffer) |
| 515 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 516 | |
| 517 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 518 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 519 | { |
| 520 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 521 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 522 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 523 | |
| 524 | if (0 == DeviceIoControl( |
| 525 | reparse_point_handle, |
| 526 | FSCTL_GET_REPARSE_POINT, |
| 527 | NULL, 0, /* in buffer */ |
| 528 | target_buffer, sizeof(target_buffer), |
| 529 | &n_bytes_returned, |
| 530 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 531 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 532 | |
| 533 | if (reparse_tag) |
| 534 | *reparse_tag = rdb->ReparseTag; |
| 535 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 536 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 537 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 538 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 539 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 540 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 541 | #ifdef WITH_NEXT_FRAMEWORK |
| 542 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 543 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 544 | */ |
| 545 | #include <crt_externs.h> |
| 546 | static char **environ; |
| 547 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 548 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 549 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 550 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 551 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 552 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 553 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 554 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 555 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 556 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 557 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 558 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 559 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 560 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 561 | APIRET rc; |
| 562 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 563 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 564 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 565 | d = PyDict_New(); |
| 566 | if (d == NULL) |
| 567 | return NULL; |
| 568 | #ifdef WITH_NEXT_FRAMEWORK |
| 569 | if (environ == NULL) |
| 570 | environ = *_NSGetEnviron(); |
| 571 | #endif |
| 572 | #ifdef MS_WINDOWS |
| 573 | /* _wenviron must be initialized in this way if the program is started |
| 574 | through main() instead of wmain(). */ |
| 575 | _wgetenv(L""); |
| 576 | if (_wenviron == NULL) |
| 577 | return d; |
| 578 | /* This part ignores errors */ |
| 579 | for (e = _wenviron; *e != NULL; e++) { |
| 580 | PyObject *k; |
| 581 | PyObject *v; |
| 582 | wchar_t *p = wcschr(*e, L'='); |
| 583 | if (p == NULL) |
| 584 | continue; |
| 585 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 586 | if (k == NULL) { |
| 587 | PyErr_Clear(); |
| 588 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 589 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 590 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 591 | if (v == NULL) { |
| 592 | PyErr_Clear(); |
| 593 | Py_DECREF(k); |
| 594 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 595 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 596 | if (PyDict_GetItem(d, k) == NULL) { |
| 597 | if (PyDict_SetItem(d, k, v) != 0) |
| 598 | PyErr_Clear(); |
| 599 | } |
| 600 | Py_DECREF(k); |
| 601 | Py_DECREF(v); |
| 602 | } |
| 603 | #else |
| 604 | if (environ == NULL) |
| 605 | return d; |
| 606 | /* This part ignores errors */ |
| 607 | for (e = environ; *e != NULL; e++) { |
| 608 | PyObject *k; |
| 609 | PyObject *v; |
| 610 | char *p = strchr(*e, '='); |
| 611 | if (p == NULL) |
| 612 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 613 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 614 | if (k == NULL) { |
| 615 | PyErr_Clear(); |
| 616 | continue; |
| 617 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 618 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 619 | if (v == NULL) { |
| 620 | PyErr_Clear(); |
| 621 | Py_DECREF(k); |
| 622 | continue; |
| 623 | } |
| 624 | if (PyDict_GetItem(d, k) == NULL) { |
| 625 | if (PyDict_SetItem(d, k, v) != 0) |
| 626 | PyErr_Clear(); |
| 627 | } |
| 628 | Py_DECREF(k); |
| 629 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 630 | } |
| 631 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 632 | #if defined(PYOS_OS2) |
| 633 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 634 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 635 | PyObject *v = PyBytes_FromString(buffer); |
| 636 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 637 | Py_DECREF(v); |
| 638 | } |
| 639 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 640 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 641 | PyObject *v = PyBytes_FromString(buffer); |
| 642 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 643 | Py_DECREF(v); |
| 644 | } |
| 645 | #endif |
| 646 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 649 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 650 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 651 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 652 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 653 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 654 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 655 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 656 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 657 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 658 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 659 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 662 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 663 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 664 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 665 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 666 | PyObject *name_str, *rc; |
| 667 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 668 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 669 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 670 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 671 | name_str); |
| 672 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 673 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 676 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 677 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 678 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 679 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 680 | /* XXX We should pass the function name along in the future. |
| 681 | (winreg.c also wants to pass the function name.) |
| 682 | This would however require an additional param to the |
| 683 | Windows error object, which is non-trivial. |
| 684 | */ |
| 685 | errno = GetLastError(); |
| 686 | if (filename) |
| 687 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 688 | else |
| 689 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 690 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 691 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 692 | static PyObject * |
| 693 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 694 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 695 | /* XXX - see win32_error for comments on 'function' */ |
| 696 | errno = GetLastError(); |
| 697 | if (filename) |
| 698 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 699 | else |
| 700 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 703 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 704 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 706 | if (PyUnicode_CheckExact(*param)) |
| 707 | Py_INCREF(*param); |
| 708 | else if (PyUnicode_Check(*param)) |
| 709 | /* For a Unicode subtype that's not a Unicode object, |
| 710 | return a true Unicode object with the same data. */ |
| 711 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 712 | PyUnicode_GET_SIZE(*param)); |
| 713 | else |
| 714 | *param = PyUnicode_FromEncodedObject(*param, |
| 715 | Py_FileSystemDefaultEncoding, |
| 716 | "strict"); |
| 717 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 720 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 721 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 722 | #if defined(PYOS_OS2) |
| 723 | /********************************************************************** |
| 724 | * Helper Function to Trim and Format OS/2 Messages |
| 725 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 726 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 727 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 728 | { |
| 729 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 730 | |
| 731 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 732 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 733 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 734 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 735 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 736 | } |
| 737 | |
| 738 | /* Add Optional Reason Text */ |
| 739 | if (reason) { |
| 740 | strcat(msgbuf, " : "); |
| 741 | strcat(msgbuf, reason); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /********************************************************************** |
| 746 | * Decode an OS/2 Operating System Error Code |
| 747 | * |
| 748 | * A convenience function to lookup an OS/2 error code and return a |
| 749 | * text message we can use to raise a Python exception. |
| 750 | * |
| 751 | * Notes: |
| 752 | * The messages for errors returned from the OS/2 kernel reside in |
| 753 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 754 | * |
| 755 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 756 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 757 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 758 | { |
| 759 | APIRET rc; |
| 760 | ULONG msglen; |
| 761 | |
| 762 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 763 | Py_BEGIN_ALLOW_THREADS |
| 764 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 765 | errorcode, "oso001.msg", &msglen); |
| 766 | Py_END_ALLOW_THREADS |
| 767 | |
| 768 | if (rc == NO_ERROR) |
| 769 | os2_formatmsg(msgbuf, msglen, reason); |
| 770 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 771 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 772 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 773 | |
| 774 | return msgbuf; |
| 775 | } |
| 776 | |
| 777 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 778 | errors are not in a global variable e.g. 'errno' nor are |
| 779 | they congruent with posix error numbers. */ |
| 780 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 781 | static PyObject * |
| 782 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 783 | { |
| 784 | char text[1024]; |
| 785 | PyObject *v; |
| 786 | |
| 787 | os2_strerror(text, sizeof(text), code, ""); |
| 788 | |
| 789 | v = Py_BuildValue("(is)", code, text); |
| 790 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 791 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 792 | Py_DECREF(v); |
| 793 | } |
| 794 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 795 | } |
| 796 | |
| 797 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 798 | |
| 799 | /* POSIX generic methods */ |
| 800 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 801 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 802 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 803 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 804 | int fd; |
| 805 | int res; |
| 806 | fd = PyObject_AsFileDescriptor(fdobj); |
| 807 | if (fd < 0) |
| 808 | return NULL; |
| 809 | if (!_PyVerify_fd(fd)) |
| 810 | return posix_error(); |
| 811 | Py_BEGIN_ALLOW_THREADS |
| 812 | res = (*func)(fd); |
| 813 | Py_END_ALLOW_THREADS |
| 814 | if (res < 0) |
| 815 | return posix_error(); |
| 816 | Py_INCREF(Py_None); |
| 817 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 818 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 819 | |
| 820 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 821 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 822 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 823 | PyObject *opath1 = NULL; |
| 824 | char *path1; |
| 825 | int res; |
| 826 | if (!PyArg_ParseTuple(args, format, |
| 827 | PyUnicode_FSConverter, &opath1)) |
| 828 | return NULL; |
| 829 | path1 = PyBytes_AsString(opath1); |
| 830 | Py_BEGIN_ALLOW_THREADS |
| 831 | res = (*func)(path1); |
| 832 | Py_END_ALLOW_THREADS |
| 833 | if (res < 0) |
| 834 | return posix_error_with_allocated_filename(opath1); |
| 835 | Py_DECREF(opath1); |
| 836 | Py_INCREF(Py_None); |
| 837 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 840 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 841 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 842 | char *format, |
| 843 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 844 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 845 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 846 | char *path1, *path2; |
| 847 | int res; |
| 848 | if (!PyArg_ParseTuple(args, format, |
| 849 | PyUnicode_FSConverter, &opath1, |
| 850 | PyUnicode_FSConverter, &opath2)) { |
| 851 | return NULL; |
| 852 | } |
| 853 | path1 = PyBytes_AsString(opath1); |
| 854 | path2 = PyBytes_AsString(opath2); |
| 855 | Py_BEGIN_ALLOW_THREADS |
| 856 | res = (*func)(path1, path2); |
| 857 | Py_END_ALLOW_THREADS |
| 858 | Py_DECREF(opath1); |
| 859 | Py_DECREF(opath2); |
| 860 | if (res != 0) |
| 861 | /* XXX how to report both path1 and path2??? */ |
| 862 | return posix_error(); |
| 863 | Py_INCREF(Py_None); |
| 864 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 867 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 868 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 869 | win32_1str(PyObject* args, char* func, |
| 870 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 871 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 872 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 873 | PyObject *uni; |
| 874 | char *ansi; |
| 875 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 876 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 877 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 878 | PyErr_Clear(); |
| 879 | else { |
| 880 | Py_BEGIN_ALLOW_THREADS |
| 881 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 882 | Py_END_ALLOW_THREADS |
| 883 | if (!result) |
| 884 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 885 | Py_INCREF(Py_None); |
| 886 | return Py_None; |
| 887 | } |
| 888 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 889 | return NULL; |
| 890 | Py_BEGIN_ALLOW_THREADS |
| 891 | result = funcA(ansi); |
| 892 | Py_END_ALLOW_THREADS |
| 893 | if (!result) |
| 894 | return win32_error(func, ansi); |
| 895 | Py_INCREF(Py_None); |
| 896 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 897 | |
| 898 | } |
| 899 | |
| 900 | /* This is a reimplementation of the C library's chdir function, |
| 901 | but one that produces Win32 errors instead of DOS error codes. |
| 902 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 903 | it also needs to set "magic" environment variables indicating |
| 904 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 905 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 906 | win32_chdir(LPCSTR path) |
| 907 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 908 | char new_path[MAX_PATH+1]; |
| 909 | int result; |
| 910 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 912 | if(!SetCurrentDirectoryA(path)) |
| 913 | return FALSE; |
| 914 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 915 | if (!result) |
| 916 | return FALSE; |
| 917 | /* In the ANSI API, there should not be any paths longer |
| 918 | than MAX_PATH. */ |
| 919 | assert(result <= MAX_PATH+1); |
| 920 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 921 | strncmp(new_path, "//", 2) == 0) |
| 922 | /* UNC path, nothing to do. */ |
| 923 | return TRUE; |
| 924 | env[1] = new_path[0]; |
| 925 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /* The Unicode version differs from the ANSI version |
| 929 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 930 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 931 | win32_wchdir(LPCWSTR path) |
| 932 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 933 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 934 | int result; |
| 935 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 936 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 937 | if(!SetCurrentDirectoryW(path)) |
| 938 | return FALSE; |
| 939 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 940 | if (!result) |
| 941 | return FALSE; |
| 942 | if (result > MAX_PATH+1) { |
| 943 | new_path = malloc(result * sizeof(wchar_t)); |
| 944 | if (!new_path) { |
| 945 | SetLastError(ERROR_OUTOFMEMORY); |
| 946 | return FALSE; |
| 947 | } |
| 948 | result = GetCurrentDirectoryW(result, new_path); |
| 949 | if (!result) { |
| 950 | free(new_path); |
| 951 | return FALSE; |
| 952 | } |
| 953 | } |
| 954 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 955 | wcsncmp(new_path, L"//", 2) == 0) |
| 956 | /* UNC path, nothing to do. */ |
| 957 | return TRUE; |
| 958 | env[1] = new_path[0]; |
| 959 | result = SetEnvironmentVariableW(env, new_path); |
| 960 | if (new_path != _new_path) |
| 961 | free(new_path); |
| 962 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 963 | } |
| 964 | #endif |
| 965 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 966 | #ifdef MS_WINDOWS |
| 967 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 968 | - time stamps are restricted to second resolution |
| 969 | - file modification times suffer from forth-and-back conversions between |
| 970 | UTC and local time |
| 971 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 972 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 973 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 974 | |
| 975 | struct win32_stat{ |
| 976 | int st_dev; |
| 977 | __int64 st_ino; |
| 978 | unsigned short st_mode; |
| 979 | int st_nlink; |
| 980 | int st_uid; |
| 981 | int st_gid; |
| 982 | int st_rdev; |
| 983 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 984 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 985 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 986 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 987 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 988 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 989 | int st_ctime_nsec; |
| 990 | }; |
| 991 | |
| 992 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 993 | |
| 994 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 995 | 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] | 996 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 997 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 998 | /* Cannot simply cast and dereference in_ptr, |
| 999 | since it might not be aligned properly */ |
| 1000 | __int64 in; |
| 1001 | memcpy(&in, in_ptr, sizeof(in)); |
| 1002 | *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] | 1003 | *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] | 1004 | } |
| 1005 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1006 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1007 | 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] | 1008 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1009 | /* XXX endianness */ |
| 1010 | __int64 out; |
| 1011 | out = time_in + secs_between_epochs; |
| 1012 | out = out * 10000000 + nsec_in / 100; |
| 1013 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1016 | /* Below, we *know* that ugo+r is 0444 */ |
| 1017 | #if _S_IREAD != 0400 |
| 1018 | #error Unsupported C library |
| 1019 | #endif |
| 1020 | static int |
| 1021 | attributes_to_mode(DWORD attr) |
| 1022 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1023 | int m = 0; |
| 1024 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1025 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1026 | else |
| 1027 | m |= _S_IFREG; |
| 1028 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1029 | m |= 0444; |
| 1030 | else |
| 1031 | m |= 0666; |
| 1032 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1036 | 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] | 1037 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1038 | memset(result, 0, sizeof(*result)); |
| 1039 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1040 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1041 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1042 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1043 | 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] | 1044 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1045 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1046 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1047 | /* first clear the S_IFMT bits */ |
| 1048 | result->st_mode ^= (result->st_mode & 0170000); |
| 1049 | /* now set the bits that make this a symlink */ |
| 1050 | result->st_mode |= 0120000; |
| 1051 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1052 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1053 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1056 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1057 | 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] | 1058 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1059 | HANDLE hFindFile; |
| 1060 | WIN32_FIND_DATAA FileData; |
| 1061 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1062 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1063 | return FALSE; |
| 1064 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1065 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1066 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1067 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1068 | info->ftCreationTime = FileData.ftCreationTime; |
| 1069 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1070 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1071 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1072 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1073 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1074 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1075 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1076 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1080 | 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] | 1081 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1082 | HANDLE hFindFile; |
| 1083 | WIN32_FIND_DATAW FileData; |
| 1084 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1085 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1086 | return FALSE; |
| 1087 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1088 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1089 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1090 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1091 | info->ftCreationTime = FileData.ftCreationTime; |
| 1092 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1093 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1094 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1095 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1096 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1097 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1098 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1099 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1102 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1103 | static int has_GetFinalPathNameByHandle = 0; |
| 1104 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1105 | DWORD); |
| 1106 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1107 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1108 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1109 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1110 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1111 | HINSTANCE hKernel32; |
| 1112 | /* only recheck */ |
| 1113 | if (!has_GetFinalPathNameByHandle) |
| 1114 | { |
| 1115 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1116 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1117 | "GetFinalPathNameByHandleA"); |
| 1118 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1119 | "GetFinalPathNameByHandleW"); |
| 1120 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1121 | Py_GetFinalPathNameByHandleW; |
| 1122 | } |
| 1123 | return has_GetFinalPathNameByHandle; |
| 1124 | } |
| 1125 | |
| 1126 | static BOOL |
| 1127 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1128 | { |
| 1129 | int buf_size, result_length; |
| 1130 | wchar_t *buf; |
| 1131 | |
| 1132 | /* We have a good handle to the target, use it to determine |
| 1133 | the target path name (then we'll call lstat on it). */ |
| 1134 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1135 | VOLUME_NAME_DOS); |
| 1136 | if(!buf_size) |
| 1137 | return FALSE; |
| 1138 | |
| 1139 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1140 | if (!buf) { |
| 1141 | SetLastError(ERROR_OUTOFMEMORY); |
| 1142 | return FALSE; |
| 1143 | } |
| 1144 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1145 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1146 | buf, buf_size, VOLUME_NAME_DOS); |
| 1147 | |
| 1148 | if(!result_length) { |
| 1149 | free(buf); |
| 1150 | return FALSE; |
| 1151 | } |
| 1152 | |
| 1153 | if(!CloseHandle(hdl)) { |
| 1154 | free(buf); |
| 1155 | return FALSE; |
| 1156 | } |
| 1157 | |
| 1158 | buf[result_length] = 0; |
| 1159 | |
| 1160 | *target_path = buf; |
| 1161 | return TRUE; |
| 1162 | } |
| 1163 | |
| 1164 | static int |
| 1165 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1166 | BOOL traverse); |
| 1167 | static int |
| 1168 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1169 | BOOL traverse) |
| 1170 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1171 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1172 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1173 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1174 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1175 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1176 | const char *dot; |
| 1177 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1178 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1179 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1180 | traverse reparse point. */ |
| 1181 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1184 | hFile = CreateFileA( |
| 1185 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1186 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1187 | 0, /* share mode */ |
| 1188 | NULL, /* security attributes */ |
| 1189 | OPEN_EXISTING, |
| 1190 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1191 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1192 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1193 | the symlink path agin and not the actual final path. */ |
| 1194 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1195 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1196 | NULL); |
| 1197 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1198 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1199 | /* Either the target doesn't exist, or we don't have access to |
| 1200 | get a handle to it. If the former, we need to return an error. |
| 1201 | If the latter, we can use attributes_from_dir. */ |
| 1202 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1203 | return -1; |
| 1204 | /* Could not get attributes on open file. Fall back to |
| 1205 | reading the directory. */ |
| 1206 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1207 | /* Very strange. This should not fail now */ |
| 1208 | return -1; |
| 1209 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1210 | if (traverse) { |
| 1211 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1212 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1213 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1214 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1215 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1216 | } else { |
| 1217 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1218 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1219 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1220 | } |
| 1221 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1222 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1223 | return -1; |
| 1224 | |
| 1225 | /* Close the outer open file handle now that we're about to |
| 1226 | reopen it with different flags. */ |
| 1227 | if (!CloseHandle(hFile)) |
| 1228 | return -1; |
| 1229 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1230 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1231 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1232 | the file without the reparse handling flag set. */ |
| 1233 | hFile2 = CreateFileA( |
| 1234 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1235 | NULL, OPEN_EXISTING, |
| 1236 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1237 | NULL); |
| 1238 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1239 | return -1; |
| 1240 | |
| 1241 | if (!get_target_path(hFile2, &target_path)) |
| 1242 | return -1; |
| 1243 | |
| 1244 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1245 | free(target_path); |
| 1246 | return code; |
| 1247 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1248 | } else |
| 1249 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1250 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1251 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1252 | |
| 1253 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1254 | dot = strrchr(path, '.'); |
| 1255 | if (dot) { |
| 1256 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1257 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1258 | result->st_mode |= 0111; |
| 1259 | } |
| 1260 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1264 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1265 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1266 | { |
| 1267 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1268 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1269 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1270 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1271 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1272 | const wchar_t *dot; |
| 1273 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1274 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1275 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1276 | traverse reparse point. */ |
| 1277 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1280 | hFile = CreateFileW( |
| 1281 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1282 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1283 | 0, /* share mode */ |
| 1284 | NULL, /* security attributes */ |
| 1285 | OPEN_EXISTING, |
| 1286 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1287 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1288 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1289 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1290 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1291 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1292 | NULL); |
| 1293 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1294 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1295 | /* Either the target doesn't exist, or we don't have access to |
| 1296 | get a handle to it. If the former, we need to return an error. |
| 1297 | If the latter, we can use attributes_from_dir. */ |
| 1298 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1299 | return -1; |
| 1300 | /* Could not get attributes on open file. Fall back to |
| 1301 | reading the directory. */ |
| 1302 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1303 | /* Very strange. This should not fail now */ |
| 1304 | return -1; |
| 1305 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1306 | if (traverse) { |
| 1307 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1308 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1309 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1310 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1311 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1312 | } else { |
| 1313 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1314 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1315 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1316 | } |
| 1317 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1318 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1319 | return -1; |
| 1320 | |
| 1321 | /* Close the outer open file handle now that we're about to |
| 1322 | reopen it with different flags. */ |
| 1323 | if (!CloseHandle(hFile)) |
| 1324 | return -1; |
| 1325 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1326 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1327 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1328 | the file without the reparse handling flag set. */ |
| 1329 | hFile2 = CreateFileW( |
| 1330 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1331 | NULL, OPEN_EXISTING, |
| 1332 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1333 | NULL); |
| 1334 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1335 | return -1; |
| 1336 | |
| 1337 | if (!get_target_path(hFile2, &target_path)) |
| 1338 | return -1; |
| 1339 | |
| 1340 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1341 | free(target_path); |
| 1342 | return code; |
| 1343 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1344 | } else |
| 1345 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1346 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1347 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1348 | |
| 1349 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1350 | dot = wcsrchr(path, '.'); |
| 1351 | if (dot) { |
| 1352 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1353 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1354 | result->st_mode |= 0111; |
| 1355 | } |
| 1356 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1360 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1361 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1362 | /* Protocol violation: we explicitly clear errno, instead of |
| 1363 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1364 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1365 | errno = 0; |
| 1366 | return code; |
| 1367 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1368 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1369 | static int |
| 1370 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1371 | { |
| 1372 | /* Protocol violation: we explicitly clear errno, instead of |
| 1373 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1374 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1375 | errno = 0; |
| 1376 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1377 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1378 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1379 | |
| 1380 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1381 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1382 | default does not traverse symlinks and instead returns attributes for |
| 1383 | the symlink. |
| 1384 | |
| 1385 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1386 | win32_stat will first explicitly resolve the symlink target and then will |
| 1387 | call win32_lstat on that result. |
| 1388 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1389 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1390 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1391 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1392 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1393 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1394 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1397 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1398 | 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] | 1399 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1400 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | static int |
| 1404 | win32_stat(const char* path, struct win32_stat *result) |
| 1405 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1406 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1409 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1410 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1411 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1412 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | static int |
| 1416 | win32_fstat(int file_number, struct win32_stat *result) |
| 1417 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1418 | BY_HANDLE_FILE_INFORMATION info; |
| 1419 | HANDLE h; |
| 1420 | int type; |
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 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1423 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1424 | /* Protocol violation: we explicitly clear errno, instead of |
| 1425 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1426 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1427 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1428 | if (h == INVALID_HANDLE_VALUE) { |
| 1429 | /* This is really a C library error (invalid file handle). |
| 1430 | We set the Win32 error to the closes one matching. */ |
| 1431 | SetLastError(ERROR_INVALID_HANDLE); |
| 1432 | return -1; |
| 1433 | } |
| 1434 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1435 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1436 | type = GetFileType(h); |
| 1437 | if (type == FILE_TYPE_UNKNOWN) { |
| 1438 | DWORD error = GetLastError(); |
| 1439 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1440 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1441 | } |
| 1442 | /* else: valid but unknown file */ |
| 1443 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1444 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1445 | if (type != FILE_TYPE_DISK) { |
| 1446 | if (type == FILE_TYPE_CHAR) |
| 1447 | result->st_mode = _S_IFCHR; |
| 1448 | else if (type == FILE_TYPE_PIPE) |
| 1449 | result->st_mode = _S_IFIFO; |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
| 1453 | if (!GetFileInformationByHandle(h, &info)) { |
| 1454 | return -1; |
| 1455 | } |
| 1456 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1457 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1458 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1459 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1460 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | #endif /* MS_WINDOWS */ |
| 1464 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1465 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1466 | "stat_result: Result from stat or lstat.\n\n\ |
| 1467 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1468 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1469 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1470 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1471 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1472 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1473 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1474 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1475 | |
| 1476 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1477 | {"st_mode", "protection bits"}, |
| 1478 | {"st_ino", "inode"}, |
| 1479 | {"st_dev", "device"}, |
| 1480 | {"st_nlink", "number of hard links"}, |
| 1481 | {"st_uid", "user ID of owner"}, |
| 1482 | {"st_gid", "group ID of owner"}, |
| 1483 | {"st_size", "total size, in bytes"}, |
| 1484 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1485 | {NULL, "integer time of last access"}, |
| 1486 | {NULL, "integer time of last modification"}, |
| 1487 | {NULL, "integer time of last change"}, |
| 1488 | {"st_atime", "time of last access"}, |
| 1489 | {"st_mtime", "time of last modification"}, |
| 1490 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1491 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1492 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1493 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1494 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1495 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1496 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1497 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1498 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1499 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1500 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1501 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1502 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1503 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1504 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1505 | #endif |
| 1506 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1507 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1508 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1509 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1510 | }; |
| 1511 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1512 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1513 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1514 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1515 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1516 | #endif |
| 1517 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1518 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1519 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1520 | #else |
| 1521 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1522 | #endif |
| 1523 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1524 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1525 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1526 | #else |
| 1527 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1528 | #endif |
| 1529 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1530 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1531 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1532 | #else |
| 1533 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1534 | #endif |
| 1535 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1536 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1537 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1538 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1539 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1540 | #endif |
| 1541 | |
| 1542 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1543 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1544 | #else |
| 1545 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1546 | #endif |
| 1547 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1548 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1549 | "stat_result", /* name */ |
| 1550 | stat_result__doc__, /* doc */ |
| 1551 | stat_result_fields, |
| 1552 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1553 | }; |
| 1554 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1555 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1556 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1557 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1558 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1559 | 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] | 1560 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1561 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1562 | |
| 1563 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1564 | {"f_bsize", }, |
| 1565 | {"f_frsize", }, |
| 1566 | {"f_blocks", }, |
| 1567 | {"f_bfree", }, |
| 1568 | {"f_bavail", }, |
| 1569 | {"f_files", }, |
| 1570 | {"f_ffree", }, |
| 1571 | {"f_favail", }, |
| 1572 | {"f_flag", }, |
| 1573 | {"f_namemax",}, |
| 1574 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1575 | }; |
| 1576 | |
| 1577 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1578 | "statvfs_result", /* name */ |
| 1579 | statvfs_result__doc__, /* doc */ |
| 1580 | statvfs_result_fields, |
| 1581 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1582 | }; |
| 1583 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1584 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1585 | PyDoc_STRVAR(waitid_result__doc__, |
| 1586 | "waitid_result: Result from waitid.\n\n\ |
| 1587 | This object may be accessed either as a tuple of\n\ |
| 1588 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1589 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1590 | \n\ |
| 1591 | See os.waitid for more information."); |
| 1592 | |
| 1593 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1594 | {"si_pid", }, |
| 1595 | {"si_uid", }, |
| 1596 | {"si_signo", }, |
| 1597 | {"si_status", }, |
| 1598 | {"si_code", }, |
| 1599 | {0} |
| 1600 | }; |
| 1601 | |
| 1602 | static PyStructSequence_Desc waitid_result_desc = { |
| 1603 | "waitid_result", /* name */ |
| 1604 | waitid_result__doc__, /* doc */ |
| 1605 | waitid_result_fields, |
| 1606 | 5 |
| 1607 | }; |
| 1608 | static PyTypeObject WaitidResultType; |
| 1609 | #endif |
| 1610 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1611 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1612 | static PyTypeObject StatResultType; |
| 1613 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1614 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1615 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1616 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1617 | static newfunc structseq_new; |
| 1618 | |
| 1619 | static PyObject * |
| 1620 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1621 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1622 | PyStructSequence *result; |
| 1623 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1624 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1625 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1626 | if (!result) |
| 1627 | return NULL; |
| 1628 | /* If we have been initialized from a tuple, |
| 1629 | st_?time might be set to None. Initialize it |
| 1630 | from the int slots. */ |
| 1631 | for (i = 7; i <= 9; i++) { |
| 1632 | if (result->ob_item[i+3] == Py_None) { |
| 1633 | Py_DECREF(Py_None); |
| 1634 | Py_INCREF(result->ob_item[i]); |
| 1635 | result->ob_item[i+3] = result->ob_item[i]; |
| 1636 | } |
| 1637 | } |
| 1638 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1644 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1645 | |
| 1646 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1647 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1648 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1649 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1650 | future calls return ints. \n\ |
| 1651 | If newval is omitted, return the current setting.\n"); |
| 1652 | |
| 1653 | static PyObject* |
| 1654 | stat_float_times(PyObject* self, PyObject *args) |
| 1655 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1656 | int newval = -1; |
| 1657 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1658 | return NULL; |
| 1659 | if (newval == -1) |
| 1660 | /* Return old value */ |
| 1661 | return PyBool_FromLong(_stat_float_times); |
| 1662 | _stat_float_times = newval; |
| 1663 | Py_INCREF(Py_None); |
| 1664 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1665 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1666 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1667 | static void |
| 1668 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1669 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1670 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1671 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1672 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1673 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1674 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1675 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1676 | if (!ival) |
| 1677 | return; |
| 1678 | if (_stat_float_times) { |
| 1679 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1680 | } else { |
| 1681 | fval = ival; |
| 1682 | Py_INCREF(fval); |
| 1683 | } |
| 1684 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1685 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1688 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1689 | (used by posix_stat() and posix_fstat()) */ |
| 1690 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1691 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1692 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1693 | unsigned long ansec, mnsec, cnsec; |
| 1694 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1695 | if (v == NULL) |
| 1696 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1697 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1698 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1699 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1700 | PyStructSequence_SET_ITEM(v, 1, |
| 1701 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1702 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1703 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1704 | #endif |
| 1705 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1706 | PyStructSequence_SET_ITEM(v, 2, |
| 1707 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1708 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1709 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1710 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1711 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1712 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1713 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1714 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1715 | PyStructSequence_SET_ITEM(v, 6, |
| 1716 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1717 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1718 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1719 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1720 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1721 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1722 | ansec = st->st_atim.tv_nsec; |
| 1723 | mnsec = st->st_mtim.tv_nsec; |
| 1724 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1725 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1726 | ansec = st->st_atimespec.tv_nsec; |
| 1727 | mnsec = st->st_mtimespec.tv_nsec; |
| 1728 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1729 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1730 | ansec = st->st_atime_nsec; |
| 1731 | mnsec = st->st_mtime_nsec; |
| 1732 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1733 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1734 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1735 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1736 | fill_time(v, 7, st->st_atime, ansec); |
| 1737 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1738 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1739 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1740 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1741 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1742 | PyLong_FromLong((long)st->st_blksize)); |
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_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1745 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1746 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1747 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1748 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1749 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1750 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1751 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1752 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1753 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1754 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1755 | #endif |
| 1756 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1757 | { |
| 1758 | PyObject *val; |
| 1759 | unsigned long bsec,bnsec; |
| 1760 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1761 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1762 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1763 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1764 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1765 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1766 | if (_stat_float_times) { |
| 1767 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1768 | } else { |
| 1769 | val = PyLong_FromLong((long)bsec); |
| 1770 | } |
| 1771 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1772 | val); |
| 1773 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1774 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1775 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1776 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1777 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1778 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1779 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1780 | if (PyErr_Occurred()) { |
| 1781 | Py_DECREF(v); |
| 1782 | return NULL; |
| 1783 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1784 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1785 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1788 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1789 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1790 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1791 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1792 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1793 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1794 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1795 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1796 | char *wformat, |
| 1797 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1798 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1799 | STRUCT_STAT st; |
| 1800 | PyObject *opath; |
| 1801 | char *path; |
| 1802 | int res; |
| 1803 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1804 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1805 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1806 | PyUnicodeObject *po; |
| 1807 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1808 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1809 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1810 | Py_BEGIN_ALLOW_THREADS |
| 1811 | /* PyUnicode_AS_UNICODE result OK without |
| 1812 | thread lock as it is a simple dereference. */ |
| 1813 | res = wstatfunc(wpath, &st); |
| 1814 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1815 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1816 | if (res != 0) |
| 1817 | return win32_error_unicode("stat", wpath); |
| 1818 | return _pystat_fromstructstat(&st); |
| 1819 | } |
| 1820 | /* Drop the argument parsing error as narrow strings |
| 1821 | are also valid. */ |
| 1822 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1823 | #endif |
| 1824 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1825 | if (!PyArg_ParseTuple(args, format, |
| 1826 | PyUnicode_FSConverter, &opath)) |
| 1827 | return NULL; |
| 1828 | path = PyBytes_AsString(opath); |
| 1829 | Py_BEGIN_ALLOW_THREADS |
| 1830 | res = (*statfunc)(path, &st); |
| 1831 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1832 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1833 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1834 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1835 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1836 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1837 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1838 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1839 | } |
| 1840 | else |
| 1841 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1842 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1843 | Py_DECREF(opath); |
| 1844 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1847 | /* POSIX methods */ |
| 1848 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1849 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1850 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1851 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1852 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1853 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1854 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1855 | 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] | 1856 | |
| 1857 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1858 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1859 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1860 | PyObject *opath; |
| 1861 | char *path; |
| 1862 | int mode; |
| 1863 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1864 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1865 | DWORD attr; |
| 1866 | PyUnicodeObject *po; |
| 1867 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1868 | Py_BEGIN_ALLOW_THREADS |
| 1869 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1870 | it is a simple dereference. */ |
| 1871 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1872 | Py_END_ALLOW_THREADS |
| 1873 | goto finish; |
| 1874 | } |
| 1875 | /* Drop the argument parsing error as narrow strings |
| 1876 | are also valid. */ |
| 1877 | PyErr_Clear(); |
| 1878 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1879 | PyUnicode_FSConverter, &opath, &mode)) |
| 1880 | return NULL; |
| 1881 | path = PyBytes_AsString(opath); |
| 1882 | Py_BEGIN_ALLOW_THREADS |
| 1883 | attr = GetFileAttributesA(path); |
| 1884 | Py_END_ALLOW_THREADS |
| 1885 | Py_DECREF(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1886 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1887 | if (attr == 0xFFFFFFFF) |
| 1888 | /* File does not exist, or cannot read attributes */ |
| 1889 | return PyBool_FromLong(0); |
| 1890 | /* Access is possible if either write access wasn't requested, or |
| 1891 | the file isn't read-only, or if it's a directory, as there are |
| 1892 | no read-only directories on Windows. */ |
| 1893 | return PyBool_FromLong(!(mode & 2) |
| 1894 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1895 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1896 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1897 | int res; |
| 1898 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1899 | PyUnicode_FSConverter, &opath, &mode)) |
| 1900 | return NULL; |
| 1901 | path = PyBytes_AsString(opath); |
| 1902 | Py_BEGIN_ALLOW_THREADS |
| 1903 | res = access(path, mode); |
| 1904 | Py_END_ALLOW_THREADS |
| 1905 | Py_DECREF(opath); |
| 1906 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1907 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1910 | #ifndef F_OK |
| 1911 | #define F_OK 0 |
| 1912 | #endif |
| 1913 | #ifndef R_OK |
| 1914 | #define R_OK 4 |
| 1915 | #endif |
| 1916 | #ifndef W_OK |
| 1917 | #define W_OK 2 |
| 1918 | #endif |
| 1919 | #ifndef X_OK |
| 1920 | #define X_OK 1 |
| 1921 | #endif |
| 1922 | |
| 1923 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1924 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1925 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1926 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1927 | |
| 1928 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1929 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1930 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1931 | int id; |
| 1932 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1933 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1934 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1935 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1936 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1937 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1938 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1939 | if (id == 0) { |
| 1940 | ret = ttyname(); |
| 1941 | } |
| 1942 | else { |
| 1943 | ret = NULL; |
| 1944 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1945 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1946 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1947 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1948 | if (ret == NULL) |
| 1949 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1950 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1951 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1952 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1953 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1954 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1955 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1956 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1957 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1958 | |
| 1959 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1960 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1961 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | char *ret; |
| 1963 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1964 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1965 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1966 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1967 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1968 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1969 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1970 | if (ret == NULL) |
| 1971 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1972 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1973 | } |
| 1974 | #endif |
| 1975 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1976 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1977 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1978 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1979 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1980 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1981 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1982 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1983 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1984 | 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] | 1985 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1986 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1987 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1988 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1989 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1990 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1991 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1994 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1995 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1996 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1997 | 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] | 1998 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1999 | |
| 2000 | static PyObject * |
| 2001 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2003 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2004 | } |
| 2005 | #endif /* HAVE_FCHDIR */ |
| 2006 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2008 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2009 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2010 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2011 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2012 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2013 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2014 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2015 | PyObject *opath = NULL; |
| 2016 | char *path = NULL; |
| 2017 | int i; |
| 2018 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2019 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2020 | DWORD attr; |
| 2021 | PyUnicodeObject *po; |
| 2022 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 2023 | Py_BEGIN_ALLOW_THREADS |
| 2024 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 2025 | if (attr != 0xFFFFFFFF) { |
| 2026 | if (i & _S_IWRITE) |
| 2027 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2028 | else |
| 2029 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2030 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 2031 | } |
| 2032 | else |
| 2033 | res = 0; |
| 2034 | Py_END_ALLOW_THREADS |
| 2035 | if (!res) |
| 2036 | return win32_error_unicode("chmod", |
| 2037 | PyUnicode_AS_UNICODE(po)); |
| 2038 | Py_INCREF(Py_None); |
| 2039 | return Py_None; |
| 2040 | } |
| 2041 | /* Drop the argument parsing error as narrow strings |
| 2042 | are also valid. */ |
| 2043 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2044 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2045 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2046 | &opath, &i)) |
| 2047 | return NULL; |
| 2048 | path = PyBytes_AsString(opath); |
| 2049 | Py_BEGIN_ALLOW_THREADS |
| 2050 | attr = GetFileAttributesA(path); |
| 2051 | if (attr != 0xFFFFFFFF) { |
| 2052 | if (i & _S_IWRITE) |
| 2053 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2054 | else |
| 2055 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2056 | res = SetFileAttributesA(path, attr); |
| 2057 | } |
| 2058 | else |
| 2059 | res = 0; |
| 2060 | Py_END_ALLOW_THREADS |
| 2061 | if (!res) { |
| 2062 | win32_error("chmod", path); |
| 2063 | Py_DECREF(opath); |
| 2064 | return NULL; |
| 2065 | } |
| 2066 | Py_DECREF(opath); |
| 2067 | Py_INCREF(Py_None); |
| 2068 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2069 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2070 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2071 | &opath, &i)) |
| 2072 | return NULL; |
| 2073 | path = PyBytes_AsString(opath); |
| 2074 | Py_BEGIN_ALLOW_THREADS |
| 2075 | res = chmod(path, i); |
| 2076 | Py_END_ALLOW_THREADS |
| 2077 | if (res < 0) |
| 2078 | return posix_error_with_allocated_filename(opath); |
| 2079 | Py_DECREF(opath); |
| 2080 | Py_INCREF(Py_None); |
| 2081 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2082 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2085 | #ifdef HAVE_FCHMOD |
| 2086 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2087 | "fchmod(fd, mode)\n\n\ |
| 2088 | Change the access permissions of the file given by file\n\ |
| 2089 | descriptor fd."); |
| 2090 | |
| 2091 | static PyObject * |
| 2092 | posix_fchmod(PyObject *self, PyObject *args) |
| 2093 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2094 | int fd, mode, res; |
| 2095 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2096 | return NULL; |
| 2097 | Py_BEGIN_ALLOW_THREADS |
| 2098 | res = fchmod(fd, mode); |
| 2099 | Py_END_ALLOW_THREADS |
| 2100 | if (res < 0) |
| 2101 | return posix_error(); |
| 2102 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2103 | } |
| 2104 | #endif /* HAVE_FCHMOD */ |
| 2105 | |
| 2106 | #ifdef HAVE_LCHMOD |
| 2107 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2108 | "lchmod(path, mode)\n\n\ |
| 2109 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2110 | affects the link itself rather than the target."); |
| 2111 | |
| 2112 | static PyObject * |
| 2113 | posix_lchmod(PyObject *self, PyObject *args) |
| 2114 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2115 | PyObject *opath; |
| 2116 | char *path; |
| 2117 | int i; |
| 2118 | int res; |
| 2119 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2120 | &opath, &i)) |
| 2121 | return NULL; |
| 2122 | path = PyBytes_AsString(opath); |
| 2123 | Py_BEGIN_ALLOW_THREADS |
| 2124 | res = lchmod(path, i); |
| 2125 | Py_END_ALLOW_THREADS |
| 2126 | if (res < 0) |
| 2127 | return posix_error_with_allocated_filename(opath); |
| 2128 | Py_DECREF(opath); |
| 2129 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2130 | } |
| 2131 | #endif /* HAVE_LCHMOD */ |
| 2132 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2133 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2134 | #ifdef HAVE_CHFLAGS |
| 2135 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2136 | "chflags(path, flags)\n\n\ |
| 2137 | Set file flags."); |
| 2138 | |
| 2139 | static PyObject * |
| 2140 | posix_chflags(PyObject *self, PyObject *args) |
| 2141 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2142 | PyObject *opath; |
| 2143 | char *path; |
| 2144 | unsigned long flags; |
| 2145 | int res; |
| 2146 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2147 | PyUnicode_FSConverter, &opath, &flags)) |
| 2148 | return NULL; |
| 2149 | path = PyBytes_AsString(opath); |
| 2150 | Py_BEGIN_ALLOW_THREADS |
| 2151 | res = chflags(path, flags); |
| 2152 | Py_END_ALLOW_THREADS |
| 2153 | if (res < 0) |
| 2154 | return posix_error_with_allocated_filename(opath); |
| 2155 | Py_DECREF(opath); |
| 2156 | Py_INCREF(Py_None); |
| 2157 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2158 | } |
| 2159 | #endif /* HAVE_CHFLAGS */ |
| 2160 | |
| 2161 | #ifdef HAVE_LCHFLAGS |
| 2162 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2163 | "lchflags(path, flags)\n\n\ |
| 2164 | Set file flags.\n\ |
| 2165 | This function will not follow symbolic links."); |
| 2166 | |
| 2167 | static PyObject * |
| 2168 | posix_lchflags(PyObject *self, PyObject *args) |
| 2169 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2170 | PyObject *opath; |
| 2171 | char *path; |
| 2172 | unsigned long flags; |
| 2173 | int res; |
| 2174 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2175 | PyUnicode_FSConverter, &opath, &flags)) |
| 2176 | return NULL; |
| 2177 | path = PyBytes_AsString(opath); |
| 2178 | Py_BEGIN_ALLOW_THREADS |
| 2179 | res = lchflags(path, flags); |
| 2180 | Py_END_ALLOW_THREADS |
| 2181 | if (res < 0) |
| 2182 | return posix_error_with_allocated_filename(opath); |
| 2183 | Py_DECREF(opath); |
| 2184 | Py_INCREF(Py_None); |
| 2185 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2186 | } |
| 2187 | #endif /* HAVE_LCHFLAGS */ |
| 2188 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2189 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2190 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2191 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2192 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2193 | |
| 2194 | static PyObject * |
| 2195 | posix_chroot(PyObject *self, PyObject *args) |
| 2196 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2197 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2198 | } |
| 2199 | #endif |
| 2200 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2201 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2202 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2203 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2204 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2205 | |
| 2206 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2207 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2208 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2209 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2210 | } |
| 2211 | #endif /* HAVE_FSYNC */ |
| 2212 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2213 | #ifdef HAVE_SYNC |
| 2214 | PyDoc_STRVAR(posix_sync__doc__, |
| 2215 | "sync()\n\n\ |
| 2216 | Force write of everything to disk."); |
| 2217 | |
| 2218 | static PyObject * |
| 2219 | posix_sync(PyObject *self, PyObject *noargs) |
| 2220 | { |
| 2221 | Py_BEGIN_ALLOW_THREADS |
| 2222 | sync(); |
| 2223 | Py_END_ALLOW_THREADS |
| 2224 | Py_RETURN_NONE; |
| 2225 | } |
| 2226 | #endif |
| 2227 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2228 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2229 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2230 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2231 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2232 | #endif |
| 2233 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2234 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2235 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2236 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2237 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2238 | |
| 2239 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2240 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2241 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2242 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2243 | } |
| 2244 | #endif /* HAVE_FDATASYNC */ |
| 2245 | |
| 2246 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2247 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2248 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2249 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2250 | 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] | 2251 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2252 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2253 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2254 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2255 | PyObject *opath; |
| 2256 | char *path; |
| 2257 | long uid, gid; |
| 2258 | int res; |
| 2259 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2260 | PyUnicode_FSConverter, &opath, |
| 2261 | &uid, &gid)) |
| 2262 | return NULL; |
| 2263 | path = PyBytes_AsString(opath); |
| 2264 | Py_BEGIN_ALLOW_THREADS |
| 2265 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2266 | Py_END_ALLOW_THREADS |
| 2267 | if (res < 0) |
| 2268 | return posix_error_with_allocated_filename(opath); |
| 2269 | Py_DECREF(opath); |
| 2270 | Py_INCREF(Py_None); |
| 2271 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2272 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2273 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2274 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2275 | #ifdef HAVE_FCHOWN |
| 2276 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2277 | "fchown(fd, uid, gid)\n\n\ |
| 2278 | Change the owner and group id of the file given by file descriptor\n\ |
| 2279 | fd to the numeric uid and gid."); |
| 2280 | |
| 2281 | static PyObject * |
| 2282 | posix_fchown(PyObject *self, PyObject *args) |
| 2283 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2284 | int fd; |
| 2285 | long uid, gid; |
| 2286 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2287 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2288 | return NULL; |
| 2289 | Py_BEGIN_ALLOW_THREADS |
| 2290 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2291 | Py_END_ALLOW_THREADS |
| 2292 | if (res < 0) |
| 2293 | return posix_error(); |
| 2294 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2295 | } |
| 2296 | #endif /* HAVE_FCHOWN */ |
| 2297 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2298 | #ifdef HAVE_LCHOWN |
| 2299 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2300 | "lchown(path, uid, gid)\n\n\ |
| 2301 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2302 | This function will not follow symbolic links."); |
| 2303 | |
| 2304 | static PyObject * |
| 2305 | posix_lchown(PyObject *self, PyObject *args) |
| 2306 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2307 | PyObject *opath; |
| 2308 | char *path; |
| 2309 | long uid, gid; |
| 2310 | int res; |
| 2311 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2312 | PyUnicode_FSConverter, &opath, |
| 2313 | &uid, &gid)) |
| 2314 | return NULL; |
| 2315 | path = PyBytes_AsString(opath); |
| 2316 | Py_BEGIN_ALLOW_THREADS |
| 2317 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2318 | Py_END_ALLOW_THREADS |
| 2319 | if (res < 0) |
| 2320 | return posix_error_with_allocated_filename(opath); |
| 2321 | Py_DECREF(opath); |
| 2322 | Py_INCREF(Py_None); |
| 2323 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2324 | } |
| 2325 | #endif /* HAVE_LCHOWN */ |
| 2326 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2327 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2328 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2329 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2330 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2331 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2332 | char buf[1026]; |
| 2333 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2334 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2335 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2336 | if (!use_bytes) { |
| 2337 | wchar_t wbuf[1026]; |
| 2338 | wchar_t *wbuf2 = wbuf; |
| 2339 | PyObject *resobj; |
| 2340 | DWORD len; |
| 2341 | Py_BEGIN_ALLOW_THREADS |
| 2342 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2343 | /* If the buffer is large enough, len does not include the |
| 2344 | terminating \0. If the buffer is too small, len includes |
| 2345 | the space needed for the terminator. */ |
| 2346 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2347 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2348 | if (wbuf2) |
| 2349 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2350 | } |
| 2351 | Py_END_ALLOW_THREADS |
| 2352 | if (!wbuf2) { |
| 2353 | PyErr_NoMemory(); |
| 2354 | return NULL; |
| 2355 | } |
| 2356 | if (!len) { |
| 2357 | if (wbuf2 != wbuf) free(wbuf2); |
| 2358 | return win32_error("getcwdu", NULL); |
| 2359 | } |
| 2360 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2361 | if (wbuf2 != wbuf) free(wbuf2); |
| 2362 | return resobj; |
| 2363 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2364 | #endif |
| 2365 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2366 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2367 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2368 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2369 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2370 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2371 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2372 | Py_END_ALLOW_THREADS |
| 2373 | if (res == NULL) |
| 2374 | return posix_error(); |
| 2375 | if (use_bytes) |
| 2376 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2377 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2378 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2379 | |
| 2380 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2381 | "getcwd() -> path\n\n\ |
| 2382 | Return a unicode string representing the current working directory."); |
| 2383 | |
| 2384 | static PyObject * |
| 2385 | posix_getcwd_unicode(PyObject *self) |
| 2386 | { |
| 2387 | return posix_getcwd(0); |
| 2388 | } |
| 2389 | |
| 2390 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2391 | "getcwdb() -> path\n\n\ |
| 2392 | Return a bytes string representing the current working directory."); |
| 2393 | |
| 2394 | static PyObject * |
| 2395 | posix_getcwd_bytes(PyObject *self) |
| 2396 | { |
| 2397 | return posix_getcwd(1); |
| 2398 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2399 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2400 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2401 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2402 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2403 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2404 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2405 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2406 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2407 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2408 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2409 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2410 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2411 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2412 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2413 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2414 | #ifdef MS_WINDOWS |
| 2415 | PyDoc_STRVAR(win32_link__doc__, |
| 2416 | "link(src, dst)\n\n\ |
| 2417 | Create a hard link to a file."); |
| 2418 | |
| 2419 | static PyObject * |
| 2420 | win32_link(PyObject *self, PyObject *args) |
| 2421 | { |
| 2422 | PyObject *osrc, *odst; |
| 2423 | char *src, *dst; |
| 2424 | BOOL rslt; |
| 2425 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2426 | PyUnicodeObject *usrc, *udst; |
| 2427 | if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) { |
| 2428 | Py_BEGIN_ALLOW_THREADS |
| 2429 | rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst), |
| 2430 | PyUnicode_AS_UNICODE(usrc), NULL); |
| 2431 | Py_END_ALLOW_THREADS |
| 2432 | |
| 2433 | if (rslt == 0) |
| 2434 | return win32_error("link", NULL); |
| 2435 | |
| 2436 | Py_RETURN_NONE; |
| 2437 | } |
| 2438 | |
| 2439 | /* Narrow strings also valid. */ |
| 2440 | PyErr_Clear(); |
| 2441 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2442 | if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, |
| 2443 | PyUnicode_FSConverter, &odst)) |
| 2444 | return NULL; |
| 2445 | |
| 2446 | src = PyBytes_AsString(osrc); |
| 2447 | dst = PyBytes_AsString(odst); |
| 2448 | |
| 2449 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2450 | rslt = CreateHardLinkA(dst, src, NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2451 | Py_END_ALLOW_THREADS |
| 2452 | |
Stefan Krah | 30b341f | 2010-11-27 11:44:18 +0000 | [diff] [blame] | 2453 | Py_DECREF(osrc); |
| 2454 | Py_DECREF(odst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2455 | if (rslt == 0) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2456 | return win32_error("link", NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2457 | |
| 2458 | Py_RETURN_NONE; |
| 2459 | } |
| 2460 | #endif /* MS_WINDOWS */ |
| 2461 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2462 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2463 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2464 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2465 | Return a list containing the names of the entries in the directory.\n\ |
| 2466 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2467 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2468 | \n\ |
| 2469 | 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] | 2470 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2471 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2472 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2473 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2474 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2475 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2476 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2477 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2478 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2479 | PyObject *d, *v; |
| 2480 | HANDLE hFindFile; |
| 2481 | BOOL result; |
| 2482 | WIN32_FIND_DATA FileData; |
| 2483 | PyObject *opath; |
| 2484 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2485 | char *bufptr = namebuf; |
| 2486 | 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] | 2487 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2488 | PyObject *po = NULL; |
| 2489 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2490 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2491 | Py_UNICODE *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2492 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2493 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2494 | po_wchars = L"."; |
| 2495 | len = 1; |
| 2496 | } else { |
| 2497 | po_wchars = PyUnicode_AS_UNICODE(po); |
| 2498 | len = PyUnicode_GET_SIZE(po); |
| 2499 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2500 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2501 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2502 | if (!wnamebuf) { |
| 2503 | PyErr_NoMemory(); |
| 2504 | return NULL; |
| 2505 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2506 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2507 | if (len > 0) { |
| 2508 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2509 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2510 | wnamebuf[len++] = L'\\'; |
| 2511 | wcscpy(wnamebuf + len, L"*.*"); |
| 2512 | } |
| 2513 | if ((d = PyList_New(0)) == NULL) { |
| 2514 | free(wnamebuf); |
| 2515 | return NULL; |
| 2516 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2517 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2518 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2519 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2520 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2521 | int error = GetLastError(); |
| 2522 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2523 | free(wnamebuf); |
| 2524 | return d; |
| 2525 | } |
| 2526 | Py_DECREF(d); |
| 2527 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2528 | free(wnamebuf); |
| 2529 | return NULL; |
| 2530 | } |
| 2531 | do { |
| 2532 | /* Skip over . and .. */ |
| 2533 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2534 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2535 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2536 | if (v == NULL) { |
| 2537 | Py_DECREF(d); |
| 2538 | d = NULL; |
| 2539 | break; |
| 2540 | } |
| 2541 | if (PyList_Append(d, v) != 0) { |
| 2542 | Py_DECREF(v); |
| 2543 | Py_DECREF(d); |
| 2544 | d = NULL; |
| 2545 | break; |
| 2546 | } |
| 2547 | Py_DECREF(v); |
| 2548 | } |
| 2549 | Py_BEGIN_ALLOW_THREADS |
| 2550 | result = FindNextFileW(hFindFile, &wFileData); |
| 2551 | Py_END_ALLOW_THREADS |
| 2552 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2553 | it got to the end of the directory. */ |
| 2554 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2555 | Py_DECREF(d); |
| 2556 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2557 | FindClose(hFindFile); |
| 2558 | free(wnamebuf); |
| 2559 | return NULL; |
| 2560 | } |
| 2561 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2562 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2563 | if (FindClose(hFindFile) == FALSE) { |
| 2564 | Py_DECREF(d); |
| 2565 | win32_error_unicode("FindClose", wnamebuf); |
| 2566 | free(wnamebuf); |
| 2567 | return NULL; |
| 2568 | } |
| 2569 | free(wnamebuf); |
| 2570 | return d; |
| 2571 | } |
| 2572 | /* Drop the argument parsing error as narrow strings |
| 2573 | are also valid. */ |
| 2574 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2575 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2576 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2577 | PyUnicode_FSConverter, &opath)) |
| 2578 | return NULL; |
| 2579 | if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { |
| 2580 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2581 | Py_DECREF(opath); |
| 2582 | return NULL; |
| 2583 | } |
| 2584 | strcpy(namebuf, PyBytes_AsString(opath)); |
| 2585 | len = PyObject_Size(opath); |
Stefan Krah | 2a7feee | 2010-11-27 22:06:49 +0000 | [diff] [blame] | 2586 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2587 | if (len > 0) { |
| 2588 | char ch = namebuf[len-1]; |
| 2589 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2590 | namebuf[len++] = '/'; |
| 2591 | strcpy(namebuf + len, "*.*"); |
| 2592 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2593 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2594 | if ((d = PyList_New(0)) == NULL) |
| 2595 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2596 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2597 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2598 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2599 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2600 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2601 | int error = GetLastError(); |
| 2602 | if (error == ERROR_FILE_NOT_FOUND) |
| 2603 | return d; |
| 2604 | Py_DECREF(d); |
| 2605 | return win32_error("FindFirstFile", namebuf); |
| 2606 | } |
| 2607 | do { |
| 2608 | /* Skip over . and .. */ |
| 2609 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2610 | strcmp(FileData.cFileName, "..") != 0) { |
| 2611 | v = PyBytes_FromString(FileData.cFileName); |
| 2612 | if (v == NULL) { |
| 2613 | Py_DECREF(d); |
| 2614 | d = NULL; |
| 2615 | break; |
| 2616 | } |
| 2617 | if (PyList_Append(d, v) != 0) { |
| 2618 | Py_DECREF(v); |
| 2619 | Py_DECREF(d); |
| 2620 | d = NULL; |
| 2621 | break; |
| 2622 | } |
| 2623 | Py_DECREF(v); |
| 2624 | } |
| 2625 | Py_BEGIN_ALLOW_THREADS |
| 2626 | result = FindNextFile(hFindFile, &FileData); |
| 2627 | Py_END_ALLOW_THREADS |
| 2628 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2629 | it got to the end of the directory. */ |
| 2630 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2631 | Py_DECREF(d); |
| 2632 | win32_error("FindNextFile", namebuf); |
| 2633 | FindClose(hFindFile); |
| 2634 | return NULL; |
| 2635 | } |
| 2636 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2637 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2638 | if (FindClose(hFindFile) == FALSE) { |
| 2639 | Py_DECREF(d); |
| 2640 | return win32_error("FindClose", namebuf); |
| 2641 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2642 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2643 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2644 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2645 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2646 | |
| 2647 | #ifndef MAX_PATH |
| 2648 | #define MAX_PATH CCHMAXPATH |
| 2649 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2650 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2651 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2652 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2653 | PyObject *d, *v; |
| 2654 | char namebuf[MAX_PATH+5]; |
| 2655 | HDIR hdir = 1; |
| 2656 | ULONG srchcnt = 1; |
| 2657 | FILEFINDBUF3 ep; |
| 2658 | APIRET rc; |
| 2659 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2660 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2661 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2662 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2663 | name = PyBytes_AsString(oname); |
| 2664 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2665 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2666 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2667 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2668 | return NULL; |
| 2669 | } |
| 2670 | strcpy(namebuf, name); |
| 2671 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2672 | if (*pt == ALTSEP) |
| 2673 | *pt = SEP; |
| 2674 | if (namebuf[len-1] != SEP) |
| 2675 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2676 | strcpy(namebuf + len, "*.*"); |
| 2677 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2678 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2679 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2680 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2681 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2682 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2683 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2684 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2685 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2686 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2687 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2688 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2689 | |
| 2690 | if (rc != NO_ERROR) { |
| 2691 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2692 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2695 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2696 | do { |
| 2697 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2698 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2699 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2700 | |
| 2701 | strcpy(namebuf, ep.achName); |
| 2702 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2703 | /* Leave Case of Name Alone -- In Native Form */ |
| 2704 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2705 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2706 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2707 | if (v == NULL) { |
| 2708 | Py_DECREF(d); |
| 2709 | d = NULL; |
| 2710 | break; |
| 2711 | } |
| 2712 | if (PyList_Append(d, v) != 0) { |
| 2713 | Py_DECREF(v); |
| 2714 | Py_DECREF(d); |
| 2715 | d = NULL; |
| 2716 | break; |
| 2717 | } |
| 2718 | Py_DECREF(v); |
| 2719 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2720 | } |
| 2721 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2722 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2723 | return d; |
| 2724 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2725 | PyObject *oname; |
| 2726 | char *name; |
| 2727 | PyObject *d, *v; |
| 2728 | DIR *dirp; |
| 2729 | struct dirent *ep; |
| 2730 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2731 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2732 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2733 | /* v is never read, so it does not need to be initialized yet. */ |
| 2734 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2735 | arg_is_unicode = 0; |
| 2736 | PyErr_Clear(); |
| 2737 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2738 | oname = NULL; |
| 2739 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2740 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2741 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2742 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2743 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2744 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2745 | Py_BEGIN_ALLOW_THREADS |
| 2746 | dirp = opendir(name); |
| 2747 | Py_END_ALLOW_THREADS |
| 2748 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2749 | return posix_error_with_allocated_filename(oname); |
| 2750 | } |
| 2751 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2752 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2753 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2754 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2755 | Py_DECREF(oname); |
| 2756 | return NULL; |
| 2757 | } |
| 2758 | for (;;) { |
| 2759 | errno = 0; |
| 2760 | Py_BEGIN_ALLOW_THREADS |
| 2761 | ep = readdir(dirp); |
| 2762 | Py_END_ALLOW_THREADS |
| 2763 | if (ep == NULL) { |
| 2764 | if (errno == 0) { |
| 2765 | break; |
| 2766 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2767 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2768 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2769 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2770 | Py_DECREF(d); |
| 2771 | return posix_error_with_allocated_filename(oname); |
| 2772 | } |
| 2773 | } |
| 2774 | if (ep->d_name[0] == '.' && |
| 2775 | (NAMLEN(ep) == 1 || |
| 2776 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2777 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2778 | if (arg_is_unicode) |
| 2779 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2780 | else |
| 2781 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2782 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2783 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2784 | break; |
| 2785 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2786 | if (PyList_Append(d, v) != 0) { |
| 2787 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2788 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2789 | break; |
| 2790 | } |
| 2791 | Py_DECREF(v); |
| 2792 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2793 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2794 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2795 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2796 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2797 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2798 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2799 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2800 | #endif /* which OS */ |
| 2801 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2802 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2803 | #ifdef HAVE_FDOPENDIR |
| 2804 | PyDoc_STRVAR(posix_fdlistdir__doc__, |
| 2805 | "fdlistdir(fd) -> list_of_strings\n\n\ |
| 2806 | Like listdir(), but uses a file descriptor instead.\n\ |
| 2807 | After succesful execution of this function, fd will be closed."); |
| 2808 | |
| 2809 | static PyObject * |
| 2810 | posix_fdlistdir(PyObject *self, PyObject *args) |
| 2811 | { |
| 2812 | PyObject *d, *v; |
| 2813 | DIR *dirp; |
| 2814 | struct dirent *ep; |
| 2815 | int fd; |
| 2816 | |
| 2817 | errno = 0; |
| 2818 | if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd)) |
| 2819 | return NULL; |
| 2820 | Py_BEGIN_ALLOW_THREADS |
| 2821 | dirp = fdopendir(fd); |
| 2822 | Py_END_ALLOW_THREADS |
| 2823 | if (dirp == NULL) { |
| 2824 | close(fd); |
| 2825 | return posix_error(); |
| 2826 | } |
| 2827 | if ((d = PyList_New(0)) == NULL) { |
| 2828 | Py_BEGIN_ALLOW_THREADS |
| 2829 | closedir(dirp); |
| 2830 | Py_END_ALLOW_THREADS |
| 2831 | return NULL; |
| 2832 | } |
| 2833 | for (;;) { |
| 2834 | errno = 0; |
| 2835 | Py_BEGIN_ALLOW_THREADS |
| 2836 | ep = readdir(dirp); |
| 2837 | Py_END_ALLOW_THREADS |
| 2838 | if (ep == NULL) { |
| 2839 | if (errno == 0) { |
| 2840 | break; |
| 2841 | } else { |
| 2842 | Py_BEGIN_ALLOW_THREADS |
| 2843 | closedir(dirp); |
| 2844 | Py_END_ALLOW_THREADS |
| 2845 | Py_DECREF(d); |
| 2846 | return posix_error(); |
| 2847 | } |
| 2848 | } |
| 2849 | if (ep->d_name[0] == '.' && |
| 2850 | (NAMLEN(ep) == 1 || |
| 2851 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2852 | continue; |
| 2853 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2854 | if (v == NULL) { |
| 2855 | Py_CLEAR(d); |
| 2856 | break; |
| 2857 | } |
| 2858 | if (PyList_Append(d, v) != 0) { |
| 2859 | Py_DECREF(v); |
| 2860 | Py_CLEAR(d); |
| 2861 | break; |
| 2862 | } |
| 2863 | Py_DECREF(v); |
| 2864 | } |
| 2865 | Py_BEGIN_ALLOW_THREADS |
| 2866 | closedir(dirp); |
| 2867 | Py_END_ALLOW_THREADS |
| 2868 | |
| 2869 | return d; |
| 2870 | } |
| 2871 | #endif |
| 2872 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2873 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2874 | /* A helper function for abspath on win32 */ |
| 2875 | static PyObject * |
| 2876 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2877 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2878 | PyObject *opath; |
| 2879 | char *path; |
| 2880 | char outbuf[MAX_PATH*2]; |
| 2881 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2882 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2883 | PyUnicodeObject *po; |
| 2884 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2885 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2886 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2887 | Py_UNICODE *wtemp; |
| 2888 | DWORD result; |
| 2889 | PyObject *v; |
| 2890 | result = GetFullPathNameW(wpath, |
| 2891 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2892 | woutbuf, &wtemp); |
| 2893 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2894 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2895 | if (!woutbufp) |
| 2896 | return PyErr_NoMemory(); |
| 2897 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2898 | } |
| 2899 | if (result) |
| 2900 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2901 | else |
| 2902 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2903 | if (woutbufp != woutbuf) |
| 2904 | free(woutbufp); |
| 2905 | return v; |
| 2906 | } |
| 2907 | /* Drop the argument parsing error as narrow strings |
| 2908 | are also valid. */ |
| 2909 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2910 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2911 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2912 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2913 | PyUnicode_FSConverter, &opath)) |
| 2914 | return NULL; |
| 2915 | path = PyBytes_AsString(opath); |
| 2916 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2917 | outbuf, &temp)) { |
| 2918 | win32_error("GetFullPathName", path); |
| 2919 | Py_DECREF(opath); |
| 2920 | return NULL; |
| 2921 | } |
| 2922 | Py_DECREF(opath); |
| 2923 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2924 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2925 | Py_FileSystemDefaultEncoding, NULL); |
| 2926 | } |
| 2927 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2928 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2929 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 2930 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2931 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2932 | /* A helper function for samepath on windows */ |
| 2933 | static PyObject * |
| 2934 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 2935 | { |
| 2936 | HANDLE hFile; |
| 2937 | int buf_size; |
| 2938 | wchar_t *target_path; |
| 2939 | int result_length; |
| 2940 | PyObject *result; |
| 2941 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2942 | |
Brian Curtin | 94622b0 | 2010-09-24 00:03:39 +0000 | [diff] [blame] | 2943 | if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) { |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2944 | return NULL; |
| 2945 | } |
| 2946 | |
| 2947 | if(!check_GetFinalPathNameByHandle()) { |
| 2948 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 2949 | NotImplementedError. */ |
| 2950 | return PyErr_Format(PyExc_NotImplementedError, |
| 2951 | "GetFinalPathNameByHandle not available on this platform"); |
| 2952 | } |
| 2953 | |
| 2954 | hFile = CreateFileW( |
| 2955 | path, |
| 2956 | 0, /* desired access */ |
| 2957 | 0, /* share mode */ |
| 2958 | NULL, /* security attributes */ |
| 2959 | OPEN_EXISTING, |
| 2960 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 2961 | FILE_FLAG_BACKUP_SEMANTICS, |
| 2962 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2963 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2964 | if(hFile == INVALID_HANDLE_VALUE) { |
| 2965 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 2966 | return PyErr_Format(PyExc_RuntimeError, |
| 2967 | "Could not get a handle to file."); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | /* We have a good handle to the target, use it to determine the |
| 2971 | target path name. */ |
| 2972 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 2973 | |
| 2974 | if(!buf_size) |
| 2975 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2976 | |
| 2977 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 2978 | if(!target_path) |
| 2979 | return PyErr_NoMemory(); |
| 2980 | |
| 2981 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 2982 | buf_size, VOLUME_NAME_DOS); |
| 2983 | if(!result_length) |
| 2984 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
| 2985 | |
| 2986 | if(!CloseHandle(hFile)) |
| 2987 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2988 | |
| 2989 | target_path[result_length] = 0; |
| 2990 | result = PyUnicode_FromUnicode(target_path, result_length); |
| 2991 | free(target_path); |
| 2992 | return result; |
| 2993 | |
| 2994 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2995 | |
| 2996 | static PyObject * |
| 2997 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 2998 | { |
| 2999 | HANDLE hFile; |
| 3000 | BY_HANDLE_FILE_INFORMATION info; |
| 3001 | int fd; |
| 3002 | |
| 3003 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3004 | return NULL; |
| 3005 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3006 | if (!_PyVerify_fd(fd)) |
| 3007 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3008 | |
| 3009 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3010 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3011 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3012 | |
| 3013 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3014 | return win32_error("_getfileinformation", NULL); |
| 3015 | |
| 3016 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3017 | info.nFileIndexHigh, |
| 3018 | info.nFileIndexLow); |
| 3019 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3020 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3021 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3022 | "Return true if the pathname refers to an existing directory."); |
| 3023 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3024 | static PyObject * |
| 3025 | posix__isdir(PyObject *self, PyObject *args) |
| 3026 | { |
| 3027 | PyObject *opath; |
| 3028 | char *path; |
| 3029 | PyUnicodeObject *po; |
| 3030 | DWORD attributes; |
| 3031 | |
| 3032 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
| 3033 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 3034 | |
| 3035 | attributes = GetFileAttributesW(wpath); |
| 3036 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3037 | Py_RETURN_FALSE; |
| 3038 | goto check; |
| 3039 | } |
| 3040 | /* Drop the argument parsing error as narrow strings |
| 3041 | are also valid. */ |
| 3042 | PyErr_Clear(); |
| 3043 | |
| 3044 | if (!PyArg_ParseTuple(args, "O&:_isdir", |
| 3045 | PyUnicode_FSConverter, &opath)) |
| 3046 | return NULL; |
| 3047 | |
| 3048 | path = PyBytes_AsString(opath); |
| 3049 | attributes = GetFileAttributesA(path); |
| 3050 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3051 | Py_RETURN_FALSE; |
| 3052 | |
| 3053 | check: |
| 3054 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3055 | Py_RETURN_TRUE; |
| 3056 | else |
| 3057 | Py_RETURN_FALSE; |
| 3058 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3059 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3061 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3062 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3063 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3064 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3065 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3066 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3067 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3068 | int res; |
| 3069 | PyObject *opath; |
| 3070 | char *path; |
| 3071 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3072 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3073 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3074 | PyUnicodeObject *po; |
| 3075 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 3076 | Py_BEGIN_ALLOW_THREADS |
| 3077 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 3078 | it is a simple dereference. */ |
| 3079 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 3080 | Py_END_ALLOW_THREADS |
| 3081 | if (!res) |
| 3082 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 3083 | Py_INCREF(Py_None); |
| 3084 | return Py_None; |
| 3085 | } |
| 3086 | /* Drop the argument parsing error as narrow strings |
| 3087 | are also valid. */ |
| 3088 | PyErr_Clear(); |
| 3089 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3090 | PyUnicode_FSConverter, &opath, &mode)) |
| 3091 | return NULL; |
| 3092 | path = PyBytes_AsString(opath); |
| 3093 | Py_BEGIN_ALLOW_THREADS |
| 3094 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 3095 | it is a simple dereference. */ |
| 3096 | res = CreateDirectoryA(path, NULL); |
| 3097 | Py_END_ALLOW_THREADS |
| 3098 | if (!res) { |
| 3099 | win32_error("mkdir", path); |
| 3100 | Py_DECREF(opath); |
| 3101 | return NULL; |
| 3102 | } |
| 3103 | Py_DECREF(opath); |
| 3104 | Py_INCREF(Py_None); |
| 3105 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3106 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3107 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3108 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3109 | PyUnicode_FSConverter, &opath, &mode)) |
| 3110 | return NULL; |
| 3111 | path = PyBytes_AsString(opath); |
| 3112 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3113 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3114 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3115 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3116 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3117 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3118 | Py_END_ALLOW_THREADS |
| 3119 | if (res < 0) |
| 3120 | return posix_error_with_allocated_filename(opath); |
| 3121 | Py_DECREF(opath); |
| 3122 | Py_INCREF(Py_None); |
| 3123 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3124 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3127 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3128 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3129 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3130 | #include <sys/resource.h> |
| 3131 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3132 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3133 | |
| 3134 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3135 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3136 | "nice(inc) -> new_priority\n\n\ |
| 3137 | 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] | 3138 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3139 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3140 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3141 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3142 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3143 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3144 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3145 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3146 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3147 | /* There are two flavours of 'nice': one that returns the new |
| 3148 | priority (as required by almost all standards out there) and the |
| 3149 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3150 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3152 | If we are of the nice family that returns the new priority, we |
| 3153 | need to clear errno before the call, and check if errno is filled |
| 3154 | before calling posix_error() on a returnvalue of -1, because the |
| 3155 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3156 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3157 | errno = 0; |
| 3158 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3159 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3160 | if (value == 0) |
| 3161 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3162 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3163 | if (value == -1 && errno != 0) |
| 3164 | /* either nice() or getpriority() returned an error */ |
| 3165 | return posix_error(); |
| 3166 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3167 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3168 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3169 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3170 | |
| 3171 | #ifdef HAVE_GETPRIORITY |
| 3172 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3173 | "getpriority(which, who) -> current_priority\n\n\ |
| 3174 | Get program scheduling priority."); |
| 3175 | |
| 3176 | static PyObject * |
| 3177 | posix_getpriority(PyObject *self, PyObject *args) |
| 3178 | { |
| 3179 | int which, who, retval; |
| 3180 | |
| 3181 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3182 | return NULL; |
| 3183 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3184 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3185 | if (errno != 0) |
| 3186 | return posix_error(); |
| 3187 | return PyLong_FromLong((long)retval); |
| 3188 | } |
| 3189 | #endif /* HAVE_GETPRIORITY */ |
| 3190 | |
| 3191 | |
| 3192 | #ifdef HAVE_SETPRIORITY |
| 3193 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3194 | "setpriority(which, who, prio) -> None\n\n\ |
| 3195 | Set program scheduling priority."); |
| 3196 | |
| 3197 | static PyObject * |
| 3198 | posix_setpriority(PyObject *self, PyObject *args) |
| 3199 | { |
| 3200 | int which, who, prio, retval; |
| 3201 | |
| 3202 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3203 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3204 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3205 | if (retval == -1) |
| 3206 | return posix_error(); |
| 3207 | Py_RETURN_NONE; |
| 3208 | } |
| 3209 | #endif /* HAVE_SETPRIORITY */ |
| 3210 | |
| 3211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3212 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3213 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3214 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3215 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3216 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3217 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3218 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3219 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3220 | PyObject *o1, *o2; |
| 3221 | char *p1, *p2; |
| 3222 | BOOL result; |
| 3223 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 3224 | goto error; |
| 3225 | if (!convert_to_unicode(&o1)) |
| 3226 | goto error; |
| 3227 | if (!convert_to_unicode(&o2)) { |
| 3228 | Py_DECREF(o1); |
| 3229 | goto error; |
| 3230 | } |
| 3231 | Py_BEGIN_ALLOW_THREADS |
| 3232 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 3233 | PyUnicode_AsUnicode(o2)); |
| 3234 | Py_END_ALLOW_THREADS |
| 3235 | Py_DECREF(o1); |
| 3236 | Py_DECREF(o2); |
| 3237 | if (!result) |
| 3238 | return win32_error("rename", NULL); |
| 3239 | Py_INCREF(Py_None); |
| 3240 | return Py_None; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3241 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3242 | PyErr_Clear(); |
| 3243 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 3244 | return NULL; |
| 3245 | Py_BEGIN_ALLOW_THREADS |
| 3246 | result = MoveFileA(p1, p2); |
| 3247 | Py_END_ALLOW_THREADS |
| 3248 | if (!result) |
| 3249 | return win32_error("rename", NULL); |
| 3250 | Py_INCREF(Py_None); |
| 3251 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3252 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3253 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3254 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3257 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3258 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3259 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3260 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3261 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3262 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3263 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3264 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3265 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3266 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3267 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3268 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3269 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3272 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3273 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3274 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3275 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3276 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3277 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3278 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3279 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3280 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3281 | 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] | 3282 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3283 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3284 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3287 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3288 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3289 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3290 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3291 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3292 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3293 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3294 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3295 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3296 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3297 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3298 | wchar_t *command; |
| 3299 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3300 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3301 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3302 | Py_BEGIN_ALLOW_THREADS |
| 3303 | sts = _wsystem(command); |
| 3304 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3305 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3306 | PyObject *command_obj; |
| 3307 | char *command; |
| 3308 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3309 | PyUnicode_FSConverter, &command_obj)) |
| 3310 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3311 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3312 | command = PyBytes_AsString(command_obj); |
| 3313 | Py_BEGIN_ALLOW_THREADS |
| 3314 | sts = system(command); |
| 3315 | Py_END_ALLOW_THREADS |
| 3316 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3317 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3318 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3319 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3320 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3321 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3323 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3324 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3325 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3326 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3327 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3328 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3329 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3330 | int i; |
| 3331 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3332 | return NULL; |
| 3333 | i = (int)umask(i); |
| 3334 | if (i < 0) |
| 3335 | return posix_error(); |
| 3336 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3339 | #ifdef MS_WINDOWS |
| 3340 | |
| 3341 | /* override the default DeleteFileW behavior so that directory |
| 3342 | symlinks can be removed with this function, the same as with |
| 3343 | Unix symlinks */ |
| 3344 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3345 | { |
| 3346 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3347 | WIN32_FIND_DATAW find_data; |
| 3348 | HANDLE find_data_handle; |
| 3349 | int is_directory = 0; |
| 3350 | int is_link = 0; |
| 3351 | |
| 3352 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3353 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3354 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3355 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3356 | it is a symlink */ |
| 3357 | if(is_directory && |
| 3358 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3359 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3360 | |
| 3361 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3362 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3363 | FindClose(find_data_handle); |
| 3364 | } |
| 3365 | } |
| 3366 | } |
| 3367 | |
| 3368 | if (is_directory && is_link) |
| 3369 | return RemoveDirectoryW(lpFileName); |
| 3370 | |
| 3371 | return DeleteFileW(lpFileName); |
| 3372 | } |
| 3373 | #endif /* MS_WINDOWS */ |
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_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3376 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3377 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3378 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3379 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3380 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3381 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3382 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3383 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3384 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3385 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3386 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3387 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3388 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3389 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3390 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3391 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3394 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3395 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3396 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3397 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3398 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3399 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3400 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3401 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3402 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3403 | struct utsname u; |
| 3404 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3405 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3406 | Py_BEGIN_ALLOW_THREADS |
| 3407 | res = uname(&u); |
| 3408 | Py_END_ALLOW_THREADS |
| 3409 | if (res < 0) |
| 3410 | return posix_error(); |
| 3411 | return Py_BuildValue("(sssss)", |
| 3412 | u.sysname, |
| 3413 | u.nodename, |
| 3414 | u.release, |
| 3415 | u.version, |
| 3416 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3417 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3418 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3419 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3420 | |
| 3421 | /* |
| 3422 | * Classic POSIX utime functions supported microseconds (1m/sec). |
| 3423 | * Newer POSIX functions support nanoseconds (1 billion per sec). |
| 3424 | * posixmodule now uses the new functions where possible. |
| 3425 | * This improves accuracy in many situations, for example shutil.copy2(). |
| 3426 | * |
| 3427 | * The implementation isn't currently sophisticated enough to handle |
| 3428 | * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false. |
| 3429 | * Specifically, posix_futimes() would break. |
| 3430 | * |
| 3431 | * Supporting such a platform wouldn't be impossible; you'd need two |
| 3432 | * extract_time() functions, or make its precision a parameter. |
| 3433 | * Since such a platform seems unlikely we haven't bothered. |
| 3434 | */ |
| 3435 | #if defined(HAVE_UTIMENSAT) |
| 3436 | #define EXTRACT_TIME_PRECISION (1e9) |
| 3437 | #if !defined(HAVE_FUTIMENS) |
| 3438 | #error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment. |
| 3439 | #endif |
| 3440 | #else |
| 3441 | #define EXTRACT_TIME_PRECISION (1e6) |
| 3442 | #endif |
| 3443 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3444 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3445 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3446 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3447 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3448 | if (PyFloat_Check(t)) { |
| 3449 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3450 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3451 | if (!intobj) |
| 3452 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3453 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3454 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3455 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3456 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3457 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3458 | Py_DECREF(intobj); |
| 3459 | if (intval == -1 && PyErr_Occurred()) |
| 3460 | return -1; |
| 3461 | *sec = intval; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3462 | |
| 3463 | *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3464 | if (*usec < 0) |
| 3465 | /* If rounding gave us a negative number, |
| 3466 | truncate. */ |
| 3467 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3468 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3469 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3470 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3471 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3472 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3473 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3474 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3475 | if (intval == -1 && PyErr_Occurred()) |
| 3476 | return -1; |
| 3477 | *sec = intval; |
| 3478 | *usec = 0; |
| 3479 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3480 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3482 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3483 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3484 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3485 | 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] | 3486 | 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] | 3487 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3488 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3489 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3490 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3491 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3492 | PyObject *arg; |
| 3493 | PyUnicodeObject *obwpath; |
| 3494 | wchar_t *wpath = NULL; |
| 3495 | PyObject *oapath; |
| 3496 | char *apath; |
| 3497 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3498 | time_t atimesec, mtimesec; |
| 3499 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3500 | FILETIME atime, mtime; |
| 3501 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3502 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3503 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 3504 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 3505 | Py_BEGIN_ALLOW_THREADS |
| 3506 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3507 | NULL, OPEN_EXISTING, |
| 3508 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3509 | Py_END_ALLOW_THREADS |
| 3510 | if (hFile == INVALID_HANDLE_VALUE) |
| 3511 | return win32_error_unicode("utime", wpath); |
| 3512 | } else |
| 3513 | /* Drop the argument parsing error as narrow strings |
| 3514 | are also valid. */ |
| 3515 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3517 | if (!wpath) { |
| 3518 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3519 | PyUnicode_FSConverter, &oapath, &arg)) |
| 3520 | return NULL; |
| 3521 | apath = PyBytes_AsString(oapath); |
| 3522 | Py_BEGIN_ALLOW_THREADS |
| 3523 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3524 | NULL, OPEN_EXISTING, |
| 3525 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3526 | Py_END_ALLOW_THREADS |
| 3527 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3528 | win32_error("utime", apath); |
| 3529 | Py_DECREF(oapath); |
| 3530 | return NULL; |
| 3531 | } |
| 3532 | Py_DECREF(oapath); |
| 3533 | } |
| 3534 | |
| 3535 | if (arg == Py_None) { |
| 3536 | SYSTEMTIME now; |
| 3537 | GetSystemTime(&now); |
| 3538 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3539 | !SystemTimeToFileTime(&now, &atime)) { |
| 3540 | win32_error("utime", NULL); |
| 3541 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3542 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3543 | } |
| 3544 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3545 | PyErr_SetString(PyExc_TypeError, |
| 3546 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3547 | goto done; |
| 3548 | } |
| 3549 | else { |
| 3550 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3551 | &atimesec, &ausec) == -1) |
| 3552 | goto done; |
| 3553 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3554 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3555 | &mtimesec, &musec) == -1) |
| 3556 | goto done; |
| 3557 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3558 | } |
| 3559 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3560 | /* Avoid putting the file name into the error here, |
| 3561 | as that may confuse the user into believing that |
| 3562 | something is wrong with the file, when it also |
| 3563 | could be the time stamp that gives a problem. */ |
| 3564 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3565 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3566 | } |
| 3567 | Py_INCREF(Py_None); |
| 3568 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3569 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3570 | CloseHandle(hFile); |
| 3571 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3572 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3573 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3574 | PyObject *opath; |
| 3575 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3576 | time_t atime, mtime; |
| 3577 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3578 | int res; |
| 3579 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3580 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3581 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3582 | PyUnicode_FSConverter, &opath, &arg)) |
| 3583 | return NULL; |
| 3584 | path = PyBytes_AsString(opath); |
| 3585 | if (arg == Py_None) { |
| 3586 | /* optional time values not given */ |
| 3587 | Py_BEGIN_ALLOW_THREADS |
| 3588 | res = utime(path, NULL); |
| 3589 | Py_END_ALLOW_THREADS |
| 3590 | } |
| 3591 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3592 | PyErr_SetString(PyExc_TypeError, |
| 3593 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3594 | Py_DECREF(opath); |
| 3595 | return NULL; |
| 3596 | } |
| 3597 | else { |
| 3598 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3599 | &atime, &ausec) == -1) { |
| 3600 | Py_DECREF(opath); |
| 3601 | return NULL; |
| 3602 | } |
| 3603 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3604 | &mtime, &musec) == -1) { |
| 3605 | Py_DECREF(opath); |
| 3606 | return NULL; |
| 3607 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3608 | |
| 3609 | Py_BEGIN_ALLOW_THREADS |
| 3610 | { |
| 3611 | #ifdef HAVE_UTIMENSAT |
| 3612 | struct timespec buf[2]; |
| 3613 | buf[0].tv_sec = atime; |
| 3614 | buf[0].tv_nsec = ausec; |
| 3615 | buf[1].tv_sec = mtime; |
| 3616 | buf[1].tv_nsec = musec; |
| 3617 | res = utimensat(AT_FDCWD, path, buf, 0); |
| 3618 | #elif defined(HAVE_UTIMES) |
| 3619 | struct timeval buf[2]; |
| 3620 | buf[0].tv_sec = atime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3621 | buf[0].tv_usec = ausec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3622 | buf[1].tv_sec = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3623 | buf[1].tv_usec = musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3624 | res = utimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3625 | #elif defined(HAVE_UTIME_H) |
| 3626 | /* XXX should define struct utimbuf instead, above */ |
| 3627 | struct utimbuf buf; |
| 3628 | buf.actime = atime; |
| 3629 | buf.modtime = mtime; |
| 3630 | res = utime(path, &buf); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3631 | #else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3632 | time_t buf[2]; |
| 3633 | buf[0] = atime; |
| 3634 | buf[1] = mtime; |
| 3635 | res = utime(path, buf); |
| 3636 | #endif |
| 3637 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3638 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3639 | } |
| 3640 | if (res < 0) { |
| 3641 | return posix_error_with_allocated_filename(opath); |
| 3642 | } |
| 3643 | Py_DECREF(opath); |
| 3644 | Py_INCREF(Py_None); |
| 3645 | return Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3646 | #undef UTIME_EXTRACT |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3647 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3650 | #ifdef HAVE_FUTIMES |
| 3651 | PyDoc_STRVAR(posix_futimes__doc__, |
| 3652 | "futimes(fd, (atime, mtime))\n\ |
| 3653 | futimes(fd, None)\n\n\ |
| 3654 | Set the access and modified time of the file specified by the file\n\ |
| 3655 | descriptor fd to the given values. If the second form is used, set the\n\ |
| 3656 | access and modified times to the current time."); |
| 3657 | |
| 3658 | static PyObject * |
| 3659 | posix_futimes(PyObject *self, PyObject *args) |
| 3660 | { |
| 3661 | int res, fd; |
| 3662 | PyObject* arg; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3663 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3664 | long ausec, musec; |
| 3665 | |
| 3666 | if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg)) |
| 3667 | return NULL; |
| 3668 | |
| 3669 | if (arg == Py_None) { |
| 3670 | /* optional time values not given */ |
| 3671 | Py_BEGIN_ALLOW_THREADS |
| 3672 | res = futimes(fd, NULL); |
| 3673 | Py_END_ALLOW_THREADS |
| 3674 | } |
| 3675 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3676 | PyErr_SetString(PyExc_TypeError, |
| 3677 | "futimes() arg 2 must be a tuple (atime, mtime)"); |
| 3678 | return NULL; |
| 3679 | } |
| 3680 | else { |
| 3681 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3682 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3683 | return NULL; |
| 3684 | } |
| 3685 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3686 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3687 | return NULL; |
| 3688 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3689 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3690 | { |
| 3691 | #ifdef HAVE_FUTIMENS |
| 3692 | struct timespec buf[2]; |
| 3693 | buf[0].tv_sec = atime; |
| 3694 | buf[0].tv_nsec = ausec; |
| 3695 | buf[1].tv_sec = mtime; |
| 3696 | buf[1].tv_nsec = musec; |
| 3697 | res = futimens(fd, buf); |
| 3698 | #else |
| 3699 | struct timeval buf[2]; |
| 3700 | buf[0].tv_sec = atime; |
| 3701 | buf[0].tv_usec = ausec; |
| 3702 | buf[1].tv_sec = mtime; |
| 3703 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3704 | res = futimes(fd, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3705 | #endif |
| 3706 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3707 | Py_END_ALLOW_THREADS |
| 3708 | } |
| 3709 | if (res < 0) |
| 3710 | return posix_error(); |
| 3711 | Py_RETURN_NONE; |
| 3712 | } |
| 3713 | #endif |
| 3714 | |
| 3715 | #ifdef HAVE_LUTIMES |
| 3716 | PyDoc_STRVAR(posix_lutimes__doc__, |
| 3717 | "lutimes(path, (atime, mtime))\n\ |
| 3718 | lutimes(path, None)\n\n\ |
| 3719 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3720 | |
| 3721 | static PyObject * |
| 3722 | posix_lutimes(PyObject *self, PyObject *args) |
| 3723 | { |
| 3724 | PyObject *opath, *arg; |
| 3725 | const char *path; |
| 3726 | int res; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3727 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3728 | long ausec, musec; |
| 3729 | |
| 3730 | if (!PyArg_ParseTuple(args, "O&O:lutimes", |
| 3731 | PyUnicode_FSConverter, &opath, &arg)) |
| 3732 | return NULL; |
| 3733 | path = PyBytes_AsString(opath); |
| 3734 | if (arg == Py_None) { |
| 3735 | /* optional time values not given */ |
| 3736 | Py_BEGIN_ALLOW_THREADS |
| 3737 | res = lutimes(path, NULL); |
| 3738 | Py_END_ALLOW_THREADS |
| 3739 | } |
| 3740 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3741 | PyErr_SetString(PyExc_TypeError, |
| 3742 | "lutimes() arg 2 must be a tuple (atime, mtime)"); |
| 3743 | Py_DECREF(opath); |
| 3744 | return NULL; |
| 3745 | } |
| 3746 | else { |
| 3747 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3748 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3749 | Py_DECREF(opath); |
| 3750 | return NULL; |
| 3751 | } |
| 3752 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3753 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3754 | Py_DECREF(opath); |
| 3755 | return NULL; |
| 3756 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3757 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3758 | { |
| 3759 | #ifdef HAVE_UTIMENSAT |
| 3760 | struct timespec buf[2]; |
| 3761 | buf[0].tv_sec = atime; |
| 3762 | buf[0].tv_nsec = ausec; |
| 3763 | buf[1].tv_sec = mtime; |
| 3764 | buf[1].tv_nsec = musec; |
| 3765 | res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); |
| 3766 | #else |
| 3767 | struct timeval buf[2]; |
| 3768 | buf[0].tv_sec = atime; |
| 3769 | buf[0].tv_usec = ausec; |
| 3770 | buf[1].tv_sec = mtime; |
| 3771 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3772 | res = lutimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3773 | #endif |
| 3774 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3775 | Py_END_ALLOW_THREADS |
| 3776 | } |
| 3777 | Py_DECREF(opath); |
| 3778 | if (res < 0) |
| 3779 | return posix_error(); |
| 3780 | Py_RETURN_NONE; |
| 3781 | } |
| 3782 | #endif |
| 3783 | |
| 3784 | #ifdef HAVE_FUTIMENS |
| 3785 | PyDoc_STRVAR(posix_futimens__doc__, |
| 3786 | "futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\ |
| 3787 | futimens(fd, None, None)\n\n\ |
| 3788 | Updates the timestamps of a file specified by the file descriptor fd, with\n\ |
| 3789 | nanosecond precision.\n\ |
| 3790 | The second form sets atime and mtime to the current time.\n\ |
| 3791 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 3792 | current time.\n\ |
| 3793 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 3794 | |
| 3795 | static PyObject * |
| 3796 | posix_futimens(PyObject *self, PyObject *args) |
| 3797 | { |
| 3798 | int res, fd; |
| 3799 | PyObject *atime, *mtime; |
| 3800 | struct timespec buf[2]; |
| 3801 | |
| 3802 | if (!PyArg_ParseTuple(args, "iOO:futimens", |
| 3803 | &fd, &atime, &mtime)) |
| 3804 | return NULL; |
| 3805 | if (atime == Py_None && mtime == Py_None) { |
| 3806 | /* optional time values not given */ |
| 3807 | Py_BEGIN_ALLOW_THREADS |
| 3808 | res = futimens(fd, NULL); |
| 3809 | Py_END_ALLOW_THREADS |
| 3810 | } |
| 3811 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 3812 | PyErr_SetString(PyExc_TypeError, |
| 3813 | "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)"); |
| 3814 | return NULL; |
| 3815 | } |
| 3816 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 3817 | PyErr_SetString(PyExc_TypeError, |
| 3818 | "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)"); |
| 3819 | return NULL; |
| 3820 | } |
| 3821 | else { |
| 3822 | if (!PyArg_ParseTuple(atime, "ll:futimens", |
| 3823 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 3824 | return NULL; |
| 3825 | } |
| 3826 | if (!PyArg_ParseTuple(mtime, "ll:futimens", |
| 3827 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 3828 | return NULL; |
| 3829 | } |
| 3830 | Py_BEGIN_ALLOW_THREADS |
| 3831 | res = futimens(fd, buf); |
| 3832 | Py_END_ALLOW_THREADS |
| 3833 | } |
| 3834 | if (res < 0) |
| 3835 | return posix_error(); |
| 3836 | Py_RETURN_NONE; |
| 3837 | } |
| 3838 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3839 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3840 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3841 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3842 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3843 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3844 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3845 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3846 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3847 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3848 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3849 | int sts; |
| 3850 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3851 | return NULL; |
| 3852 | _exit(sts); |
| 3853 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3856 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3857 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3858 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3859 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3860 | Py_ssize_t i; |
| 3861 | for (i = 0; i < count; i++) |
| 3862 | PyMem_Free(array[i]); |
| 3863 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3864 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3865 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3866 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3867 | int fsconvert_strdup(PyObject *o, char**out) |
| 3868 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3869 | PyObject *bytes; |
| 3870 | Py_ssize_t size; |
| 3871 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3872 | return 0; |
| 3873 | size = PyBytes_GET_SIZE(bytes); |
| 3874 | *out = PyMem_Malloc(size+1); |
| 3875 | if (!*out) |
| 3876 | return 0; |
| 3877 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3878 | Py_DECREF(bytes); |
| 3879 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3880 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3881 | #endif |
| 3882 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3883 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3884 | static char** |
| 3885 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3886 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3887 | char **envlist; |
| 3888 | Py_ssize_t i, pos, envc; |
| 3889 | PyObject *keys=NULL, *vals=NULL; |
| 3890 | PyObject *key, *val, *key2, *val2; |
| 3891 | char *p, *k, *v; |
| 3892 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3893 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3894 | i = PyMapping_Size(env); |
| 3895 | if (i < 0) |
| 3896 | return NULL; |
| 3897 | envlist = PyMem_NEW(char *, i + 1); |
| 3898 | if (envlist == NULL) { |
| 3899 | PyErr_NoMemory(); |
| 3900 | return NULL; |
| 3901 | } |
| 3902 | envc = 0; |
| 3903 | keys = PyMapping_Keys(env); |
| 3904 | vals = PyMapping_Values(env); |
| 3905 | if (!keys || !vals) |
| 3906 | goto error; |
| 3907 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3908 | PyErr_Format(PyExc_TypeError, |
| 3909 | "env.keys() or env.values() is not a list"); |
| 3910 | goto error; |
| 3911 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3913 | for (pos = 0; pos < i; pos++) { |
| 3914 | key = PyList_GetItem(keys, pos); |
| 3915 | val = PyList_GetItem(vals, pos); |
| 3916 | if (!key || !val) |
| 3917 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3918 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3919 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3920 | goto error; |
| 3921 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3922 | Py_DECREF(key2); |
| 3923 | goto error; |
| 3924 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3925 | |
| 3926 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3927 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3928 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3929 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3930 | k = PyBytes_AsString(key2); |
| 3931 | v = PyBytes_AsString(val2); |
| 3932 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3933 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3934 | p = PyMem_NEW(char, len); |
| 3935 | if (p == NULL) { |
| 3936 | PyErr_NoMemory(); |
| 3937 | Py_DECREF(key2); |
| 3938 | Py_DECREF(val2); |
| 3939 | goto error; |
| 3940 | } |
| 3941 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3942 | envlist[envc++] = p; |
| 3943 | Py_DECREF(key2); |
| 3944 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3945 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3946 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3947 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3948 | } |
| 3949 | Py_DECREF(vals); |
| 3950 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3951 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3952 | envlist[envc] = 0; |
| 3953 | *envc_ptr = envc; |
| 3954 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3955 | |
| 3956 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3957 | Py_XDECREF(keys); |
| 3958 | Py_XDECREF(vals); |
| 3959 | while (--envc >= 0) |
| 3960 | PyMem_DEL(envlist[envc]); |
| 3961 | PyMem_DEL(envlist); |
| 3962 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3963 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3964 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3965 | static char** |
| 3966 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 3967 | { |
| 3968 | int i; |
| 3969 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 3970 | if (argvlist == NULL) { |
| 3971 | PyErr_NoMemory(); |
| 3972 | return NULL; |
| 3973 | } |
| 3974 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3975 | PyObject* item = PySequence_ITEM(argv, i); |
| 3976 | if (item == NULL) |
| 3977 | goto fail; |
| 3978 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 3979 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3980 | goto fail; |
| 3981 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3982 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3983 | } |
| 3984 | argvlist[*argc] = NULL; |
| 3985 | return argvlist; |
| 3986 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3987 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3988 | free_string_array(argvlist, *argc); |
| 3989 | return NULL; |
| 3990 | } |
| 3991 | #endif |
| 3992 | |
| 3993 | #ifdef HAVE_EXECV |
| 3994 | PyDoc_STRVAR(posix_execv__doc__, |
| 3995 | "execv(path, args)\n\n\ |
| 3996 | Execute an executable path with arguments, replacing current process.\n\ |
| 3997 | \n\ |
| 3998 | path: path of executable file\n\ |
| 3999 | args: tuple or list of strings"); |
| 4000 | |
| 4001 | static PyObject * |
| 4002 | posix_execv(PyObject *self, PyObject *args) |
| 4003 | { |
| 4004 | PyObject *opath; |
| 4005 | char *path; |
| 4006 | PyObject *argv; |
| 4007 | char **argvlist; |
| 4008 | Py_ssize_t argc; |
| 4009 | |
| 4010 | /* execv has two arguments: (path, argv), where |
| 4011 | argv is a list or tuple of strings. */ |
| 4012 | |
| 4013 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4014 | PyUnicode_FSConverter, |
| 4015 | &opath, &argv)) |
| 4016 | return NULL; |
| 4017 | path = PyBytes_AsString(opath); |
| 4018 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4019 | PyErr_SetString(PyExc_TypeError, |
| 4020 | "execv() arg 2 must be a tuple or list"); |
| 4021 | Py_DECREF(opath); |
| 4022 | return NULL; |
| 4023 | } |
| 4024 | argc = PySequence_Size(argv); |
| 4025 | if (argc < 1) { |
| 4026 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4027 | Py_DECREF(opath); |
| 4028 | return NULL; |
| 4029 | } |
| 4030 | |
| 4031 | argvlist = parse_arglist(argv, &argc); |
| 4032 | if (argvlist == NULL) { |
| 4033 | Py_DECREF(opath); |
| 4034 | return NULL; |
| 4035 | } |
| 4036 | |
| 4037 | execv(path, argvlist); |
| 4038 | |
| 4039 | /* If we get here it's definitely an error */ |
| 4040 | |
| 4041 | free_string_array(argvlist, argc); |
| 4042 | Py_DECREF(opath); |
| 4043 | return posix_error(); |
| 4044 | } |
| 4045 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4046 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4047 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4048 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4049 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4050 | path: path of executable file\n\ |
| 4051 | args: tuple or list of arguments\n\ |
| 4052 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4053 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4054 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4055 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4056 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4057 | PyObject *opath; |
| 4058 | char *path; |
| 4059 | PyObject *argv, *env; |
| 4060 | char **argvlist; |
| 4061 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4062 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4063 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4064 | /* execve has three arguments: (path, argv, env), where |
| 4065 | argv is a list or tuple of strings and env is a dictionary |
| 4066 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4067 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4068 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4069 | PyUnicode_FSConverter, |
| 4070 | &opath, &argv, &env)) |
| 4071 | return NULL; |
| 4072 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4073 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4074 | PyErr_SetString(PyExc_TypeError, |
| 4075 | "execve() arg 2 must be a tuple or list"); |
| 4076 | goto fail_0; |
| 4077 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4078 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4079 | if (!PyMapping_Check(env)) { |
| 4080 | PyErr_SetString(PyExc_TypeError, |
| 4081 | "execve() arg 3 must be a mapping object"); |
| 4082 | goto fail_0; |
| 4083 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4084 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4085 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4086 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4087 | goto fail_0; |
| 4088 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4089 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4090 | envlist = parse_envlist(env, &envc); |
| 4091 | if (envlist == NULL) |
| 4092 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4093 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4094 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4096 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4098 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4099 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4100 | while (--envc >= 0) |
| 4101 | PyMem_DEL(envlist[envc]); |
| 4102 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4103 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4104 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4105 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4106 | Py_DECREF(opath); |
| 4107 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4108 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4109 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4110 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4111 | #ifdef HAVE_FEXECVE |
| 4112 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4113 | "fexecve(fd, args, env)\n\n\ |
| 4114 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4115 | environment, replacing the current process.\n\ |
| 4116 | \n\ |
| 4117 | fd: file descriptor of executable\n\ |
| 4118 | args: tuple or list of arguments\n\ |
| 4119 | env: dictionary of strings mapping to strings"); |
| 4120 | |
| 4121 | static PyObject * |
| 4122 | posix_fexecve(PyObject *self, PyObject *args) |
| 4123 | { |
| 4124 | int fd; |
| 4125 | PyObject *argv, *env; |
| 4126 | char **argvlist; |
| 4127 | char **envlist; |
| 4128 | Py_ssize_t argc, envc; |
| 4129 | |
| 4130 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4131 | &fd, &argv, &env)) |
| 4132 | return NULL; |
| 4133 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4134 | PyErr_SetString(PyExc_TypeError, |
| 4135 | "fexecve() arg 2 must be a tuple or list"); |
| 4136 | return NULL; |
| 4137 | } |
| 4138 | argc = PySequence_Size(argv); |
| 4139 | if (!PyMapping_Check(env)) { |
| 4140 | PyErr_SetString(PyExc_TypeError, |
| 4141 | "fexecve() arg 3 must be a mapping object"); |
| 4142 | return NULL; |
| 4143 | } |
| 4144 | |
| 4145 | argvlist = parse_arglist(argv, &argc); |
| 4146 | if (argvlist == NULL) |
| 4147 | return NULL; |
| 4148 | |
| 4149 | envlist = parse_envlist(env, &envc); |
| 4150 | if (envlist == NULL) |
| 4151 | goto fail; |
| 4152 | |
| 4153 | fexecve(fd, argvlist, envlist); |
| 4154 | |
| 4155 | /* If we get here it's definitely an error */ |
| 4156 | |
| 4157 | (void) posix_error(); |
| 4158 | |
| 4159 | while (--envc >= 0) |
| 4160 | PyMem_DEL(envlist[envc]); |
| 4161 | PyMem_DEL(envlist); |
| 4162 | fail: |
| 4163 | free_string_array(argvlist, argc); |
| 4164 | return NULL; |
| 4165 | } |
| 4166 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4167 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4168 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4169 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4170 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4171 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4172 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4173 | mode: mode of process creation\n\ |
| 4174 | path: path of executable file\n\ |
| 4175 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4176 | |
| 4177 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4178 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4179 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | PyObject *opath; |
| 4181 | char *path; |
| 4182 | PyObject *argv; |
| 4183 | char **argvlist; |
| 4184 | int mode, i; |
| 4185 | Py_ssize_t argc; |
| 4186 | Py_intptr_t spawnval; |
| 4187 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4188 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4189 | /* spawnv has three arguments: (mode, path, argv), where |
| 4190 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4192 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4193 | PyUnicode_FSConverter, |
| 4194 | &opath, &argv)) |
| 4195 | return NULL; |
| 4196 | path = PyBytes_AsString(opath); |
| 4197 | if (PyList_Check(argv)) { |
| 4198 | argc = PyList_Size(argv); |
| 4199 | getitem = PyList_GetItem; |
| 4200 | } |
| 4201 | else if (PyTuple_Check(argv)) { |
| 4202 | argc = PyTuple_Size(argv); |
| 4203 | getitem = PyTuple_GetItem; |
| 4204 | } |
| 4205 | else { |
| 4206 | PyErr_SetString(PyExc_TypeError, |
| 4207 | "spawnv() arg 2 must be a tuple or list"); |
| 4208 | Py_DECREF(opath); |
| 4209 | return NULL; |
| 4210 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4211 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4212 | argvlist = PyMem_NEW(char *, argc+1); |
| 4213 | if (argvlist == NULL) { |
| 4214 | Py_DECREF(opath); |
| 4215 | return PyErr_NoMemory(); |
| 4216 | } |
| 4217 | for (i = 0; i < argc; i++) { |
| 4218 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4219 | &argvlist[i])) { |
| 4220 | free_string_array(argvlist, i); |
| 4221 | PyErr_SetString( |
| 4222 | PyExc_TypeError, |
| 4223 | "spawnv() arg 2 must contain only strings"); |
| 4224 | Py_DECREF(opath); |
| 4225 | return NULL; |
| 4226 | } |
| 4227 | } |
| 4228 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4229 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4230 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4231 | Py_BEGIN_ALLOW_THREADS |
| 4232 | spawnval = spawnv(mode, path, argvlist); |
| 4233 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4234 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4235 | if (mode == _OLD_P_OVERLAY) |
| 4236 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4237 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4238 | Py_BEGIN_ALLOW_THREADS |
| 4239 | spawnval = _spawnv(mode, path, argvlist); |
| 4240 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4241 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4242 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4243 | free_string_array(argvlist, argc); |
| 4244 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4245 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4246 | if (spawnval == -1) |
| 4247 | return posix_error(); |
| 4248 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4249 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4250 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4251 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4252 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4253 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4257 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4258 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4259 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4260 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4261 | mode: mode of process creation\n\ |
| 4262 | path: path of executable file\n\ |
| 4263 | args: tuple or list of arguments\n\ |
| 4264 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4265 | |
| 4266 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4267 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4268 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4269 | PyObject *opath; |
| 4270 | char *path; |
| 4271 | PyObject *argv, *env; |
| 4272 | char **argvlist; |
| 4273 | char **envlist; |
| 4274 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4275 | int mode; |
| 4276 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4277 | Py_intptr_t spawnval; |
| 4278 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4279 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4280 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4281 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4282 | argv is a list or tuple of strings and env is a dictionary |
| 4283 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4284 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4285 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4286 | PyUnicode_FSConverter, |
| 4287 | &opath, &argv, &env)) |
| 4288 | return NULL; |
| 4289 | path = PyBytes_AsString(opath); |
| 4290 | if (PyList_Check(argv)) { |
| 4291 | argc = PyList_Size(argv); |
| 4292 | getitem = PyList_GetItem; |
| 4293 | } |
| 4294 | else if (PyTuple_Check(argv)) { |
| 4295 | argc = PyTuple_Size(argv); |
| 4296 | getitem = PyTuple_GetItem; |
| 4297 | } |
| 4298 | else { |
| 4299 | PyErr_SetString(PyExc_TypeError, |
| 4300 | "spawnve() arg 2 must be a tuple or list"); |
| 4301 | goto fail_0; |
| 4302 | } |
| 4303 | if (!PyMapping_Check(env)) { |
| 4304 | PyErr_SetString(PyExc_TypeError, |
| 4305 | "spawnve() arg 3 must be a mapping object"); |
| 4306 | goto fail_0; |
| 4307 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4308 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4309 | argvlist = PyMem_NEW(char *, argc+1); |
| 4310 | if (argvlist == NULL) { |
| 4311 | PyErr_NoMemory(); |
| 4312 | goto fail_0; |
| 4313 | } |
| 4314 | for (i = 0; i < argc; i++) { |
| 4315 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4316 | &argvlist[i])) |
| 4317 | { |
| 4318 | lastarg = i; |
| 4319 | goto fail_1; |
| 4320 | } |
| 4321 | } |
| 4322 | lastarg = argc; |
| 4323 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4324 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4325 | envlist = parse_envlist(env, &envc); |
| 4326 | if (envlist == NULL) |
| 4327 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4328 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4329 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4330 | Py_BEGIN_ALLOW_THREADS |
| 4331 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4332 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4333 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4334 | if (mode == _OLD_P_OVERLAY) |
| 4335 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4336 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4337 | Py_BEGIN_ALLOW_THREADS |
| 4338 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4339 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4340 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4341 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4342 | if (spawnval == -1) |
| 4343 | (void) posix_error(); |
| 4344 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4345 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4346 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4347 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4348 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4349 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4351 | while (--envc >= 0) |
| 4352 | PyMem_DEL(envlist[envc]); |
| 4353 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4354 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4355 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4356 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4357 | Py_DECREF(opath); |
| 4358 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4359 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4360 | |
| 4361 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4362 | #if defined(PYOS_OS2) |
| 4363 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4364 | "spawnvp(mode, file, args)\n\n\ |
| 4365 | Execute the program 'file' in a new process, using the environment\n\ |
| 4366 | search path to find the file.\n\ |
| 4367 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4368 | mode: mode of process creation\n\ |
| 4369 | file: executable file name\n\ |
| 4370 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4371 | |
| 4372 | static PyObject * |
| 4373 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4374 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4375 | PyObject *opath; |
| 4376 | char *path; |
| 4377 | PyObject *argv; |
| 4378 | char **argvlist; |
| 4379 | int mode, i, argc; |
| 4380 | Py_intptr_t spawnval; |
| 4381 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4382 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4383 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4384 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4385 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4386 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4387 | PyUnicode_FSConverter, |
| 4388 | &opath, &argv)) |
| 4389 | return NULL; |
| 4390 | path = PyBytes_AsString(opath); |
| 4391 | if (PyList_Check(argv)) { |
| 4392 | argc = PyList_Size(argv); |
| 4393 | getitem = PyList_GetItem; |
| 4394 | } |
| 4395 | else if (PyTuple_Check(argv)) { |
| 4396 | argc = PyTuple_Size(argv); |
| 4397 | getitem = PyTuple_GetItem; |
| 4398 | } |
| 4399 | else { |
| 4400 | PyErr_SetString(PyExc_TypeError, |
| 4401 | "spawnvp() arg 2 must be a tuple or list"); |
| 4402 | Py_DECREF(opath); |
| 4403 | return NULL; |
| 4404 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4405 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4406 | argvlist = PyMem_NEW(char *, argc+1); |
| 4407 | if (argvlist == NULL) { |
| 4408 | Py_DECREF(opath); |
| 4409 | return PyErr_NoMemory(); |
| 4410 | } |
| 4411 | for (i = 0; i < argc; i++) { |
| 4412 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4413 | &argvlist[i])) { |
| 4414 | free_string_array(argvlist, i); |
| 4415 | PyErr_SetString( |
| 4416 | PyExc_TypeError, |
| 4417 | "spawnvp() arg 2 must contain only strings"); |
| 4418 | Py_DECREF(opath); |
| 4419 | return NULL; |
| 4420 | } |
| 4421 | } |
| 4422 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4423 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4424 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4425 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4426 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4427 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4428 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4429 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4430 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4431 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4432 | free_string_array(argvlist, argc); |
| 4433 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4434 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4435 | if (spawnval == -1) |
| 4436 | return posix_error(); |
| 4437 | else |
| 4438 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | |
| 4442 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4443 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4444 | Execute the program 'file' in a new process, using the environment\n\ |
| 4445 | search path to find the file.\n\ |
| 4446 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4447 | mode: mode of process creation\n\ |
| 4448 | file: executable file name\n\ |
| 4449 | args: tuple or list of arguments\n\ |
| 4450 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4451 | |
| 4452 | static PyObject * |
| 4453 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4454 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4455 | PyObject *opath |
| 4456 | char *path; |
| 4457 | PyObject *argv, *env; |
| 4458 | char **argvlist; |
| 4459 | char **envlist; |
| 4460 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4461 | int mode; |
| 4462 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4463 | Py_intptr_t spawnval; |
| 4464 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4465 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4466 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4467 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4468 | argv is a list or tuple of strings and env is a dictionary |
| 4469 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4470 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4471 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4472 | PyUnicode_FSConverter, |
| 4473 | &opath, &argv, &env)) |
| 4474 | return NULL; |
| 4475 | path = PyBytes_AsString(opath); |
| 4476 | if (PyList_Check(argv)) { |
| 4477 | argc = PyList_Size(argv); |
| 4478 | getitem = PyList_GetItem; |
| 4479 | } |
| 4480 | else if (PyTuple_Check(argv)) { |
| 4481 | argc = PyTuple_Size(argv); |
| 4482 | getitem = PyTuple_GetItem; |
| 4483 | } |
| 4484 | else { |
| 4485 | PyErr_SetString(PyExc_TypeError, |
| 4486 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4487 | goto fail_0; |
| 4488 | } |
| 4489 | if (!PyMapping_Check(env)) { |
| 4490 | PyErr_SetString(PyExc_TypeError, |
| 4491 | "spawnvpe() arg 3 must be a mapping object"); |
| 4492 | goto fail_0; |
| 4493 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4494 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4495 | argvlist = PyMem_NEW(char *, argc+1); |
| 4496 | if (argvlist == NULL) { |
| 4497 | PyErr_NoMemory(); |
| 4498 | goto fail_0; |
| 4499 | } |
| 4500 | for (i = 0; i < argc; i++) { |
| 4501 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4502 | &argvlist[i])) |
| 4503 | { |
| 4504 | lastarg = i; |
| 4505 | goto fail_1; |
| 4506 | } |
| 4507 | } |
| 4508 | lastarg = argc; |
| 4509 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4510 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4511 | envlist = parse_envlist(env, &envc); |
| 4512 | if (envlist == NULL) |
| 4513 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4514 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4515 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4516 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4517 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4518 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4519 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4520 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4521 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4522 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4523 | if (spawnval == -1) |
| 4524 | (void) posix_error(); |
| 4525 | else |
| 4526 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4527 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4528 | while (--envc >= 0) |
| 4529 | PyMem_DEL(envlist[envc]); |
| 4530 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4531 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4532 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4533 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4534 | Py_DECREF(opath); |
| 4535 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4536 | } |
| 4537 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4538 | #endif /* HAVE_SPAWNV */ |
| 4539 | |
| 4540 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4541 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4542 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4543 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4544 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4545 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4546 | 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] | 4547 | |
| 4548 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4549 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4550 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4551 | pid_t pid; |
| 4552 | int result = 0; |
| 4553 | _PyImport_AcquireLock(); |
| 4554 | pid = fork1(); |
| 4555 | if (pid == 0) { |
| 4556 | /* child: this clobbers and resets the import lock. */ |
| 4557 | PyOS_AfterFork(); |
| 4558 | } else { |
| 4559 | /* parent: release the import lock. */ |
| 4560 | result = _PyImport_ReleaseLock(); |
| 4561 | } |
| 4562 | if (pid == -1) |
| 4563 | return posix_error(); |
| 4564 | if (result < 0) { |
| 4565 | /* Don't clobber the OSError if the fork failed. */ |
| 4566 | PyErr_SetString(PyExc_RuntimeError, |
| 4567 | "not holding the import lock"); |
| 4568 | return NULL; |
| 4569 | } |
| 4570 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4571 | } |
| 4572 | #endif |
| 4573 | |
| 4574 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4575 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4576 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4577 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4578 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4579 | 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] | 4580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4581 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4582 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4584 | pid_t pid; |
| 4585 | int result = 0; |
| 4586 | _PyImport_AcquireLock(); |
| 4587 | pid = fork(); |
| 4588 | if (pid == 0) { |
| 4589 | /* child: this clobbers and resets the import lock. */ |
| 4590 | PyOS_AfterFork(); |
| 4591 | } else { |
| 4592 | /* parent: release the import lock. */ |
| 4593 | result = _PyImport_ReleaseLock(); |
| 4594 | } |
| 4595 | if (pid == -1) |
| 4596 | return posix_error(); |
| 4597 | if (result < 0) { |
| 4598 | /* Don't clobber the OSError if the fork failed. */ |
| 4599 | PyErr_SetString(PyExc_RuntimeError, |
| 4600 | "not holding the import lock"); |
| 4601 | return NULL; |
| 4602 | } |
| 4603 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4604 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4605 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4606 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4607 | #ifdef HAVE_SCHED_H |
| 4608 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4609 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 4610 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4611 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 4612 | "sched_get_priority_max(policy)\n\n\ |
| 4613 | Get the maximum scheduling priority for *policy*."); |
| 4614 | |
| 4615 | static PyObject * |
| 4616 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 4617 | { |
| 4618 | int policy, max; |
| 4619 | |
| 4620 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 4621 | return NULL; |
| 4622 | max = sched_get_priority_max(policy); |
| 4623 | if (max < 0) |
| 4624 | return posix_error(); |
| 4625 | return PyLong_FromLong(max); |
| 4626 | } |
| 4627 | |
| 4628 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 4629 | "sched_get_priority_min(policy)\n\n\ |
| 4630 | Get the minimum scheduling priority for *policy*."); |
| 4631 | |
| 4632 | static PyObject * |
| 4633 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 4634 | { |
| 4635 | int policy, min; |
| 4636 | |
| 4637 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 4638 | return NULL; |
| 4639 | min = sched_get_priority_min(policy); |
| 4640 | if (min < 0) |
| 4641 | return posix_error(); |
| 4642 | return PyLong_FromLong(min); |
| 4643 | } |
| 4644 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4645 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 4646 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4647 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4648 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4649 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 4650 | "sched_getscheduler(pid)\n\n\ |
| 4651 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 4652 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 4653 | |
| 4654 | static PyObject * |
| 4655 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 4656 | { |
| 4657 | pid_t pid; |
| 4658 | int policy; |
| 4659 | |
| 4660 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 4661 | return NULL; |
| 4662 | policy = sched_getscheduler(pid); |
| 4663 | if (policy < 0) |
| 4664 | return posix_error(); |
| 4665 | return PyLong_FromLong(policy); |
| 4666 | } |
| 4667 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4668 | #endif |
| 4669 | |
| 4670 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 4671 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4672 | static PyObject * |
| 4673 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4674 | { |
| 4675 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 4676 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4677 | |
| 4678 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 4679 | return NULL; |
| 4680 | res = PyStructSequence_New(type); |
| 4681 | if (!res) |
| 4682 | return NULL; |
| 4683 | Py_INCREF(priority); |
| 4684 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4685 | return res; |
| 4686 | } |
| 4687 | |
| 4688 | PyDoc_STRVAR(sched_param__doc__, |
| 4689 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 4690 | Current has only one field: sched_priority"); |
| 4691 | |
| 4692 | static PyStructSequence_Field sched_param_fields[] = { |
| 4693 | {"sched_priority", "the scheduling priority"}, |
| 4694 | {0} |
| 4695 | }; |
| 4696 | |
| 4697 | static PyStructSequence_Desc sched_param_desc = { |
| 4698 | "sched_param", /* name */ |
| 4699 | sched_param__doc__, /* doc */ |
| 4700 | sched_param_fields, |
| 4701 | 1 |
| 4702 | }; |
| 4703 | |
| 4704 | static int |
| 4705 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 4706 | { |
| 4707 | long priority; |
| 4708 | |
| 4709 | if (Py_TYPE(param) != &SchedParamType) { |
| 4710 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 4711 | return 0; |
| 4712 | } |
| 4713 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 4714 | if (priority == -1 && PyErr_Occurred()) |
| 4715 | return 0; |
| 4716 | if (priority > INT_MAX || priority < INT_MIN) { |
| 4717 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 4718 | return 0; |
| 4719 | } |
| 4720 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 4721 | return 1; |
| 4722 | } |
| 4723 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4724 | #endif |
| 4725 | |
| 4726 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4727 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4728 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 4729 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 4730 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 4731 | If *pid* is 0, the calling process is changed.\n\ |
| 4732 | *param* is an instance of sched_param."); |
| 4733 | |
| 4734 | static PyObject * |
| 4735 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 4736 | { |
| 4737 | pid_t pid; |
| 4738 | int policy; |
| 4739 | struct sched_param param; |
| 4740 | |
| 4741 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 4742 | &pid, &policy, &convert_sched_param, ¶m)) |
| 4743 | return NULL; |
| 4744 | if (sched_setscheduler(pid, policy, ¶m)) |
| 4745 | return posix_error(); |
| 4746 | Py_RETURN_NONE; |
| 4747 | } |
| 4748 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4749 | #endif |
| 4750 | |
| 4751 | #ifdef HAVE_SCHED_SETPARAM |
| 4752 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4753 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 4754 | "sched_getparam(pid) -> sched_param\n\n\ |
| 4755 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 4756 | sched_param class. A PID of 0 means the calling process."); |
| 4757 | |
| 4758 | static PyObject * |
| 4759 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 4760 | { |
| 4761 | pid_t pid; |
| 4762 | struct sched_param param; |
| 4763 | PyObject *res, *priority; |
| 4764 | |
| 4765 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 4766 | return NULL; |
| 4767 | if (sched_getparam(pid, ¶m)) |
| 4768 | return posix_error(); |
| 4769 | res = PyStructSequence_New(&SchedParamType); |
| 4770 | if (!res) |
| 4771 | return NULL; |
| 4772 | priority = PyLong_FromLong(param.sched_priority); |
| 4773 | if (!priority) { |
| 4774 | Py_DECREF(res); |
| 4775 | return NULL; |
| 4776 | } |
| 4777 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4778 | return res; |
| 4779 | } |
| 4780 | |
| 4781 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 4782 | "sched_setparam(pid, param)\n\n\ |
| 4783 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 4784 | A PID of 0 means the calling process."); |
| 4785 | |
| 4786 | static PyObject * |
| 4787 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 4788 | { |
| 4789 | pid_t pid; |
| 4790 | struct sched_param param; |
| 4791 | |
| 4792 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 4793 | &pid, &convert_sched_param, ¶m)) |
| 4794 | return NULL; |
| 4795 | if (sched_setparam(pid, ¶m)) |
| 4796 | return posix_error(); |
| 4797 | Py_RETURN_NONE; |
| 4798 | } |
| 4799 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4800 | #endif |
| 4801 | |
| 4802 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 4803 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4804 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 4805 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 4806 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 4807 | |
| 4808 | static PyObject * |
| 4809 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 4810 | { |
| 4811 | pid_t pid; |
| 4812 | struct timespec interval; |
| 4813 | |
| 4814 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 4815 | return NULL; |
| 4816 | if (sched_rr_get_interval(pid, &interval)) |
| 4817 | return posix_error(); |
| 4818 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 4819 | } |
| 4820 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4821 | #endif |
| 4822 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4823 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 4824 | "sched_yield()\n\n\ |
| 4825 | Voluntarily relinquish the CPU."); |
| 4826 | |
| 4827 | static PyObject * |
| 4828 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 4829 | { |
| 4830 | if (sched_yield()) |
| 4831 | return posix_error(); |
| 4832 | Py_RETURN_NONE; |
| 4833 | } |
| 4834 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 4835 | #ifdef HAVE_SCHED_SETAFFINITY |
| 4836 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4837 | typedef struct { |
| 4838 | PyObject_HEAD; |
| 4839 | Py_ssize_t size; |
| 4840 | int ncpus; |
| 4841 | cpu_set_t *set; |
| 4842 | } Py_cpu_set; |
| 4843 | |
| 4844 | static PyTypeObject cpu_set_type; |
| 4845 | |
| 4846 | static void |
| 4847 | cpu_set_dealloc(Py_cpu_set *set) |
| 4848 | { |
| 4849 | assert(set->set); |
| 4850 | CPU_FREE(set->set); |
| 4851 | Py_TYPE(set)->tp_free(set); |
| 4852 | } |
| 4853 | |
| 4854 | static Py_cpu_set * |
| 4855 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 4856 | { |
| 4857 | Py_cpu_set *set; |
| 4858 | |
| 4859 | if (size < 0) { |
| 4860 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 4861 | return NULL; |
| 4862 | } |
| 4863 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 4864 | if (!set) |
| 4865 | return NULL; |
| 4866 | set->ncpus = size; |
| 4867 | set->size = CPU_ALLOC_SIZE(size); |
| 4868 | set->set = CPU_ALLOC(size); |
| 4869 | if (!set->set) { |
| 4870 | type->tp_free(set); |
| 4871 | PyErr_NoMemory(); |
| 4872 | return NULL; |
| 4873 | } |
| 4874 | CPU_ZERO_S(set->size, set->set); |
| 4875 | return set; |
| 4876 | } |
| 4877 | |
| 4878 | static PyObject * |
| 4879 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4880 | { |
| 4881 | int size; |
| 4882 | |
| 4883 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 4884 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 4885 | return NULL; |
| 4886 | return (PyObject *)make_new_cpu_set(type, size); |
| 4887 | } |
| 4888 | |
| 4889 | static PyObject * |
| 4890 | cpu_set_repr(Py_cpu_set *set) |
| 4891 | { |
| 4892 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
| 4893 | } |
| 4894 | |
| 4895 | static Py_ssize_t |
| 4896 | cpu_set_len(Py_cpu_set *set) |
| 4897 | { |
| 4898 | return set->ncpus; |
| 4899 | } |
| 4900 | |
| 4901 | static int |
| 4902 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 4903 | { |
| 4904 | int cpu; |
| 4905 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 4906 | return -1; |
| 4907 | if (cpu < 0) { |
| 4908 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 4909 | return -1; |
| 4910 | } |
| 4911 | if (cpu >= set->ncpus) { |
| 4912 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 4913 | return -1; |
| 4914 | } |
| 4915 | return cpu; |
| 4916 | } |
| 4917 | |
| 4918 | PyDoc_STRVAR(cpu_set_set_doc, |
| 4919 | "cpu_set.set(i)\n\n\ |
| 4920 | Add CPU *i* to the set."); |
| 4921 | |
| 4922 | static PyObject * |
| 4923 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 4924 | { |
| 4925 | int cpu = _get_cpu(set, "i|set", args); |
| 4926 | if (cpu == -1) |
| 4927 | return NULL; |
| 4928 | CPU_SET_S(cpu, set->size, set->set); |
| 4929 | Py_RETURN_NONE; |
| 4930 | } |
| 4931 | |
| 4932 | PyDoc_STRVAR(cpu_set_count_doc, |
| 4933 | "cpu_set.count() -> int\n\n\ |
| 4934 | Return the number of CPUs active in the set."); |
| 4935 | |
| 4936 | static PyObject * |
| 4937 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 4938 | { |
| 4939 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 4940 | } |
| 4941 | |
| 4942 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 4943 | "cpu_set.clear(i)\n\n\ |
| 4944 | Remove CPU *i* from the set."); |
| 4945 | |
| 4946 | static PyObject * |
| 4947 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 4948 | { |
| 4949 | int cpu = _get_cpu(set, "i|clear", args); |
| 4950 | if (cpu == -1) |
| 4951 | return NULL; |
| 4952 | CPU_CLR_S(cpu, set->size, set->set); |
| 4953 | Py_RETURN_NONE; |
| 4954 | } |
| 4955 | |
| 4956 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 4957 | "cpu_set.isset(i) -> bool\n\n\ |
| 4958 | Test if CPU *i* is in the set."); |
| 4959 | |
| 4960 | static PyObject * |
| 4961 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 4962 | { |
| 4963 | int cpu = _get_cpu(set, "i|isset", args); |
| 4964 | if (cpu == -1) |
| 4965 | return NULL; |
| 4966 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 4967 | Py_RETURN_TRUE; |
| 4968 | Py_RETURN_FALSE; |
| 4969 | } |
| 4970 | |
| 4971 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 4972 | "cpu_set.zero()\n\n\ |
| 4973 | Clear the cpu_set."); |
| 4974 | |
| 4975 | static PyObject * |
| 4976 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 4977 | { |
| 4978 | CPU_ZERO_S(set->size, set->set); |
| 4979 | Py_RETURN_NONE; |
| 4980 | } |
| 4981 | |
| 4982 | static PyObject * |
| 4983 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 4984 | { |
| 4985 | int eq; |
| 4986 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4987 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 4988 | Py_RETURN_NOTIMPLEMENTED; |
| 4989 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4990 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 4991 | if ((op == Py_EQ) ? eq : !eq) |
| 4992 | Py_RETURN_TRUE; |
| 4993 | else |
| 4994 | Py_RETURN_FALSE; |
| 4995 | } |
| 4996 | |
| 4997 | #define CPU_SET_BINOP(name, op) \ |
| 4998 | static PyObject * \ |
| 4999 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5000 | if (res) { \ |
| 5001 | Py_INCREF(res); \ |
| 5002 | } \ |
| 5003 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5004 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5005 | if (!res) \ |
| 5006 | return NULL; \ |
| 5007 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5008 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5009 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5010 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5011 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5012 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5013 | op(res->size, res->set, left->set, right->set); \ |
| 5014 | return (PyObject *)res; \ |
| 5015 | } \ |
| 5016 | static PyObject * \ |
| 5017 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5018 | return do_cpu_set_##name(left, right, NULL); \ |
| 5019 | } \ |
| 5020 | static PyObject * \ |
| 5021 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5022 | return do_cpu_set_##name(left, right, left); \ |
| 5023 | } \ |
| 5024 | |
| 5025 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5026 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5027 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5028 | #undef CPU_SET_BINOP |
| 5029 | |
| 5030 | PyDoc_STRVAR(cpu_set_doc, |
| 5031 | "cpu_set(size)\n\n\ |
| 5032 | Create an empty mask of CPUs."); |
| 5033 | |
| 5034 | static PyNumberMethods cpu_set_as_number = { |
| 5035 | 0, /*nb_add*/ |
| 5036 | 0, /*nb_subtract*/ |
| 5037 | 0, /*nb_multiply*/ |
| 5038 | 0, /*nb_remainder*/ |
| 5039 | 0, /*nb_divmod*/ |
| 5040 | 0, /*nb_power*/ |
| 5041 | 0, /*nb_negative*/ |
| 5042 | 0, /*nb_positive*/ |
| 5043 | 0, /*nb_absolute*/ |
| 5044 | 0, /*nb_bool*/ |
| 5045 | 0, /*nb_invert*/ |
| 5046 | 0, /*nb_lshift*/ |
| 5047 | 0, /*nb_rshift*/ |
| 5048 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5049 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5050 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5051 | 0, /*nb_int*/ |
| 5052 | 0, /*nb_reserved*/ |
| 5053 | 0, /*nb_float*/ |
| 5054 | 0, /*nb_inplace_add*/ |
| 5055 | 0, /*nb_inplace_subtract*/ |
| 5056 | 0, /*nb_inplace_multiply*/ |
| 5057 | 0, /*nb_inplace_remainder*/ |
| 5058 | 0, /*nb_inplace_power*/ |
| 5059 | 0, /*nb_inplace_lshift*/ |
| 5060 | 0, /*nb_inplace_rshift*/ |
| 5061 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5062 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5063 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5064 | }; |
| 5065 | |
| 5066 | static PySequenceMethods cpu_set_as_sequence = { |
| 5067 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5068 | }; |
| 5069 | |
| 5070 | static PyMethodDef cpu_set_methods[] = { |
| 5071 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5072 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5073 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5074 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5075 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5076 | {NULL, NULL} /* sentinel */ |
| 5077 | }; |
| 5078 | |
| 5079 | static PyTypeObject cpu_set_type = { |
| 5080 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5081 | "posix.cpu_set", /* tp_name */ |
| 5082 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5083 | 0, /* tp_itemsize */ |
| 5084 | /* methods */ |
| 5085 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5086 | 0, /* tp_print */ |
| 5087 | 0, /* tp_getattr */ |
| 5088 | 0, /* tp_setattr */ |
| 5089 | 0, /* tp_reserved */ |
| 5090 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5091 | &cpu_set_as_number, /* tp_as_number */ |
| 5092 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5093 | 0, /* tp_as_mapping */ |
| 5094 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5095 | 0, /* tp_call */ |
| 5096 | 0, /* tp_str */ |
| 5097 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5098 | 0, /* tp_setattro */ |
| 5099 | 0, /* tp_as_buffer */ |
| 5100 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5101 | cpu_set_doc, /* tp_doc */ |
| 5102 | 0, /* tp_traverse */ |
| 5103 | 0, /* tp_clear */ |
| 5104 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5105 | 0, /* tp_weaklistoffset */ |
| 5106 | 0, /* tp_iter */ |
| 5107 | 0, /* tp_iternext */ |
| 5108 | cpu_set_methods, /* tp_methods */ |
| 5109 | 0, /* tp_members */ |
| 5110 | 0, /* tp_getset */ |
| 5111 | 0, /* tp_base */ |
| 5112 | 0, /* tp_dict */ |
| 5113 | 0, /* tp_descr_get */ |
| 5114 | 0, /* tp_descr_set */ |
| 5115 | 0, /* tp_dictoffset */ |
| 5116 | 0, /* tp_init */ |
| 5117 | PyType_GenericAlloc, /* tp_alloc */ |
| 5118 | cpu_set_new, /* tp_new */ |
| 5119 | PyObject_Del, /* tp_free */ |
| 5120 | }; |
| 5121 | |
| 5122 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5123 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5124 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5125 | |
| 5126 | static PyObject * |
| 5127 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5128 | { |
| 5129 | pid_t pid; |
| 5130 | Py_cpu_set *cpu_set; |
| 5131 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5132 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5133 | &pid, &cpu_set_type, &cpu_set)) |
| 5134 | return NULL; |
| 5135 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5136 | return posix_error(); |
| 5137 | Py_RETURN_NONE; |
| 5138 | } |
| 5139 | |
| 5140 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5141 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5142 | Return the affinity of the process with PID *pid*.\n\ |
| 5143 | The returned cpu_set will be of size *ncpus*."); |
| 5144 | |
| 5145 | static PyObject * |
| 5146 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5147 | { |
| 5148 | pid_t pid; |
| 5149 | int ncpus; |
| 5150 | Py_cpu_set *res; |
| 5151 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5152 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5153 | &pid, &ncpus)) |
| 5154 | return NULL; |
| 5155 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5156 | if (!res) |
| 5157 | return NULL; |
| 5158 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5159 | Py_DECREF(res); |
| 5160 | return posix_error(); |
| 5161 | } |
| 5162 | return (PyObject *)res; |
| 5163 | } |
| 5164 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5165 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5166 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5167 | #endif /* HAVE_SCHED_H */ |
| 5168 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5169 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5170 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5171 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5172 | #define DEV_PTY_FILE "/dev/ptc" |
| 5173 | #define HAVE_DEV_PTMX |
| 5174 | #else |
| 5175 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5176 | #endif |
| 5177 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5178 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5179 | #ifdef HAVE_PTY_H |
| 5180 | #include <pty.h> |
| 5181 | #else |
| 5182 | #ifdef HAVE_LIBUTIL_H |
| 5183 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5184 | #else |
| 5185 | #ifdef HAVE_UTIL_H |
| 5186 | #include <util.h> |
| 5187 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5188 | #endif /* HAVE_LIBUTIL_H */ |
| 5189 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5190 | #ifdef HAVE_STROPTS_H |
| 5191 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5192 | #endif |
| 5193 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5194 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5195 | #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] | 5196 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5197 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5198 | 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] | 5199 | |
| 5200 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5201 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5202 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5203 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5204 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5205 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5206 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5207 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5208 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5209 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5210 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5211 | #endif |
| 5212 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5213 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5214 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5215 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5216 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5217 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5218 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5219 | if (slave_name == NULL) |
| 5220 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5221 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5222 | slave_fd = open(slave_name, O_RDWR); |
| 5223 | if (slave_fd < 0) |
| 5224 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5225 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5226 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5227 | if (master_fd < 0) |
| 5228 | return posix_error(); |
| 5229 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5230 | /* change permission of slave */ |
| 5231 | if (grantpt(master_fd) < 0) { |
| 5232 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5233 | return posix_error(); |
| 5234 | } |
| 5235 | /* unlock slave */ |
| 5236 | if (unlockpt(master_fd) < 0) { |
| 5237 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5238 | return posix_error(); |
| 5239 | } |
| 5240 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5241 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5242 | if (slave_name == NULL) |
| 5243 | return posix_error(); |
| 5244 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5245 | if (slave_fd < 0) |
| 5246 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5247 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5248 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5249 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5250 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5251 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5252 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5253 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5254 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5255 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5256 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5257 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5258 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5259 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5260 | |
| 5261 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5262 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5263 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5264 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5265 | 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] | 5266 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5267 | |
| 5268 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5269 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5270 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5271 | int master_fd = -1, result = 0; |
| 5272 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5273 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5274 | _PyImport_AcquireLock(); |
| 5275 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5276 | if (pid == 0) { |
| 5277 | /* child: this clobbers and resets the import lock. */ |
| 5278 | PyOS_AfterFork(); |
| 5279 | } else { |
| 5280 | /* parent: release the import lock. */ |
| 5281 | result = _PyImport_ReleaseLock(); |
| 5282 | } |
| 5283 | if (pid == -1) |
| 5284 | return posix_error(); |
| 5285 | if (result < 0) { |
| 5286 | /* Don't clobber the OSError if the fork failed. */ |
| 5287 | PyErr_SetString(PyExc_RuntimeError, |
| 5288 | "not holding the import lock"); |
| 5289 | return NULL; |
| 5290 | } |
| 5291 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5292 | } |
| 5293 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5294 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5295 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5296 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5297 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5298 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5299 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5300 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5301 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5302 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5303 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5304 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5305 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5306 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5307 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5308 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5309 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5310 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5311 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5312 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5313 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5314 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5315 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5316 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5317 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5318 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5319 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5320 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5321 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5322 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5323 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5324 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5325 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5326 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5327 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5328 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5329 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5330 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5331 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5332 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5333 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5335 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5336 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5337 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5338 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5339 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5340 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5341 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5342 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5343 | } |
| 5344 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5345 | #ifdef HAVE_GETGROUPLIST |
| 5346 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5347 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5348 | Returns a list of groups to which a user belongs.\n\n\ |
| 5349 | user: username to lookup\n\ |
| 5350 | group: base group id of the user"); |
| 5351 | |
| 5352 | static PyObject * |
| 5353 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5354 | { |
| 5355 | #ifdef NGROUPS_MAX |
| 5356 | #define MAX_GROUPS NGROUPS_MAX |
| 5357 | #else |
| 5358 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5359 | #define MAX_GROUPS 64 |
| 5360 | #endif |
| 5361 | |
| 5362 | const char *user; |
| 5363 | int i, ngroups; |
| 5364 | PyObject *list; |
| 5365 | #ifdef __APPLE__ |
| 5366 | int *groups, basegid; |
| 5367 | #else |
| 5368 | gid_t *groups, basegid; |
| 5369 | #endif |
| 5370 | ngroups = MAX_GROUPS; |
| 5371 | |
| 5372 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5373 | return NULL; |
| 5374 | |
| 5375 | #ifdef __APPLE__ |
| 5376 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5377 | #else |
| 5378 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5379 | #endif |
| 5380 | if (groups == NULL) |
| 5381 | return PyErr_NoMemory(); |
| 5382 | |
| 5383 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5384 | PyMem_Del(groups); |
| 5385 | return posix_error(); |
| 5386 | } |
| 5387 | |
| 5388 | list = PyList_New(ngroups); |
| 5389 | if (list == NULL) { |
| 5390 | PyMem_Del(groups); |
| 5391 | return NULL; |
| 5392 | } |
| 5393 | |
| 5394 | for (i = 0; i < ngroups; i++) { |
| 5395 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5396 | if (o == NULL) { |
| 5397 | Py_DECREF(list); |
| 5398 | PyMem_Del(groups); |
| 5399 | return NULL; |
| 5400 | } |
| 5401 | PyList_SET_ITEM(list, i, o); |
| 5402 | } |
| 5403 | |
| 5404 | PyMem_Del(groups); |
| 5405 | |
| 5406 | return list; |
| 5407 | } |
| 5408 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5409 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5410 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5411 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5412 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5413 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5414 | |
| 5415 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5416 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5417 | { |
| 5418 | PyObject *result = NULL; |
| 5419 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5420 | #ifdef NGROUPS_MAX |
| 5421 | #define MAX_GROUPS NGROUPS_MAX |
| 5422 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5423 | /* 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] | 5424 | #define MAX_GROUPS 64 |
| 5425 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5426 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5427 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5428 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5429 | * This is a helper variable to store the intermediate result when |
| 5430 | * that happens. |
| 5431 | * |
| 5432 | * To keep the code readable the OSX behaviour is unconditional, |
| 5433 | * according to the POSIX spec this should be safe on all unix-y |
| 5434 | * systems. |
| 5435 | */ |
| 5436 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5437 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5438 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5439 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5440 | if (n < 0) { |
| 5441 | if (errno == EINVAL) { |
| 5442 | n = getgroups(0, NULL); |
| 5443 | if (n == -1) { |
| 5444 | return posix_error(); |
| 5445 | } |
| 5446 | if (n == 0) { |
| 5447 | /* Avoid malloc(0) */ |
| 5448 | alt_grouplist = grouplist; |
| 5449 | } else { |
| 5450 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5451 | if (alt_grouplist == NULL) { |
| 5452 | errno = EINVAL; |
| 5453 | return posix_error(); |
| 5454 | } |
| 5455 | n = getgroups(n, alt_grouplist); |
| 5456 | if (n == -1) { |
| 5457 | PyMem_Free(alt_grouplist); |
| 5458 | return posix_error(); |
| 5459 | } |
| 5460 | } |
| 5461 | } else { |
| 5462 | return posix_error(); |
| 5463 | } |
| 5464 | } |
| 5465 | result = PyList_New(n); |
| 5466 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5467 | int i; |
| 5468 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5469 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5470 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5471 | Py_DECREF(result); |
| 5472 | result = NULL; |
| 5473 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5474 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5475 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5476 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | if (alt_grouplist != grouplist) { |
| 5480 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5481 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5482 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5483 | return result; |
| 5484 | } |
| 5485 | #endif |
| 5486 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5487 | #ifdef HAVE_INITGROUPS |
| 5488 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5489 | "initgroups(username, gid) -> None\n\n\ |
| 5490 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5491 | the groups of which the specified username is a member, plus the specified\n\ |
| 5492 | group id."); |
| 5493 | |
| 5494 | static PyObject * |
| 5495 | posix_initgroups(PyObject *self, PyObject *args) |
| 5496 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5497 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5498 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5499 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5500 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5501 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5502 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5503 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5504 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5505 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5506 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5507 | res = initgroups(username, (gid_t) gid); |
| 5508 | Py_DECREF(oname); |
| 5509 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5510 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5511 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5512 | Py_INCREF(Py_None); |
| 5513 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5514 | } |
| 5515 | #endif |
| 5516 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5517 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5518 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5519 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5520 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5521 | |
| 5522 | static PyObject * |
| 5523 | posix_getpgid(PyObject *self, PyObject *args) |
| 5524 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5525 | pid_t pid, pgid; |
| 5526 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5527 | return NULL; |
| 5528 | pgid = getpgid(pid); |
| 5529 | if (pgid < 0) |
| 5530 | return posix_error(); |
| 5531 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5532 | } |
| 5533 | #endif /* HAVE_GETPGID */ |
| 5534 | |
| 5535 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5536 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5537 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5538 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5539 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5540 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5541 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5542 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5543 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5544 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5545 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5546 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5547 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5548 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5549 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5550 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5551 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5552 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5553 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5554 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5555 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5556 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5558 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5559 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5560 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5561 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5562 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5563 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5564 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5565 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5566 | return posix_error(); |
| 5567 | Py_INCREF(Py_None); |
| 5568 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5571 | #endif /* HAVE_SETPGRP */ |
| 5572 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5573 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5574 | |
| 5575 | #ifdef MS_WINDOWS |
| 5576 | #include <tlhelp32.h> |
| 5577 | |
| 5578 | static PyObject* |
| 5579 | win32_getppid() |
| 5580 | { |
| 5581 | HANDLE snapshot; |
| 5582 | pid_t mypid; |
| 5583 | PyObject* result = NULL; |
| 5584 | BOOL have_record; |
| 5585 | PROCESSENTRY32 pe; |
| 5586 | |
| 5587 | mypid = getpid(); /* This function never fails */ |
| 5588 | |
| 5589 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5590 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5591 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5592 | |
| 5593 | pe.dwSize = sizeof(pe); |
| 5594 | have_record = Process32First(snapshot, &pe); |
| 5595 | while (have_record) { |
| 5596 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5597 | /* We could cache the ulong value in a static variable. */ |
| 5598 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5599 | break; |
| 5600 | } |
| 5601 | |
| 5602 | have_record = Process32Next(snapshot, &pe); |
| 5603 | } |
| 5604 | |
| 5605 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5606 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5607 | * error anyway, so let's raise it. */ |
| 5608 | if (!result) |
| 5609 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5610 | |
| 5611 | CloseHandle(snapshot); |
| 5612 | |
| 5613 | return result; |
| 5614 | } |
| 5615 | #endif /*MS_WINDOWS*/ |
| 5616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5617 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5618 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5619 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5620 | Windows machines will still return its id; others systems will return the id\n\ |
| 5621 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5622 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5623 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5624 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5625 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5626 | #ifdef MS_WINDOWS |
| 5627 | return win32_getppid(); |
| 5628 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5629 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5630 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5631 | } |
| 5632 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5633 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5634 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5635 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5636 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5637 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5638 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5639 | |
| 5640 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5641 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5642 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5643 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5644 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5645 | wchar_t user_name[UNLEN + 1]; |
| 5646 | DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]); |
| 5647 | |
| 5648 | if (GetUserNameW(user_name, &num_chars)) { |
| 5649 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5650 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5651 | } |
| 5652 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5653 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5654 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5655 | char *name; |
| 5656 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5657 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5658 | errno = 0; |
| 5659 | name = getlogin(); |
| 5660 | if (name == NULL) { |
| 5661 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5662 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5663 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5664 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5665 | } |
| 5666 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5667 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5668 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5669 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5670 | return result; |
| 5671 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5672 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5673 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5674 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5675 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5676 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5677 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5678 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5679 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5680 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5681 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5682 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5683 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5684 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5685 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5686 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5687 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5688 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5689 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5690 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5691 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5692 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5693 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5694 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5695 | pid_t pid; |
| 5696 | int sig; |
| 5697 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5698 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5699 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5700 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 5701 | APIRET rc; |
| 5702 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5703 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5704 | |
| 5705 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 5706 | APIRET rc; |
| 5707 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5708 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5709 | |
| 5710 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5711 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5712 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5713 | if (kill(pid, sig) == -1) |
| 5714 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5715 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5716 | Py_INCREF(Py_None); |
| 5717 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5718 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5719 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5720 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5721 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5722 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5723 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5724 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5725 | |
| 5726 | static PyObject * |
| 5727 | posix_killpg(PyObject *self, PyObject *args) |
| 5728 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5729 | int sig; |
| 5730 | pid_t pgid; |
| 5731 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5732 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5733 | take the same type. Moreover, pid_t is always at least as wide as |
| 5734 | int (else compilation of this module fails), which is safe. */ |
| 5735 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5736 | return NULL; |
| 5737 | if (killpg(pgid, sig) == -1) |
| 5738 | return posix_error(); |
| 5739 | Py_INCREF(Py_None); |
| 5740 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5741 | } |
| 5742 | #endif |
| 5743 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5744 | #ifdef MS_WINDOWS |
| 5745 | PyDoc_STRVAR(win32_kill__doc__, |
| 5746 | "kill(pid, sig)\n\n\ |
| 5747 | Kill a process with a signal."); |
| 5748 | |
| 5749 | static PyObject * |
| 5750 | win32_kill(PyObject *self, PyObject *args) |
| 5751 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5752 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5753 | DWORD pid, sig, err; |
| 5754 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5755 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5756 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5757 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5758 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5759 | /* Console processes which share a common console can be sent CTRL+C or |
| 5760 | CTRL+BREAK events, provided they handle said events. */ |
| 5761 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5762 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5763 | err = GetLastError(); |
| 5764 | PyErr_SetFromWindowsErr(err); |
| 5765 | } |
| 5766 | else |
| 5767 | Py_RETURN_NONE; |
| 5768 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5769 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5770 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5771 | attempt to open and terminate the process. */ |
| 5772 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5773 | if (handle == NULL) { |
| 5774 | err = GetLastError(); |
| 5775 | return PyErr_SetFromWindowsErr(err); |
| 5776 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5777 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5778 | if (TerminateProcess(handle, sig) == 0) { |
| 5779 | err = GetLastError(); |
| 5780 | result = PyErr_SetFromWindowsErr(err); |
| 5781 | } else { |
| 5782 | Py_INCREF(Py_None); |
| 5783 | result = Py_None; |
| 5784 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5785 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5786 | CloseHandle(handle); |
| 5787 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5788 | } |
| 5789 | #endif /* MS_WINDOWS */ |
| 5790 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5791 | #ifdef HAVE_PLOCK |
| 5792 | |
| 5793 | #ifdef HAVE_SYS_LOCK_H |
| 5794 | #include <sys/lock.h> |
| 5795 | #endif |
| 5796 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5797 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5798 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5799 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5800 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5801 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5802 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5803 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5804 | int op; |
| 5805 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5806 | return NULL; |
| 5807 | if (plock(op) == -1) |
| 5808 | return posix_error(); |
| 5809 | Py_INCREF(Py_None); |
| 5810 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5811 | } |
| 5812 | #endif |
| 5813 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5814 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5815 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5816 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5817 | Set the current process's user id."); |
| 5818 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5819 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5820 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5821 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5822 | long uid_arg; |
| 5823 | uid_t uid; |
| 5824 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5825 | return NULL; |
| 5826 | uid = uid_arg; |
| 5827 | if (uid != uid_arg) { |
| 5828 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5829 | return NULL; |
| 5830 | } |
| 5831 | if (setuid(uid) < 0) |
| 5832 | return posix_error(); |
| 5833 | Py_INCREF(Py_None); |
| 5834 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5835 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5836 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5837 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5838 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5839 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5840 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5841 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5842 | Set the current process's effective user id."); |
| 5843 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5844 | static PyObject * |
| 5845 | posix_seteuid (PyObject *self, PyObject *args) |
| 5846 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5847 | long euid_arg; |
| 5848 | uid_t euid; |
| 5849 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5850 | return NULL; |
| 5851 | euid = euid_arg; |
| 5852 | if (euid != euid_arg) { |
| 5853 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5854 | return NULL; |
| 5855 | } |
| 5856 | if (seteuid(euid) < 0) { |
| 5857 | return posix_error(); |
| 5858 | } else { |
| 5859 | Py_INCREF(Py_None); |
| 5860 | return Py_None; |
| 5861 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5862 | } |
| 5863 | #endif /* HAVE_SETEUID */ |
| 5864 | |
| 5865 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5866 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5867 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5868 | Set the current process's effective group id."); |
| 5869 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5870 | static PyObject * |
| 5871 | posix_setegid (PyObject *self, PyObject *args) |
| 5872 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5873 | long egid_arg; |
| 5874 | gid_t egid; |
| 5875 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5876 | return NULL; |
| 5877 | egid = egid_arg; |
| 5878 | if (egid != egid_arg) { |
| 5879 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5880 | return NULL; |
| 5881 | } |
| 5882 | if (setegid(egid) < 0) { |
| 5883 | return posix_error(); |
| 5884 | } else { |
| 5885 | Py_INCREF(Py_None); |
| 5886 | return Py_None; |
| 5887 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5888 | } |
| 5889 | #endif /* HAVE_SETEGID */ |
| 5890 | |
| 5891 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5892 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5893 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5894 | Set the current process's real and effective user ids."); |
| 5895 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5896 | static PyObject * |
| 5897 | posix_setreuid (PyObject *self, PyObject *args) |
| 5898 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5899 | long ruid_arg, euid_arg; |
| 5900 | uid_t ruid, euid; |
| 5901 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5902 | return NULL; |
| 5903 | if (ruid_arg == -1) |
| 5904 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5905 | else |
| 5906 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5907 | if (euid_arg == -1) |
| 5908 | euid = (uid_t)-1; |
| 5909 | else |
| 5910 | euid = euid_arg; |
| 5911 | if ((euid_arg != -1 && euid != euid_arg) || |
| 5912 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 5913 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5914 | return NULL; |
| 5915 | } |
| 5916 | if (setreuid(ruid, euid) < 0) { |
| 5917 | return posix_error(); |
| 5918 | } else { |
| 5919 | Py_INCREF(Py_None); |
| 5920 | return Py_None; |
| 5921 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5922 | } |
| 5923 | #endif /* HAVE_SETREUID */ |
| 5924 | |
| 5925 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5926 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5927 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5928 | Set the current process's real and effective group ids."); |
| 5929 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5930 | static PyObject * |
| 5931 | posix_setregid (PyObject *self, PyObject *args) |
| 5932 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5933 | long rgid_arg, egid_arg; |
| 5934 | gid_t rgid, egid; |
| 5935 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 5936 | return NULL; |
| 5937 | if (rgid_arg == -1) |
| 5938 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 5939 | else |
| 5940 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 5941 | if (egid_arg == -1) |
| 5942 | egid = (gid_t)-1; |
| 5943 | else |
| 5944 | egid = egid_arg; |
| 5945 | if ((egid_arg != -1 && egid != egid_arg) || |
| 5946 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 5947 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5948 | return NULL; |
| 5949 | } |
| 5950 | if (setregid(rgid, egid) < 0) { |
| 5951 | return posix_error(); |
| 5952 | } else { |
| 5953 | Py_INCREF(Py_None); |
| 5954 | return Py_None; |
| 5955 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5956 | } |
| 5957 | #endif /* HAVE_SETREGID */ |
| 5958 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5959 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5960 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5961 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5962 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5963 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5964 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5965 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5966 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5967 | long gid_arg; |
| 5968 | gid_t gid; |
| 5969 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 5970 | return NULL; |
| 5971 | gid = gid_arg; |
| 5972 | if (gid != gid_arg) { |
| 5973 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5974 | return NULL; |
| 5975 | } |
| 5976 | if (setgid(gid) < 0) |
| 5977 | return posix_error(); |
| 5978 | Py_INCREF(Py_None); |
| 5979 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5980 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5981 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5982 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5983 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5984 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5985 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5986 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5987 | |
| 5988 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 5989 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5990 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5991 | int i, len; |
| 5992 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5993 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5994 | if (!PySequence_Check(groups)) { |
| 5995 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5996 | return NULL; |
| 5997 | } |
| 5998 | len = PySequence_Size(groups); |
| 5999 | if (len > MAX_GROUPS) { |
| 6000 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6001 | return NULL; |
| 6002 | } |
| 6003 | for(i = 0; i < len; i++) { |
| 6004 | PyObject *elem; |
| 6005 | elem = PySequence_GetItem(groups, i); |
| 6006 | if (!elem) |
| 6007 | return NULL; |
| 6008 | if (!PyLong_Check(elem)) { |
| 6009 | PyErr_SetString(PyExc_TypeError, |
| 6010 | "groups must be integers"); |
| 6011 | Py_DECREF(elem); |
| 6012 | return NULL; |
| 6013 | } else { |
| 6014 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6015 | if (PyErr_Occurred()) { |
| 6016 | PyErr_SetString(PyExc_TypeError, |
| 6017 | "group id too big"); |
| 6018 | Py_DECREF(elem); |
| 6019 | return NULL; |
| 6020 | } |
| 6021 | grouplist[i] = x; |
| 6022 | /* read back the value to see if it fitted in gid_t */ |
| 6023 | if (grouplist[i] != x) { |
| 6024 | PyErr_SetString(PyExc_TypeError, |
| 6025 | "group id too big"); |
| 6026 | Py_DECREF(elem); |
| 6027 | return NULL; |
| 6028 | } |
| 6029 | } |
| 6030 | Py_DECREF(elem); |
| 6031 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6032 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6033 | if (setgroups(len, grouplist) < 0) |
| 6034 | return posix_error(); |
| 6035 | Py_INCREF(Py_None); |
| 6036 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6037 | } |
| 6038 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6039 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6040 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6041 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 6042 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6043 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6044 | PyObject *result; |
| 6045 | static PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6046 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6047 | if (pid == -1) |
| 6048 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6049 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6050 | if (struct_rusage == NULL) { |
| 6051 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6052 | if (m == NULL) |
| 6053 | return NULL; |
| 6054 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 6055 | Py_DECREF(m); |
| 6056 | if (struct_rusage == NULL) |
| 6057 | return NULL; |
| 6058 | } |
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 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6061 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6062 | if (!result) |
| 6063 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6064 | |
| 6065 | #ifndef doubletime |
| 6066 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6067 | #endif |
| 6068 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6069 | PyStructSequence_SET_ITEM(result, 0, |
| 6070 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 6071 | PyStructSequence_SET_ITEM(result, 1, |
| 6072 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6073 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6074 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6075 | SET_INT(result, 2, ru->ru_maxrss); |
| 6076 | SET_INT(result, 3, ru->ru_ixrss); |
| 6077 | SET_INT(result, 4, ru->ru_idrss); |
| 6078 | SET_INT(result, 5, ru->ru_isrss); |
| 6079 | SET_INT(result, 6, ru->ru_minflt); |
| 6080 | SET_INT(result, 7, ru->ru_majflt); |
| 6081 | SET_INT(result, 8, ru->ru_nswap); |
| 6082 | SET_INT(result, 9, ru->ru_inblock); |
| 6083 | SET_INT(result, 10, ru->ru_oublock); |
| 6084 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6085 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6086 | SET_INT(result, 13, ru->ru_nsignals); |
| 6087 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6088 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6089 | #undef SET_INT |
| 6090 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6091 | if (PyErr_Occurred()) { |
| 6092 | Py_DECREF(result); |
| 6093 | return NULL; |
| 6094 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6096 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6097 | } |
| 6098 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6099 | |
| 6100 | #ifdef HAVE_WAIT3 |
| 6101 | PyDoc_STRVAR(posix_wait3__doc__, |
| 6102 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 6103 | Wait for completion of a child process."); |
| 6104 | |
| 6105 | static PyObject * |
| 6106 | posix_wait3(PyObject *self, PyObject *args) |
| 6107 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6108 | pid_t pid; |
| 6109 | int options; |
| 6110 | struct rusage ru; |
| 6111 | WAIT_TYPE status; |
| 6112 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6114 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 6115 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6116 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6117 | Py_BEGIN_ALLOW_THREADS |
| 6118 | pid = wait3(&status, options, &ru); |
| 6119 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6120 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6121 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6122 | } |
| 6123 | #endif /* HAVE_WAIT3 */ |
| 6124 | |
| 6125 | #ifdef HAVE_WAIT4 |
| 6126 | PyDoc_STRVAR(posix_wait4__doc__, |
| 6127 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 6128 | Wait for completion of a given child process."); |
| 6129 | |
| 6130 | static PyObject * |
| 6131 | posix_wait4(PyObject *self, PyObject *args) |
| 6132 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6133 | pid_t pid; |
| 6134 | int options; |
| 6135 | struct rusage ru; |
| 6136 | WAIT_TYPE status; |
| 6137 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6138 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6139 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 6140 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6141 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6142 | Py_BEGIN_ALLOW_THREADS |
| 6143 | pid = wait4(pid, &status, options, &ru); |
| 6144 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6145 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6146 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6147 | } |
| 6148 | #endif /* HAVE_WAIT4 */ |
| 6149 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6150 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6151 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6152 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6153 | Wait for the completion of one or more child processes.\n\n\ |
| 6154 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6155 | id specifies the pid to wait on.\n\ |
| 6156 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6157 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6158 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6159 | no children in a waitable state."); |
| 6160 | |
| 6161 | static PyObject * |
| 6162 | posix_waitid(PyObject *self, PyObject *args) |
| 6163 | { |
| 6164 | PyObject *result; |
| 6165 | idtype_t idtype; |
| 6166 | id_t id; |
| 6167 | int options, res; |
| 6168 | siginfo_t si; |
| 6169 | si.si_pid = 0; |
| 6170 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6171 | return NULL; |
| 6172 | Py_BEGIN_ALLOW_THREADS |
| 6173 | res = waitid(idtype, id, &si, options); |
| 6174 | Py_END_ALLOW_THREADS |
| 6175 | if (res == -1) |
| 6176 | return posix_error(); |
| 6177 | |
| 6178 | if (si.si_pid == 0) |
| 6179 | Py_RETURN_NONE; |
| 6180 | |
| 6181 | result = PyStructSequence_New(&WaitidResultType); |
| 6182 | if (!result) |
| 6183 | return NULL; |
| 6184 | |
| 6185 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6186 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6187 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6188 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6189 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6190 | if (PyErr_Occurred()) { |
| 6191 | Py_DECREF(result); |
| 6192 | return NULL; |
| 6193 | } |
| 6194 | |
| 6195 | return result; |
| 6196 | } |
| 6197 | #endif |
| 6198 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6199 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6200 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6201 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6202 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6203 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6204 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6205 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6206 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6207 | pid_t pid; |
| 6208 | int options; |
| 6209 | WAIT_TYPE status; |
| 6210 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6211 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6212 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6213 | return NULL; |
| 6214 | Py_BEGIN_ALLOW_THREADS |
| 6215 | pid = waitpid(pid, &status, options); |
| 6216 | Py_END_ALLOW_THREADS |
| 6217 | if (pid == -1) |
| 6218 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6219 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6220 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6223 | #elif defined(HAVE_CWAIT) |
| 6224 | |
| 6225 | /* 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] | 6226 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6227 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6228 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6229 | |
| 6230 | static PyObject * |
| 6231 | posix_waitpid(PyObject *self, PyObject *args) |
| 6232 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6233 | Py_intptr_t pid; |
| 6234 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6235 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6236 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6237 | return NULL; |
| 6238 | Py_BEGIN_ALLOW_THREADS |
| 6239 | pid = _cwait(&status, pid, options); |
| 6240 | Py_END_ALLOW_THREADS |
| 6241 | if (pid == -1) |
| 6242 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6243 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6244 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6245 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6246 | } |
| 6247 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6248 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6249 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6250 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6251 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6252 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6253 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6254 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6255 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6256 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6257 | pid_t pid; |
| 6258 | WAIT_TYPE status; |
| 6259 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6260 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6261 | Py_BEGIN_ALLOW_THREADS |
| 6262 | pid = wait(&status); |
| 6263 | Py_END_ALLOW_THREADS |
| 6264 | if (pid == -1) |
| 6265 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6266 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6267 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6268 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6269 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6270 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6271 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6272 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6273 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6274 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6275 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6276 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6277 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6278 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6279 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6280 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6281 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6282 | #ifdef MS_WINDOWS |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 6283 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6284 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6285 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6286 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6287 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6288 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6289 | } |
| 6290 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6291 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6292 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6293 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6294 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6295 | 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] | 6296 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6297 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6298 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6299 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6300 | PyObject* v; |
| 6301 | char buf[MAXPATHLEN]; |
| 6302 | PyObject *opath; |
| 6303 | char *path; |
| 6304 | int n; |
| 6305 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6306 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6307 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6308 | PyUnicode_FSConverter, &opath)) |
| 6309 | return NULL; |
| 6310 | path = PyBytes_AsString(opath); |
| 6311 | v = PySequence_GetItem(args, 0); |
| 6312 | if (v == NULL) { |
| 6313 | Py_DECREF(opath); |
| 6314 | return NULL; |
| 6315 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6316 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6317 | if (PyUnicode_Check(v)) { |
| 6318 | arg_is_unicode = 1; |
| 6319 | } |
| 6320 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6321 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6322 | Py_BEGIN_ALLOW_THREADS |
| 6323 | n = readlink(path, buf, (int) sizeof buf); |
| 6324 | Py_END_ALLOW_THREADS |
| 6325 | if (n < 0) |
| 6326 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6327 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6328 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6329 | if (arg_is_unicode) |
| 6330 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6331 | else |
| 6332 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6333 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6334 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6335 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6336 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6337 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6338 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6339 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6340 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6341 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6342 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6343 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6344 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6345 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6346 | } |
| 6347 | #endif /* HAVE_SYMLINK */ |
| 6348 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6349 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6350 | |
| 6351 | PyDoc_STRVAR(win_readlink__doc__, |
| 6352 | "readlink(path) -> path\n\n\ |
| 6353 | Return a string representing the path to which the symbolic link points."); |
| 6354 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6355 | /* Windows readlink implementation */ |
| 6356 | static PyObject * |
| 6357 | win_readlink(PyObject *self, PyObject *args) |
| 6358 | { |
| 6359 | wchar_t *path; |
| 6360 | DWORD n_bytes_returned; |
| 6361 | DWORD io_result; |
| 6362 | PyObject *result; |
| 6363 | HANDLE reparse_point_handle; |
| 6364 | |
| 6365 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6366 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6367 | wchar_t *print_name; |
| 6368 | |
| 6369 | if (!PyArg_ParseTuple(args, |
| 6370 | "u:readlink", |
| 6371 | &path)) |
| 6372 | return NULL; |
| 6373 | |
| 6374 | /* First get a handle to the reparse point */ |
| 6375 | Py_BEGIN_ALLOW_THREADS |
| 6376 | reparse_point_handle = CreateFileW( |
| 6377 | path, |
| 6378 | 0, |
| 6379 | 0, |
| 6380 | 0, |
| 6381 | OPEN_EXISTING, |
| 6382 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6383 | 0); |
| 6384 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6385 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6386 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 6387 | { |
| 6388 | return win32_error_unicode("readlink", path); |
| 6389 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6390 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6391 | Py_BEGIN_ALLOW_THREADS |
| 6392 | /* New call DeviceIoControl to read the reparse point */ |
| 6393 | io_result = DeviceIoControl( |
| 6394 | reparse_point_handle, |
| 6395 | FSCTL_GET_REPARSE_POINT, |
| 6396 | 0, 0, /* in buffer */ |
| 6397 | target_buffer, sizeof(target_buffer), |
| 6398 | &n_bytes_returned, |
| 6399 | 0 /* we're not using OVERLAPPED_IO */ |
| 6400 | ); |
| 6401 | CloseHandle(reparse_point_handle); |
| 6402 | Py_END_ALLOW_THREADS |
| 6403 | |
| 6404 | if (io_result==0) |
| 6405 | { |
| 6406 | return win32_error_unicode("readlink", path); |
| 6407 | } |
| 6408 | |
| 6409 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6410 | { |
| 6411 | PyErr_SetString(PyExc_ValueError, |
| 6412 | "not a symbolic link"); |
| 6413 | return NULL; |
| 6414 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6415 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6416 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6417 | |
| 6418 | result = PyUnicode_FromWideChar(print_name, |
| 6419 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6420 | return result; |
| 6421 | } |
| 6422 | |
| 6423 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6424 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6425 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6426 | |
| 6427 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6428 | static int has_CreateSymbolicLinkW = 0; |
| 6429 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6430 | static int |
| 6431 | check_CreateSymbolicLinkW() |
| 6432 | { |
| 6433 | HINSTANCE hKernel32; |
| 6434 | /* only recheck */ |
| 6435 | if (has_CreateSymbolicLinkW) |
| 6436 | return has_CreateSymbolicLinkW; |
| 6437 | hKernel32 = GetModuleHandle("KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6438 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6439 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6440 | if (Py_CreateSymbolicLinkW) |
| 6441 | has_CreateSymbolicLinkW = 1; |
| 6442 | return has_CreateSymbolicLinkW; |
| 6443 | } |
| 6444 | |
| 6445 | PyDoc_STRVAR(win_symlink__doc__, |
| 6446 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6447 | Create a symbolic link pointing to src named dst.\n\ |
| 6448 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6449 | a directory.\n\ |
| 6450 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6451 | NotImplementedError otherwise."); |
| 6452 | |
| 6453 | static PyObject * |
| 6454 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6455 | { |
| 6456 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
| 6457 | PyObject *src, *dest; |
| 6458 | int target_is_directory = 0; |
| 6459 | DWORD res; |
| 6460 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6461 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6462 | if (!check_CreateSymbolicLinkW()) |
| 6463 | { |
| 6464 | /* raise NotImplementedError */ |
| 6465 | return PyErr_Format(PyExc_NotImplementedError, |
| 6466 | "CreateSymbolicLinkW not found"); |
| 6467 | } |
| 6468 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink", |
| 6469 | kwlist, &src, &dest, &target_is_directory)) |
| 6470 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6471 | |
| 6472 | if (win32_can_symlink == 0) |
| 6473 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6474 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6475 | if (!convert_to_unicode(&src)) { return NULL; } |
| 6476 | if (!convert_to_unicode(&dest)) { |
| 6477 | Py_DECREF(src); |
| 6478 | return NULL; |
| 6479 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6480 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6481 | /* if src is a directory, ensure target_is_directory==1 */ |
| 6482 | if( |
| 6483 | GetFileAttributesExW( |
| 6484 | PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info |
| 6485 | )) |
| 6486 | { |
| 6487 | target_is_directory = target_is_directory || |
| 6488 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 6489 | } |
| 6490 | |
| 6491 | Py_BEGIN_ALLOW_THREADS |
| 6492 | res = Py_CreateSymbolicLinkW( |
| 6493 | PyUnicode_AsUnicode(dest), |
| 6494 | PyUnicode_AsUnicode(src), |
| 6495 | target_is_directory); |
| 6496 | Py_END_ALLOW_THREADS |
| 6497 | Py_DECREF(src); |
| 6498 | Py_DECREF(dest); |
| 6499 | if (!res) |
| 6500 | { |
| 6501 | return win32_error_unicode("symlink", PyUnicode_AsUnicode(src)); |
| 6502 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6503 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6504 | Py_INCREF(Py_None); |
| 6505 | return Py_None; |
| 6506 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6507 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6508 | |
| 6509 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6510 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6511 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6512 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6513 | { |
| 6514 | ULONG value = 0; |
| 6515 | |
| 6516 | Py_BEGIN_ALLOW_THREADS |
| 6517 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6518 | Py_END_ALLOW_THREADS |
| 6519 | |
| 6520 | return value; |
| 6521 | } |
| 6522 | |
| 6523 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6524 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6525 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6526 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6527 | return Py_BuildValue("ddddd", |
| 6528 | (double)0 /* t.tms_utime / HZ */, |
| 6529 | (double)0 /* t.tms_stime / HZ */, |
| 6530 | (double)0 /* t.tms_cutime / HZ */, |
| 6531 | (double)0 /* t.tms_cstime / HZ */, |
| 6532 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6533 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6534 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6535 | #define NEED_TICKS_PER_SECOND |
| 6536 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6537 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6538 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6539 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6540 | struct tms t; |
| 6541 | clock_t c; |
| 6542 | errno = 0; |
| 6543 | c = times(&t); |
| 6544 | if (c == (clock_t) -1) |
| 6545 | return posix_error(); |
| 6546 | return Py_BuildValue("ddddd", |
| 6547 | (double)t.tms_utime / ticks_per_second, |
| 6548 | (double)t.tms_stime / ticks_per_second, |
| 6549 | (double)t.tms_cutime / ticks_per_second, |
| 6550 | (double)t.tms_cstime / ticks_per_second, |
| 6551 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6552 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6553 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6554 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6555 | |
| 6556 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6557 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6558 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6559 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6560 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6561 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6562 | FILETIME create, exit, kernel, user; |
| 6563 | HANDLE hProc; |
| 6564 | hProc = GetCurrentProcess(); |
| 6565 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6566 | /* The fields of a FILETIME structure are the hi and lo part |
| 6567 | of a 64-bit value expressed in 100 nanosecond units. |
| 6568 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6569 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6570 | */ |
| 6571 | return Py_BuildValue( |
| 6572 | "ddddd", |
| 6573 | (double)(user.dwHighDateTime*429.4967296 + |
| 6574 | user.dwLowDateTime*1e-7), |
| 6575 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6576 | kernel.dwLowDateTime*1e-7), |
| 6577 | (double)0, |
| 6578 | (double)0, |
| 6579 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6580 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6581 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6582 | |
| 6583 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6584 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6585 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6586 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6587 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6588 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6589 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6590 | #ifdef HAVE_GETSID |
| 6591 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6592 | "getsid(pid) -> sid\n\n\ |
| 6593 | Call the system call getsid()."); |
| 6594 | |
| 6595 | static PyObject * |
| 6596 | posix_getsid(PyObject *self, PyObject *args) |
| 6597 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6598 | pid_t pid; |
| 6599 | int sid; |
| 6600 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6601 | return NULL; |
| 6602 | sid = getsid(pid); |
| 6603 | if (sid < 0) |
| 6604 | return posix_error(); |
| 6605 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6606 | } |
| 6607 | #endif /* HAVE_GETSID */ |
| 6608 | |
| 6609 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6610 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6611 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6612 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6613 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6614 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6615 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6616 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6617 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6618 | if (setsid() < 0) |
| 6619 | return posix_error(); |
| 6620 | Py_INCREF(Py_None); |
| 6621 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6622 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6623 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6624 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6625 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6626 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6627 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6628 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6629 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6630 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6631 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6632 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6633 | pid_t pid; |
| 6634 | int pgrp; |
| 6635 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6636 | return NULL; |
| 6637 | if (setpgid(pid, pgrp) < 0) |
| 6638 | return posix_error(); |
| 6639 | Py_INCREF(Py_None); |
| 6640 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6641 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6642 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6643 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6644 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6645 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6646 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6647 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6648 | 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] | 6649 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6650 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6651 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6652 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6653 | int fd; |
| 6654 | pid_t pgid; |
| 6655 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6656 | return NULL; |
| 6657 | pgid = tcgetpgrp(fd); |
| 6658 | if (pgid < 0) |
| 6659 | return posix_error(); |
| 6660 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6661 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6662 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6663 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6664 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6665 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6666 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6667 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6668 | 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] | 6669 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6670 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6671 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6672 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6673 | int fd; |
| 6674 | pid_t pgid; |
| 6675 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6676 | return NULL; |
| 6677 | if (tcsetpgrp(fd, pgid) < 0) |
| 6678 | return posix_error(); |
| 6679 | Py_INCREF(Py_None); |
| 6680 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6681 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6682 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6683 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6684 | /* Functions acting on file descriptors */ |
| 6685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6686 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6687 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6688 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6689 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6690 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6691 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6692 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6693 | PyObject *ofile; |
| 6694 | char *file; |
| 6695 | int flag; |
| 6696 | int mode = 0777; |
| 6697 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6698 | |
| 6699 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6700 | PyUnicodeObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6701 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6702 | Py_BEGIN_ALLOW_THREADS |
| 6703 | /* PyUnicode_AS_UNICODE OK without thread |
| 6704 | lock as it is a simple dereference. */ |
| 6705 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 6706 | Py_END_ALLOW_THREADS |
| 6707 | if (fd < 0) |
| 6708 | return posix_error(); |
| 6709 | return PyLong_FromLong((long)fd); |
| 6710 | } |
| 6711 | /* Drop the argument parsing error as narrow strings |
| 6712 | are also valid. */ |
| 6713 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6714 | #endif |
| 6715 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6716 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6717 | PyUnicode_FSConverter, &ofile, |
| 6718 | &flag, &mode)) |
| 6719 | return NULL; |
| 6720 | file = PyBytes_AsString(ofile); |
| 6721 | Py_BEGIN_ALLOW_THREADS |
| 6722 | fd = open(file, flag, mode); |
| 6723 | Py_END_ALLOW_THREADS |
| 6724 | if (fd < 0) |
| 6725 | return posix_error_with_allocated_filename(ofile); |
| 6726 | Py_DECREF(ofile); |
| 6727 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6728 | } |
| 6729 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6731 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6732 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6733 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6734 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6735 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6736 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6737 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6738 | int fd, res; |
| 6739 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6740 | return NULL; |
| 6741 | if (!_PyVerify_fd(fd)) |
| 6742 | return posix_error(); |
| 6743 | Py_BEGIN_ALLOW_THREADS |
| 6744 | res = close(fd); |
| 6745 | Py_END_ALLOW_THREADS |
| 6746 | if (res < 0) |
| 6747 | return posix_error(); |
| 6748 | Py_INCREF(Py_None); |
| 6749 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6752 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6753 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6754 | "closerange(fd_low, fd_high)\n\n\ |
| 6755 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6756 | |
| 6757 | static PyObject * |
| 6758 | posix_closerange(PyObject *self, PyObject *args) |
| 6759 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6760 | int fd_from, fd_to, i; |
| 6761 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6762 | return NULL; |
| 6763 | Py_BEGIN_ALLOW_THREADS |
| 6764 | for (i = fd_from; i < fd_to; i++) |
| 6765 | if (_PyVerify_fd(i)) |
| 6766 | close(i); |
| 6767 | Py_END_ALLOW_THREADS |
| 6768 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6769 | } |
| 6770 | |
| 6771 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6772 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6773 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6774 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6775 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6776 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6777 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6778 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6779 | int fd; |
| 6780 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6781 | return NULL; |
| 6782 | if (!_PyVerify_fd(fd)) |
| 6783 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6784 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6785 | if (fd < 0) |
| 6786 | return posix_error(); |
| 6787 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6788 | } |
| 6789 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6791 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6792 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6793 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6794 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6795 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6796 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6797 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6798 | int fd, fd2, res; |
| 6799 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6800 | return NULL; |
| 6801 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6802 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6803 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6804 | if (res < 0) |
| 6805 | return posix_error(); |
| 6806 | Py_INCREF(Py_None); |
| 6807 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6810 | #ifdef HAVE_LOCKF |
| 6811 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6812 | "lockf(fd, cmd, len)\n\n\ |
| 6813 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6814 | fd is an open file descriptor.\n\ |
| 6815 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6816 | F_TEST.\n\ |
| 6817 | len specifies the section of the file to lock."); |
| 6818 | |
| 6819 | static PyObject * |
| 6820 | posix_lockf(PyObject *self, PyObject *args) |
| 6821 | { |
| 6822 | int fd, cmd, res; |
| 6823 | off_t len; |
| 6824 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6825 | &fd, &cmd, _parse_off_t, &len)) |
| 6826 | return NULL; |
| 6827 | |
| 6828 | Py_BEGIN_ALLOW_THREADS |
| 6829 | res = lockf(fd, cmd, len); |
| 6830 | Py_END_ALLOW_THREADS |
| 6831 | |
| 6832 | if (res < 0) |
| 6833 | return posix_error(); |
| 6834 | |
| 6835 | Py_RETURN_NONE; |
| 6836 | } |
| 6837 | #endif |
| 6838 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6839 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6840 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6841 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6842 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6843 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6844 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6845 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6846 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6847 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6848 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6849 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6850 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6851 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6852 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6853 | PyObject *posobj; |
| 6854 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6855 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6856 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6857 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6858 | switch (how) { |
| 6859 | case 0: how = SEEK_SET; break; |
| 6860 | case 1: how = SEEK_CUR; break; |
| 6861 | case 2: how = SEEK_END; break; |
| 6862 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6863 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6864 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6865 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6866 | pos = PyLong_AsLong(posobj); |
| 6867 | #else |
| 6868 | pos = PyLong_AsLongLong(posobj); |
| 6869 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6870 | if (PyErr_Occurred()) |
| 6871 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6872 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6873 | if (!_PyVerify_fd(fd)) |
| 6874 | return posix_error(); |
| 6875 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6876 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6877 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6878 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6879 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6880 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6881 | Py_END_ALLOW_THREADS |
| 6882 | if (res < 0) |
| 6883 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6884 | |
| 6885 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6886 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6887 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6888 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6889 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6890 | } |
| 6891 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6892 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6893 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6894 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6895 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6896 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6897 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6898 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6899 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6900 | int fd, size; |
| 6901 | Py_ssize_t n; |
| 6902 | PyObject *buffer; |
| 6903 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6904 | return NULL; |
| 6905 | if (size < 0) { |
| 6906 | errno = EINVAL; |
| 6907 | return posix_error(); |
| 6908 | } |
| 6909 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6910 | if (buffer == NULL) |
| 6911 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6912 | if (!_PyVerify_fd(fd)) { |
| 6913 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6914 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6915 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6916 | Py_BEGIN_ALLOW_THREADS |
| 6917 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 6918 | Py_END_ALLOW_THREADS |
| 6919 | if (n < 0) { |
| 6920 | Py_DECREF(buffer); |
| 6921 | return posix_error(); |
| 6922 | } |
| 6923 | if (n != size) |
| 6924 | _PyBytes_Resize(&buffer, n); |
| 6925 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6926 | } |
| 6927 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6928 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 6929 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6930 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6931 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 6932 | { |
| 6933 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6934 | Py_ssize_t blen, total = 0; |
| 6935 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6936 | *iov = PyMem_New(struct iovec, cnt); |
| 6937 | if (*iov == NULL) { |
| 6938 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6939 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6940 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6941 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6942 | *buf = PyMem_New(Py_buffer, cnt); |
| 6943 | if (*buf == NULL) { |
| 6944 | PyMem_Del(*iov); |
| 6945 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6946 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
| 6949 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6950 | PyObject *item = PySequence_GetItem(seq, i); |
| 6951 | if (item == NULL) |
| 6952 | goto fail; |
| 6953 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 6954 | Py_DECREF(item); |
| 6955 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6956 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6957 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6958 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6959 | blen = (*buf)[i].len; |
| 6960 | (*iov)[i].iov_len = blen; |
| 6961 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6962 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6963 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6964 | |
| 6965 | fail: |
| 6966 | PyMem_Del(*iov); |
| 6967 | for (j = 0; j < i; j++) { |
| 6968 | PyBuffer_Release(&(*buf)[j]); |
| 6969 | } |
| 6970 | PyMem_Del(*buf); |
| 6971 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6972 | } |
| 6973 | |
| 6974 | static void |
| 6975 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 6976 | { |
| 6977 | int i; |
| 6978 | PyMem_Del(iov); |
| 6979 | for (i = 0; i < cnt; i++) { |
| 6980 | PyBuffer_Release(&buf[i]); |
| 6981 | } |
| 6982 | PyMem_Del(buf); |
| 6983 | } |
| 6984 | #endif |
| 6985 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6986 | #ifdef HAVE_READV |
| 6987 | PyDoc_STRVAR(posix_readv__doc__, |
| 6988 | "readv(fd, buffers) -> bytesread\n\n\ |
| 6989 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 6990 | is an arbitrary sequence of writable buffers.\n\ |
| 6991 | Returns the total number of bytes read."); |
| 6992 | |
| 6993 | static PyObject * |
| 6994 | posix_readv(PyObject *self, PyObject *args) |
| 6995 | { |
| 6996 | int fd, cnt; |
| 6997 | Py_ssize_t n; |
| 6998 | PyObject *seq; |
| 6999 | struct iovec *iov; |
| 7000 | Py_buffer *buf; |
| 7001 | |
| 7002 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7003 | return NULL; |
| 7004 | if (!PySequence_Check(seq)) { |
| 7005 | PyErr_SetString(PyExc_TypeError, |
| 7006 | "readv() arg 2 must be a sequence"); |
| 7007 | return NULL; |
| 7008 | } |
| 7009 | cnt = PySequence_Size(seq); |
| 7010 | |
| 7011 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7012 | return NULL; |
| 7013 | |
| 7014 | Py_BEGIN_ALLOW_THREADS |
| 7015 | n = readv(fd, iov, cnt); |
| 7016 | Py_END_ALLOW_THREADS |
| 7017 | |
| 7018 | iov_cleanup(iov, buf, cnt); |
| 7019 | return PyLong_FromSsize_t(n); |
| 7020 | } |
| 7021 | #endif |
| 7022 | |
| 7023 | #ifdef HAVE_PREAD |
| 7024 | PyDoc_STRVAR(posix_pread__doc__, |
| 7025 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7026 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7027 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7028 | |
| 7029 | static PyObject * |
| 7030 | posix_pread(PyObject *self, PyObject *args) |
| 7031 | { |
| 7032 | int fd, size; |
| 7033 | off_t offset; |
| 7034 | Py_ssize_t n; |
| 7035 | PyObject *buffer; |
| 7036 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7037 | return NULL; |
| 7038 | |
| 7039 | if (size < 0) { |
| 7040 | errno = EINVAL; |
| 7041 | return posix_error(); |
| 7042 | } |
| 7043 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7044 | if (buffer == NULL) |
| 7045 | return NULL; |
| 7046 | if (!_PyVerify_fd(fd)) { |
| 7047 | Py_DECREF(buffer); |
| 7048 | return posix_error(); |
| 7049 | } |
| 7050 | Py_BEGIN_ALLOW_THREADS |
| 7051 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7052 | Py_END_ALLOW_THREADS |
| 7053 | if (n < 0) { |
| 7054 | Py_DECREF(buffer); |
| 7055 | return posix_error(); |
| 7056 | } |
| 7057 | if (n != size) |
| 7058 | _PyBytes_Resize(&buffer, n); |
| 7059 | return buffer; |
| 7060 | } |
| 7061 | #endif |
| 7062 | |
| 7063 | PyDoc_STRVAR(posix_write__doc__, |
| 7064 | "write(fd, string) -> byteswritten\n\n\ |
| 7065 | Write a string to a file descriptor."); |
| 7066 | |
| 7067 | static PyObject * |
| 7068 | posix_write(PyObject *self, PyObject *args) |
| 7069 | { |
| 7070 | Py_buffer pbuf; |
| 7071 | int fd; |
| 7072 | Py_ssize_t size, len; |
| 7073 | |
| 7074 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7075 | return NULL; |
| 7076 | if (!_PyVerify_fd(fd)) { |
| 7077 | PyBuffer_Release(&pbuf); |
| 7078 | return posix_error(); |
| 7079 | } |
| 7080 | len = pbuf.len; |
| 7081 | Py_BEGIN_ALLOW_THREADS |
| 7082 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7083 | if (len > INT_MAX) |
| 7084 | len = INT_MAX; |
| 7085 | size = write(fd, pbuf.buf, (int)len); |
| 7086 | #else |
| 7087 | size = write(fd, pbuf.buf, len); |
| 7088 | #endif |
| 7089 | Py_END_ALLOW_THREADS |
| 7090 | PyBuffer_Release(&pbuf); |
| 7091 | if (size < 0) |
| 7092 | return posix_error(); |
| 7093 | return PyLong_FromSsize_t(size); |
| 7094 | } |
| 7095 | |
| 7096 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7097 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7098 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7099 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7100 | -> byteswritten\n\ |
| 7101 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7102 | |
| 7103 | static PyObject * |
| 7104 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7105 | { |
| 7106 | int in, out; |
| 7107 | Py_ssize_t ret; |
| 7108 | off_t offset; |
| 7109 | |
| 7110 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7111 | #ifndef __APPLE__ |
| 7112 | Py_ssize_t len; |
| 7113 | #endif |
| 7114 | PyObject *headers = NULL, *trailers = NULL; |
| 7115 | Py_buffer *hbuf, *tbuf; |
| 7116 | off_t sbytes; |
| 7117 | struct sf_hdtr sf; |
| 7118 | int flags = 0; |
| 7119 | sf.headers = NULL; |
| 7120 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7121 | static char *keywords[] = {"out", "in", |
| 7122 | "offset", "count", |
| 7123 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7124 | |
| 7125 | #ifdef __APPLE__ |
| 7126 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7127 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7128 | #else |
| 7129 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7130 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7131 | #endif |
| 7132 | &headers, &trailers, &flags)) |
| 7133 | return NULL; |
| 7134 | if (headers != NULL) { |
| 7135 | if (!PySequence_Check(headers)) { |
| 7136 | PyErr_SetString(PyExc_TypeError, |
| 7137 | "sendfile() headers must be a sequence or None"); |
| 7138 | return NULL; |
| 7139 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7140 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7141 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7142 | if (sf.hdr_cnt > 0 && |
| 7143 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7144 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7145 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7146 | #ifdef __APPLE__ |
| 7147 | sbytes += i; |
| 7148 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7149 | } |
| 7150 | } |
| 7151 | if (trailers != NULL) { |
| 7152 | if (!PySequence_Check(trailers)) { |
| 7153 | PyErr_SetString(PyExc_TypeError, |
| 7154 | "sendfile() trailers must be a sequence or None"); |
| 7155 | return NULL; |
| 7156 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7157 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7158 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7159 | if (sf.trl_cnt > 0 && |
| 7160 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7161 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7162 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7163 | #ifdef __APPLE__ |
| 7164 | sbytes += i; |
| 7165 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7166 | } |
| 7167 | } |
| 7168 | |
| 7169 | Py_BEGIN_ALLOW_THREADS |
| 7170 | #ifdef __APPLE__ |
| 7171 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7172 | #else |
| 7173 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7174 | #endif |
| 7175 | Py_END_ALLOW_THREADS |
| 7176 | |
| 7177 | if (sf.headers != NULL) |
| 7178 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7179 | if (sf.trailers != NULL) |
| 7180 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7181 | |
| 7182 | if (ret < 0) { |
| 7183 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7184 | if (sbytes != 0) { |
| 7185 | // some data has been sent |
| 7186 | goto done; |
| 7187 | } |
| 7188 | else { |
| 7189 | // no data has been sent; upper application is supposed |
| 7190 | // to retry on EAGAIN or EBUSY |
| 7191 | return posix_error(); |
| 7192 | } |
| 7193 | } |
| 7194 | return posix_error(); |
| 7195 | } |
| 7196 | goto done; |
| 7197 | |
| 7198 | done: |
| 7199 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7200 | return Py_BuildValue("l", sbytes); |
| 7201 | #else |
| 7202 | return Py_BuildValue("L", sbytes); |
| 7203 | #endif |
| 7204 | |
| 7205 | #else |
| 7206 | Py_ssize_t count; |
| 7207 | PyObject *offobj; |
| 7208 | static char *keywords[] = {"out", "in", |
| 7209 | "offset", "count", NULL}; |
| 7210 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7211 | keywords, &out, &in, &offobj, &count)) |
| 7212 | return NULL; |
| 7213 | #ifdef linux |
| 7214 | if (offobj == Py_None) { |
| 7215 | Py_BEGIN_ALLOW_THREADS |
| 7216 | ret = sendfile(out, in, NULL, count); |
| 7217 | Py_END_ALLOW_THREADS |
| 7218 | if (ret < 0) |
| 7219 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7220 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7221 | } |
| 7222 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7223 | if (!_parse_off_t(offobj, &offset)) |
| 7224 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7225 | Py_BEGIN_ALLOW_THREADS |
| 7226 | ret = sendfile(out, in, &offset, count); |
| 7227 | Py_END_ALLOW_THREADS |
| 7228 | if (ret < 0) |
| 7229 | return posix_error(); |
| 7230 | return Py_BuildValue("n", ret); |
| 7231 | #endif |
| 7232 | } |
| 7233 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7234 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7235 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7236 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7237 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7238 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7239 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7240 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7241 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7242 | int fd; |
| 7243 | STRUCT_STAT st; |
| 7244 | int res; |
| 7245 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 7246 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7247 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7248 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7249 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7250 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7251 | if (!_PyVerify_fd(fd)) |
| 7252 | return posix_error(); |
| 7253 | Py_BEGIN_ALLOW_THREADS |
| 7254 | res = FSTAT(fd, &st); |
| 7255 | Py_END_ALLOW_THREADS |
| 7256 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7257 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7258 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7259 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7260 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7261 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7262 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7263 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7264 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7265 | } |
| 7266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7267 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7268 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7269 | 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] | 7270 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7271 | |
| 7272 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7273 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7274 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7275 | int fd; |
| 7276 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7277 | return NULL; |
| 7278 | if (!_PyVerify_fd(fd)) |
| 7279 | return PyBool_FromLong(0); |
| 7280 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7281 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7282 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7283 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7284 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7285 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7286 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7287 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7288 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7289 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7290 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7291 | #if defined(PYOS_OS2) |
| 7292 | HFILE read, write; |
| 7293 | APIRET rc; |
| 7294 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7295 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7296 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7297 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7298 | |
| 7299 | return Py_BuildValue("(ii)", read, write); |
| 7300 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7301 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7302 | int fds[2]; |
| 7303 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7304 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7305 | if (res != 0) |
| 7306 | return posix_error(); |
| 7307 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7308 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7309 | HANDLE read, write; |
| 7310 | int read_fd, write_fd; |
| 7311 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7312 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7313 | if (!ok) |
| 7314 | return win32_error("CreatePipe", NULL); |
| 7315 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7316 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7317 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7318 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7319 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7320 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7321 | #endif /* HAVE_PIPE */ |
| 7322 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7323 | #ifdef HAVE_PIPE2 |
| 7324 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7325 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7326 | Create a pipe with flags set atomically.\n\ |
| 7327 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7328 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7329 | "); |
| 7330 | |
| 7331 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7332 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7333 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7334 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7335 | int fds[2]; |
| 7336 | int res; |
| 7337 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7338 | flags = PyLong_AsLong(arg); |
| 7339 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7340 | return NULL; |
| 7341 | |
| 7342 | res = pipe2(fds, flags); |
| 7343 | if (res != 0) |
| 7344 | return posix_error(); |
| 7345 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7346 | } |
| 7347 | #endif /* HAVE_PIPE2 */ |
| 7348 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7349 | #ifdef HAVE_WRITEV |
| 7350 | PyDoc_STRVAR(posix_writev__doc__, |
| 7351 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7352 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7353 | arbitrary sequence of buffers.\n\ |
| 7354 | Returns the total bytes written."); |
| 7355 | |
| 7356 | static PyObject * |
| 7357 | posix_writev(PyObject *self, PyObject *args) |
| 7358 | { |
| 7359 | int fd, cnt; |
| 7360 | Py_ssize_t res; |
| 7361 | PyObject *seq; |
| 7362 | struct iovec *iov; |
| 7363 | Py_buffer *buf; |
| 7364 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7365 | return NULL; |
| 7366 | if (!PySequence_Check(seq)) { |
| 7367 | PyErr_SetString(PyExc_TypeError, |
| 7368 | "writev() arg 2 must be a sequence"); |
| 7369 | return NULL; |
| 7370 | } |
| 7371 | cnt = PySequence_Size(seq); |
| 7372 | |
| 7373 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7374 | return NULL; |
| 7375 | } |
| 7376 | |
| 7377 | Py_BEGIN_ALLOW_THREADS |
| 7378 | res = writev(fd, iov, cnt); |
| 7379 | Py_END_ALLOW_THREADS |
| 7380 | |
| 7381 | iov_cleanup(iov, buf, cnt); |
| 7382 | return PyLong_FromSsize_t(res); |
| 7383 | } |
| 7384 | #endif |
| 7385 | |
| 7386 | #ifdef HAVE_PWRITE |
| 7387 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7388 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7389 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7390 | offset unchanged."); |
| 7391 | |
| 7392 | static PyObject * |
| 7393 | posix_pwrite(PyObject *self, PyObject *args) |
| 7394 | { |
| 7395 | Py_buffer pbuf; |
| 7396 | int fd; |
| 7397 | off_t offset; |
| 7398 | Py_ssize_t size; |
| 7399 | |
| 7400 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7401 | return NULL; |
| 7402 | |
| 7403 | if (!_PyVerify_fd(fd)) { |
| 7404 | PyBuffer_Release(&pbuf); |
| 7405 | return posix_error(); |
| 7406 | } |
| 7407 | Py_BEGIN_ALLOW_THREADS |
| 7408 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7409 | Py_END_ALLOW_THREADS |
| 7410 | PyBuffer_Release(&pbuf); |
| 7411 | if (size < 0) |
| 7412 | return posix_error(); |
| 7413 | return PyLong_FromSsize_t(size); |
| 7414 | } |
| 7415 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7416 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7417 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7418 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7419 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7420 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7421 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7422 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7423 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7424 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7425 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7426 | char *filename; |
| 7427 | int mode = 0666; |
| 7428 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7429 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7430 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7431 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7432 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7433 | Py_BEGIN_ALLOW_THREADS |
| 7434 | res = mkfifo(filename, mode); |
| 7435 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7436 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7437 | if (res < 0) |
| 7438 | return posix_error(); |
| 7439 | Py_INCREF(Py_None); |
| 7440 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7441 | } |
| 7442 | #endif |
| 7443 | |
| 7444 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7445 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7446 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7447 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7448 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7449 | named filename. mode specifies both the permissions to use and the\n\ |
| 7450 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7451 | 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] | 7452 | device defines the newly created device special file (probably using\n\ |
| 7453 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7454 | |
| 7455 | |
| 7456 | static PyObject * |
| 7457 | posix_mknod(PyObject *self, PyObject *args) |
| 7458 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7459 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7460 | char *filename; |
| 7461 | int mode = 0600; |
| 7462 | int device = 0; |
| 7463 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7464 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7465 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7466 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7467 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7468 | Py_BEGIN_ALLOW_THREADS |
| 7469 | res = mknod(filename, mode, device); |
| 7470 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7471 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7472 | if (res < 0) |
| 7473 | return posix_error(); |
| 7474 | Py_INCREF(Py_None); |
| 7475 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7476 | } |
| 7477 | #endif |
| 7478 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7479 | #ifdef HAVE_DEVICE_MACROS |
| 7480 | PyDoc_STRVAR(posix_major__doc__, |
| 7481 | "major(device) -> major number\n\ |
| 7482 | Extracts a device major number from a raw device number."); |
| 7483 | |
| 7484 | static PyObject * |
| 7485 | posix_major(PyObject *self, PyObject *args) |
| 7486 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7487 | int device; |
| 7488 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7489 | return NULL; |
| 7490 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7491 | } |
| 7492 | |
| 7493 | PyDoc_STRVAR(posix_minor__doc__, |
| 7494 | "minor(device) -> minor number\n\ |
| 7495 | Extracts a device minor number from a raw device number."); |
| 7496 | |
| 7497 | static PyObject * |
| 7498 | posix_minor(PyObject *self, PyObject *args) |
| 7499 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7500 | int device; |
| 7501 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7502 | return NULL; |
| 7503 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7504 | } |
| 7505 | |
| 7506 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7507 | "makedev(major, minor) -> device number\n\ |
| 7508 | Composes a raw device number from the major and minor device numbers."); |
| 7509 | |
| 7510 | static PyObject * |
| 7511 | posix_makedev(PyObject *self, PyObject *args) |
| 7512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7513 | int major, minor; |
| 7514 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7515 | return NULL; |
| 7516 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7517 | } |
| 7518 | #endif /* device macros */ |
| 7519 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7520 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7521 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7522 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7523 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7524 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7526 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7527 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7528 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7529 | int fd; |
| 7530 | off_t length; |
| 7531 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7532 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7533 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7534 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7535 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7536 | Py_BEGIN_ALLOW_THREADS |
| 7537 | res = ftruncate(fd, length); |
| 7538 | Py_END_ALLOW_THREADS |
| 7539 | if (res < 0) |
| 7540 | return posix_error(); |
| 7541 | Py_INCREF(Py_None); |
| 7542 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7543 | } |
| 7544 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7545 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7546 | #ifdef HAVE_TRUNCATE |
| 7547 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7548 | "truncate(path, length)\n\n\ |
| 7549 | Truncate the file given by path to length bytes."); |
| 7550 | |
| 7551 | static PyObject * |
| 7552 | posix_truncate(PyObject *self, PyObject *args) |
| 7553 | { |
| 7554 | PyObject *opath; |
| 7555 | const char *path; |
| 7556 | off_t length; |
| 7557 | int res; |
| 7558 | |
| 7559 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 7560 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 7561 | return NULL; |
| 7562 | path = PyBytes_AsString(opath); |
| 7563 | |
| 7564 | Py_BEGIN_ALLOW_THREADS |
| 7565 | res = truncate(path, length); |
| 7566 | Py_END_ALLOW_THREADS |
| 7567 | Py_DECREF(opath); |
| 7568 | if (res < 0) |
| 7569 | return posix_error(); |
| 7570 | Py_RETURN_NONE; |
| 7571 | } |
| 7572 | #endif |
| 7573 | |
| 7574 | #ifdef HAVE_POSIX_FALLOCATE |
| 7575 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7576 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7577 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7578 | starting from offset and continuing for len bytes."); |
| 7579 | |
| 7580 | static PyObject * |
| 7581 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7582 | { |
| 7583 | off_t len, offset; |
| 7584 | int res, fd; |
| 7585 | |
| 7586 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7587 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7588 | return NULL; |
| 7589 | |
| 7590 | Py_BEGIN_ALLOW_THREADS |
| 7591 | res = posix_fallocate(fd, offset, len); |
| 7592 | Py_END_ALLOW_THREADS |
| 7593 | if (res != 0) { |
| 7594 | errno = res; |
| 7595 | return posix_error(); |
| 7596 | } |
| 7597 | Py_RETURN_NONE; |
| 7598 | } |
| 7599 | #endif |
| 7600 | |
| 7601 | #ifdef HAVE_POSIX_FADVISE |
| 7602 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 7603 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 7604 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 7605 | the kernel to make optimizations.\n\ |
| 7606 | The advice applies to the region of the file specified by fd starting at\n\ |
| 7607 | offset and continuing for len bytes.\n\ |
| 7608 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 7609 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 7610 | POSIX_FADV_DONTNEED."); |
| 7611 | |
| 7612 | static PyObject * |
| 7613 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 7614 | { |
| 7615 | off_t len, offset; |
| 7616 | int res, fd, advice; |
| 7617 | |
| 7618 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 7619 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 7620 | return NULL; |
| 7621 | |
| 7622 | Py_BEGIN_ALLOW_THREADS |
| 7623 | res = posix_fadvise(fd, offset, len, advice); |
| 7624 | Py_END_ALLOW_THREADS |
| 7625 | if (res != 0) { |
| 7626 | errno = res; |
| 7627 | return posix_error(); |
| 7628 | } |
| 7629 | Py_RETURN_NONE; |
| 7630 | } |
| 7631 | #endif |
| 7632 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7633 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7634 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7635 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7636 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7637 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7638 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7639 | * get re-set with another call for the same key. */ |
| 7640 | static PyObject *posix_putenv_garbage; |
| 7641 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7642 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7643 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7644 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7645 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7646 | wchar_t *s1, *s2; |
| 7647 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7648 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7649 | PyObject *os1, *os2; |
| 7650 | char *s1, *s2; |
| 7651 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7652 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7653 | PyObject *newstr = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7654 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7655 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7656 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7657 | if (!PyArg_ParseTuple(args, |
| 7658 | "uu:putenv", |
| 7659 | &s1, &s2)) |
| 7660 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7661 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7662 | if (!PyArg_ParseTuple(args, |
| 7663 | "O&O&:putenv", |
| 7664 | PyUnicode_FSConverter, &os1, |
| 7665 | PyUnicode_FSConverter, &os2)) |
| 7666 | return NULL; |
| 7667 | s1 = PyBytes_AsString(os1); |
| 7668 | s2 = PyBytes_AsString(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7669 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7670 | |
| 7671 | #if defined(PYOS_OS2) |
| 7672 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 7673 | APIRET rc; |
| 7674 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7675 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7676 | if (rc != NO_ERROR) { |
| 7677 | os2_error(rc); |
| 7678 | goto error; |
| 7679 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7680 | |
| 7681 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 7682 | APIRET rc; |
| 7683 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7684 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7685 | if (rc != NO_ERROR) { |
| 7686 | os2_error(rc); |
| 7687 | goto error; |
| 7688 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7689 | } else { |
| 7690 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7691 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 7692 | /* len includes space for a trailing \0; the size arg to |
| 7693 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7694 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7695 | len = wcslen(s1) + wcslen(s2) + 2; |
| 7696 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7697 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7698 | len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7699 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7700 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7701 | if (newstr == NULL) { |
| 7702 | PyErr_NoMemory(); |
| 7703 | goto error; |
| 7704 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7705 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7706 | newenv = PyUnicode_AsUnicode(newstr); |
| 7707 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 7708 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7709 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7710 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7711 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7712 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7713 | newenv = PyBytes_AS_STRING(newstr); |
| 7714 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 7715 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7716 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7717 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7718 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7719 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7720 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7721 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7722 | * this will cause previous value to be collected. This has to |
| 7723 | * happen after the real putenv() call because the old value |
| 7724 | * was still accessible until then. */ |
| 7725 | if (PyDict_SetItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7726 | #ifdef MS_WINDOWS |
| 7727 | PyTuple_GET_ITEM(args, 0), |
| 7728 | #else |
| 7729 | os1, |
| 7730 | #endif |
| 7731 | newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7732 | /* really not much we can do; just leak */ |
| 7733 | PyErr_Clear(); |
| 7734 | } |
| 7735 | else { |
| 7736 | Py_DECREF(newstr); |
| 7737 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7738 | |
| 7739 | #if defined(PYOS_OS2) |
| 7740 | } |
| 7741 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7742 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7743 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7744 | Py_DECREF(os1); |
| 7745 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7746 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7747 | Py_RETURN_NONE; |
| 7748 | |
| 7749 | error: |
| 7750 | #ifndef MS_WINDOWS |
| 7751 | Py_DECREF(os1); |
| 7752 | Py_DECREF(os2); |
| 7753 | #endif |
| 7754 | Py_XDECREF(newstr); |
| 7755 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7756 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7757 | #endif /* putenv */ |
| 7758 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7759 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7760 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7761 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7762 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7763 | |
| 7764 | static PyObject * |
| 7765 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7766 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7767 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7768 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7769 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7770 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 7771 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7772 | #else |
| 7773 | PyObject *os1; |
| 7774 | char *s1; |
| 7775 | |
| 7776 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
| 7777 | PyUnicode_FSConverter, &os1)) |
| 7778 | return NULL; |
| 7779 | s1 = PyBytes_AsString(os1); |
| 7780 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7781 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7782 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7783 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | /* Remove the key from posix_putenv_garbage; |
| 7785 | * this will cause it to be collected. This has to |
| 7786 | * happen after the real unsetenv() call because the |
| 7787 | * old value was still accessible until then. |
| 7788 | */ |
| 7789 | if (PyDict_DelItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7790 | #ifdef MS_WINDOWS |
| 7791 | PyTuple_GET_ITEM(args, 0) |
| 7792 | #else |
| 7793 | os1 |
| 7794 | #endif |
| 7795 | )) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7796 | /* really not much we can do; just leak */ |
| 7797 | PyErr_Clear(); |
| 7798 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7799 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7800 | #ifndef MS_WINDOWS |
| 7801 | Py_DECREF(os1); |
| 7802 | #endif |
| 7803 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7804 | } |
| 7805 | #endif /* unsetenv */ |
| 7806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7807 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7808 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7809 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7810 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7811 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7812 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7813 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7814 | int code; |
| 7815 | char *message; |
| 7816 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7817 | return NULL; |
| 7818 | message = strerror(code); |
| 7819 | if (message == NULL) { |
| 7820 | PyErr_SetString(PyExc_ValueError, |
| 7821 | "strerror() argument out of range"); |
| 7822 | return NULL; |
| 7823 | } |
| 7824 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7825 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7826 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7827 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7828 | #ifdef HAVE_SYS_WAIT_H |
| 7829 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7830 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7831 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7832 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7833 | 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] | 7834 | |
| 7835 | static PyObject * |
| 7836 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7837 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7838 | WAIT_TYPE status; |
| 7839 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7840 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7841 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7842 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7843 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7844 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7845 | } |
| 7846 | #endif /* WCOREDUMP */ |
| 7847 | |
| 7848 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7849 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7850 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7851 | 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] | 7852 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7853 | |
| 7854 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7855 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7856 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7857 | WAIT_TYPE status; |
| 7858 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7859 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7860 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7861 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7862 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7863 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7864 | } |
| 7865 | #endif /* WIFCONTINUED */ |
| 7866 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7867 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7868 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7869 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7870 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7871 | |
| 7872 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7873 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7874 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7875 | WAIT_TYPE status; |
| 7876 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7877 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7878 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7879 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7880 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7881 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7882 | } |
| 7883 | #endif /* WIFSTOPPED */ |
| 7884 | |
| 7885 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7886 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7887 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7888 | 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] | 7889 | |
| 7890 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7891 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7892 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7893 | WAIT_TYPE status; |
| 7894 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7895 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7896 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7897 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7898 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7899 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7900 | } |
| 7901 | #endif /* WIFSIGNALED */ |
| 7902 | |
| 7903 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7904 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7905 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7906 | 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] | 7907 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7908 | |
| 7909 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7910 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7911 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7912 | WAIT_TYPE status; |
| 7913 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7915 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7916 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7917 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7918 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7919 | } |
| 7920 | #endif /* WIFEXITED */ |
| 7921 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7922 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7923 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7924 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7925 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7926 | |
| 7927 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7928 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7929 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7930 | WAIT_TYPE status; |
| 7931 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7933 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 7934 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7936 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7937 | } |
| 7938 | #endif /* WEXITSTATUS */ |
| 7939 | |
| 7940 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7941 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7942 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7943 | 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] | 7944 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7945 | |
| 7946 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7947 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7948 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7949 | WAIT_TYPE status; |
| 7950 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7951 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7952 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 7953 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7954 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7955 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7956 | } |
| 7957 | #endif /* WTERMSIG */ |
| 7958 | |
| 7959 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7960 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7961 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7962 | Return the signal that stopped the process that provided\n\ |
| 7963 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7964 | |
| 7965 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7966 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7967 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7968 | WAIT_TYPE status; |
| 7969 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7970 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7971 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 7972 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7973 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7974 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7975 | } |
| 7976 | #endif /* WSTOPSIG */ |
| 7977 | |
| 7978 | #endif /* HAVE_SYS_WAIT_H */ |
| 7979 | |
| 7980 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7981 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 7982 | #ifdef _SCO_DS |
| 7983 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 7984 | needed definitions in sys/statvfs.h */ |
| 7985 | #define _SVID3 |
| 7986 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7987 | #include <sys/statvfs.h> |
| 7988 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7989 | static PyObject* |
| 7990 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7991 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 7992 | if (v == NULL) |
| 7993 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7994 | |
| 7995 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7996 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 7997 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 7998 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 7999 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8000 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8001 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8002 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8003 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8004 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8005 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8006 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8007 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8008 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8009 | PyStructSequence_SET_ITEM(v, 2, |
| 8010 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8011 | PyStructSequence_SET_ITEM(v, 3, |
| 8012 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8013 | PyStructSequence_SET_ITEM(v, 4, |
| 8014 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8015 | PyStructSequence_SET_ITEM(v, 5, |
| 8016 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8017 | PyStructSequence_SET_ITEM(v, 6, |
| 8018 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8019 | PyStructSequence_SET_ITEM(v, 7, |
| 8020 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8021 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8022 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8023 | #endif |
| 8024 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8025 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8026 | } |
| 8027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8028 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8029 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8030 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8031 | |
| 8032 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8033 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8034 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8035 | int fd, res; |
| 8036 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8037 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8038 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8039 | return NULL; |
| 8040 | Py_BEGIN_ALLOW_THREADS |
| 8041 | res = fstatvfs(fd, &st); |
| 8042 | Py_END_ALLOW_THREADS |
| 8043 | if (res != 0) |
| 8044 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8045 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8046 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8047 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8048 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8049 | |
| 8050 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8051 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8052 | #include <sys/statvfs.h> |
| 8053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8054 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8055 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8056 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8057 | |
| 8058 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8059 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8060 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8061 | char *path; |
| 8062 | int res; |
| 8063 | struct statvfs st; |
| 8064 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
| 8065 | return NULL; |
| 8066 | Py_BEGIN_ALLOW_THREADS |
| 8067 | res = statvfs(path, &st); |
| 8068 | Py_END_ALLOW_THREADS |
| 8069 | if (res != 0) |
| 8070 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8071 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8072 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8073 | } |
| 8074 | #endif /* HAVE_STATVFS */ |
| 8075 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8076 | #ifdef MS_WINDOWS |
| 8077 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8078 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8079 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8080 | |
| 8081 | static PyObject * |
| 8082 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8083 | { |
| 8084 | BOOL retval; |
| 8085 | ULARGE_INTEGER _, total, free; |
| 8086 | LPCTSTR path; |
| 8087 | |
| 8088 | if (! PyArg_ParseTuple(args, "s", &path)) |
| 8089 | return NULL; |
| 8090 | |
| 8091 | Py_BEGIN_ALLOW_THREADS |
| 8092 | retval = GetDiskFreeSpaceEx(path, &_, &total, &free); |
| 8093 | Py_END_ALLOW_THREADS |
| 8094 | if (retval == 0) |
| 8095 | return PyErr_SetFromWindowsErr(0); |
| 8096 | |
| 8097 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8098 | } |
| 8099 | #endif |
| 8100 | |
| 8101 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8102 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8103 | * It maps strings representing configuration variable names to |
| 8104 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8105 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8106 | * rarely-used constants. There are three separate tables that use |
| 8107 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8108 | * |
| 8109 | * This code is always included, even if none of the interfaces that |
| 8110 | * need it are included. The #if hackery needed to avoid it would be |
| 8111 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8112 | */ |
| 8113 | struct constdef { |
| 8114 | char *name; |
| 8115 | long value; |
| 8116 | }; |
| 8117 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8118 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8119 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8120 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8121 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8122 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8123 | *valuep = PyLong_AS_LONG(arg); |
| 8124 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8125 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8126 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8127 | /* look up the value in the table using a binary search */ |
| 8128 | size_t lo = 0; |
| 8129 | size_t mid; |
| 8130 | size_t hi = tablesize; |
| 8131 | int cmp; |
| 8132 | const char *confname; |
| 8133 | if (!PyUnicode_Check(arg)) { |
| 8134 | PyErr_SetString(PyExc_TypeError, |
| 8135 | "configuration names must be strings or integers"); |
| 8136 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8137 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8138 | confname = _PyUnicode_AsString(arg); |
| 8139 | if (confname == NULL) |
| 8140 | return 0; |
| 8141 | while (lo < hi) { |
| 8142 | mid = (lo + hi) / 2; |
| 8143 | cmp = strcmp(confname, table[mid].name); |
| 8144 | if (cmp < 0) |
| 8145 | hi = mid; |
| 8146 | else if (cmp > 0) |
| 8147 | lo = mid + 1; |
| 8148 | else { |
| 8149 | *valuep = table[mid].value; |
| 8150 | return 1; |
| 8151 | } |
| 8152 | } |
| 8153 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8154 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8155 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8156 | } |
| 8157 | |
| 8158 | |
| 8159 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8160 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8161 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8162 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8163 | #endif |
| 8164 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8165 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8166 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8167 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8168 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8169 | #endif |
| 8170 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8171 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8172 | #endif |
| 8173 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8174 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8175 | #endif |
| 8176 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8177 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8178 | #endif |
| 8179 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8180 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8181 | #endif |
| 8182 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8183 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8184 | #endif |
| 8185 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8186 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8187 | #endif |
| 8188 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8189 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8190 | #endif |
| 8191 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8192 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8193 | #endif |
| 8194 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8195 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8196 | #endif |
| 8197 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8198 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8199 | #endif |
| 8200 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8201 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8202 | #endif |
| 8203 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8204 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8205 | #endif |
| 8206 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8207 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8208 | #endif |
| 8209 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8210 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8211 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8212 | #ifdef _PC_ACL_ENABLED |
| 8213 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8214 | #endif |
| 8215 | #ifdef _PC_MIN_HOLE_SIZE |
| 8216 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8217 | #endif |
| 8218 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8219 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8220 | #endif |
| 8221 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8222 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8223 | #endif |
| 8224 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8225 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8226 | #endif |
| 8227 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8228 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8229 | #endif |
| 8230 | #ifdef _PC_REC_XFER_ALIGN |
| 8231 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8232 | #endif |
| 8233 | #ifdef _PC_SYMLINK_MAX |
| 8234 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8235 | #endif |
| 8236 | #ifdef _PC_XATTR_ENABLED |
| 8237 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8238 | #endif |
| 8239 | #ifdef _PC_XATTR_EXISTS |
| 8240 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8241 | #endif |
| 8242 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8243 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8244 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8245 | }; |
| 8246 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8247 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8248 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8249 | { |
| 8250 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8251 | sizeof(posix_constants_pathconf) |
| 8252 | / sizeof(struct constdef)); |
| 8253 | } |
| 8254 | #endif |
| 8255 | |
| 8256 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8257 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8258 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8259 | 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] | 8260 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8261 | |
| 8262 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8263 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8264 | { |
| 8265 | PyObject *result = NULL; |
| 8266 | int name, fd; |
| 8267 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8268 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8269 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8270 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8271 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8272 | errno = 0; |
| 8273 | limit = fpathconf(fd, name); |
| 8274 | if (limit == -1 && errno != 0) |
| 8275 | posix_error(); |
| 8276 | else |
| 8277 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8278 | } |
| 8279 | return result; |
| 8280 | } |
| 8281 | #endif |
| 8282 | |
| 8283 | |
| 8284 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8285 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8286 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8287 | 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] | 8288 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8289 | |
| 8290 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8291 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8292 | { |
| 8293 | PyObject *result = NULL; |
| 8294 | int name; |
| 8295 | char *path; |
| 8296 | |
| 8297 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8298 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8299 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8301 | errno = 0; |
| 8302 | limit = pathconf(path, name); |
| 8303 | if (limit == -1 && errno != 0) { |
| 8304 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8305 | /* could be a path or name problem */ |
| 8306 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8307 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8308 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8309 | } |
| 8310 | else |
| 8311 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8312 | } |
| 8313 | return result; |
| 8314 | } |
| 8315 | #endif |
| 8316 | |
| 8317 | #ifdef HAVE_CONFSTR |
| 8318 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8319 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8320 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8321 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8322 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8323 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8324 | #endif |
| 8325 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8326 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8327 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8328 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8329 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8330 | #endif |
| 8331 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8332 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8333 | #endif |
| 8334 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8335 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8336 | #endif |
| 8337 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8338 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8339 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8340 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8341 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8342 | #endif |
| 8343 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8344 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8345 | #endif |
| 8346 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8347 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8348 | #endif |
| 8349 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8350 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8351 | #endif |
| 8352 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8353 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8354 | #endif |
| 8355 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8356 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8357 | #endif |
| 8358 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8359 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8360 | #endif |
| 8361 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8362 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8363 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8364 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8365 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8366 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8367 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8368 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8369 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8370 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8371 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8372 | #endif |
| 8373 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8374 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8375 | #endif |
| 8376 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8377 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8378 | #endif |
| 8379 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8380 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8381 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8382 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8383 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8384 | #endif |
| 8385 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8386 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8387 | #endif |
| 8388 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8389 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8390 | #endif |
| 8391 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8392 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8393 | #endif |
| 8394 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8395 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8396 | #endif |
| 8397 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8398 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8399 | #endif |
| 8400 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8401 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8402 | #endif |
| 8403 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8404 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8405 | #endif |
| 8406 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8407 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8408 | #endif |
| 8409 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8410 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8411 | #endif |
| 8412 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8413 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8414 | #endif |
| 8415 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8416 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8417 | #endif |
| 8418 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8419 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8420 | #endif |
| 8421 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8422 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8423 | #endif |
| 8424 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8425 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8426 | #endif |
| 8427 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8428 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8429 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8430 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8431 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8432 | #endif |
| 8433 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8434 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8435 | #endif |
| 8436 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8437 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8438 | #endif |
| 8439 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8440 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8441 | #endif |
| 8442 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8443 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8444 | #endif |
| 8445 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8446 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8447 | #endif |
| 8448 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8449 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8450 | #endif |
| 8451 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8452 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8453 | #endif |
| 8454 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8455 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8456 | #endif |
| 8457 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8458 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8459 | #endif |
| 8460 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8461 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8462 | #endif |
| 8463 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8464 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8465 | #endif |
| 8466 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8467 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8468 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8469 | }; |
| 8470 | |
| 8471 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8472 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8473 | { |
| 8474 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8475 | sizeof(posix_constants_confstr) |
| 8476 | / sizeof(struct constdef)); |
| 8477 | } |
| 8478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8479 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8480 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8481 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8482 | |
| 8483 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8484 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8485 | { |
| 8486 | PyObject *result = NULL; |
| 8487 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8488 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8489 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8490 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8491 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8492 | return NULL; |
| 8493 | |
| 8494 | errno = 0; |
| 8495 | len = confstr(name, buffer, sizeof(buffer)); |
| 8496 | if (len == 0) { |
| 8497 | if (errno) { |
| 8498 | posix_error(); |
| 8499 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8500 | } |
| 8501 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8502 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8503 | } |
| 8504 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8505 | |
| 8506 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8507 | char *buf = PyMem_Malloc(len); |
| 8508 | if (buf == NULL) |
| 8509 | return PyErr_NoMemory(); |
| 8510 | confstr(name, buf, len); |
| 8511 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8512 | PyMem_Free(buf); |
| 8513 | } |
| 8514 | else |
| 8515 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8516 | return result; |
| 8517 | } |
| 8518 | #endif |
| 8519 | |
| 8520 | |
| 8521 | #ifdef HAVE_SYSCONF |
| 8522 | static struct constdef posix_constants_sysconf[] = { |
| 8523 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8524 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8525 | #endif |
| 8526 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8527 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8528 | #endif |
| 8529 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8530 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8531 | #endif |
| 8532 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8533 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8534 | #endif |
| 8535 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8536 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8537 | #endif |
| 8538 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8539 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8540 | #endif |
| 8541 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8542 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8543 | #endif |
| 8544 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8545 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8546 | #endif |
| 8547 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8548 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8549 | #endif |
| 8550 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8551 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8552 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8553 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8554 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8555 | #endif |
| 8556 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8557 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8558 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8559 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8560 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8561 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8562 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8563 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8564 | #endif |
| 8565 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8566 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8567 | #endif |
| 8568 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8569 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8570 | #endif |
| 8571 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8572 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8573 | #endif |
| 8574 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8575 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8576 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8577 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8578 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8579 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8580 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8581 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8582 | #endif |
| 8583 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8584 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8585 | #endif |
| 8586 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8587 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8588 | #endif |
| 8589 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8590 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8591 | #endif |
| 8592 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8593 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8594 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8595 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8596 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8597 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8598 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8599 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8600 | #endif |
| 8601 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8602 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8603 | #endif |
| 8604 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8605 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8606 | #endif |
| 8607 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8608 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8609 | #endif |
| 8610 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8611 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8612 | #endif |
| 8613 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8614 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8615 | #endif |
| 8616 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8617 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8618 | #endif |
| 8619 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8620 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8621 | #endif |
| 8622 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8623 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8624 | #endif |
| 8625 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8626 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8627 | #endif |
| 8628 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8629 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8630 | #endif |
| 8631 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8632 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8633 | #endif |
| 8634 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8635 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8636 | #endif |
| 8637 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8638 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8639 | #endif |
| 8640 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8641 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8642 | #endif |
| 8643 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8644 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8645 | #endif |
| 8646 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8647 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8648 | #endif |
| 8649 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8650 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8651 | #endif |
| 8652 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8653 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8654 | #endif |
| 8655 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8656 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8657 | #endif |
| 8658 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8659 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8660 | #endif |
| 8661 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8662 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8663 | #endif |
| 8664 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8665 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8666 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8667 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8668 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8669 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8670 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8671 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8672 | #endif |
| 8673 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8674 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8675 | #endif |
| 8676 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8677 | {"SC_IOV_MAX", _SC_IOV_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_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8680 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
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_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8683 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8684 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8685 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8686 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8687 | #endif |
| 8688 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8689 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8690 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8691 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8692 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8693 | #endif |
| 8694 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8695 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8696 | #endif |
| 8697 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8698 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8699 | #endif |
| 8700 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8701 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8702 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8703 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8704 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8705 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8706 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8707 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8708 | #endif |
| 8709 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8710 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8711 | #endif |
| 8712 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8713 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8714 | #endif |
| 8715 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8716 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8717 | #endif |
| 8718 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8719 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8720 | #endif |
| 8721 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8722 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8723 | #endif |
| 8724 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8725 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8726 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8727 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8728 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8729 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8730 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8731 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8732 | #endif |
| 8733 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8734 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8735 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8736 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8737 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8738 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8739 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8740 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8741 | #endif |
| 8742 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8743 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8744 | #endif |
| 8745 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8746 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8747 | #endif |
| 8748 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8749 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8750 | #endif |
| 8751 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8752 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8753 | #endif |
| 8754 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8755 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8756 | #endif |
| 8757 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8758 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8759 | #endif |
| 8760 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8761 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8762 | #endif |
| 8763 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8764 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8765 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8766 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8767 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8768 | #endif |
| 8769 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8770 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8771 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8772 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8773 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8774 | #endif |
| 8775 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8776 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8777 | #endif |
| 8778 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8779 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8780 | #endif |
| 8781 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8782 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8783 | #endif |
| 8784 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8785 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8786 | #endif |
| 8787 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8788 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8789 | #endif |
| 8790 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8791 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8792 | #endif |
| 8793 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8794 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8795 | #endif |
| 8796 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8797 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8798 | #endif |
| 8799 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8800 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8801 | #endif |
| 8802 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8803 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8804 | #endif |
| 8805 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8806 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8807 | #endif |
| 8808 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8809 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8810 | #endif |
| 8811 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8812 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8813 | #endif |
| 8814 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8815 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8816 | #endif |
| 8817 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8818 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8819 | #endif |
| 8820 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8821 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8822 | #endif |
| 8823 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8824 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8825 | #endif |
| 8826 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8827 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8828 | #endif |
| 8829 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8830 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8831 | #endif |
| 8832 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8833 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8834 | #endif |
| 8835 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8836 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8837 | #endif |
| 8838 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8839 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8840 | #endif |
| 8841 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8842 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8843 | #endif |
| 8844 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8845 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8846 | #endif |
| 8847 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8848 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8849 | #endif |
| 8850 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8851 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8852 | #endif |
| 8853 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8854 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8855 | #endif |
| 8856 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8857 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8858 | #endif |
| 8859 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8860 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8861 | #endif |
| 8862 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8863 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8864 | #endif |
| 8865 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8866 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8867 | #endif |
| 8868 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8869 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8870 | #endif |
| 8871 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8872 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8873 | #endif |
| 8874 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8875 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8876 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8877 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8878 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8879 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8880 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8881 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8882 | #endif |
| 8883 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8884 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8885 | #endif |
| 8886 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8887 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8888 | #endif |
| 8889 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8890 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8891 | #endif |
| 8892 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8893 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8894 | #endif |
| 8895 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8896 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8897 | #endif |
| 8898 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8899 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8900 | #endif |
| 8901 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8902 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8903 | #endif |
| 8904 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8905 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8906 | #endif |
| 8907 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8908 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8909 | #endif |
| 8910 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8911 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8912 | #endif |
| 8913 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8914 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8915 | #endif |
| 8916 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8917 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8918 | #endif |
| 8919 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8920 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8921 | #endif |
| 8922 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8923 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8924 | #endif |
| 8925 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8926 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8927 | #endif |
| 8928 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8929 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8930 | #endif |
| 8931 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8932 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8933 | #endif |
| 8934 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8935 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8936 | #endif |
| 8937 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8938 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8939 | #endif |
| 8940 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8941 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8942 | #endif |
| 8943 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8944 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8945 | #endif |
| 8946 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8947 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8948 | #endif |
| 8949 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8950 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8951 | #endif |
| 8952 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8953 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8954 | #endif |
| 8955 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8956 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8957 | #endif |
| 8958 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8959 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8960 | #endif |
| 8961 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8962 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8963 | #endif |
| 8964 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8965 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8966 | #endif |
| 8967 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8968 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8969 | #endif |
| 8970 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8971 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8972 | #endif |
| 8973 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8974 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8975 | #endif |
| 8976 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8977 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8978 | #endif |
| 8979 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8980 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8981 | #endif |
| 8982 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8983 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8984 | #endif |
| 8985 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8986 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8987 | #endif |
| 8988 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8989 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8990 | #endif |
| 8991 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8992 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8993 | #endif |
| 8994 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8995 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8996 | #endif |
| 8997 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8998 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8999 | #endif |
| 9000 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9001 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9002 | #endif |
| 9003 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9004 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9005 | #endif |
| 9006 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9007 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9008 | #endif |
| 9009 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9010 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9011 | #endif |
| 9012 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9013 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9014 | #endif |
| 9015 | }; |
| 9016 | |
| 9017 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9018 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9019 | { |
| 9020 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9021 | sizeof(posix_constants_sysconf) |
| 9022 | / sizeof(struct constdef)); |
| 9023 | } |
| 9024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9025 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9026 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9027 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9028 | |
| 9029 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9030 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9031 | { |
| 9032 | PyObject *result = NULL; |
| 9033 | int name; |
| 9034 | |
| 9035 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9036 | int value; |
| 9037 | |
| 9038 | errno = 0; |
| 9039 | value = sysconf(name); |
| 9040 | if (value == -1 && errno != 0) |
| 9041 | posix_error(); |
| 9042 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9043 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9044 | } |
| 9045 | return result; |
| 9046 | } |
| 9047 | #endif |
| 9048 | |
| 9049 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9050 | /* This code is used to ensure that the tables of configuration value names |
| 9051 | * are in sorted order as required by conv_confname(), and also to build the |
| 9052 | * the exported dictionaries that are used to publish information about the |
| 9053 | * names available on the host platform. |
| 9054 | * |
| 9055 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9056 | * when used, even for platforms we're not able to test on. It also makes |
| 9057 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9058 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9059 | |
| 9060 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9061 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9062 | { |
| 9063 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9064 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9065 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9066 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9067 | |
| 9068 | return strcmp(c1->name, c2->name); |
| 9069 | } |
| 9070 | |
| 9071 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9072 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9073 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9074 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9075 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9076 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9077 | |
| 9078 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9079 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9080 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9081 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9082 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9083 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9084 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9085 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9086 | Py_XDECREF(o); |
| 9087 | Py_DECREF(d); |
| 9088 | return -1; |
| 9089 | } |
| 9090 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9091 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9092 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9093 | } |
| 9094 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9095 | /* Return -1 on failure, 0 on success. */ |
| 9096 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9097 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9098 | { |
| 9099 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9100 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9101 | sizeof(posix_constants_pathconf) |
| 9102 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9103 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9104 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9105 | #endif |
| 9106 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9107 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9108 | sizeof(posix_constants_confstr) |
| 9109 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9110 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9111 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9112 | #endif |
| 9113 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9114 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9115 | sizeof(posix_constants_sysconf) |
| 9116 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9117 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9118 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9119 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9120 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9121 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9122 | |
| 9123 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9124 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9125 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9126 | 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] | 9127 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9128 | |
| 9129 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9130 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9131 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9132 | abort(); |
| 9133 | /*NOTREACHED*/ |
| 9134 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9135 | return NULL; |
| 9136 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9137 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9138 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9139 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9140 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9141 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9142 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9143 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9144 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9145 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9146 | application (if any) its extension is associated.\n\ |
| 9147 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9148 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9149 | \n\ |
| 9150 | startfile returns as soon as the associated application is launched.\n\ |
| 9151 | There is no option to wait for the application to close, and no way\n\ |
| 9152 | to retrieve the application's exit status.\n\ |
| 9153 | \n\ |
| 9154 | The filepath is relative to the current directory. If you want to use\n\ |
| 9155 | 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] | 9156 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9157 | |
| 9158 | static PyObject * |
| 9159 | win32_startfile(PyObject *self, PyObject *args) |
| 9160 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | PyObject *ofilepath; |
| 9162 | char *filepath; |
| 9163 | char *operation = NULL; |
| 9164 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9165 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9166 | PyObject *unipath, *woperation = NULL; |
| 9167 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9168 | &unipath, &operation)) { |
| 9169 | PyErr_Clear(); |
| 9170 | goto normal; |
| 9171 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9172 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9173 | if (operation) { |
| 9174 | woperation = PyUnicode_DecodeASCII(operation, |
| 9175 | strlen(operation), NULL); |
| 9176 | if (!woperation) { |
| 9177 | PyErr_Clear(); |
| 9178 | operation = NULL; |
| 9179 | goto normal; |
| 9180 | } |
| 9181 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9182 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9183 | Py_BEGIN_ALLOW_THREADS |
| 9184 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 9185 | PyUnicode_AS_UNICODE(unipath), |
| 9186 | NULL, NULL, SW_SHOWNORMAL); |
| 9187 | Py_END_ALLOW_THREADS |
| 9188 | |
| 9189 | Py_XDECREF(woperation); |
| 9190 | if (rc <= (HINSTANCE)32) { |
| 9191 | PyObject *errval = win32_error_unicode("startfile", |
| 9192 | PyUnicode_AS_UNICODE(unipath)); |
| 9193 | return errval; |
| 9194 | } |
| 9195 | Py_INCREF(Py_None); |
| 9196 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9197 | |
| 9198 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9199 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9200 | PyUnicode_FSConverter, &ofilepath, |
| 9201 | &operation)) |
| 9202 | return NULL; |
| 9203 | filepath = PyBytes_AsString(ofilepath); |
| 9204 | Py_BEGIN_ALLOW_THREADS |
| 9205 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9206 | NULL, NULL, SW_SHOWNORMAL); |
| 9207 | Py_END_ALLOW_THREADS |
| 9208 | if (rc <= (HINSTANCE)32) { |
| 9209 | PyObject *errval = win32_error("startfile", filepath); |
| 9210 | Py_DECREF(ofilepath); |
| 9211 | return errval; |
| 9212 | } |
| 9213 | Py_DECREF(ofilepath); |
| 9214 | Py_INCREF(Py_None); |
| 9215 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9216 | } |
| 9217 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9218 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9219 | #ifdef HAVE_GETLOADAVG |
| 9220 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9221 | "getloadavg() -> (float, float, float)\n\n\ |
| 9222 | Return the number of processes in the system run queue averaged over\n\ |
| 9223 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9224 | was unobtainable"); |
| 9225 | |
| 9226 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9227 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9228 | { |
| 9229 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9230 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9231 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9232 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9233 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9234 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9235 | } |
| 9236 | #endif |
| 9237 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9238 | #ifdef MS_WINDOWS |
| 9239 | |
| 9240 | PyDoc_STRVAR(win32_urandom__doc__, |
| 9241 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9242 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9243 | |
| 9244 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 9245 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 9246 | DWORD dwFlags ); |
| 9247 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 9248 | BYTE *pbBuffer ); |
| 9249 | |
| 9250 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 9251 | /* This handle is never explicitly released. Instead, the operating |
| 9252 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9253 | static HCRYPTPROV hCryptProv = 0; |
| 9254 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 9255 | static PyObject* |
| 9256 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9257 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9258 | int howMany; |
| 9259 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9260 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9261 | /* Read arguments */ |
| 9262 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9263 | return NULL; |
| 9264 | if (howMany < 0) |
| 9265 | return PyErr_Format(PyExc_ValueError, |
| 9266 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9267 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9268 | if (hCryptProv == 0) { |
| 9269 | HINSTANCE hAdvAPI32 = NULL; |
| 9270 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9271 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | /* Obtain handle to the DLL containing CryptoAPI |
| 9273 | This should not fail */ |
| 9274 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 9275 | if(hAdvAPI32 == NULL) |
| 9276 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9277 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | /* Obtain pointers to the CryptoAPI functions |
| 9279 | This will fail on some early versions of Win95 */ |
| 9280 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 9281 | hAdvAPI32, |
| 9282 | "CryptAcquireContextA"); |
| 9283 | if (pCryptAcquireContext == NULL) |
| 9284 | return PyErr_Format(PyExc_NotImplementedError, |
| 9285 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9286 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9287 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 9288 | hAdvAPI32, "CryptGenRandom"); |
| 9289 | if (pCryptGenRandom == NULL) |
| 9290 | return PyErr_Format(PyExc_NotImplementedError, |
| 9291 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9292 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9293 | /* Acquire context */ |
| 9294 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 9295 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 9296 | return win32_error("CryptAcquireContext", NULL); |
| 9297 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9298 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9299 | /* Allocate bytes */ |
| 9300 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9301 | if (result != NULL) { |
| 9302 | /* Get random data */ |
| 9303 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 9304 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 9305 | PyBytes_AS_STRING(result))) { |
| 9306 | Py_DECREF(result); |
| 9307 | return win32_error("CryptGenRandom", NULL); |
| 9308 | } |
| 9309 | } |
| 9310 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9311 | } |
| 9312 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9313 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9314 | PyDoc_STRVAR(device_encoding__doc__, |
| 9315 | "device_encoding(fd) -> str\n\n\ |
| 9316 | Return a string describing the encoding of the device\n\ |
| 9317 | if the output is a terminal; else return None."); |
| 9318 | |
| 9319 | static PyObject * |
| 9320 | device_encoding(PyObject *self, PyObject *args) |
| 9321 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9322 | int fd; |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9323 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 9324 | UINT cp; |
| 9325 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9326 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9327 | return NULL; |
| 9328 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 9329 | Py_INCREF(Py_None); |
| 9330 | return Py_None; |
| 9331 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9332 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9333 | if (fd == 0) |
| 9334 | cp = GetConsoleCP(); |
| 9335 | else if (fd == 1 || fd == 2) |
| 9336 | cp = GetConsoleOutputCP(); |
| 9337 | else |
| 9338 | cp = 0; |
| 9339 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 9340 | has no console */ |
| 9341 | if (cp != 0) |
| 9342 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9343 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9344 | { |
| 9345 | char *codeset = nl_langinfo(CODESET); |
| 9346 | if (codeset != NULL && codeset[0] != 0) |
| 9347 | return PyUnicode_FromString(codeset); |
| 9348 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9349 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9350 | Py_INCREF(Py_None); |
| 9351 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9352 | } |
| 9353 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9354 | #ifdef __VMS |
| 9355 | /* Use openssl random routine */ |
| 9356 | #include <openssl/rand.h> |
| 9357 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9358 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9359 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9360 | |
| 9361 | static PyObject* |
| 9362 | vms_urandom(PyObject *self, PyObject *args) |
| 9363 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9364 | int howMany; |
| 9365 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | /* Read arguments */ |
| 9368 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9369 | return NULL; |
| 9370 | if (howMany < 0) |
| 9371 | return PyErr_Format(PyExc_ValueError, |
| 9372 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9373 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | /* Allocate bytes */ |
| 9375 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9376 | if (result != NULL) { |
| 9377 | /* Get random data */ |
| 9378 | if (RAND_pseudo_bytes((unsigned char*) |
| 9379 | PyBytes_AS_STRING(result), |
| 9380 | howMany) < 0) { |
| 9381 | Py_DECREF(result); |
| 9382 | return PyErr_Format(PyExc_ValueError, |
| 9383 | "RAND_pseudo_bytes"); |
| 9384 | } |
| 9385 | } |
| 9386 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9387 | } |
| 9388 | #endif |
| 9389 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9390 | #ifdef HAVE_SETRESUID |
| 9391 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9392 | "setresuid(ruid, euid, suid)\n\n\ |
| 9393 | Set the current process's real, effective, and saved user ids."); |
| 9394 | |
| 9395 | static PyObject* |
| 9396 | posix_setresuid (PyObject *self, PyObject *args) |
| 9397 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | /* We assume uid_t is no larger than a long. */ |
| 9399 | long ruid, euid, suid; |
| 9400 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9401 | return NULL; |
| 9402 | if (setresuid(ruid, euid, suid) < 0) |
| 9403 | return posix_error(); |
| 9404 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9405 | } |
| 9406 | #endif |
| 9407 | |
| 9408 | #ifdef HAVE_SETRESGID |
| 9409 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9410 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9411 | Set the current process's real, effective, and saved group ids."); |
| 9412 | |
| 9413 | static PyObject* |
| 9414 | posix_setresgid (PyObject *self, PyObject *args) |
| 9415 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9416 | /* We assume uid_t is no larger than a long. */ |
| 9417 | long rgid, egid, sgid; |
| 9418 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9419 | return NULL; |
| 9420 | if (setresgid(rgid, egid, sgid) < 0) |
| 9421 | return posix_error(); |
| 9422 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9423 | } |
| 9424 | #endif |
| 9425 | |
| 9426 | #ifdef HAVE_GETRESUID |
| 9427 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9428 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9429 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9430 | |
| 9431 | static PyObject* |
| 9432 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9433 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9434 | uid_t ruid, euid, suid; |
| 9435 | long l_ruid, l_euid, l_suid; |
| 9436 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9437 | return posix_error(); |
| 9438 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9439 | l_ruid = ruid; |
| 9440 | l_euid = euid; |
| 9441 | l_suid = suid; |
| 9442 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9443 | } |
| 9444 | #endif |
| 9445 | |
| 9446 | #ifdef HAVE_GETRESGID |
| 9447 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9448 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9449 | 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] | 9450 | |
| 9451 | static PyObject* |
| 9452 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9453 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9454 | uid_t rgid, egid, sgid; |
| 9455 | long l_rgid, l_egid, l_sgid; |
| 9456 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9457 | return posix_error(); |
| 9458 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9459 | l_rgid = rgid; |
| 9460 | l_egid = egid; |
| 9461 | l_sgid = sgid; |
| 9462 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9463 | } |
| 9464 | #endif |
| 9465 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9466 | /* Posix *at family of functions: |
| 9467 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9468 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9469 | unlinkat, utimensat, mkfifoat */ |
| 9470 | |
| 9471 | #ifdef HAVE_FACCESSAT |
| 9472 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9473 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9474 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9475 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9476 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9477 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9478 | is interpreted relative to the current working directory."); |
| 9479 | |
| 9480 | static PyObject * |
| 9481 | posix_faccessat(PyObject *self, PyObject *args) |
| 9482 | { |
| 9483 | PyObject *opath; |
| 9484 | char *path; |
| 9485 | int mode; |
| 9486 | int res; |
| 9487 | int dirfd, flags = 0; |
| 9488 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9489 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9490 | return NULL; |
| 9491 | path = PyBytes_AsString(opath); |
| 9492 | Py_BEGIN_ALLOW_THREADS |
| 9493 | res = faccessat(dirfd, path, mode, flags); |
| 9494 | Py_END_ALLOW_THREADS |
| 9495 | Py_DECREF(opath); |
| 9496 | return PyBool_FromLong(res == 0); |
| 9497 | } |
| 9498 | #endif |
| 9499 | |
| 9500 | #ifdef HAVE_FCHMODAT |
| 9501 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9502 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9503 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9504 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9505 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9506 | is interpreted relative to the current working directory."); |
| 9507 | |
| 9508 | static PyObject * |
| 9509 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9510 | { |
| 9511 | int dirfd, mode, res; |
| 9512 | int flags = 0; |
| 9513 | PyObject *opath; |
| 9514 | char *path; |
| 9515 | |
| 9516 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9517 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9518 | return NULL; |
| 9519 | |
| 9520 | path = PyBytes_AsString(opath); |
| 9521 | |
| 9522 | Py_BEGIN_ALLOW_THREADS |
| 9523 | res = fchmodat(dirfd, path, mode, flags); |
| 9524 | Py_END_ALLOW_THREADS |
| 9525 | Py_DECREF(opath); |
| 9526 | if (res < 0) |
| 9527 | return posix_error(); |
| 9528 | Py_RETURN_NONE; |
| 9529 | } |
| 9530 | #endif /* HAVE_FCHMODAT */ |
| 9531 | |
| 9532 | #ifdef HAVE_FCHOWNAT |
| 9533 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9534 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9535 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9536 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9537 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9538 | is interpreted relative to the current working directory."); |
| 9539 | |
| 9540 | static PyObject * |
| 9541 | posix_fchownat(PyObject *self, PyObject *args) |
| 9542 | { |
| 9543 | PyObject *opath; |
| 9544 | int dirfd, res; |
| 9545 | long uid, gid; |
| 9546 | int flags = 0; |
| 9547 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9548 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9549 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9550 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9551 | return NULL; |
| 9552 | |
| 9553 | path = PyBytes_AsString(opath); |
| 9554 | |
| 9555 | Py_BEGIN_ALLOW_THREADS |
| 9556 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9557 | Py_END_ALLOW_THREADS |
| 9558 | Py_DECREF(opath); |
| 9559 | if (res < 0) |
| 9560 | return posix_error(); |
| 9561 | Py_RETURN_NONE; |
| 9562 | } |
| 9563 | #endif /* HAVE_FCHOWNAT */ |
| 9564 | |
| 9565 | #ifdef HAVE_FSTATAT |
| 9566 | PyDoc_STRVAR(posix_fstatat__doc__, |
| 9567 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
| 9568 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9569 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9570 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9571 | is interpreted relative to the current working directory."); |
| 9572 | |
| 9573 | static PyObject * |
| 9574 | posix_fstatat(PyObject *self, PyObject *args) |
| 9575 | { |
| 9576 | PyObject *opath; |
| 9577 | char *path; |
| 9578 | STRUCT_STAT st; |
| 9579 | int dirfd, res, flags = 0; |
| 9580 | |
| 9581 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9582 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9583 | return NULL; |
| 9584 | path = PyBytes_AsString(opath); |
| 9585 | |
| 9586 | Py_BEGIN_ALLOW_THREADS |
| 9587 | res = fstatat(dirfd, path, &st, flags); |
| 9588 | Py_END_ALLOW_THREADS |
| 9589 | Py_DECREF(opath); |
| 9590 | if (res != 0) |
| 9591 | return posix_error(); |
| 9592 | |
| 9593 | return _pystat_fromstructstat(&st); |
| 9594 | } |
| 9595 | #endif |
| 9596 | |
| 9597 | #ifdef HAVE_FUTIMESAT |
| 9598 | PyDoc_STRVAR(posix_futimesat__doc__, |
| 9599 | "futimesat(dirfd, path, (atime, mtime))\n\ |
| 9600 | futimesat(dirfd, path, None)\n\n\ |
| 9601 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9602 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9603 | is interpreted relative to the current working directory."); |
| 9604 | |
| 9605 | static PyObject * |
| 9606 | posix_futimesat(PyObject *self, PyObject *args) |
| 9607 | { |
| 9608 | PyObject *opath; |
| 9609 | char *path; |
| 9610 | int res, dirfd; |
| 9611 | PyObject* arg; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9612 | time_t atime, mtime; |
| 9613 | long ausec, musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9614 | |
| 9615 | if (!PyArg_ParseTuple(args, "iO&O:futimesat", |
| 9616 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9617 | return NULL; |
| 9618 | path = PyBytes_AsString(opath); |
| 9619 | if (arg == Py_None) { |
| 9620 | /* optional time values not given */ |
| 9621 | Py_BEGIN_ALLOW_THREADS |
| 9622 | res = futimesat(dirfd, path, NULL); |
| 9623 | Py_END_ALLOW_THREADS |
| 9624 | } |
| 9625 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9626 | PyErr_SetString(PyExc_TypeError, |
| 9627 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9628 | Py_DECREF(opath); |
| 9629 | return NULL; |
| 9630 | } |
| 9631 | else { |
| 9632 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9633 | &atime, &ausec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9634 | Py_DECREF(opath); |
| 9635 | return NULL; |
| 9636 | } |
| 9637 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9638 | &mtime, &musec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9639 | Py_DECREF(opath); |
| 9640 | return NULL; |
| 9641 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9642 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9643 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9644 | { |
| 9645 | #ifdef HAVE_UTIMENSAT |
| 9646 | struct timespec buf[2]; |
| 9647 | buf[0].tv_sec = atime; |
| 9648 | buf[0].tv_nsec = ausec; |
| 9649 | buf[1].tv_sec = mtime; |
| 9650 | buf[1].tv_nsec = musec; |
| 9651 | res = utimensat(dirfd, path, buf, 0); |
| 9652 | #else |
| 9653 | struct timeval buf[2]; |
| 9654 | buf[0].tv_sec = atime; |
| 9655 | buf[0].tv_usec = ausec; |
| 9656 | buf[1].tv_sec = mtime; |
| 9657 | buf[1].tv_usec = musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9658 | res = futimesat(dirfd, path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9659 | #endif |
| 9660 | } |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9661 | Py_END_ALLOW_THREADS |
| 9662 | } |
| 9663 | Py_DECREF(opath); |
| 9664 | if (res < 0) { |
| 9665 | return posix_error(); |
| 9666 | } |
| 9667 | Py_RETURN_NONE; |
| 9668 | } |
| 9669 | #endif |
| 9670 | |
| 9671 | #ifdef HAVE_LINKAT |
| 9672 | PyDoc_STRVAR(posix_linkat__doc__, |
| 9673 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 9674 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 9675 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 9676 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 9677 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 9678 | srcpath is interpreted relative to the current working directory. This\n\ |
| 9679 | also applies for dstpath."); |
| 9680 | |
| 9681 | static PyObject * |
| 9682 | posix_linkat(PyObject *self, PyObject *args) |
| 9683 | { |
| 9684 | PyObject *osrc, *odst; |
| 9685 | char *src, *dst; |
| 9686 | int res, srcfd, dstfd; |
| 9687 | int flags = 0; |
| 9688 | |
| 9689 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 9690 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 9691 | return NULL; |
| 9692 | src = PyBytes_AsString(osrc); |
| 9693 | dst = PyBytes_AsString(odst); |
| 9694 | Py_BEGIN_ALLOW_THREADS |
| 9695 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 9696 | Py_END_ALLOW_THREADS |
| 9697 | Py_DECREF(osrc); |
| 9698 | Py_DECREF(odst); |
| 9699 | if (res < 0) |
| 9700 | return posix_error(); |
| 9701 | Py_RETURN_NONE; |
| 9702 | } |
| 9703 | #endif /* HAVE_LINKAT */ |
| 9704 | |
| 9705 | #ifdef HAVE_MKDIRAT |
| 9706 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 9707 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 9708 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9709 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9710 | is interpreted relative to the current working directory."); |
| 9711 | |
| 9712 | static PyObject * |
| 9713 | posix_mkdirat(PyObject *self, PyObject *args) |
| 9714 | { |
| 9715 | int res, dirfd; |
| 9716 | PyObject *opath; |
| 9717 | char *path; |
| 9718 | int mode = 0777; |
| 9719 | |
| 9720 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 9721 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9722 | return NULL; |
| 9723 | path = PyBytes_AsString(opath); |
| 9724 | Py_BEGIN_ALLOW_THREADS |
| 9725 | res = mkdirat(dirfd, path, mode); |
| 9726 | Py_END_ALLOW_THREADS |
| 9727 | Py_DECREF(opath); |
| 9728 | if (res < 0) |
| 9729 | return posix_error(); |
| 9730 | Py_RETURN_NONE; |
| 9731 | } |
| 9732 | #endif |
| 9733 | |
| 9734 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9735 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 9736 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 9737 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9738 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9739 | is interpreted relative to the current working directory."); |
| 9740 | |
| 9741 | static PyObject * |
| 9742 | posix_mknodat(PyObject *self, PyObject *args) |
| 9743 | { |
| 9744 | PyObject *opath; |
| 9745 | char *filename; |
| 9746 | int mode = 0600; |
| 9747 | int device = 0; |
| 9748 | int res, dirfd; |
| 9749 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 9750 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 9751 | return NULL; |
| 9752 | filename = PyBytes_AS_STRING(opath); |
| 9753 | Py_BEGIN_ALLOW_THREADS |
| 9754 | res = mknodat(dirfd, filename, mode, device); |
| 9755 | Py_END_ALLOW_THREADS |
| 9756 | Py_DECREF(opath); |
| 9757 | if (res < 0) |
| 9758 | return posix_error(); |
| 9759 | Py_RETURN_NONE; |
| 9760 | } |
| 9761 | #endif |
| 9762 | |
| 9763 | #ifdef HAVE_OPENAT |
| 9764 | PyDoc_STRVAR(posix_openat__doc__, |
| 9765 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 9766 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9767 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9768 | is interpreted relative to the current working directory."); |
| 9769 | |
| 9770 | static PyObject * |
| 9771 | posix_openat(PyObject *self, PyObject *args) |
| 9772 | { |
| 9773 | PyObject *ofile; |
| 9774 | char *file; |
| 9775 | int flag, dirfd, fd; |
| 9776 | int mode = 0777; |
| 9777 | |
| 9778 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9779 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9780 | &flag, &mode)) |
| 9781 | return NULL; |
| 9782 | file = PyBytes_AsString(ofile); |
| 9783 | Py_BEGIN_ALLOW_THREADS |
| 9784 | fd = openat(dirfd, file, flag, mode); |
| 9785 | Py_END_ALLOW_THREADS |
| 9786 | Py_DECREF(ofile); |
| 9787 | if (fd < 0) |
| 9788 | return posix_error(); |
| 9789 | return PyLong_FromLong((long)fd); |
| 9790 | } |
| 9791 | #endif |
| 9792 | |
| 9793 | #ifdef HAVE_READLINKAT |
| 9794 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9795 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9796 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9797 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9798 | is interpreted relative to the current working directory."); |
| 9799 | |
| 9800 | static PyObject * |
| 9801 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9802 | { |
| 9803 | PyObject *v, *opath; |
| 9804 | char buf[MAXPATHLEN]; |
| 9805 | char *path; |
| 9806 | int n, dirfd; |
| 9807 | int arg_is_unicode = 0; |
| 9808 | |
| 9809 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9810 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9811 | return NULL; |
| 9812 | path = PyBytes_AsString(opath); |
| 9813 | v = PySequence_GetItem(args, 1); |
| 9814 | if (v == NULL) { |
| 9815 | Py_DECREF(opath); |
| 9816 | return NULL; |
| 9817 | } |
| 9818 | |
| 9819 | if (PyUnicode_Check(v)) { |
| 9820 | arg_is_unicode = 1; |
| 9821 | } |
| 9822 | Py_DECREF(v); |
| 9823 | |
| 9824 | Py_BEGIN_ALLOW_THREADS |
| 9825 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9826 | Py_END_ALLOW_THREADS |
| 9827 | Py_DECREF(opath); |
| 9828 | if (n < 0) |
| 9829 | return posix_error(); |
| 9830 | |
| 9831 | if (arg_is_unicode) |
| 9832 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9833 | else |
| 9834 | return PyBytes_FromStringAndSize(buf, n); |
| 9835 | } |
| 9836 | #endif /* HAVE_READLINKAT */ |
| 9837 | |
| 9838 | #ifdef HAVE_RENAMEAT |
| 9839 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9840 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9841 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9842 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9843 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9844 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9845 | also applies for newpath."); |
| 9846 | |
| 9847 | static PyObject * |
| 9848 | posix_renameat(PyObject *self, PyObject *args) |
| 9849 | { |
| 9850 | int res; |
| 9851 | PyObject *opathold, *opathnew; |
| 9852 | char *opath, *npath; |
| 9853 | int oldfd, newfd; |
| 9854 | |
| 9855 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9856 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9857 | return NULL; |
| 9858 | opath = PyBytes_AsString(opathold); |
| 9859 | npath = PyBytes_AsString(opathnew); |
| 9860 | Py_BEGIN_ALLOW_THREADS |
| 9861 | res = renameat(oldfd, opath, newfd, npath); |
| 9862 | Py_END_ALLOW_THREADS |
| 9863 | Py_DECREF(opathold); |
| 9864 | Py_DECREF(opathnew); |
| 9865 | if (res < 0) |
| 9866 | return posix_error(); |
| 9867 | Py_RETURN_NONE; |
| 9868 | } |
| 9869 | #endif |
| 9870 | |
| 9871 | #if HAVE_SYMLINKAT |
| 9872 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9873 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9874 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9875 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9876 | is interpreted relative to the current working directory."); |
| 9877 | |
| 9878 | static PyObject * |
| 9879 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9880 | { |
| 9881 | int res, dstfd; |
| 9882 | PyObject *osrc, *odst; |
| 9883 | char *src, *dst; |
| 9884 | |
| 9885 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9886 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9887 | return NULL; |
| 9888 | src = PyBytes_AsString(osrc); |
| 9889 | dst = PyBytes_AsString(odst); |
| 9890 | Py_BEGIN_ALLOW_THREADS |
| 9891 | res = symlinkat(src, dstfd, dst); |
| 9892 | Py_END_ALLOW_THREADS |
| 9893 | Py_DECREF(osrc); |
| 9894 | Py_DECREF(odst); |
| 9895 | if (res < 0) |
| 9896 | return posix_error(); |
| 9897 | Py_RETURN_NONE; |
| 9898 | } |
| 9899 | #endif /* HAVE_SYMLINKAT */ |
| 9900 | |
| 9901 | #ifdef HAVE_UNLINKAT |
| 9902 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9903 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9904 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9905 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9906 | specified, unlinkat() behaves like rmdir().\n\ |
| 9907 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9908 | is interpreted relative to the current working directory."); |
| 9909 | |
| 9910 | static PyObject * |
| 9911 | posix_unlinkat(PyObject *self, PyObject *args) |
| 9912 | { |
| 9913 | int dirfd, res, flags = 0; |
| 9914 | PyObject *opath; |
| 9915 | char *path; |
| 9916 | |
| 9917 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 9918 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9919 | return NULL; |
| 9920 | path = PyBytes_AsString(opath); |
| 9921 | Py_BEGIN_ALLOW_THREADS |
| 9922 | res = unlinkat(dirfd, path, flags); |
| 9923 | Py_END_ALLOW_THREADS |
| 9924 | Py_DECREF(opath); |
| 9925 | if (res < 0) |
| 9926 | return posix_error(); |
| 9927 | Py_RETURN_NONE; |
| 9928 | } |
| 9929 | #endif |
| 9930 | |
| 9931 | #ifdef HAVE_UTIMENSAT |
| 9932 | PyDoc_STRVAR(posix_utimensat__doc__, |
| 9933 | "utimensat(dirfd, path, (atime_sec, atime_nsec),\n\ |
| 9934 | (mtime_sec, mtime_nsec), flags)\n\ |
| 9935 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 9936 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 9937 | relative, it is taken as relative to dirfd.\n\ |
| 9938 | The second form sets atime and mtime to the current time.\n\ |
| 9939 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9940 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9941 | is interpreted relative to the current working directory.\n\ |
| 9942 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 9943 | current time.\n\ |
| 9944 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 9945 | |
| 9946 | static PyObject * |
| 9947 | posix_utimensat(PyObject *self, PyObject *args) |
| 9948 | { |
| 9949 | PyObject *opath; |
| 9950 | char *path; |
| 9951 | int res, dirfd, flags = 0; |
| 9952 | PyObject *atime, *mtime; |
| 9953 | |
| 9954 | struct timespec buf[2]; |
| 9955 | |
| 9956 | if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat", |
| 9957 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 9958 | return NULL; |
| 9959 | path = PyBytes_AsString(opath); |
| 9960 | if (atime == Py_None && mtime == Py_None) { |
| 9961 | /* optional time values not given */ |
| 9962 | Py_BEGIN_ALLOW_THREADS |
| 9963 | res = utimensat(dirfd, path, NULL, flags); |
| 9964 | Py_END_ALLOW_THREADS |
| 9965 | } |
| 9966 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 9967 | PyErr_SetString(PyExc_TypeError, |
| 9968 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 9969 | Py_DECREF(opath); |
| 9970 | return NULL; |
| 9971 | } |
| 9972 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 9973 | PyErr_SetString(PyExc_TypeError, |
| 9974 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 9975 | Py_DECREF(opath); |
| 9976 | return NULL; |
| 9977 | } |
| 9978 | else { |
| 9979 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 9980 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 9981 | Py_DECREF(opath); |
| 9982 | return NULL; |
| 9983 | } |
| 9984 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 9985 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 9986 | Py_DECREF(opath); |
| 9987 | return NULL; |
| 9988 | } |
| 9989 | Py_BEGIN_ALLOW_THREADS |
| 9990 | res = utimensat(dirfd, path, buf, flags); |
| 9991 | Py_END_ALLOW_THREADS |
| 9992 | } |
| 9993 | Py_DECREF(opath); |
| 9994 | if (res < 0) { |
| 9995 | return posix_error(); |
| 9996 | } |
| 9997 | Py_RETURN_NONE; |
| 9998 | } |
| 9999 | #endif |
| 10000 | |
| 10001 | #ifdef HAVE_MKFIFOAT |
| 10002 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 10003 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 10004 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10005 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10006 | is interpreted relative to the current working directory."); |
| 10007 | |
| 10008 | static PyObject * |
| 10009 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 10010 | { |
| 10011 | PyObject *opath; |
| 10012 | char *filename; |
| 10013 | int mode = 0666; |
| 10014 | int res, dirfd; |
| 10015 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 10016 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10017 | return NULL; |
| 10018 | filename = PyBytes_AS_STRING(opath); |
| 10019 | Py_BEGIN_ALLOW_THREADS |
| 10020 | res = mkfifoat(dirfd, filename, mode); |
| 10021 | Py_END_ALLOW_THREADS |
| 10022 | Py_DECREF(opath); |
| 10023 | if (res < 0) |
| 10024 | return posix_error(); |
| 10025 | Py_RETURN_NONE; |
| 10026 | } |
| 10027 | #endif |
| 10028 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10029 | #ifdef HAVE_ATTR_XATTR_H |
| 10030 | |
| 10031 | static int |
| 10032 | try_getxattr(const char *path, const char *name, |
| 10033 | ssize_t (*get)(const char *, const char *, void *, size_t), |
| 10034 | Py_ssize_t buf_size, PyObject **res) |
| 10035 | { |
| 10036 | PyObject *value; |
| 10037 | Py_ssize_t len; |
| 10038 | |
| 10039 | assert(buf_size <= XATTR_SIZE_MAX); |
| 10040 | value = PyBytes_FromStringAndSize(NULL, buf_size); |
| 10041 | if (!value) |
| 10042 | return 0; |
| 10043 | Py_BEGIN_ALLOW_THREADS; |
| 10044 | len = get(path, name, PyBytes_AS_STRING(value), buf_size); |
| 10045 | Py_END_ALLOW_THREADS; |
| 10046 | if (len < 0) { |
| 10047 | Py_DECREF(value); |
| 10048 | if (errno == ERANGE) { |
| 10049 | value = NULL; |
| 10050 | } |
| 10051 | else { |
| 10052 | posix_error(); |
| 10053 | return 0; |
| 10054 | } |
| 10055 | } |
| 10056 | else if (len != buf_size) { |
| 10057 | /* Can only shrink. */ |
| 10058 | _PyBytes_Resize(&value, len); |
| 10059 | } |
| 10060 | *res = value; |
| 10061 | return 1; |
| 10062 | } |
| 10063 | |
| 10064 | static PyObject * |
| 10065 | getxattr_common(const char *path, PyObject *name_obj, |
| 10066 | ssize_t (*get)(const char *, const char *, void *, size_t)) |
| 10067 | { |
| 10068 | PyObject *value; |
| 10069 | const char *name = PyBytes_AS_STRING(name_obj); |
| 10070 | |
| 10071 | /* Try a small value first. */ |
| 10072 | if (!try_getxattr(path, name, get, 128, &value)) |
| 10073 | return NULL; |
| 10074 | if (value) |
| 10075 | return value; |
| 10076 | /* Now the maximum possible one. */ |
| 10077 | if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value)) |
| 10078 | return NULL; |
| 10079 | assert(value); |
| 10080 | return value; |
| 10081 | } |
| 10082 | |
| 10083 | PyDoc_STRVAR(posix_getxattr__doc__, |
| 10084 | "getxattr(path, attr) -> value\n\n\ |
| 10085 | Return the value of extended attribute *name* on *path*."); |
| 10086 | |
| 10087 | static PyObject * |
| 10088 | posix_getxattr(PyObject *self, PyObject *args) |
| 10089 | { |
| 10090 | PyObject *path, *res, *name; |
| 10091 | |
| 10092 | if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path, |
| 10093 | PyUnicode_FSConverter, &name)) |
| 10094 | return NULL; |
| 10095 | res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr); |
| 10096 | Py_DECREF(path); |
| 10097 | Py_DECREF(name); |
| 10098 | return res; |
| 10099 | } |
| 10100 | |
| 10101 | PyDoc_STRVAR(posix_lgetxattr__doc__, |
| 10102 | "lgetxattr(path, attr) -> value\n\n\ |
| 10103 | Like getxattr but don't follow symlinks."); |
| 10104 | |
| 10105 | static PyObject * |
| 10106 | posix_lgetxattr(PyObject *self, PyObject *args) |
| 10107 | { |
| 10108 | PyObject *path, *res, *name; |
| 10109 | |
| 10110 | if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path, |
| 10111 | PyUnicode_FSConverter, &name)) |
| 10112 | return NULL; |
| 10113 | res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr); |
| 10114 | Py_DECREF(path); |
| 10115 | Py_DECREF(name); |
| 10116 | return res; |
| 10117 | } |
| 10118 | |
| 10119 | static ssize_t |
| 10120 | wrap_fgetxattr(const char *path, const char *name, void *value, size_t size) |
| 10121 | { |
| 10122 | /* Hack to share code. */ |
| 10123 | return fgetxattr((int)(Py_uintptr_t)path, name, value, size); |
| 10124 | } |
| 10125 | |
| 10126 | PyDoc_STRVAR(posix_fgetxattr__doc__, |
| 10127 | "fgetxattr(fd, attr) -> value\n\n\ |
| 10128 | Like getxattr but operate on a fd instead of a path."); |
| 10129 | |
| 10130 | static PyObject * |
| 10131 | posix_fgetxattr(PyObject *self, PyObject *args) |
| 10132 | { |
| 10133 | PyObject *res, *name; |
| 10134 | int fd; |
| 10135 | |
| 10136 | if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name)) |
| 10137 | return NULL; |
| 10138 | res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr); |
| 10139 | Py_DECREF(name); |
| 10140 | return res; |
| 10141 | } |
| 10142 | |
| 10143 | PyDoc_STRVAR(posix_setxattr__doc__, |
| 10144 | "setxattr(path, attr, value, flags=0)\n\n\ |
| 10145 | Set extended attribute *attr* on *path* to *value*."); |
| 10146 | |
| 10147 | static PyObject * |
| 10148 | posix_setxattr(PyObject *self, PyObject *args) |
| 10149 | { |
| 10150 | PyObject *path, *name; |
| 10151 | Py_buffer data; |
| 10152 | int flags = 0, err; |
| 10153 | |
| 10154 | if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter, |
| 10155 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10156 | return NULL; |
| 10157 | Py_BEGIN_ALLOW_THREADS; |
| 10158 | err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10159 | data.buf, data.len, flags); |
| 10160 | Py_END_ALLOW_THREADS; |
| 10161 | Py_DECREF(path); |
| 10162 | Py_DECREF(name); |
| 10163 | PyBuffer_Release(&data); |
| 10164 | if (err) |
| 10165 | return posix_error(); |
| 10166 | Py_RETURN_NONE; |
| 10167 | } |
| 10168 | |
| 10169 | PyDoc_STRVAR(posix_lsetxattr__doc__, |
| 10170 | "lsetxattr(path, attr, value, flags=0)\n\n\ |
| 10171 | Like setxattr but don't follow symlinks."); |
| 10172 | |
| 10173 | static PyObject * |
| 10174 | posix_lsetxattr(PyObject *self, PyObject *args) |
| 10175 | { |
| 10176 | PyObject *path, *name; |
| 10177 | Py_buffer data; |
| 10178 | int flags = 0, err; |
| 10179 | |
| 10180 | if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter, |
| 10181 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10182 | return NULL; |
| 10183 | Py_BEGIN_ALLOW_THREADS; |
| 10184 | err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10185 | data.buf, data.len, flags); |
| 10186 | Py_END_ALLOW_THREADS; |
| 10187 | Py_DECREF(path); |
| 10188 | Py_DECREF(name); |
| 10189 | PyBuffer_Release(&data); |
| 10190 | if (err) |
| 10191 | return posix_error(); |
| 10192 | Py_RETURN_NONE; |
| 10193 | } |
| 10194 | |
| 10195 | PyDoc_STRVAR(posix_fsetxattr__doc__, |
| 10196 | "fsetxattr(fd, attr, value, flags=0)\n\n\ |
| 10197 | Like setxattr but operates on *fd* instead of a path."); |
| 10198 | |
| 10199 | static PyObject * |
| 10200 | posix_fsetxattr(PyObject *self, PyObject *args) |
| 10201 | { |
| 10202 | Py_buffer data; |
| 10203 | const char *name; |
| 10204 | int fd, flags = 0, err; |
| 10205 | |
| 10206 | if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter, |
| 10207 | &name, &data, &flags)) |
| 10208 | return NULL; |
| 10209 | Py_BEGIN_ALLOW_THREADS; |
| 10210 | err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags); |
| 10211 | Py_END_ALLOW_THREADS; |
| 10212 | Py_DECREF(name); |
| 10213 | PyBuffer_Release(&data); |
| 10214 | if (err) |
| 10215 | return posix_error(); |
| 10216 | Py_RETURN_NONE; |
| 10217 | } |
| 10218 | |
| 10219 | PyDoc_STRVAR(posix_removexattr__doc__, |
| 10220 | "removexattr(path, attr)\n\n\ |
| 10221 | Remove extended attribute *attr* on *path*."); |
| 10222 | |
| 10223 | static PyObject * |
| 10224 | posix_removexattr(PyObject *self, PyObject *args) |
| 10225 | { |
| 10226 | PyObject *path, *name; |
| 10227 | int err; |
| 10228 | |
| 10229 | if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path, |
| 10230 | PyUnicode_FSConverter, &name)) |
| 10231 | return NULL; |
| 10232 | Py_BEGIN_ALLOW_THREADS; |
| 10233 | err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10234 | Py_END_ALLOW_THREADS; |
| 10235 | Py_DECREF(path); |
| 10236 | Py_DECREF(name); |
| 10237 | if (err) |
| 10238 | return posix_error(); |
| 10239 | Py_RETURN_NONE; |
| 10240 | } |
| 10241 | |
| 10242 | PyDoc_STRVAR(posix_lremovexattr__doc__, |
| 10243 | "lremovexattr(path, attr)\n\n\ |
| 10244 | Like removexattr but don't follow symlinks."); |
| 10245 | |
| 10246 | static PyObject * |
| 10247 | posix_lremovexattr(PyObject *self, PyObject *args) |
| 10248 | { |
| 10249 | PyObject *path, *name; |
| 10250 | int err; |
| 10251 | |
| 10252 | if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path, |
| 10253 | PyUnicode_FSConverter, &name)) |
| 10254 | return NULL; |
| 10255 | Py_BEGIN_ALLOW_THREADS; |
| 10256 | err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10257 | Py_END_ALLOW_THREADS; |
| 10258 | Py_DECREF(path); |
| 10259 | Py_DECREF(name); |
| 10260 | if (err) |
| 10261 | return posix_error(); |
| 10262 | Py_RETURN_NONE; |
| 10263 | } |
| 10264 | |
| 10265 | PyDoc_STRVAR(posix_fremovexattr__doc__, |
| 10266 | "fremovexattr(fd, attr)\n\n\ |
| 10267 | Like removexattr but operates on a file descriptor."); |
| 10268 | |
| 10269 | static PyObject * |
| 10270 | posix_fremovexattr(PyObject *self, PyObject *args) |
| 10271 | { |
| 10272 | PyObject *name; |
| 10273 | int fd, err; |
| 10274 | |
| 10275 | if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd, |
| 10276 | PyUnicode_FSConverter, &name)) |
| 10277 | return NULL; |
| 10278 | Py_BEGIN_ALLOW_THREADS; |
| 10279 | err = fremovexattr(fd, PyBytes_AS_STRING(name)); |
| 10280 | Py_END_ALLOW_THREADS; |
| 10281 | Py_DECREF(name); |
| 10282 | if (err) |
| 10283 | return posix_error(); |
| 10284 | Py_RETURN_NONE; |
| 10285 | } |
| 10286 | |
| 10287 | static Py_ssize_t |
| 10288 | try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t), |
| 10289 | Py_ssize_t buf_size, char **buf) |
| 10290 | { |
| 10291 | Py_ssize_t len; |
| 10292 | |
| 10293 | *buf = PyMem_MALLOC(buf_size); |
| 10294 | if (!*buf) { |
| 10295 | PyErr_NoMemory(); |
| 10296 | return -1; |
| 10297 | } |
| 10298 | Py_BEGIN_ALLOW_THREADS; |
| 10299 | len = list(path, *buf, buf_size); |
| 10300 | Py_END_ALLOW_THREADS; |
| 10301 | if (len < 0) { |
| 10302 | PyMem_FREE(*buf); |
| 10303 | if (errno != ERANGE) |
| 10304 | posix_error(); |
| 10305 | return -1; |
| 10306 | } |
| 10307 | return len; |
| 10308 | } |
| 10309 | |
| 10310 | static PyObject * |
| 10311 | listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t)) |
| 10312 | { |
| 10313 | PyObject *res, *attr; |
| 10314 | Py_ssize_t len, err, start, i; |
| 10315 | char *buf; |
| 10316 | |
| 10317 | len = try_listxattr(path, list, 256, &buf); |
| 10318 | if (len < 0) { |
| 10319 | if (PyErr_Occurred()) |
| 10320 | return NULL; |
| 10321 | len = try_listxattr(path, list, XATTR_LIST_MAX, &buf); |
| 10322 | if (len < 0) |
| 10323 | return NULL; |
| 10324 | } |
| 10325 | res = PyList_New(0); |
| 10326 | if (!res) { |
| 10327 | PyMem_FREE(buf); |
| 10328 | return NULL; |
| 10329 | } |
| 10330 | for (start = i = 0; i < len; i++) { |
| 10331 | if (!buf[i]) { |
| 10332 | attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start); |
| 10333 | if (!attr) { |
| 10334 | Py_DECREF(res); |
| 10335 | PyMem_FREE(buf); |
| 10336 | return NULL; |
| 10337 | } |
| 10338 | err = PyList_Append(res, attr); |
| 10339 | Py_DECREF(attr); |
| 10340 | if (err) { |
| 10341 | Py_DECREF(res); |
| 10342 | PyMem_FREE(buf); |
| 10343 | return NULL; |
| 10344 | } |
| 10345 | start = i + 1; |
| 10346 | } |
| 10347 | } |
| 10348 | PyMem_FREE(buf); |
| 10349 | return res; |
| 10350 | } |
| 10351 | |
| 10352 | PyDoc_STRVAR(posix_listxattr__doc__, |
| 10353 | "listxattr(path)\n\n\ |
| 10354 | Return a list of extended attributes on *path*."); |
| 10355 | |
| 10356 | static PyObject * |
| 10357 | posix_listxattr(PyObject *self, PyObject *args) |
| 10358 | { |
| 10359 | PyObject *path, *res; |
| 10360 | |
| 10361 | if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path)) |
| 10362 | return NULL; |
| 10363 | res = listxattr_common(PyBytes_AS_STRING(path), listxattr); |
| 10364 | Py_DECREF(path); |
| 10365 | return res; |
| 10366 | } |
| 10367 | |
| 10368 | PyDoc_STRVAR(posix_llistxattr__doc__, |
| 10369 | "llistxattr(path)\n\n\ |
| 10370 | Like listxattr but don't follow symlinks.."); |
| 10371 | |
| 10372 | static PyObject * |
| 10373 | posix_llistxattr(PyObject *self, PyObject *args) |
| 10374 | { |
| 10375 | PyObject *path, *res; |
| 10376 | |
| 10377 | if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path)) |
| 10378 | return NULL; |
| 10379 | res = listxattr_common(PyBytes_AS_STRING(path), llistxattr); |
| 10380 | Py_DECREF(path); |
| 10381 | return res; |
| 10382 | } |
| 10383 | |
| 10384 | static ssize_t |
| 10385 | wrap_flistxattr(const char *path, char *buf, size_t len) |
| 10386 | { |
| 10387 | /* Hack to share code. */ |
| 10388 | return flistxattr((int)(Py_uintptr_t)path, buf, len); |
| 10389 | } |
| 10390 | |
| 10391 | PyDoc_STRVAR(posix_flistxattr__doc__, |
| 10392 | "flistxattr(path)\n\n\ |
| 10393 | Like flistxattr but operates on a file descriptor."); |
| 10394 | |
| 10395 | static PyObject * |
| 10396 | posix_flistxattr(PyObject *self, PyObject *args) |
| 10397 | { |
| 10398 | long fd; |
| 10399 | |
| 10400 | if (!PyArg_ParseTuple(args, "i:flistxattr", &fd)) |
| 10401 | return NULL; |
| 10402 | return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr); |
| 10403 | } |
| 10404 | |
| 10405 | #endif /* HAVE_ATTR_XATTR_H */ |
| 10406 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10407 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10408 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10409 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10410 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10411 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10412 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10413 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10414 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10415 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10416 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10417 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10418 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10419 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10420 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10421 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10422 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10423 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10424 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10425 | #endif /* HAVE_LCHMOD */ |
| 10426 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10427 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10428 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10429 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10430 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10431 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10432 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10433 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10434 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10435 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10436 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10437 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10438 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10439 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10440 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 10441 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10442 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10443 | METH_NOARGS, posix_getcwd__doc__}, |
| 10444 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10445 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 10446 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10447 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10448 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10449 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10450 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10451 | #ifdef HAVE_FDOPENDIR |
| 10452 | {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__}, |
| 10453 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10454 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 10455 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10456 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10457 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10458 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10459 | #ifdef HAVE_GETPRIORITY |
| 10460 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10461 | #endif /* HAVE_GETPRIORITY */ |
| 10462 | #ifdef HAVE_SETPRIORITY |
| 10463 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10464 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10465 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10466 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10467 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10468 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10469 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10470 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10471 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 10472 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 10473 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10474 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10475 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10476 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10477 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10478 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10479 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10480 | win_symlink__doc__}, |
| 10481 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10482 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10483 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10484 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10485 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10486 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10487 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10488 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10489 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10490 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 10491 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10492 | #ifdef HAVE_FUTIMES |
| 10493 | {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__}, |
| 10494 | #endif |
| 10495 | #ifdef HAVE_LUTIMES |
| 10496 | {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__}, |
| 10497 | #endif |
| 10498 | #ifdef HAVE_FUTIMENS |
| 10499 | {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__}, |
| 10500 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10501 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10502 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10503 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10504 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10505 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10506 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 10507 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10508 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10509 | #ifdef HAVE_FEXECVE |
| 10510 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 10511 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10512 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10513 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10514 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10515 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10516 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10517 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10518 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10519 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10520 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10521 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10522 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10523 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10524 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10525 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10526 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10527 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10528 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10529 | {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__}, |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10530 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10531 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10532 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10533 | #endif |
| 10534 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10535 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10536 | #endif |
| 10537 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10538 | {"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] | 10539 | #endif |
| 10540 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10541 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10542 | #endif |
| 10543 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10544 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10545 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10546 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10547 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10548 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10549 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10550 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10551 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10552 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10553 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10554 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10555 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10556 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10557 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10558 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10559 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10560 | #endif /* HAVE_GETEGID */ |
| 10561 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10562 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10563 | #endif /* HAVE_GETEUID */ |
| 10564 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10565 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10566 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10567 | #ifdef HAVE_GETGROUPLIST |
| 10568 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10569 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10570 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10571 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10572 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10573 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10574 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10575 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10576 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10577 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10578 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10579 | #endif /* HAVE_GETPPID */ |
| 10580 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10581 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10582 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10583 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10584 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10585 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10586 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10587 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10588 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10589 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10590 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10591 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10592 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10593 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10594 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10595 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10596 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10597 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 10598 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10599 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10600 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10601 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10602 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10603 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10604 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10605 | #endif /* HAVE_SETEUID */ |
| 10606 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10607 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10608 | #endif /* HAVE_SETEGID */ |
| 10609 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10610 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10611 | #endif /* HAVE_SETREUID */ |
| 10612 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10613 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10614 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10615 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10616 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10617 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10618 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10619 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10620 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10621 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10622 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10623 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10624 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10625 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10626 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10627 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10628 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10629 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10630 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10631 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10632 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10633 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10634 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10635 | #endif /* HAVE_WAIT3 */ |
| 10636 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10637 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10638 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10639 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10640 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10641 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10642 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10643 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10644 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10645 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10646 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10647 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10648 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10649 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10650 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10651 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10652 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10653 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10654 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10655 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10656 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10657 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10658 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10659 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10660 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 10661 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10662 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10663 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10664 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10665 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10666 | #ifdef HAVE_LOCKF |
| 10667 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10668 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10670 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10671 | #ifdef HAVE_READV |
| 10672 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10673 | #endif |
| 10674 | #ifdef HAVE_PREAD |
| 10675 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10676 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10677 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10678 | #ifdef HAVE_WRITEV |
| 10679 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10680 | #endif |
| 10681 | #ifdef HAVE_PWRITE |
| 10682 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10683 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10684 | #ifdef HAVE_SENDFILE |
| 10685 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10686 | posix_sendfile__doc__}, |
| 10687 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10688 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 10689 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10690 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10691 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10692 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10693 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10694 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10695 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10696 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10697 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10698 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10699 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10700 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10701 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10702 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10703 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10704 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10705 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10706 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10707 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10708 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10709 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10710 | #ifdef HAVE_TRUNCATE |
| 10711 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 10712 | #endif |
| 10713 | #ifdef HAVE_POSIX_FALLOCATE |
| 10714 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10715 | #endif |
| 10716 | #ifdef HAVE_POSIX_FADVISE |
| 10717 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10718 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10719 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10720 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10721 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10722 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10723 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10724 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10726 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10727 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10728 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10729 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10730 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10731 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10732 | #ifdef HAVE_SYNC |
| 10733 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10734 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10735 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10736 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10737 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10738 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10739 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10740 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10741 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10742 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10744 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10745 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10746 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10747 | #endif /* WIFSTOPPED */ |
| 10748 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10750 | #endif /* WIFSIGNALED */ |
| 10751 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10752 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10753 | #endif /* WIFEXITED */ |
| 10754 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10755 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10756 | #endif /* WEXITSTATUS */ |
| 10757 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10758 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10759 | #endif /* WTERMSIG */ |
| 10760 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10761 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10762 | #endif /* WSTOPSIG */ |
| 10763 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10764 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10765 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10766 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10767 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10768 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10769 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10770 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10771 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10772 | #endif |
| 10773 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10774 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10775 | #endif |
| 10776 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10777 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10778 | #endif |
| 10779 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10780 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10781 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10782 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10783 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10784 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10785 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10786 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10787 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10788 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10789 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10790 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10791 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10792 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10793 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10795 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10796 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10798 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10799 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10800 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10801 | #endif |
| 10802 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10803 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10804 | #endif |
| 10805 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10806 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10807 | #endif |
| 10808 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10809 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10810 | #endif |
| 10811 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10812 | /* posix *at family of functions */ |
| 10813 | #ifdef HAVE_FACCESSAT |
| 10814 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 10815 | #endif |
| 10816 | #ifdef HAVE_FCHMODAT |
| 10817 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 10818 | #endif /* HAVE_FCHMODAT */ |
| 10819 | #ifdef HAVE_FCHOWNAT |
| 10820 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 10821 | #endif /* HAVE_FCHOWNAT */ |
| 10822 | #ifdef HAVE_FSTATAT |
| 10823 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
| 10824 | #endif |
| 10825 | #ifdef HAVE_FUTIMESAT |
| 10826 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 10827 | #endif |
| 10828 | #ifdef HAVE_LINKAT |
| 10829 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 10830 | #endif /* HAVE_LINKAT */ |
| 10831 | #ifdef HAVE_MKDIRAT |
| 10832 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 10833 | #endif |
| 10834 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 10835 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 10836 | #endif |
| 10837 | #ifdef HAVE_OPENAT |
| 10838 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 10839 | #endif |
| 10840 | #ifdef HAVE_READLINKAT |
| 10841 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 10842 | #endif /* HAVE_READLINKAT */ |
| 10843 | #ifdef HAVE_RENAMEAT |
| 10844 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 10845 | #endif |
| 10846 | #if HAVE_SYMLINKAT |
| 10847 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 10848 | #endif /* HAVE_SYMLINKAT */ |
| 10849 | #ifdef HAVE_UNLINKAT |
| 10850 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 10851 | #endif |
| 10852 | #ifdef HAVE_UTIMENSAT |
| 10853 | {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__}, |
| 10854 | #endif |
| 10855 | #ifdef HAVE_MKFIFOAT |
| 10856 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 10857 | #endif |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10858 | #ifdef HAVE_ATTR_XATTR_H |
| 10859 | {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__}, |
| 10860 | {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__}, |
| 10861 | {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__}, |
| 10862 | {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__}, |
| 10863 | {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__}, |
| 10864 | {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__}, |
| 10865 | {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__}, |
| 10866 | {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__}, |
| 10867 | {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__}, |
| 10868 | {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__}, |
| 10869 | {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__}, |
| 10870 | {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__}, |
| 10871 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10872 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10873 | }; |
| 10874 | |
| 10875 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10876 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10877 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10878 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10879 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10880 | } |
| 10881 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10882 | #if defined(PYOS_OS2) |
| 10883 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10884 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10885 | { |
| 10886 | APIRET rc; |
| 10887 | ULONG values[QSV_MAX+1]; |
| 10888 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 10889 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10890 | |
| 10891 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 10892 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10893 | Py_END_ALLOW_THREADS |
| 10894 | |
| 10895 | if (rc != NO_ERROR) { |
| 10896 | os2_error(rc); |
| 10897 | return -1; |
| 10898 | } |
| 10899 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10900 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 10901 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 10902 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 10903 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 10904 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 10905 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 10906 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10907 | |
| 10908 | switch (values[QSV_VERSION_MINOR]) { |
| 10909 | case 0: ver = "2.00"; break; |
| 10910 | case 10: ver = "2.10"; break; |
| 10911 | case 11: ver = "2.11"; break; |
| 10912 | case 30: ver = "3.00"; break; |
| 10913 | case 40: ver = "4.00"; break; |
| 10914 | case 50: ver = "5.00"; break; |
| 10915 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 10916 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10917 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 10918 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10919 | ver = &tmp[0]; |
| 10920 | } |
| 10921 | |
| 10922 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10923 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10924 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10925 | |
| 10926 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 10927 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 10928 | tmp[1] = ':'; |
| 10929 | tmp[2] = '\0'; |
| 10930 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10931 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10932 | } |
| 10933 | #endif |
| 10934 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10935 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10936 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10937 | enable_symlink() |
| 10938 | { |
| 10939 | HANDLE tok; |
| 10940 | TOKEN_PRIVILEGES tok_priv; |
| 10941 | LUID luid; |
| 10942 | int meth_idx = 0; |
| 10943 | |
| 10944 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10945 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10946 | |
| 10947 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10948 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10949 | |
| 10950 | tok_priv.PrivilegeCount = 1; |
| 10951 | tok_priv.Privileges[0].Luid = luid; |
| 10952 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 10953 | |
| 10954 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 10955 | sizeof(TOKEN_PRIVILEGES), |
| 10956 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10957 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10958 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10959 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 10960 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10961 | } |
| 10962 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 10963 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10964 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 10965 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10966 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10967 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10968 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10969 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10970 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10971 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10972 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10973 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10975 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10976 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10977 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10978 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10979 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10981 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10982 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10983 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10984 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10985 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10986 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10987 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10988 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10989 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10990 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10991 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10992 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10993 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10994 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10995 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10996 | #endif |
| 10997 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10998 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10999 | #endif |
| 11000 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11001 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11002 | #endif |
| 11003 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11004 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11005 | #endif |
| 11006 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11007 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11008 | #endif |
| 11009 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11011 | #endif |
| 11012 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11013 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11014 | #endif |
| 11015 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11016 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11017 | #endif |
| 11018 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11019 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11020 | #endif |
| 11021 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11022 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11023 | #endif |
| 11024 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11025 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11026 | #endif |
| 11027 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11028 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11029 | #endif |
| 11030 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11032 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11033 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11034 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11035 | #endif |
| 11036 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11037 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11038 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11039 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11040 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11041 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11042 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11043 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11044 | #endif |
| 11045 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11046 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11047 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11048 | #ifdef PRIO_PROCESS |
| 11049 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11050 | #endif |
| 11051 | #ifdef PRIO_PGRP |
| 11052 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11053 | #endif |
| 11054 | #ifdef PRIO_USER |
| 11055 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11056 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11057 | #ifdef O_CLOEXEC |
| 11058 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11059 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11060 | /* posix - constants for *at functions */ |
| 11061 | #ifdef AT_SYMLINK_NOFOLLOW |
| 11062 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 11063 | #endif |
| 11064 | #ifdef AT_EACCESS |
| 11065 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 11066 | #endif |
| 11067 | #ifdef AT_FDCWD |
| 11068 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 11069 | #endif |
| 11070 | #ifdef AT_REMOVEDIR |
| 11071 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 11072 | #endif |
| 11073 | #ifdef AT_SYMLINK_FOLLOW |
| 11074 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 11075 | #endif |
| 11076 | #ifdef UTIME_NOW |
| 11077 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11078 | #endif |
| 11079 | #ifdef UTIME_OMIT |
| 11080 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11081 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11082 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11083 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11084 | /* MS Windows */ |
| 11085 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11086 | /* Don't inherit in child processes. */ |
| 11087 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11088 | #endif |
| 11089 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11090 | /* Optimize for short life (keep in memory). */ |
| 11091 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11092 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11093 | #endif |
| 11094 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | /* Automatically delete when last handle is closed. */ |
| 11096 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11097 | #endif |
| 11098 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11099 | /* Optimize for random access. */ |
| 11100 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | /* Optimize for sequential access. */ |
| 11104 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11107 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11108 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | /* Send a SIGIO signal whenever input or output |
| 11110 | becomes available on file descriptor */ |
| 11111 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11112 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11113 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11114 | /* Direct disk access. */ |
| 11115 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11116 | #endif |
| 11117 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11118 | /* Must be a directory. */ |
| 11119 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11120 | #endif |
| 11121 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11122 | /* Do not follow links. */ |
| 11123 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11124 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11125 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11126 | /* Do not update the access time. */ |
| 11127 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11128 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11129 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11131 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11132 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11133 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11134 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11135 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11136 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11137 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11138 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11139 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11140 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11141 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11142 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11143 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11144 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11145 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11146 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11147 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11148 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11149 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11150 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11151 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11152 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11153 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11154 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11155 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11156 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11157 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11158 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11159 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11160 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11161 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11162 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11163 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11164 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11165 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11166 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11167 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11168 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11169 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11170 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11171 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11172 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11173 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11174 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11175 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11176 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11177 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11178 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11179 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11180 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11181 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11182 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11183 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11184 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11185 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11186 | #endif /* ST_RDONLY */ |
| 11187 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11188 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11189 | #endif /* ST_NOSUID */ |
| 11190 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11191 | /* FreeBSD sendfile() constants */ |
| 11192 | #ifdef SF_NODISKIO |
| 11193 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11194 | #endif |
| 11195 | #ifdef SF_MNOWAIT |
| 11196 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11197 | #endif |
| 11198 | #ifdef SF_SYNC |
| 11199 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11200 | #endif |
| 11201 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11202 | /* constants for posix_fadvise */ |
| 11203 | #ifdef POSIX_FADV_NORMAL |
| 11204 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11205 | #endif |
| 11206 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11207 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11208 | #endif |
| 11209 | #ifdef POSIX_FADV_RANDOM |
| 11210 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11211 | #endif |
| 11212 | #ifdef POSIX_FADV_NOREUSE |
| 11213 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11214 | #endif |
| 11215 | #ifdef POSIX_FADV_WILLNEED |
| 11216 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11217 | #endif |
| 11218 | #ifdef POSIX_FADV_DONTNEED |
| 11219 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11220 | #endif |
| 11221 | |
| 11222 | /* constants for waitid */ |
| 11223 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11224 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11225 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11226 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11227 | #endif |
| 11228 | #ifdef WEXITED |
| 11229 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11230 | #endif |
| 11231 | #ifdef WNOWAIT |
| 11232 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11233 | #endif |
| 11234 | #ifdef WSTOPPED |
| 11235 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11236 | #endif |
| 11237 | #ifdef CLD_EXITED |
| 11238 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11239 | #endif |
| 11240 | #ifdef CLD_DUMPED |
| 11241 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11242 | #endif |
| 11243 | #ifdef CLD_TRAPPED |
| 11244 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11245 | #endif |
| 11246 | #ifdef CLD_CONTINUED |
| 11247 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11248 | #endif |
| 11249 | |
| 11250 | /* constants for lockf */ |
| 11251 | #ifdef F_LOCK |
| 11252 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11253 | #endif |
| 11254 | #ifdef F_TLOCK |
| 11255 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11256 | #endif |
| 11257 | #ifdef F_ULOCK |
| 11258 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11259 | #endif |
| 11260 | #ifdef F_TEST |
| 11261 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11262 | #endif |
| 11263 | |
| 11264 | /* constants for futimens */ |
| 11265 | #ifdef UTIME_NOW |
| 11266 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11267 | #endif |
| 11268 | #ifdef UTIME_OMIT |
| 11269 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11270 | #endif |
| 11271 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11272 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11273 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11275 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11276 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11277 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11278 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11279 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11280 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11281 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11282 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11283 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11284 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11285 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11286 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11287 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11288 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11289 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11290 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11291 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11292 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11293 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11294 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11296 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11297 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11298 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11299 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11300 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11301 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11302 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11303 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame^] | 11304 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11305 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11306 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11307 | #ifdef SCHED_SPORADIC |
| 11308 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11309 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11310 | #ifdef SCHED_BATCH |
| 11311 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11312 | #endif |
| 11313 | #ifdef SCHED_IDLE |
| 11314 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11315 | #endif |
| 11316 | #ifdef SCHED_RESET_ON_FORK |
| 11317 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11318 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame^] | 11319 | #ifdef SCHED_SYS |
| 11320 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11321 | #endif |
| 11322 | #ifdef SCHED_IA |
| 11323 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11324 | #endif |
| 11325 | #ifdef SCHED_FSS |
| 11326 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11327 | #endif |
| 11328 | #ifdef SCHED_FX |
| 11329 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11330 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11331 | #endif |
| 11332 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11333 | #ifdef HAVE_ATTR_XATTR_H |
| 11334 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11335 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11336 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11337 | #endif |
| 11338 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11339 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11341 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11342 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11343 | } |
| 11344 | |
| 11345 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11346 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11347 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11348 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11349 | |
| 11350 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11351 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11352 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11353 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11354 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11355 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11356 | #define MODNAME "posix" |
| 11357 | #endif |
| 11358 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11359 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11360 | PyModuleDef_HEAD_INIT, |
| 11361 | MODNAME, |
| 11362 | posix__doc__, |
| 11363 | -1, |
| 11364 | posix_methods, |
| 11365 | NULL, |
| 11366 | NULL, |
| 11367 | NULL, |
| 11368 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11369 | }; |
| 11370 | |
| 11371 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11372 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11373 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11374 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11375 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11376 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11377 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11378 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11379 | #endif |
| 11380 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11381 | m = PyModule_Create(&posixmodule); |
| 11382 | if (m == NULL) |
| 11383 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11384 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11385 | /* Initialize environ dictionary */ |
| 11386 | v = convertenviron(); |
| 11387 | Py_XINCREF(v); |
| 11388 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11389 | return NULL; |
| 11390 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11391 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | if (all_ins(m)) |
| 11393 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11394 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11395 | if (setup_confname_tables(m)) |
| 11396 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11397 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11398 | Py_INCREF(PyExc_OSError); |
| 11399 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11400 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11401 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11402 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11403 | return NULL; |
| 11404 | Py_INCREF(&cpu_set_type); |
| 11405 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11406 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11407 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11408 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | if (posix_putenv_garbage == NULL) |
| 11410 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11411 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11413 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11414 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11415 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11416 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11417 | #endif |
| 11418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11419 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11420 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11421 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11422 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11423 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11424 | structseq_new = StatResultType.tp_new; |
| 11425 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11426 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11428 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11429 | #ifdef NEED_TICKS_PER_SECOND |
| 11430 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11431 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11432 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11433 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11434 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11435 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11436 | # endif |
| 11437 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11438 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11439 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11440 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11441 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11442 | SchedParamType.tp_new = sched_param_new; |
| 11443 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11444 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11445 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11446 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11447 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11448 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11449 | Py_INCREF((PyObject*) &StatResultType); |
| 11450 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11451 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11452 | PyModule_AddObject(m, "statvfs_result", |
| 11453 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11454 | |
| 11455 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11456 | Py_INCREF(&SchedParamType); |
| 11457 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11458 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11459 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11460 | |
| 11461 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11462 | /* |
| 11463 | * Step 2 of weak-linking support on Mac OS X. |
| 11464 | * |
| 11465 | * The code below removes functions that are not available on the |
| 11466 | * currently active platform. |
| 11467 | * |
| 11468 | * This block allow one to use a python binary that was build on |
| 11469 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 11470 | * OSX 10.4. |
| 11471 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11472 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | if (fstatvfs == NULL) { |
| 11474 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11475 | return NULL; |
| 11476 | } |
| 11477 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11478 | #endif /* HAVE_FSTATVFS */ |
| 11479 | |
| 11480 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11481 | if (statvfs == NULL) { |
| 11482 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11483 | return NULL; |
| 11484 | } |
| 11485 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11486 | #endif /* HAVE_STATVFS */ |
| 11487 | |
| 11488 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11489 | if (lchown == NULL) { |
| 11490 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11491 | return NULL; |
| 11492 | } |
| 11493 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11494 | #endif /* HAVE_LCHOWN */ |
| 11495 | |
| 11496 | |
| 11497 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11498 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11499 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11500 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11501 | |
| 11502 | #ifdef __cplusplus |
| 11503 | } |
| 11504 | #endif |