Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 31 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | #endif /* defined(__VMS) */ |
| 35 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 40 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 41 | "This module provides access to operating system functionality that is\n\ |
| 42 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 43 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 44 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 47 | #if defined(PYOS_OS2) |
| 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 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 62 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | #endif /* HAVE_SYS_TYPES_H */ |
| 65 | |
| 66 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 70 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 71 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 72 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 73 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 75 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | #ifdef HAVE_FCNTL_H |
| 79 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 80 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 82 | #ifdef HAVE_GRP_H |
| 83 | #include <grp.h> |
| 84 | #endif |
| 85 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 86 | #ifdef HAVE_SYSEXITS_H |
| 87 | #include <sysexits.h> |
| 88 | #endif /* HAVE_SYSEXITS_H */ |
| 89 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 90 | #ifdef HAVE_SYS_LOADAVG_H |
| 91 | #include <sys/loadavg.h> |
| 92 | #endif |
| 93 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 94 | #ifdef HAVE_LANGINFO_H |
| 95 | #include <langinfo.h> |
| 96 | #endif |
| 97 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 98 | #ifdef HAVE_SYS_SENDFILE_H |
| 99 | #include <sys/sendfile.h> |
| 100 | #endif |
| 101 | |
| 102 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 103 | #ifdef HAVE_SYS_SOCKET_H |
| 104 | #include <sys/socket.h> |
| 105 | #endif |
| 106 | |
| 107 | #ifdef HAVE_SYS_UIO_H |
| 108 | #include <sys/uio.h> |
| 109 | #endif |
| 110 | #endif |
| 111 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 113 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 114 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 115 | #include <process.h> |
| 116 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 117 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 118 | #define HAVE_GETCWD 1 |
| 119 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 120 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 121 | #if defined(__OS2__) |
| 122 | #define HAVE_EXECV 1 |
| 123 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 124 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #include <process.h> |
| 126 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 127 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | #define HAVE_EXECV 1 |
| 129 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #define HAVE_OPENDIR 1 |
| 131 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 132 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 133 | #define HAVE_WAIT 1 |
| 134 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 135 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 136 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 137 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 138 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 139 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | #define HAVE_EXECV 1 |
| 141 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 142 | #define HAVE_SYSTEM 1 |
| 143 | #define HAVE_CWAIT 1 |
| 144 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 145 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 146 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 147 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 148 | /* 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] | 149 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | /* Unix functions that the configure script doesn't check for */ |
| 151 | #define HAVE_EXECV 1 |
| 152 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 153 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 154 | #define HAVE_FORK1 1 |
| 155 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 156 | #define HAVE_GETCWD 1 |
| 157 | #define HAVE_GETEGID 1 |
| 158 | #define HAVE_GETEUID 1 |
| 159 | #define HAVE_GETGID 1 |
| 160 | #define HAVE_GETPPID 1 |
| 161 | #define HAVE_GETUID 1 |
| 162 | #define HAVE_KILL 1 |
| 163 | #define HAVE_OPENDIR 1 |
| 164 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 165 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 166 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 167 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 168 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 169 | #endif /* _MSC_VER */ |
| 170 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 171 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 172 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 174 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 175 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 176 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 177 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 178 | (default) */ |
| 179 | extern char *ctermid_r(char *); |
| 180 | #endif |
| 181 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 182 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 184 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 185 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 186 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 188 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 189 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 190 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 191 | #endif |
| 192 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 193 | extern int chdir(char *); |
| 194 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 195 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 196 | extern int chdir(const char *); |
| 197 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 198 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 199 | #ifdef __BORLANDC__ |
| 200 | extern int chmod(const char *, int); |
| 201 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 202 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 203 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 204 | /*#ifdef HAVE_FCHMOD |
| 205 | extern int fchmod(int, mode_t); |
| 206 | #endif*/ |
| 207 | /*#ifdef HAVE_LCHMOD |
| 208 | extern int lchmod(const char *, mode_t); |
| 209 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int chown(const char *, uid_t, gid_t); |
| 211 | extern char *getcwd(char *, int); |
| 212 | extern char *strerror(int); |
| 213 | extern int link(const char *, const char *); |
| 214 | extern int rename(const char *, const char *); |
| 215 | extern int stat(const char *, struct stat *); |
| 216 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 218 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 219 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 220 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 221 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 222 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 225 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | #ifdef HAVE_UTIME_H |
| 228 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 229 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 231 | #ifdef HAVE_SYS_UTIME_H |
| 232 | #include <sys/utime.h> |
| 233 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 234 | #endif /* HAVE_SYS_UTIME_H */ |
| 235 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | #ifdef HAVE_SYS_TIMES_H |
| 237 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 238 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | |
| 240 | #ifdef HAVE_SYS_PARAM_H |
| 241 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 242 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | |
| 244 | #ifdef HAVE_SYS_UTSNAME_H |
| 245 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 246 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 248 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 250 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 251 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 252 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 253 | #include <direct.h> |
| 254 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 255 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 257 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 258 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 259 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 261 | #endif |
| 262 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 264 | #endif |
| 265 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 267 | #endif |
| 268 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 270 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 271 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 273 | #endif |
| 274 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 276 | #endif |
| 277 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 279 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 280 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 281 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 282 | #endif |
| 283 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 284 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 285 | #endif |
| 286 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 287 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 288 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 289 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 290 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 291 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 292 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 293 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 294 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 295 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 296 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 297 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 298 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 300 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 302 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 303 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 304 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 305 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 306 | #define MAXPATHLEN PATH_MAX |
| 307 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 308 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 309 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 310 | #endif /* MAXPATHLEN */ |
| 311 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 312 | #ifdef UNION_WAIT |
| 313 | /* Emulate some macros on systems that have a union instead of macros */ |
| 314 | |
| 315 | #ifndef WIFEXITED |
| 316 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 317 | #endif |
| 318 | |
| 319 | #ifndef WEXITSTATUS |
| 320 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 321 | #endif |
| 322 | |
| 323 | #ifndef WTERMSIG |
| 324 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 325 | #endif |
| 326 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 327 | #define WAIT_TYPE union wait |
| 328 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 329 | |
| 330 | #else /* !UNION_WAIT */ |
| 331 | #define WAIT_TYPE int |
| 332 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 333 | #endif /* UNION_WAIT */ |
| 334 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 335 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 336 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 337 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 338 | #define USE_CTERMID_R |
| 339 | #endif |
| 340 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 341 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 342 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 343 | #undef FSTAT |
| 344 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 345 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 346 | # define STAT win32_stat |
| 347 | # define FSTAT win32_fstat |
| 348 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 349 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 350 | # define STAT stat |
| 351 | # define FSTAT fstat |
| 352 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 353 | #endif |
| 354 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 355 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 356 | #include <sys/mkdev.h> |
| 357 | #else |
| 358 | #if defined(MAJOR_IN_SYSMACROS) |
| 359 | #include <sys/sysmacros.h> |
| 360 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 361 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 362 | #include <sys/mkdev.h> |
| 363 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 364 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 365 | |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 366 | static int |
| 367 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 368 | { |
| 369 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 370 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 371 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 372 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 373 | #endif |
| 374 | if (PyErr_Occurred()) |
| 375 | return 0; |
| 376 | return 1; |
| 377 | } |
| 378 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 379 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 380 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 381 | * valid and throw an assertion if it isn't. |
| 382 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 383 | * an assertion can be useful, but it does contradict the POSIX standard |
| 384 | * which for write(2) states: |
| 385 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 386 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 387 | * writing." |
| 388 | * Furthermore, python allows the user to enter any old integer |
| 389 | * as a fd and should merely raise a python exception on error. |
| 390 | * The Microsoft CRT doesn't provide an official way to check for the |
| 391 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 392 | * 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] | 393 | * internal structures involved. |
| 394 | * The structures below must be updated for each version of visual studio |
| 395 | * according to the file internal.h in the CRT source, until MS comes |
| 396 | * up with a less hacky way to do this. |
| 397 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 398 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 399 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 400 | /* The actual size of the structure is determined at runtime. |
| 401 | * Only the first items must be present. |
| 402 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 403 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 404 | intptr_t osfhnd; |
| 405 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 406 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 407 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 408 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 409 | #define IOINFO_L2E 5 |
| 410 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 411 | #define IOINFO_ARRAYS 64 |
| 412 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 413 | #define FOPEN 0x01 |
| 414 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 415 | |
| 416 | /* This function emulates what the windows CRT does to validate file handles */ |
| 417 | int |
| 418 | _PyVerify_fd(int fd) |
| 419 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 420 | const int i1 = fd >> IOINFO_L2E; |
| 421 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 422 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 423 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 424 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 425 | /* Determine the actual size of the ioinfo structure, |
| 426 | * as used by the CRT loaded in memory |
| 427 | */ |
| 428 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 429 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 430 | } |
| 431 | if (sizeof_ioinfo == 0) { |
| 432 | /* This should not happen... */ |
| 433 | goto fail; |
| 434 | } |
| 435 | |
| 436 | /* See that it isn't a special CLEAR fileno */ |
| 437 | if (fd != _NO_CONSOLE_FILENO) { |
| 438 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 439 | * we check pointer validity and other info |
| 440 | */ |
| 441 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 442 | /* finally, check that the file is open */ |
| 443 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 444 | if (info->osfile & FOPEN) { |
| 445 | return 1; |
| 446 | } |
| 447 | } |
| 448 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 449 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 450 | errno = EBADF; |
| 451 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 455 | static int |
| 456 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 457 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 458 | if (!_PyVerify_fd(fd1)) |
| 459 | return 0; |
| 460 | if (fd2 == _NO_CONSOLE_FILENO) |
| 461 | return 0; |
| 462 | if ((unsigned)fd2 < _NHANDLE_) |
| 463 | return 1; |
| 464 | else |
| 465 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 466 | } |
| 467 | #else |
| 468 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 469 | #define _PyVerify_fd_dup2(A, B) (1) |
| 470 | #endif |
| 471 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 472 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 473 | /* The following structure was copied from |
| 474 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 475 | include doesn't seem to be present in the Windows SDK (at least as included |
| 476 | with Visual Studio Express). */ |
| 477 | typedef struct _REPARSE_DATA_BUFFER { |
| 478 | ULONG ReparseTag; |
| 479 | USHORT ReparseDataLength; |
| 480 | USHORT Reserved; |
| 481 | union { |
| 482 | struct { |
| 483 | USHORT SubstituteNameOffset; |
| 484 | USHORT SubstituteNameLength; |
| 485 | USHORT PrintNameOffset; |
| 486 | USHORT PrintNameLength; |
| 487 | ULONG Flags; |
| 488 | WCHAR PathBuffer[1]; |
| 489 | } SymbolicLinkReparseBuffer; |
| 490 | |
| 491 | struct { |
| 492 | USHORT SubstituteNameOffset; |
| 493 | USHORT SubstituteNameLength; |
| 494 | USHORT PrintNameOffset; |
| 495 | USHORT PrintNameLength; |
| 496 | WCHAR PathBuffer[1]; |
| 497 | } MountPointReparseBuffer; |
| 498 | |
| 499 | struct { |
| 500 | UCHAR DataBuffer[1]; |
| 501 | } GenericReparseBuffer; |
| 502 | }; |
| 503 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 504 | |
| 505 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 506 | GenericReparseBuffer) |
| 507 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 508 | |
| 509 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 510 | win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 511 | { |
| 512 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 513 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 514 | DWORD n_bytes_returned; |
| 515 | const wchar_t *ptr; |
| 516 | wchar_t *buf; |
| 517 | size_t len; |
| 518 | |
| 519 | if (0 == DeviceIoControl( |
| 520 | reparse_point_handle, |
| 521 | FSCTL_GET_REPARSE_POINT, |
| 522 | NULL, 0, /* in buffer */ |
| 523 | target_buffer, sizeof(target_buffer), |
| 524 | &n_bytes_returned, |
| 525 | NULL)) /* we're not using OVERLAPPED_IO */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 526 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 527 | |
| 528 | if (reparse_tag) |
| 529 | *reparse_tag = rdb->ReparseTag; |
| 530 | |
| 531 | if (target_path) { |
| 532 | switch (rdb->ReparseTag) { |
| 533 | case IO_REPARSE_TAG_SYMLINK: |
| 534 | /* XXX: Maybe should use SubstituteName? */ |
| 535 | ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 536 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR); |
| 537 | len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR); |
| 538 | break; |
| 539 | case IO_REPARSE_TAG_MOUNT_POINT: |
| 540 | ptr = rdb->MountPointReparseBuffer.PathBuffer + |
| 541 | rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR); |
| 542 | len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR); |
| 543 | break; |
| 544 | default: |
| 545 | SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 546 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 547 | } |
| 548 | buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1)); |
| 549 | if (!buf) { |
| 550 | SetLastError(ERROR_OUTOFMEMORY); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 551 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 552 | } |
| 553 | wcsncpy(buf, ptr, len); |
| 554 | buf[len] = L'\0'; |
| 555 | if (wcsncmp(buf, L"\\??\\", 4) == 0) |
| 556 | buf[1] = L'\\'; |
| 557 | *target_path = buf; |
| 558 | } |
| 559 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 560 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 561 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 562 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 564 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 565 | #ifdef WITH_NEXT_FRAMEWORK |
| 566 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 567 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 568 | */ |
| 569 | #include <crt_externs.h> |
| 570 | static char **environ; |
| 571 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 572 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 573 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 574 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 575 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 576 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 577 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 578 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 579 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 580 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 581 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 582 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 583 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 584 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 585 | APIRET rc; |
| 586 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 587 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 588 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 589 | d = PyDict_New(); |
| 590 | if (d == NULL) |
| 591 | return NULL; |
| 592 | #ifdef WITH_NEXT_FRAMEWORK |
| 593 | if (environ == NULL) |
| 594 | environ = *_NSGetEnviron(); |
| 595 | #endif |
| 596 | #ifdef MS_WINDOWS |
| 597 | /* _wenviron must be initialized in this way if the program is started |
| 598 | through main() instead of wmain(). */ |
| 599 | _wgetenv(L""); |
| 600 | if (_wenviron == NULL) |
| 601 | return d; |
| 602 | /* This part ignores errors */ |
| 603 | for (e = _wenviron; *e != NULL; e++) { |
| 604 | PyObject *k; |
| 605 | PyObject *v; |
| 606 | wchar_t *p = wcschr(*e, L'='); |
| 607 | if (p == NULL) |
| 608 | continue; |
| 609 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 610 | if (k == NULL) { |
| 611 | PyErr_Clear(); |
| 612 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 613 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 614 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 615 | if (v == NULL) { |
| 616 | PyErr_Clear(); |
| 617 | Py_DECREF(k); |
| 618 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 619 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 620 | if (PyDict_GetItem(d, k) == NULL) { |
| 621 | if (PyDict_SetItem(d, k, v) != 0) |
| 622 | PyErr_Clear(); |
| 623 | } |
| 624 | Py_DECREF(k); |
| 625 | Py_DECREF(v); |
| 626 | } |
| 627 | #else |
| 628 | if (environ == NULL) |
| 629 | return d; |
| 630 | /* This part ignores errors */ |
| 631 | for (e = environ; *e != NULL; e++) { |
| 632 | PyObject *k; |
| 633 | PyObject *v; |
| 634 | char *p = strchr(*e, '='); |
| 635 | if (p == NULL) |
| 636 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 637 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 638 | if (k == NULL) { |
| 639 | PyErr_Clear(); |
| 640 | continue; |
| 641 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 642 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 643 | if (v == NULL) { |
| 644 | PyErr_Clear(); |
| 645 | Py_DECREF(k); |
| 646 | continue; |
| 647 | } |
| 648 | if (PyDict_GetItem(d, k) == NULL) { |
| 649 | if (PyDict_SetItem(d, k, v) != 0) |
| 650 | PyErr_Clear(); |
| 651 | } |
| 652 | Py_DECREF(k); |
| 653 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 654 | } |
| 655 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 656 | #if defined(PYOS_OS2) |
| 657 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 658 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 659 | PyObject *v = PyBytes_FromString(buffer); |
| 660 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 661 | Py_DECREF(v); |
| 662 | } |
| 663 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 664 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 665 | PyObject *v = PyBytes_FromString(buffer); |
| 666 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 667 | Py_DECREF(v); |
| 668 | } |
| 669 | #endif |
| 670 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 673 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 674 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 675 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 676 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 677 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 678 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 679 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 680 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 681 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 682 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 683 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 686 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 687 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 688 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 689 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 690 | PyObject *name_str, *rc; |
| 691 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 692 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 693 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 694 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 695 | name_str); |
| 696 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 697 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 700 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 701 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 702 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 703 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 704 | /* XXX We should pass the function name along in the future. |
| 705 | (winreg.c also wants to pass the function name.) |
| 706 | This would however require an additional param to the |
| 707 | Windows error object, which is non-trivial. |
| 708 | */ |
| 709 | errno = GetLastError(); |
| 710 | if (filename) |
| 711 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 712 | else |
| 713 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 714 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 715 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 716 | static PyObject * |
| 717 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 718 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 719 | /* XXX - see win32_error for comments on 'function' */ |
| 720 | errno = GetLastError(); |
| 721 | if (filename) |
| 722 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 723 | else |
| 724 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 727 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 728 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 729 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 730 | if (PyUnicode_CheckExact(*param)) |
| 731 | Py_INCREF(*param); |
| 732 | else if (PyUnicode_Check(*param)) |
| 733 | /* For a Unicode subtype that's not a Unicode object, |
| 734 | return a true Unicode object with the same data. */ |
| 735 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 736 | PyUnicode_GET_SIZE(*param)); |
| 737 | else |
| 738 | *param = PyUnicode_FromEncodedObject(*param, |
| 739 | Py_FileSystemDefaultEncoding, |
| 740 | "strict"); |
| 741 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 744 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 746 | #if defined(PYOS_OS2) |
| 747 | /********************************************************************** |
| 748 | * Helper Function to Trim and Format OS/2 Messages |
| 749 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 750 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 751 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 752 | { |
| 753 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 754 | |
| 755 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 756 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 757 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 758 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 759 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 760 | } |
| 761 | |
| 762 | /* Add Optional Reason Text */ |
| 763 | if (reason) { |
| 764 | strcat(msgbuf, " : "); |
| 765 | strcat(msgbuf, reason); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /********************************************************************** |
| 770 | * Decode an OS/2 Operating System Error Code |
| 771 | * |
| 772 | * A convenience function to lookup an OS/2 error code and return a |
| 773 | * text message we can use to raise a Python exception. |
| 774 | * |
| 775 | * Notes: |
| 776 | * The messages for errors returned from the OS/2 kernel reside in |
| 777 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 778 | * |
| 779 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 780 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 781 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 782 | { |
| 783 | APIRET rc; |
| 784 | ULONG msglen; |
| 785 | |
| 786 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 787 | Py_BEGIN_ALLOW_THREADS |
| 788 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 789 | errorcode, "oso001.msg", &msglen); |
| 790 | Py_END_ALLOW_THREADS |
| 791 | |
| 792 | if (rc == NO_ERROR) |
| 793 | os2_formatmsg(msgbuf, msglen, reason); |
| 794 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 795 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 796 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 797 | |
| 798 | return msgbuf; |
| 799 | } |
| 800 | |
| 801 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 802 | errors are not in a global variable e.g. 'errno' nor are |
| 803 | they congruent with posix error numbers. */ |
| 804 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 805 | static PyObject * |
| 806 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 807 | { |
| 808 | char text[1024]; |
| 809 | PyObject *v; |
| 810 | |
| 811 | os2_strerror(text, sizeof(text), code, ""); |
| 812 | |
| 813 | v = Py_BuildValue("(is)", code, text); |
| 814 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 815 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 816 | Py_DECREF(v); |
| 817 | } |
| 818 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 819 | } |
| 820 | |
| 821 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 822 | |
| 823 | /* POSIX generic methods */ |
| 824 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 825 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 826 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 827 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 828 | int fd; |
| 829 | int res; |
| 830 | fd = PyObject_AsFileDescriptor(fdobj); |
| 831 | if (fd < 0) |
| 832 | return NULL; |
| 833 | if (!_PyVerify_fd(fd)) |
| 834 | return posix_error(); |
| 835 | Py_BEGIN_ALLOW_THREADS |
| 836 | res = (*func)(fd); |
| 837 | Py_END_ALLOW_THREADS |
| 838 | if (res < 0) |
| 839 | return posix_error(); |
| 840 | Py_INCREF(Py_None); |
| 841 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 842 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 843 | |
| 844 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 845 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 846 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 847 | PyObject *opath1 = NULL; |
| 848 | char *path1; |
| 849 | int res; |
| 850 | if (!PyArg_ParseTuple(args, format, |
| 851 | PyUnicode_FSConverter, &opath1)) |
| 852 | return NULL; |
| 853 | path1 = PyBytes_AsString(opath1); |
| 854 | Py_BEGIN_ALLOW_THREADS |
| 855 | res = (*func)(path1); |
| 856 | Py_END_ALLOW_THREADS |
| 857 | if (res < 0) |
| 858 | return posix_error_with_allocated_filename(opath1); |
| 859 | Py_DECREF(opath1); |
| 860 | Py_INCREF(Py_None); |
| 861 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 864 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 865 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 866 | char *format, |
| 867 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 868 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 869 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 870 | char *path1, *path2; |
| 871 | int res; |
| 872 | if (!PyArg_ParseTuple(args, format, |
| 873 | PyUnicode_FSConverter, &opath1, |
| 874 | PyUnicode_FSConverter, &opath2)) { |
| 875 | return NULL; |
| 876 | } |
| 877 | path1 = PyBytes_AsString(opath1); |
| 878 | path2 = PyBytes_AsString(opath2); |
| 879 | Py_BEGIN_ALLOW_THREADS |
| 880 | res = (*func)(path1, path2); |
| 881 | Py_END_ALLOW_THREADS |
| 882 | Py_DECREF(opath1); |
| 883 | Py_DECREF(opath2); |
| 884 | if (res != 0) |
| 885 | /* XXX how to report both path1 and path2??? */ |
| 886 | return posix_error(); |
| 887 | Py_INCREF(Py_None); |
| 888 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 891 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 892 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 893 | win32_1str(PyObject* args, char* func, |
| 894 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 895 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 896 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 897 | PyObject *uni; |
| 898 | char *ansi; |
| 899 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 900 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 901 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 902 | PyErr_Clear(); |
| 903 | else { |
| 904 | Py_BEGIN_ALLOW_THREADS |
| 905 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 906 | Py_END_ALLOW_THREADS |
| 907 | if (!result) |
| 908 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 909 | Py_INCREF(Py_None); |
| 910 | return Py_None; |
| 911 | } |
| 912 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 913 | return NULL; |
| 914 | Py_BEGIN_ALLOW_THREADS |
| 915 | result = funcA(ansi); |
| 916 | Py_END_ALLOW_THREADS |
| 917 | if (!result) |
| 918 | return win32_error(func, ansi); |
| 919 | Py_INCREF(Py_None); |
| 920 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 921 | |
| 922 | } |
| 923 | |
| 924 | /* This is a reimplementation of the C library's chdir function, |
| 925 | but one that produces Win32 errors instead of DOS error codes. |
| 926 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 927 | it also needs to set "magic" environment variables indicating |
| 928 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 929 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 930 | win32_chdir(LPCSTR path) |
| 931 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 932 | char new_path[MAX_PATH+1]; |
| 933 | int result; |
| 934 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 936 | if(!SetCurrentDirectoryA(path)) |
| 937 | return FALSE; |
| 938 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 939 | if (!result) |
| 940 | return FALSE; |
| 941 | /* In the ANSI API, there should not be any paths longer |
| 942 | than MAX_PATH. */ |
| 943 | assert(result <= MAX_PATH+1); |
| 944 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 945 | strncmp(new_path, "//", 2) == 0) |
| 946 | /* UNC path, nothing to do. */ |
| 947 | return TRUE; |
| 948 | env[1] = new_path[0]; |
| 949 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* The Unicode version differs from the ANSI version |
| 953 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 954 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 955 | win32_wchdir(LPCWSTR path) |
| 956 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 957 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 958 | int result; |
| 959 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 960 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 961 | if(!SetCurrentDirectoryW(path)) |
| 962 | return FALSE; |
| 963 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 964 | if (!result) |
| 965 | return FALSE; |
| 966 | if (result > MAX_PATH+1) { |
| 967 | new_path = malloc(result * sizeof(wchar_t)); |
| 968 | if (!new_path) { |
| 969 | SetLastError(ERROR_OUTOFMEMORY); |
| 970 | return FALSE; |
| 971 | } |
| 972 | result = GetCurrentDirectoryW(result, new_path); |
| 973 | if (!result) { |
| 974 | free(new_path); |
| 975 | return FALSE; |
| 976 | } |
| 977 | } |
| 978 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 979 | wcsncmp(new_path, L"//", 2) == 0) |
| 980 | /* UNC path, nothing to do. */ |
| 981 | return TRUE; |
| 982 | env[1] = new_path[0]; |
| 983 | result = SetEnvironmentVariableW(env, new_path); |
| 984 | if (new_path != _new_path) |
| 985 | free(new_path); |
| 986 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 987 | } |
| 988 | #endif |
| 989 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 990 | #ifdef MS_WINDOWS |
| 991 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 992 | - time stamps are restricted to second resolution |
| 993 | - file modification times suffer from forth-and-back conversions between |
| 994 | UTC and local time |
| 995 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 996 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 997 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 998 | |
| 999 | struct win32_stat{ |
| 1000 | int st_dev; |
| 1001 | __int64 st_ino; |
| 1002 | unsigned short st_mode; |
| 1003 | int st_nlink; |
| 1004 | int st_uid; |
| 1005 | int st_gid; |
| 1006 | int st_rdev; |
| 1007 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1008 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1009 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1010 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1011 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1012 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1013 | int st_ctime_nsec; |
| 1014 | }; |
| 1015 | |
| 1016 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1017 | |
| 1018 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1019 | 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] | 1020 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1021 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1022 | /* Cannot simply cast and dereference in_ptr, |
| 1023 | since it might not be aligned properly */ |
| 1024 | __int64 in; |
| 1025 | memcpy(&in, in_ptr, sizeof(in)); |
| 1026 | *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] | 1027 | *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] | 1028 | } |
| 1029 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1030 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1031 | 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] | 1032 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1033 | /* XXX endianness */ |
| 1034 | __int64 out; |
| 1035 | out = time_in + secs_between_epochs; |
| 1036 | out = out * 10000000 + nsec_in / 100; |
| 1037 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1040 | /* Below, we *know* that ugo+r is 0444 */ |
| 1041 | #if _S_IREAD != 0400 |
| 1042 | #error Unsupported C library |
| 1043 | #endif |
| 1044 | static int |
| 1045 | attributes_to_mode(DWORD attr) |
| 1046 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1047 | int m = 0; |
| 1048 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1049 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1050 | else |
| 1051 | m |= _S_IFREG; |
| 1052 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1053 | m |= 0444; |
| 1054 | else |
| 1055 | m |= 0666; |
| 1056 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1060 | 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] | 1061 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1062 | memset(result, 0, sizeof(*result)); |
| 1063 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1064 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1065 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1066 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1067 | 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] | 1068 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1069 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1070 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1071 | /* first clear the S_IFMT bits */ |
| 1072 | result->st_mode ^= (result->st_mode & 0170000); |
| 1073 | /* now set the bits that make this a symlink */ |
| 1074 | result->st_mode |= 0120000; |
| 1075 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1076 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1077 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1080 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1081 | 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] | 1082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1083 | HANDLE hFindFile; |
| 1084 | WIN32_FIND_DATAA FileData; |
| 1085 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1086 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1087 | return FALSE; |
| 1088 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1089 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1090 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1091 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1092 | info->ftCreationTime = FileData.ftCreationTime; |
| 1093 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1094 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1095 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1096 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1097 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1098 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1099 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1100 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1104 | 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] | 1105 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1106 | HANDLE hFindFile; |
| 1107 | WIN32_FIND_DATAW FileData; |
| 1108 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1109 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1110 | return FALSE; |
| 1111 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1112 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1113 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1114 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1115 | info->ftCreationTime = FileData.ftCreationTime; |
| 1116 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1117 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1118 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1119 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1120 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1121 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1122 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1123 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1126 | #ifndef SYMLOOP_MAX |
| 1127 | #define SYMLOOP_MAX ( 88 ) |
| 1128 | #endif |
| 1129 | |
| 1130 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1131 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1132 | |
| 1133 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1134 | win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1135 | { |
| 1136 | int code; |
| 1137 | HANDLE hFile; |
| 1138 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1139 | ULONG reparse_tag = 0; |
Hirokazu Yamamoto | 7467351 | 2010-12-05 02:48:08 +0000 | [diff] [blame] | 1140 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1141 | const char *dot; |
| 1142 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1143 | if (depth > SYMLOOP_MAX) { |
| 1144 | SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */ |
| 1145 | return -1; |
| 1146 | } |
| 1147 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1148 | hFile = CreateFileA( |
| 1149 | path, |
| 1150 | 0, /* desired access */ |
| 1151 | 0, /* share mode */ |
| 1152 | NULL, /* security attributes */ |
| 1153 | OPEN_EXISTING, |
| 1154 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 1155 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT, |
| 1156 | NULL); |
| 1157 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1158 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1159 | /* Either the target doesn't exist, or we don't have access to |
| 1160 | get a handle to it. If the former, we need to return an error. |
| 1161 | If the latter, we can use attributes_from_dir. */ |
| 1162 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1163 | return -1; |
| 1164 | /* Could not get attributes on open file. Fall back to |
| 1165 | reading the directory. */ |
| 1166 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1167 | /* Very strange. This should not fail now */ |
| 1168 | return -1; |
| 1169 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1170 | if (traverse) { |
| 1171 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1172 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1173 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1174 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1175 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1176 | } else { |
| 1177 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1178 | CloseHandle(hFile); |
| 1179 | return -1;; |
| 1180 | } |
| 1181 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1182 | code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL); |
| 1183 | CloseHandle(hFile); |
| 1184 | if (code < 0) |
| 1185 | return code; |
| 1186 | if (traverse) { |
| 1187 | code = win32_xstat_impl_w(target_path, result, traverse, depth + 1); |
| 1188 | free(target_path); |
| 1189 | return code; |
| 1190 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1191 | } else |
| 1192 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1193 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1194 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1195 | |
| 1196 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1197 | dot = strrchr(path, '.'); |
| 1198 | if (dot) { |
| 1199 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1200 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1201 | result->st_mode |= 0111; |
| 1202 | } |
| 1203 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1207 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1208 | { |
| 1209 | int code; |
| 1210 | HANDLE hFile; |
| 1211 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1212 | ULONG reparse_tag = 0; |
| 1213 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1214 | const wchar_t *dot; |
| 1215 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1216 | if (depth > SYMLOOP_MAX) { |
| 1217 | SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */ |
| 1218 | return -1; |
| 1219 | } |
| 1220 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1221 | hFile = CreateFileW( |
| 1222 | path, |
| 1223 | 0, /* desired access */ |
| 1224 | 0, /* share mode */ |
| 1225 | NULL, /* security attributes */ |
| 1226 | OPEN_EXISTING, |
| 1227 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 1228 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT, |
| 1229 | NULL); |
| 1230 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1231 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1232 | /* Either the target doesn't exist, or we don't have access to |
| 1233 | get a handle to it. If the former, we need to return an error. |
| 1234 | If the latter, we can use attributes_from_dir. */ |
| 1235 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1236 | return -1; |
| 1237 | /* Could not get attributes on open file. Fall back to |
| 1238 | reading the directory. */ |
| 1239 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1240 | /* Very strange. This should not fail now */ |
| 1241 | return -1; |
| 1242 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1243 | if (traverse) { |
| 1244 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1245 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1246 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1247 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1248 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1249 | } else { |
| 1250 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1251 | CloseHandle(hFile); |
| 1252 | return -1;; |
| 1253 | } |
| 1254 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1255 | code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL); |
| 1256 | CloseHandle(hFile); |
| 1257 | if (code < 0) |
| 1258 | return code; |
| 1259 | if (traverse) { |
| 1260 | code = win32_xstat_impl_w(target_path, result, traverse, depth + 1); |
| 1261 | free(target_path); |
| 1262 | return code; |
| 1263 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1264 | } else |
| 1265 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1266 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1267 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1268 | |
| 1269 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1270 | dot = wcsrchr(path, '.'); |
| 1271 | if (dot) { |
| 1272 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1273 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1274 | result->st_mode |= 0111; |
| 1275 | } |
| 1276 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1280 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1281 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1282 | /* Protocol violation: we explicitly clear errno, instead of |
| 1283 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1284 | int code = win32_xstat_impl(path, result, traverse, 0); |
| 1285 | errno = 0; |
| 1286 | return code; |
| 1287 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1288 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1289 | static int |
| 1290 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1291 | { |
| 1292 | /* Protocol violation: we explicitly clear errno, instead of |
| 1293 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1294 | int code = win32_xstat_impl_w(path, result, traverse, 0); |
| 1295 | errno = 0; |
| 1296 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1299 | /* About the following functions: win32_lstat, win32_lstat_w, win32_stat, |
| 1300 | win32_stat_w |
| 1301 | |
| 1302 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1303 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1304 | default does not traverse symlinks and instead returns attributes for |
| 1305 | the symlink. |
| 1306 | |
| 1307 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1308 | win32_stat will first explicitly resolve the symlink target and then will |
| 1309 | call win32_lstat on that result. |
| 1310 | |
| 1311 | The _w represent Unicode equivalents of the aformentioned ANSI functions. */ |
| 1312 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1313 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1314 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1315 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1316 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1319 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1320 | 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] | 1321 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1322 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | static int |
| 1326 | win32_stat(const char* path, struct win32_stat *result) |
| 1327 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1328 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1331 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1332 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1333 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1334 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static int |
| 1338 | win32_fstat(int file_number, struct win32_stat *result) |
| 1339 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1340 | BY_HANDLE_FILE_INFORMATION info; |
| 1341 | HANDLE h; |
| 1342 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1343 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1344 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1345 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1346 | /* Protocol violation: we explicitly clear errno, instead of |
| 1347 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1348 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1349 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1350 | if (h == INVALID_HANDLE_VALUE) { |
| 1351 | /* This is really a C library error (invalid file handle). |
| 1352 | We set the Win32 error to the closes one matching. */ |
| 1353 | SetLastError(ERROR_INVALID_HANDLE); |
| 1354 | return -1; |
| 1355 | } |
| 1356 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1358 | type = GetFileType(h); |
| 1359 | if (type == FILE_TYPE_UNKNOWN) { |
| 1360 | DWORD error = GetLastError(); |
| 1361 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1362 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1363 | } |
| 1364 | /* else: valid but unknown file */ |
| 1365 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1367 | if (type != FILE_TYPE_DISK) { |
| 1368 | if (type == FILE_TYPE_CHAR) |
| 1369 | result->st_mode = _S_IFCHR; |
| 1370 | else if (type == FILE_TYPE_PIPE) |
| 1371 | result->st_mode = _S_IFIFO; |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | if (!GetFileInformationByHandle(h, &info)) { |
| 1376 | return -1; |
| 1377 | } |
| 1378 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1379 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1380 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1381 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1382 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | #endif /* MS_WINDOWS */ |
| 1386 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1387 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1388 | "stat_result: Result from stat or lstat.\n\n\ |
| 1389 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1390 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1391 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1392 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1393 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1394 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1395 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1396 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1397 | |
| 1398 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1399 | {"st_mode", "protection bits"}, |
| 1400 | {"st_ino", "inode"}, |
| 1401 | {"st_dev", "device"}, |
| 1402 | {"st_nlink", "number of hard links"}, |
| 1403 | {"st_uid", "user ID of owner"}, |
| 1404 | {"st_gid", "group ID of owner"}, |
| 1405 | {"st_size", "total size, in bytes"}, |
| 1406 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1407 | {NULL, "integer time of last access"}, |
| 1408 | {NULL, "integer time of last modification"}, |
| 1409 | {NULL, "integer time of last change"}, |
| 1410 | {"st_atime", "time of last access"}, |
| 1411 | {"st_mtime", "time of last modification"}, |
| 1412 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1413 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1414 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1415 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1416 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1417 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1418 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1419 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1420 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1421 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1422 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1423 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1424 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1425 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1426 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1427 | #endif |
| 1428 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1429 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1430 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1431 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1432 | }; |
| 1433 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1434 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1435 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1436 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1437 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1438 | #endif |
| 1439 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1440 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1441 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1442 | #else |
| 1443 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1444 | #endif |
| 1445 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1446 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1447 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1448 | #else |
| 1449 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1450 | #endif |
| 1451 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1452 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1453 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1454 | #else |
| 1455 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1456 | #endif |
| 1457 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1458 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1459 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1460 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1461 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1462 | #endif |
| 1463 | |
| 1464 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1465 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1466 | #else |
| 1467 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1468 | #endif |
| 1469 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1470 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1471 | "stat_result", /* name */ |
| 1472 | stat_result__doc__, /* doc */ |
| 1473 | stat_result_fields, |
| 1474 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1475 | }; |
| 1476 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1477 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1478 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1479 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1480 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1481 | 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] | 1482 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1483 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1484 | |
| 1485 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1486 | {"f_bsize", }, |
| 1487 | {"f_frsize", }, |
| 1488 | {"f_blocks", }, |
| 1489 | {"f_bfree", }, |
| 1490 | {"f_bavail", }, |
| 1491 | {"f_files", }, |
| 1492 | {"f_ffree", }, |
| 1493 | {"f_favail", }, |
| 1494 | {"f_flag", }, |
| 1495 | {"f_namemax",}, |
| 1496 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1497 | }; |
| 1498 | |
| 1499 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1500 | "statvfs_result", /* name */ |
| 1501 | statvfs_result__doc__, /* doc */ |
| 1502 | statvfs_result_fields, |
| 1503 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1504 | }; |
| 1505 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1506 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1507 | static PyTypeObject StatResultType; |
| 1508 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1509 | static newfunc structseq_new; |
| 1510 | |
| 1511 | static PyObject * |
| 1512 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1513 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1514 | PyStructSequence *result; |
| 1515 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1517 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1518 | if (!result) |
| 1519 | return NULL; |
| 1520 | /* If we have been initialized from a tuple, |
| 1521 | st_?time might be set to None. Initialize it |
| 1522 | from the int slots. */ |
| 1523 | for (i = 7; i <= 9; i++) { |
| 1524 | if (result->ob_item[i+3] == Py_None) { |
| 1525 | Py_DECREF(Py_None); |
| 1526 | Py_INCREF(result->ob_item[i]); |
| 1527 | result->ob_item[i+3] = result->ob_item[i]; |
| 1528 | } |
| 1529 | } |
| 1530 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1536 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1537 | |
| 1538 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1539 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1540 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1541 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1542 | future calls return ints. \n\ |
| 1543 | If newval is omitted, return the current setting.\n"); |
| 1544 | |
| 1545 | static PyObject* |
| 1546 | stat_float_times(PyObject* self, PyObject *args) |
| 1547 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1548 | int newval = -1; |
| 1549 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1550 | return NULL; |
| 1551 | if (newval == -1) |
| 1552 | /* Return old value */ |
| 1553 | return PyBool_FromLong(_stat_float_times); |
| 1554 | _stat_float_times = newval; |
| 1555 | Py_INCREF(Py_None); |
| 1556 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1557 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1558 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1559 | static void |
| 1560 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1561 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1562 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1563 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1564 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1565 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1566 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1567 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1568 | if (!ival) |
| 1569 | return; |
| 1570 | if (_stat_float_times) { |
| 1571 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1572 | } else { |
| 1573 | fval = ival; |
| 1574 | Py_INCREF(fval); |
| 1575 | } |
| 1576 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1577 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1580 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1581 | (used by posix_stat() and posix_fstat()) */ |
| 1582 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1583 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1584 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1585 | unsigned long ansec, mnsec, cnsec; |
| 1586 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1587 | if (v == NULL) |
| 1588 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1590 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1591 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1592 | PyStructSequence_SET_ITEM(v, 1, |
| 1593 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1594 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1595 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1596 | #endif |
| 1597 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1598 | PyStructSequence_SET_ITEM(v, 2, |
| 1599 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1600 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1601 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1602 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1603 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1604 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1605 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1606 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1607 | PyStructSequence_SET_ITEM(v, 6, |
| 1608 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1609 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1610 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1611 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1612 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1613 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1614 | ansec = st->st_atim.tv_nsec; |
| 1615 | mnsec = st->st_mtim.tv_nsec; |
| 1616 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1617 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1618 | ansec = st->st_atimespec.tv_nsec; |
| 1619 | mnsec = st->st_mtimespec.tv_nsec; |
| 1620 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1621 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1622 | ansec = st->st_atime_nsec; |
| 1623 | mnsec = st->st_mtime_nsec; |
| 1624 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1625 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1626 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1627 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1628 | fill_time(v, 7, st->st_atime, ansec); |
| 1629 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1630 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1631 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1632 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1633 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1634 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1635 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1636 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1637 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1638 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1639 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1640 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1641 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1642 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1643 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1644 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1645 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1646 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1647 | #endif |
| 1648 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1649 | { |
| 1650 | PyObject *val; |
| 1651 | unsigned long bsec,bnsec; |
| 1652 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1653 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1654 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1655 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1656 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1657 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1658 | if (_stat_float_times) { |
| 1659 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1660 | } else { |
| 1661 | val = PyLong_FromLong((long)bsec); |
| 1662 | } |
| 1663 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1664 | val); |
| 1665 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1666 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1667 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1668 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1669 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1670 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1671 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1672 | if (PyErr_Occurred()) { |
| 1673 | Py_DECREF(v); |
| 1674 | return NULL; |
| 1675 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1676 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1677 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1680 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1681 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1682 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1683 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1684 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1685 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1686 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1687 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1688 | char *wformat, |
| 1689 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1691 | STRUCT_STAT st; |
| 1692 | PyObject *opath; |
| 1693 | char *path; |
| 1694 | int res; |
| 1695 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1696 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1697 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1698 | PyUnicodeObject *po; |
| 1699 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1700 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1701 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1702 | Py_BEGIN_ALLOW_THREADS |
| 1703 | /* PyUnicode_AS_UNICODE result OK without |
| 1704 | thread lock as it is a simple dereference. */ |
| 1705 | res = wstatfunc(wpath, &st); |
| 1706 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1707 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1708 | if (res != 0) |
| 1709 | return win32_error_unicode("stat", wpath); |
| 1710 | return _pystat_fromstructstat(&st); |
| 1711 | } |
| 1712 | /* Drop the argument parsing error as narrow strings |
| 1713 | are also valid. */ |
| 1714 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1715 | #endif |
| 1716 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1717 | if (!PyArg_ParseTuple(args, format, |
| 1718 | PyUnicode_FSConverter, &opath)) |
| 1719 | return NULL; |
| 1720 | path = PyBytes_AsString(opath); |
| 1721 | Py_BEGIN_ALLOW_THREADS |
| 1722 | res = (*statfunc)(path, &st); |
| 1723 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1724 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1725 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1726 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1727 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1728 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1729 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1730 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1731 | } |
| 1732 | else |
| 1733 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1734 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1735 | Py_DECREF(opath); |
| 1736 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1739 | /* POSIX methods */ |
| 1740 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1741 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1742 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1743 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1744 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1745 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1746 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1747 | 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] | 1748 | |
| 1749 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1750 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1751 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1752 | PyObject *opath; |
| 1753 | char *path; |
| 1754 | int mode; |
| 1755 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1756 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1757 | DWORD attr; |
| 1758 | PyUnicodeObject *po; |
| 1759 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1760 | Py_BEGIN_ALLOW_THREADS |
| 1761 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1762 | it is a simple dereference. */ |
| 1763 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1764 | Py_END_ALLOW_THREADS |
| 1765 | goto finish; |
| 1766 | } |
| 1767 | /* Drop the argument parsing error as narrow strings |
| 1768 | are also valid. */ |
| 1769 | PyErr_Clear(); |
| 1770 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1771 | PyUnicode_FSConverter, &opath, &mode)) |
| 1772 | return NULL; |
| 1773 | path = PyBytes_AsString(opath); |
| 1774 | Py_BEGIN_ALLOW_THREADS |
| 1775 | attr = GetFileAttributesA(path); |
| 1776 | Py_END_ALLOW_THREADS |
| 1777 | Py_DECREF(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1778 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1779 | if (attr == 0xFFFFFFFF) |
| 1780 | /* File does not exist, or cannot read attributes */ |
| 1781 | return PyBool_FromLong(0); |
| 1782 | /* Access is possible if either write access wasn't requested, or |
| 1783 | the file isn't read-only, or if it's a directory, as there are |
| 1784 | no read-only directories on Windows. */ |
| 1785 | return PyBool_FromLong(!(mode & 2) |
| 1786 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1787 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1788 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1789 | int res; |
| 1790 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1791 | PyUnicode_FSConverter, &opath, &mode)) |
| 1792 | return NULL; |
| 1793 | path = PyBytes_AsString(opath); |
| 1794 | Py_BEGIN_ALLOW_THREADS |
| 1795 | res = access(path, mode); |
| 1796 | Py_END_ALLOW_THREADS |
| 1797 | Py_DECREF(opath); |
| 1798 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1799 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1802 | #ifndef F_OK |
| 1803 | #define F_OK 0 |
| 1804 | #endif |
| 1805 | #ifndef R_OK |
| 1806 | #define R_OK 4 |
| 1807 | #endif |
| 1808 | #ifndef W_OK |
| 1809 | #define W_OK 2 |
| 1810 | #endif |
| 1811 | #ifndef X_OK |
| 1812 | #define X_OK 1 |
| 1813 | #endif |
| 1814 | |
| 1815 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1816 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1817 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1818 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1819 | |
| 1820 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1821 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1822 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1823 | int id; |
| 1824 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1825 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1826 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1827 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1828 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1829 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1830 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1831 | if (id == 0) { |
| 1832 | ret = ttyname(); |
| 1833 | } |
| 1834 | else { |
| 1835 | ret = NULL; |
| 1836 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1837 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1838 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1839 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1840 | if (ret == NULL) |
| 1841 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1842 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1843 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1844 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1845 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1846 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1847 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1848 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1849 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1850 | |
| 1851 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1852 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1853 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1854 | char *ret; |
| 1855 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1856 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1857 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1858 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1859 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1860 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1861 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1862 | if (ret == NULL) |
| 1863 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1864 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1865 | } |
| 1866 | #endif |
| 1867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1868 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1869 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1870 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1871 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1872 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1873 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1874 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1875 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1876 | 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] | 1877 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1878 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1879 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1880 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1881 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1882 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1883 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1886 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1887 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1888 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1889 | 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] | 1890 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1891 | |
| 1892 | static PyObject * |
| 1893 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1894 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1895 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1896 | } |
| 1897 | #endif /* HAVE_FCHDIR */ |
| 1898 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1899 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1900 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1901 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1902 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1903 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1904 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1905 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1906 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1907 | PyObject *opath = NULL; |
| 1908 | char *path = NULL; |
| 1909 | int i; |
| 1910 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1911 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1912 | DWORD attr; |
| 1913 | PyUnicodeObject *po; |
| 1914 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1915 | Py_BEGIN_ALLOW_THREADS |
| 1916 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1917 | if (attr != 0xFFFFFFFF) { |
| 1918 | if (i & _S_IWRITE) |
| 1919 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1920 | else |
| 1921 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1922 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1923 | } |
| 1924 | else |
| 1925 | res = 0; |
| 1926 | Py_END_ALLOW_THREADS |
| 1927 | if (!res) |
| 1928 | return win32_error_unicode("chmod", |
| 1929 | PyUnicode_AS_UNICODE(po)); |
| 1930 | Py_INCREF(Py_None); |
| 1931 | return Py_None; |
| 1932 | } |
| 1933 | /* Drop the argument parsing error as narrow strings |
| 1934 | are also valid. */ |
| 1935 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1936 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1937 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1938 | &opath, &i)) |
| 1939 | return NULL; |
| 1940 | path = PyBytes_AsString(opath); |
| 1941 | Py_BEGIN_ALLOW_THREADS |
| 1942 | attr = GetFileAttributesA(path); |
| 1943 | if (attr != 0xFFFFFFFF) { |
| 1944 | if (i & _S_IWRITE) |
| 1945 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1946 | else |
| 1947 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1948 | res = SetFileAttributesA(path, attr); |
| 1949 | } |
| 1950 | else |
| 1951 | res = 0; |
| 1952 | Py_END_ALLOW_THREADS |
| 1953 | if (!res) { |
| 1954 | win32_error("chmod", path); |
| 1955 | Py_DECREF(opath); |
| 1956 | return NULL; |
| 1957 | } |
| 1958 | Py_DECREF(opath); |
| 1959 | Py_INCREF(Py_None); |
| 1960 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1961 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1963 | &opath, &i)) |
| 1964 | return NULL; |
| 1965 | path = PyBytes_AsString(opath); |
| 1966 | Py_BEGIN_ALLOW_THREADS |
| 1967 | res = chmod(path, i); |
| 1968 | Py_END_ALLOW_THREADS |
| 1969 | if (res < 0) |
| 1970 | return posix_error_with_allocated_filename(opath); |
| 1971 | Py_DECREF(opath); |
| 1972 | Py_INCREF(Py_None); |
| 1973 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1974 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1977 | #ifdef HAVE_FCHMOD |
| 1978 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1979 | "fchmod(fd, mode)\n\n\ |
| 1980 | Change the access permissions of the file given by file\n\ |
| 1981 | descriptor fd."); |
| 1982 | |
| 1983 | static PyObject * |
| 1984 | posix_fchmod(PyObject *self, PyObject *args) |
| 1985 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1986 | int fd, mode, res; |
| 1987 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1988 | return NULL; |
| 1989 | Py_BEGIN_ALLOW_THREADS |
| 1990 | res = fchmod(fd, mode); |
| 1991 | Py_END_ALLOW_THREADS |
| 1992 | if (res < 0) |
| 1993 | return posix_error(); |
| 1994 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1995 | } |
| 1996 | #endif /* HAVE_FCHMOD */ |
| 1997 | |
| 1998 | #ifdef HAVE_LCHMOD |
| 1999 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2000 | "lchmod(path, mode)\n\n\ |
| 2001 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2002 | affects the link itself rather than the target."); |
| 2003 | |
| 2004 | static PyObject * |
| 2005 | posix_lchmod(PyObject *self, PyObject *args) |
| 2006 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2007 | PyObject *opath; |
| 2008 | char *path; |
| 2009 | int i; |
| 2010 | int res; |
| 2011 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2012 | &opath, &i)) |
| 2013 | return NULL; |
| 2014 | path = PyBytes_AsString(opath); |
| 2015 | Py_BEGIN_ALLOW_THREADS |
| 2016 | res = lchmod(path, i); |
| 2017 | Py_END_ALLOW_THREADS |
| 2018 | if (res < 0) |
| 2019 | return posix_error_with_allocated_filename(opath); |
| 2020 | Py_DECREF(opath); |
| 2021 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2022 | } |
| 2023 | #endif /* HAVE_LCHMOD */ |
| 2024 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2025 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2026 | #ifdef HAVE_CHFLAGS |
| 2027 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2028 | "chflags(path, flags)\n\n\ |
| 2029 | Set file flags."); |
| 2030 | |
| 2031 | static PyObject * |
| 2032 | posix_chflags(PyObject *self, PyObject *args) |
| 2033 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2034 | PyObject *opath; |
| 2035 | char *path; |
| 2036 | unsigned long flags; |
| 2037 | int res; |
| 2038 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2039 | PyUnicode_FSConverter, &opath, &flags)) |
| 2040 | return NULL; |
| 2041 | path = PyBytes_AsString(opath); |
| 2042 | Py_BEGIN_ALLOW_THREADS |
| 2043 | res = chflags(path, flags); |
| 2044 | Py_END_ALLOW_THREADS |
| 2045 | if (res < 0) |
| 2046 | return posix_error_with_allocated_filename(opath); |
| 2047 | Py_DECREF(opath); |
| 2048 | Py_INCREF(Py_None); |
| 2049 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2050 | } |
| 2051 | #endif /* HAVE_CHFLAGS */ |
| 2052 | |
| 2053 | #ifdef HAVE_LCHFLAGS |
| 2054 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2055 | "lchflags(path, flags)\n\n\ |
| 2056 | Set file flags.\n\ |
| 2057 | This function will not follow symbolic links."); |
| 2058 | |
| 2059 | static PyObject * |
| 2060 | posix_lchflags(PyObject *self, PyObject *args) |
| 2061 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2062 | PyObject *opath; |
| 2063 | char *path; |
| 2064 | unsigned long flags; |
| 2065 | int res; |
| 2066 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2067 | PyUnicode_FSConverter, &opath, &flags)) |
| 2068 | return NULL; |
| 2069 | path = PyBytes_AsString(opath); |
| 2070 | Py_BEGIN_ALLOW_THREADS |
| 2071 | res = lchflags(path, flags); |
| 2072 | Py_END_ALLOW_THREADS |
| 2073 | if (res < 0) |
| 2074 | return posix_error_with_allocated_filename(opath); |
| 2075 | Py_DECREF(opath); |
| 2076 | Py_INCREF(Py_None); |
| 2077 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2078 | } |
| 2079 | #endif /* HAVE_LCHFLAGS */ |
| 2080 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2081 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2082 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2083 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2084 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2085 | |
| 2086 | static PyObject * |
| 2087 | posix_chroot(PyObject *self, PyObject *args) |
| 2088 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2089 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2090 | } |
| 2091 | #endif |
| 2092 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2093 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2094 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2095 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2096 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2097 | |
| 2098 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2099 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2100 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2101 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2102 | } |
| 2103 | #endif /* HAVE_FSYNC */ |
| 2104 | |
| 2105 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2106 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2107 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2108 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2109 | #endif |
| 2110 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2111 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2112 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2113 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2114 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2115 | |
| 2116 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2117 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2118 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2119 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2120 | } |
| 2121 | #endif /* HAVE_FDATASYNC */ |
| 2122 | |
| 2123 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2124 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2125 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2126 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2127 | 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] | 2128 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2129 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2130 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2131 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2132 | PyObject *opath; |
| 2133 | char *path; |
| 2134 | long uid, gid; |
| 2135 | int res; |
| 2136 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2137 | PyUnicode_FSConverter, &opath, |
| 2138 | &uid, &gid)) |
| 2139 | return NULL; |
| 2140 | path = PyBytes_AsString(opath); |
| 2141 | Py_BEGIN_ALLOW_THREADS |
| 2142 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2143 | Py_END_ALLOW_THREADS |
| 2144 | if (res < 0) |
| 2145 | return posix_error_with_allocated_filename(opath); |
| 2146 | Py_DECREF(opath); |
| 2147 | Py_INCREF(Py_None); |
| 2148 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2149 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2150 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2151 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2152 | #ifdef HAVE_FCHOWN |
| 2153 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2154 | "fchown(fd, uid, gid)\n\n\ |
| 2155 | Change the owner and group id of the file given by file descriptor\n\ |
| 2156 | fd to the numeric uid and gid."); |
| 2157 | |
| 2158 | static PyObject * |
| 2159 | posix_fchown(PyObject *self, PyObject *args) |
| 2160 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2161 | int fd; |
| 2162 | long uid, gid; |
| 2163 | int res; |
| 2164 | if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) |
| 2165 | return NULL; |
| 2166 | Py_BEGIN_ALLOW_THREADS |
| 2167 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2168 | Py_END_ALLOW_THREADS |
| 2169 | if (res < 0) |
| 2170 | return posix_error(); |
| 2171 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2172 | } |
| 2173 | #endif /* HAVE_FCHOWN */ |
| 2174 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2175 | #ifdef HAVE_LCHOWN |
| 2176 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2177 | "lchown(path, uid, gid)\n\n\ |
| 2178 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2179 | This function will not follow symbolic links."); |
| 2180 | |
| 2181 | static PyObject * |
| 2182 | posix_lchown(PyObject *self, PyObject *args) |
| 2183 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2184 | PyObject *opath; |
| 2185 | char *path; |
| 2186 | long uid, gid; |
| 2187 | int res; |
| 2188 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2189 | PyUnicode_FSConverter, &opath, |
| 2190 | &uid, &gid)) |
| 2191 | return NULL; |
| 2192 | path = PyBytes_AsString(opath); |
| 2193 | Py_BEGIN_ALLOW_THREADS |
| 2194 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2195 | Py_END_ALLOW_THREADS |
| 2196 | if (res < 0) |
| 2197 | return posix_error_with_allocated_filename(opath); |
| 2198 | Py_DECREF(opath); |
| 2199 | Py_INCREF(Py_None); |
| 2200 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2201 | } |
| 2202 | #endif /* HAVE_LCHOWN */ |
| 2203 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2204 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2205 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2206 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2207 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2208 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2209 | char buf[1026]; |
| 2210 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2211 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2212 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2213 | if (!use_bytes) { |
| 2214 | wchar_t wbuf[1026]; |
| 2215 | wchar_t *wbuf2 = wbuf; |
| 2216 | PyObject *resobj; |
| 2217 | DWORD len; |
| 2218 | Py_BEGIN_ALLOW_THREADS |
| 2219 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2220 | /* If the buffer is large enough, len does not include the |
| 2221 | terminating \0. If the buffer is too small, len includes |
| 2222 | the space needed for the terminator. */ |
| 2223 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2224 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2225 | if (wbuf2) |
| 2226 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2227 | } |
| 2228 | Py_END_ALLOW_THREADS |
| 2229 | if (!wbuf2) { |
| 2230 | PyErr_NoMemory(); |
| 2231 | return NULL; |
| 2232 | } |
| 2233 | if (!len) { |
| 2234 | if (wbuf2 != wbuf) free(wbuf2); |
| 2235 | return win32_error("getcwdu", NULL); |
| 2236 | } |
| 2237 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2238 | if (wbuf2 != wbuf) free(wbuf2); |
| 2239 | return resobj; |
| 2240 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2241 | #endif |
| 2242 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2243 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2244 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2245 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2246 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2247 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2248 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2249 | Py_END_ALLOW_THREADS |
| 2250 | if (res == NULL) |
| 2251 | return posix_error(); |
| 2252 | if (use_bytes) |
| 2253 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2254 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2255 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2256 | |
| 2257 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2258 | "getcwd() -> path\n\n\ |
| 2259 | Return a unicode string representing the current working directory."); |
| 2260 | |
| 2261 | static PyObject * |
| 2262 | posix_getcwd_unicode(PyObject *self) |
| 2263 | { |
| 2264 | return posix_getcwd(0); |
| 2265 | } |
| 2266 | |
| 2267 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2268 | "getcwdb() -> path\n\n\ |
| 2269 | Return a bytes string representing the current working directory."); |
| 2270 | |
| 2271 | static PyObject * |
| 2272 | posix_getcwd_bytes(PyObject *self) |
| 2273 | { |
| 2274 | return posix_getcwd(1); |
| 2275 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2276 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2277 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2278 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2279 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2280 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2281 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2282 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2284 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2285 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2286 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2287 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2288 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2289 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2290 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2291 | #ifdef MS_WINDOWS |
| 2292 | PyDoc_STRVAR(win32_link__doc__, |
| 2293 | "link(src, dst)\n\n\ |
| 2294 | Create a hard link to a file."); |
| 2295 | |
| 2296 | static PyObject * |
| 2297 | win32_link(PyObject *self, PyObject *args) |
| 2298 | { |
| 2299 | PyObject *osrc, *odst; |
| 2300 | char *src, *dst; |
| 2301 | BOOL rslt; |
| 2302 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2303 | PyUnicodeObject *usrc, *udst; |
| 2304 | if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) { |
| 2305 | Py_BEGIN_ALLOW_THREADS |
| 2306 | rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst), |
| 2307 | PyUnicode_AS_UNICODE(usrc), NULL); |
| 2308 | Py_END_ALLOW_THREADS |
| 2309 | |
| 2310 | if (rslt == 0) |
| 2311 | return win32_error("link", NULL); |
| 2312 | |
| 2313 | Py_RETURN_NONE; |
| 2314 | } |
| 2315 | |
| 2316 | /* Narrow strings also valid. */ |
| 2317 | PyErr_Clear(); |
| 2318 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2319 | if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, |
| 2320 | PyUnicode_FSConverter, &odst)) |
| 2321 | return NULL; |
| 2322 | |
| 2323 | src = PyBytes_AsString(osrc); |
| 2324 | dst = PyBytes_AsString(odst); |
| 2325 | |
| 2326 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2327 | rslt = CreateHardLinkA(dst, src, NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2328 | Py_END_ALLOW_THREADS |
| 2329 | |
Stefan Krah | 30b341f | 2010-11-27 11:44:18 +0000 | [diff] [blame] | 2330 | Py_DECREF(osrc); |
| 2331 | Py_DECREF(odst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2332 | if (rslt == 0) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2333 | return win32_error("link", NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2334 | |
| 2335 | Py_RETURN_NONE; |
| 2336 | } |
| 2337 | #endif /* MS_WINDOWS */ |
| 2338 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2339 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2340 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2341 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2342 | Return a list containing the names of the entries in the directory.\n\ |
| 2343 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2344 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2345 | \n\ |
| 2346 | 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] | 2347 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2348 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2349 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2350 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2351 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2352 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2353 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2354 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2355 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2356 | PyObject *d, *v; |
| 2357 | HANDLE hFindFile; |
| 2358 | BOOL result; |
| 2359 | WIN32_FIND_DATA FileData; |
| 2360 | PyObject *opath; |
| 2361 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2362 | char *bufptr = namebuf; |
| 2363 | 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] | 2364 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2365 | PyObject *po = NULL; |
| 2366 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2367 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2368 | Py_UNICODE *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2370 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2371 | po_wchars = L"."; |
| 2372 | len = 1; |
| 2373 | } else { |
| 2374 | po_wchars = PyUnicode_AS_UNICODE(po); |
| 2375 | len = PyUnicode_GET_SIZE(po); |
| 2376 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2377 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2378 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2379 | if (!wnamebuf) { |
| 2380 | PyErr_NoMemory(); |
| 2381 | return NULL; |
| 2382 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2383 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2384 | if (len > 0) { |
| 2385 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2386 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2387 | wnamebuf[len++] = L'\\'; |
| 2388 | wcscpy(wnamebuf + len, L"*.*"); |
| 2389 | } |
| 2390 | if ((d = PyList_New(0)) == NULL) { |
| 2391 | free(wnamebuf); |
| 2392 | return NULL; |
| 2393 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2394 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2395 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2396 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2397 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2398 | int error = GetLastError(); |
| 2399 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2400 | free(wnamebuf); |
| 2401 | return d; |
| 2402 | } |
| 2403 | Py_DECREF(d); |
| 2404 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2405 | free(wnamebuf); |
| 2406 | return NULL; |
| 2407 | } |
| 2408 | do { |
| 2409 | /* Skip over . and .. */ |
| 2410 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2411 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2412 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2413 | if (v == NULL) { |
| 2414 | Py_DECREF(d); |
| 2415 | d = NULL; |
| 2416 | break; |
| 2417 | } |
| 2418 | if (PyList_Append(d, v) != 0) { |
| 2419 | Py_DECREF(v); |
| 2420 | Py_DECREF(d); |
| 2421 | d = NULL; |
| 2422 | break; |
| 2423 | } |
| 2424 | Py_DECREF(v); |
| 2425 | } |
| 2426 | Py_BEGIN_ALLOW_THREADS |
| 2427 | result = FindNextFileW(hFindFile, &wFileData); |
| 2428 | Py_END_ALLOW_THREADS |
| 2429 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2430 | it got to the end of the directory. */ |
| 2431 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2432 | Py_DECREF(d); |
| 2433 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2434 | FindClose(hFindFile); |
| 2435 | free(wnamebuf); |
| 2436 | return NULL; |
| 2437 | } |
| 2438 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2440 | if (FindClose(hFindFile) == FALSE) { |
| 2441 | Py_DECREF(d); |
| 2442 | win32_error_unicode("FindClose", wnamebuf); |
| 2443 | free(wnamebuf); |
| 2444 | return NULL; |
| 2445 | } |
| 2446 | free(wnamebuf); |
| 2447 | return d; |
| 2448 | } |
| 2449 | /* Drop the argument parsing error as narrow strings |
| 2450 | are also valid. */ |
| 2451 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2452 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2453 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2454 | PyUnicode_FSConverter, &opath)) |
| 2455 | return NULL; |
| 2456 | if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { |
| 2457 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2458 | Py_DECREF(opath); |
| 2459 | return NULL; |
| 2460 | } |
| 2461 | strcpy(namebuf, PyBytes_AsString(opath)); |
| 2462 | len = PyObject_Size(opath); |
Stefan Krah | 2a7feee | 2010-11-27 22:06:49 +0000 | [diff] [blame] | 2463 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2464 | if (len > 0) { |
| 2465 | char ch = namebuf[len-1]; |
| 2466 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2467 | namebuf[len++] = '/'; |
| 2468 | strcpy(namebuf + len, "*.*"); |
| 2469 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2470 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2471 | if ((d = PyList_New(0)) == NULL) |
| 2472 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2473 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2474 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2475 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2476 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2477 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2478 | int error = GetLastError(); |
| 2479 | if (error == ERROR_FILE_NOT_FOUND) |
| 2480 | return d; |
| 2481 | Py_DECREF(d); |
| 2482 | return win32_error("FindFirstFile", namebuf); |
| 2483 | } |
| 2484 | do { |
| 2485 | /* Skip over . and .. */ |
| 2486 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2487 | strcmp(FileData.cFileName, "..") != 0) { |
| 2488 | v = PyBytes_FromString(FileData.cFileName); |
| 2489 | if (v == NULL) { |
| 2490 | Py_DECREF(d); |
| 2491 | d = NULL; |
| 2492 | break; |
| 2493 | } |
| 2494 | if (PyList_Append(d, v) != 0) { |
| 2495 | Py_DECREF(v); |
| 2496 | Py_DECREF(d); |
| 2497 | d = NULL; |
| 2498 | break; |
| 2499 | } |
| 2500 | Py_DECREF(v); |
| 2501 | } |
| 2502 | Py_BEGIN_ALLOW_THREADS |
| 2503 | result = FindNextFile(hFindFile, &FileData); |
| 2504 | Py_END_ALLOW_THREADS |
| 2505 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2506 | it got to the end of the directory. */ |
| 2507 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2508 | Py_DECREF(d); |
| 2509 | win32_error("FindNextFile", namebuf); |
| 2510 | FindClose(hFindFile); |
| 2511 | return NULL; |
| 2512 | } |
| 2513 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2514 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2515 | if (FindClose(hFindFile) == FALSE) { |
| 2516 | Py_DECREF(d); |
| 2517 | return win32_error("FindClose", namebuf); |
| 2518 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2519 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2520 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2521 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2522 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2523 | |
| 2524 | #ifndef MAX_PATH |
| 2525 | #define MAX_PATH CCHMAXPATH |
| 2526 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2527 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2528 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2529 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2530 | PyObject *d, *v; |
| 2531 | char namebuf[MAX_PATH+5]; |
| 2532 | HDIR hdir = 1; |
| 2533 | ULONG srchcnt = 1; |
| 2534 | FILEFINDBUF3 ep; |
| 2535 | APIRET rc; |
| 2536 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2537 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2538 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2539 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2540 | name = PyBytes_AsString(oname); |
| 2541 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2542 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2543 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2544 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2545 | return NULL; |
| 2546 | } |
| 2547 | strcpy(namebuf, name); |
| 2548 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2549 | if (*pt == ALTSEP) |
| 2550 | *pt = SEP; |
| 2551 | if (namebuf[len-1] != SEP) |
| 2552 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2553 | strcpy(namebuf + len, "*.*"); |
| 2554 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2555 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2556 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2557 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2558 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2559 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2560 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2561 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2562 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2563 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2564 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2565 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2566 | |
| 2567 | if (rc != NO_ERROR) { |
| 2568 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2569 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2572 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2573 | do { |
| 2574 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2575 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2576 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2577 | |
| 2578 | strcpy(namebuf, ep.achName); |
| 2579 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2580 | /* Leave Case of Name Alone -- In Native Form */ |
| 2581 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2582 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2583 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2584 | if (v == NULL) { |
| 2585 | Py_DECREF(d); |
| 2586 | d = NULL; |
| 2587 | break; |
| 2588 | } |
| 2589 | if (PyList_Append(d, v) != 0) { |
| 2590 | Py_DECREF(v); |
| 2591 | Py_DECREF(d); |
| 2592 | d = NULL; |
| 2593 | break; |
| 2594 | } |
| 2595 | Py_DECREF(v); |
| 2596 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2597 | } |
| 2598 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2599 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2600 | return d; |
| 2601 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2602 | PyObject *oname; |
| 2603 | char *name; |
| 2604 | PyObject *d, *v; |
| 2605 | DIR *dirp; |
| 2606 | struct dirent *ep; |
| 2607 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2608 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2609 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2610 | /* v is never read, so it does not need to be initialized yet. */ |
| 2611 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2612 | arg_is_unicode = 0; |
| 2613 | PyErr_Clear(); |
| 2614 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2615 | oname = NULL; |
| 2616 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2617 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2618 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2619 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2620 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2621 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2622 | Py_BEGIN_ALLOW_THREADS |
| 2623 | dirp = opendir(name); |
| 2624 | Py_END_ALLOW_THREADS |
| 2625 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2626 | return posix_error_with_allocated_filename(oname); |
| 2627 | } |
| 2628 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2629 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2630 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2631 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2632 | Py_DECREF(oname); |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | for (;;) { |
| 2636 | errno = 0; |
| 2637 | Py_BEGIN_ALLOW_THREADS |
| 2638 | ep = readdir(dirp); |
| 2639 | Py_END_ALLOW_THREADS |
| 2640 | if (ep == NULL) { |
| 2641 | if (errno == 0) { |
| 2642 | break; |
| 2643 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2644 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2645 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2646 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2647 | Py_DECREF(d); |
| 2648 | return posix_error_with_allocated_filename(oname); |
| 2649 | } |
| 2650 | } |
| 2651 | if (ep->d_name[0] == '.' && |
| 2652 | (NAMLEN(ep) == 1 || |
| 2653 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2654 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2655 | if (arg_is_unicode) |
| 2656 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2657 | else |
| 2658 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2659 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2660 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2661 | break; |
| 2662 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2663 | if (PyList_Append(d, v) != 0) { |
| 2664 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2665 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2666 | break; |
| 2667 | } |
| 2668 | Py_DECREF(v); |
| 2669 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2670 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2671 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2672 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2673 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2674 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2675 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2676 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2677 | #endif /* which OS */ |
| 2678 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2679 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2680 | #ifdef HAVE_FDOPENDIR |
| 2681 | PyDoc_STRVAR(posix_fdlistdir__doc__, |
| 2682 | "fdlistdir(fd) -> list_of_strings\n\n\ |
| 2683 | Like listdir(), but uses a file descriptor instead.\n\ |
| 2684 | After succesful execution of this function, fd will be closed."); |
| 2685 | |
| 2686 | static PyObject * |
| 2687 | posix_fdlistdir(PyObject *self, PyObject *args) |
| 2688 | { |
| 2689 | PyObject *d, *v; |
| 2690 | DIR *dirp; |
| 2691 | struct dirent *ep; |
| 2692 | int fd; |
| 2693 | |
| 2694 | errno = 0; |
| 2695 | if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd)) |
| 2696 | return NULL; |
| 2697 | Py_BEGIN_ALLOW_THREADS |
| 2698 | dirp = fdopendir(fd); |
| 2699 | Py_END_ALLOW_THREADS |
| 2700 | if (dirp == NULL) { |
| 2701 | close(fd); |
| 2702 | return posix_error(); |
| 2703 | } |
| 2704 | if ((d = PyList_New(0)) == NULL) { |
| 2705 | Py_BEGIN_ALLOW_THREADS |
| 2706 | closedir(dirp); |
| 2707 | Py_END_ALLOW_THREADS |
| 2708 | return NULL; |
| 2709 | } |
| 2710 | for (;;) { |
| 2711 | errno = 0; |
| 2712 | Py_BEGIN_ALLOW_THREADS |
| 2713 | ep = readdir(dirp); |
| 2714 | Py_END_ALLOW_THREADS |
| 2715 | if (ep == NULL) { |
| 2716 | if (errno == 0) { |
| 2717 | break; |
| 2718 | } else { |
| 2719 | Py_BEGIN_ALLOW_THREADS |
| 2720 | closedir(dirp); |
| 2721 | Py_END_ALLOW_THREADS |
| 2722 | Py_DECREF(d); |
| 2723 | return posix_error(); |
| 2724 | } |
| 2725 | } |
| 2726 | if (ep->d_name[0] == '.' && |
| 2727 | (NAMLEN(ep) == 1 || |
| 2728 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2729 | continue; |
| 2730 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2731 | if (v == NULL) { |
| 2732 | Py_CLEAR(d); |
| 2733 | break; |
| 2734 | } |
| 2735 | if (PyList_Append(d, v) != 0) { |
| 2736 | Py_DECREF(v); |
| 2737 | Py_CLEAR(d); |
| 2738 | break; |
| 2739 | } |
| 2740 | Py_DECREF(v); |
| 2741 | } |
| 2742 | Py_BEGIN_ALLOW_THREADS |
| 2743 | closedir(dirp); |
| 2744 | Py_END_ALLOW_THREADS |
| 2745 | |
| 2746 | return d; |
| 2747 | } |
| 2748 | #endif |
| 2749 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2750 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2751 | /* A helper function for abspath on win32 */ |
| 2752 | static PyObject * |
| 2753 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2754 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2755 | PyObject *opath; |
| 2756 | char *path; |
| 2757 | char outbuf[MAX_PATH*2]; |
| 2758 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2759 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2760 | PyUnicodeObject *po; |
| 2761 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2762 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2763 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2764 | Py_UNICODE *wtemp; |
| 2765 | DWORD result; |
| 2766 | PyObject *v; |
| 2767 | result = GetFullPathNameW(wpath, |
| 2768 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2769 | woutbuf, &wtemp); |
| 2770 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2771 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2772 | if (!woutbufp) |
| 2773 | return PyErr_NoMemory(); |
| 2774 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2775 | } |
| 2776 | if (result) |
| 2777 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2778 | else |
| 2779 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2780 | if (woutbufp != woutbuf) |
| 2781 | free(woutbufp); |
| 2782 | return v; |
| 2783 | } |
| 2784 | /* Drop the argument parsing error as narrow strings |
| 2785 | are also valid. */ |
| 2786 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2787 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2788 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2789 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2790 | PyUnicode_FSConverter, &opath)) |
| 2791 | return NULL; |
| 2792 | path = PyBytes_AsString(opath); |
| 2793 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2794 | outbuf, &temp)) { |
| 2795 | win32_error("GetFullPathName", path); |
| 2796 | Py_DECREF(opath); |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | Py_DECREF(opath); |
| 2800 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2801 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2802 | Py_FileSystemDefaultEncoding, NULL); |
| 2803 | } |
| 2804 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2805 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2806 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2807 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 2808 | static int has_GetFinalPathNameByHandle = 0; |
| 2809 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 2810 | DWORD); |
| 2811 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 2812 | DWORD); |
| 2813 | static int |
| 2814 | check_GetFinalPathNameByHandle() |
| 2815 | { |
| 2816 | HINSTANCE hKernel32; |
| 2817 | /* only recheck */ |
| 2818 | if (!has_GetFinalPathNameByHandle) |
| 2819 | { |
| 2820 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 2821 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 2822 | "GetFinalPathNameByHandleA"); |
| 2823 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 2824 | "GetFinalPathNameByHandleW"); |
| 2825 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 2826 | Py_GetFinalPathNameByHandleW; |
| 2827 | } |
| 2828 | return has_GetFinalPathNameByHandle; |
| 2829 | } |
| 2830 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2831 | /* A helper function for samepath on windows */ |
| 2832 | static PyObject * |
| 2833 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 2834 | { |
| 2835 | HANDLE hFile; |
| 2836 | int buf_size; |
| 2837 | wchar_t *target_path; |
| 2838 | int result_length; |
| 2839 | PyObject *result; |
| 2840 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2841 | |
Brian Curtin | 94622b0 | 2010-09-24 00:03:39 +0000 | [diff] [blame] | 2842 | if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) { |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2843 | return NULL; |
| 2844 | } |
| 2845 | |
| 2846 | if(!check_GetFinalPathNameByHandle()) { |
| 2847 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 2848 | NotImplementedError. */ |
| 2849 | return PyErr_Format(PyExc_NotImplementedError, |
| 2850 | "GetFinalPathNameByHandle not available on this platform"); |
| 2851 | } |
| 2852 | |
| 2853 | hFile = CreateFileW( |
| 2854 | path, |
| 2855 | 0, /* desired access */ |
| 2856 | 0, /* share mode */ |
| 2857 | NULL, /* security attributes */ |
| 2858 | OPEN_EXISTING, |
| 2859 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 2860 | FILE_FLAG_BACKUP_SEMANTICS, |
| 2861 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2862 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2863 | if(hFile == INVALID_HANDLE_VALUE) { |
| 2864 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 2865 | return PyErr_Format(PyExc_RuntimeError, |
| 2866 | "Could not get a handle to file."); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | /* We have a good handle to the target, use it to determine the |
| 2870 | target path name. */ |
| 2871 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 2872 | |
| 2873 | if(!buf_size) |
| 2874 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2875 | |
| 2876 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 2877 | if(!target_path) |
| 2878 | return PyErr_NoMemory(); |
| 2879 | |
| 2880 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 2881 | buf_size, VOLUME_NAME_DOS); |
| 2882 | if(!result_length) |
| 2883 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
| 2884 | |
| 2885 | if(!CloseHandle(hFile)) |
| 2886 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2887 | |
| 2888 | target_path[result_length] = 0; |
| 2889 | result = PyUnicode_FromUnicode(target_path, result_length); |
| 2890 | free(target_path); |
| 2891 | return result; |
| 2892 | |
| 2893 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2894 | |
| 2895 | static PyObject * |
| 2896 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 2897 | { |
| 2898 | HANDLE hFile; |
| 2899 | BY_HANDLE_FILE_INFORMATION info; |
| 2900 | int fd; |
| 2901 | |
| 2902 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 2903 | return NULL; |
| 2904 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 2905 | if (!_PyVerify_fd(fd)) |
| 2906 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2907 | |
| 2908 | hFile = (HANDLE)_get_osfhandle(fd); |
| 2909 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 2910 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2911 | |
| 2912 | if (!GetFileInformationByHandle(hFile, &info)) |
| 2913 | return win32_error("_getfileinformation", NULL); |
| 2914 | |
| 2915 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 2916 | info.nFileIndexHigh, |
| 2917 | info.nFileIndexLow); |
| 2918 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2919 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2920 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2921 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2922 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2923 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2924 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2925 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2926 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2927 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2928 | int res; |
| 2929 | PyObject *opath; |
| 2930 | char *path; |
| 2931 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2932 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2933 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2934 | PyUnicodeObject *po; |
| 2935 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 2936 | Py_BEGIN_ALLOW_THREADS |
| 2937 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2938 | it is a simple dereference. */ |
| 2939 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 2940 | Py_END_ALLOW_THREADS |
| 2941 | if (!res) |
| 2942 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 2943 | Py_INCREF(Py_None); |
| 2944 | return Py_None; |
| 2945 | } |
| 2946 | /* Drop the argument parsing error as narrow strings |
| 2947 | are also valid. */ |
| 2948 | PyErr_Clear(); |
| 2949 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2950 | PyUnicode_FSConverter, &opath, &mode)) |
| 2951 | return NULL; |
| 2952 | path = PyBytes_AsString(opath); |
| 2953 | Py_BEGIN_ALLOW_THREADS |
| 2954 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2955 | it is a simple dereference. */ |
| 2956 | res = CreateDirectoryA(path, NULL); |
| 2957 | Py_END_ALLOW_THREADS |
| 2958 | if (!res) { |
| 2959 | win32_error("mkdir", path); |
| 2960 | Py_DECREF(opath); |
| 2961 | return NULL; |
| 2962 | } |
| 2963 | Py_DECREF(opath); |
| 2964 | Py_INCREF(Py_None); |
| 2965 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2966 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2967 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2968 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2969 | PyUnicode_FSConverter, &opath, &mode)) |
| 2970 | return NULL; |
| 2971 | path = PyBytes_AsString(opath); |
| 2972 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2973 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2974 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2975 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2976 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2977 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2978 | Py_END_ALLOW_THREADS |
| 2979 | if (res < 0) |
| 2980 | return posix_error_with_allocated_filename(opath); |
| 2981 | Py_DECREF(opath); |
| 2982 | Py_INCREF(Py_None); |
| 2983 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2984 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2987 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2988 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2989 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2990 | #include <sys/resource.h> |
| 2991 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2992 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2993 | |
| 2994 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2995 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2996 | "nice(inc) -> new_priority\n\n\ |
| 2997 | 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] | 2998 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2999 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3000 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3001 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3002 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3003 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3004 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3005 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3006 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3007 | /* There are two flavours of 'nice': one that returns the new |
| 3008 | priority (as required by almost all standards out there) and the |
| 3009 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3010 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3011 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3012 | If we are of the nice family that returns the new priority, we |
| 3013 | need to clear errno before the call, and check if errno is filled |
| 3014 | before calling posix_error() on a returnvalue of -1, because the |
| 3015 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3016 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3017 | errno = 0; |
| 3018 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3019 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3020 | if (value == 0) |
| 3021 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3022 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3023 | if (value == -1 && errno != 0) |
| 3024 | /* either nice() or getpriority() returned an error */ |
| 3025 | return posix_error(); |
| 3026 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3027 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3028 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3029 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3030 | |
| 3031 | #ifdef HAVE_GETPRIORITY |
| 3032 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3033 | "getpriority(which, who) -> current_priority\n\n\ |
| 3034 | Get program scheduling priority."); |
| 3035 | |
| 3036 | static PyObject * |
| 3037 | posix_getpriority(PyObject *self, PyObject *args) |
| 3038 | { |
| 3039 | int which, who, retval; |
| 3040 | |
| 3041 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3042 | return NULL; |
| 3043 | errno = 0; |
| 3044 | Py_BEGIN_ALLOW_THREADS |
| 3045 | retval = getpriority(which, who); |
| 3046 | Py_END_ALLOW_THREADS |
| 3047 | if (errno != 0) |
| 3048 | return posix_error(); |
| 3049 | return PyLong_FromLong((long)retval); |
| 3050 | } |
| 3051 | #endif /* HAVE_GETPRIORITY */ |
| 3052 | |
| 3053 | |
| 3054 | #ifdef HAVE_SETPRIORITY |
| 3055 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3056 | "setpriority(which, who, prio) -> None\n\n\ |
| 3057 | Set program scheduling priority."); |
| 3058 | |
| 3059 | static PyObject * |
| 3060 | posix_setpriority(PyObject *self, PyObject *args) |
| 3061 | { |
| 3062 | int which, who, prio, retval; |
| 3063 | |
| 3064 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3065 | return NULL; |
| 3066 | Py_BEGIN_ALLOW_THREADS |
| 3067 | retval = setpriority(which, who, prio); |
| 3068 | Py_END_ALLOW_THREADS |
| 3069 | if (retval == -1) |
| 3070 | return posix_error(); |
| 3071 | Py_RETURN_NONE; |
| 3072 | } |
| 3073 | #endif /* HAVE_SETPRIORITY */ |
| 3074 | |
| 3075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3076 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3077 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3078 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3080 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3081 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3082 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3083 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3084 | PyObject *o1, *o2; |
| 3085 | char *p1, *p2; |
| 3086 | BOOL result; |
| 3087 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 3088 | goto error; |
| 3089 | if (!convert_to_unicode(&o1)) |
| 3090 | goto error; |
| 3091 | if (!convert_to_unicode(&o2)) { |
| 3092 | Py_DECREF(o1); |
| 3093 | goto error; |
| 3094 | } |
| 3095 | Py_BEGIN_ALLOW_THREADS |
| 3096 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 3097 | PyUnicode_AsUnicode(o2)); |
| 3098 | Py_END_ALLOW_THREADS |
| 3099 | Py_DECREF(o1); |
| 3100 | Py_DECREF(o2); |
| 3101 | if (!result) |
| 3102 | return win32_error("rename", NULL); |
| 3103 | Py_INCREF(Py_None); |
| 3104 | return Py_None; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3105 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3106 | PyErr_Clear(); |
| 3107 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 3108 | return NULL; |
| 3109 | Py_BEGIN_ALLOW_THREADS |
| 3110 | result = MoveFileA(p1, p2); |
| 3111 | Py_END_ALLOW_THREADS |
| 3112 | if (!result) |
| 3113 | return win32_error("rename", NULL); |
| 3114 | Py_INCREF(Py_None); |
| 3115 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3116 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3117 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3118 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3122 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3123 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3124 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3125 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3126 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3127 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3128 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3129 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3130 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3131 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3132 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3133 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3137 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3138 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3139 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3140 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3141 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3142 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3143 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3144 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3145 | 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] | 3146 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3147 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3148 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3151 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3152 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3153 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3154 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3155 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3156 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3157 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3158 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3159 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3160 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3161 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3162 | wchar_t *command; |
| 3163 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3164 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3165 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3166 | Py_BEGIN_ALLOW_THREADS |
| 3167 | sts = _wsystem(command); |
| 3168 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3169 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3170 | PyObject *command_obj; |
| 3171 | char *command; |
| 3172 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3173 | PyUnicode_FSConverter, &command_obj)) |
| 3174 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3175 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3176 | command = PyBytes_AsString(command_obj); |
| 3177 | Py_BEGIN_ALLOW_THREADS |
| 3178 | sts = system(command); |
| 3179 | Py_END_ALLOW_THREADS |
| 3180 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3181 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3182 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3183 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3184 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3185 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3187 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3188 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3189 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3190 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3191 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3192 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3193 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3194 | int i; |
| 3195 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3196 | return NULL; |
| 3197 | i = (int)umask(i); |
| 3198 | if (i < 0) |
| 3199 | return posix_error(); |
| 3200 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3203 | #ifdef MS_WINDOWS |
| 3204 | |
| 3205 | /* override the default DeleteFileW behavior so that directory |
| 3206 | symlinks can be removed with this function, the same as with |
| 3207 | Unix symlinks */ |
| 3208 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3209 | { |
| 3210 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3211 | WIN32_FIND_DATAW find_data; |
| 3212 | HANDLE find_data_handle; |
| 3213 | int is_directory = 0; |
| 3214 | int is_link = 0; |
| 3215 | |
| 3216 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3217 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3218 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3219 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3220 | it is a symlink */ |
| 3221 | if(is_directory && |
| 3222 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3223 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3224 | |
| 3225 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3226 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3227 | FindClose(find_data_handle); |
| 3228 | } |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | if (is_directory && is_link) |
| 3233 | return RemoveDirectoryW(lpFileName); |
| 3234 | |
| 3235 | return DeleteFileW(lpFileName); |
| 3236 | } |
| 3237 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3238 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3239 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3240 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3241 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3242 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3243 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3244 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3245 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3246 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3247 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3248 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3249 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3250 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3251 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3252 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3253 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3254 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3255 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3258 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3259 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3260 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3261 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3262 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3263 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3264 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3265 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3266 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3267 | struct utsname u; |
| 3268 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3269 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3270 | Py_BEGIN_ALLOW_THREADS |
| 3271 | res = uname(&u); |
| 3272 | Py_END_ALLOW_THREADS |
| 3273 | if (res < 0) |
| 3274 | return posix_error(); |
| 3275 | return Py_BuildValue("(sssss)", |
| 3276 | u.sysname, |
| 3277 | u.nodename, |
| 3278 | u.release, |
| 3279 | u.version, |
| 3280 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3281 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3282 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3283 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3284 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3285 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3286 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3287 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3288 | if (PyFloat_Check(t)) { |
| 3289 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3290 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3291 | if (!intobj) |
| 3292 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3293 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3294 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3295 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3296 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3297 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3298 | Py_DECREF(intobj); |
| 3299 | if (intval == -1 && PyErr_Occurred()) |
| 3300 | return -1; |
| 3301 | *sec = intval; |
| 3302 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
| 3303 | if (*usec < 0) |
| 3304 | /* If rounding gave us a negative number, |
| 3305 | truncate. */ |
| 3306 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3307 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3308 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3309 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3310 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3311 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3312 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3313 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3314 | if (intval == -1 && PyErr_Occurred()) |
| 3315 | return -1; |
| 3316 | *sec = intval; |
| 3317 | *usec = 0; |
| 3318 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3319 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3320 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3321 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3322 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3323 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3324 | 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] | 3325 | 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] | 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_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3329 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3330 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3331 | PyObject *arg; |
| 3332 | PyUnicodeObject *obwpath; |
| 3333 | wchar_t *wpath = NULL; |
| 3334 | PyObject *oapath; |
| 3335 | char *apath; |
| 3336 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3337 | time_t atimesec, mtimesec; |
| 3338 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3339 | FILETIME atime, mtime; |
| 3340 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3341 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3342 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 3343 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 3344 | Py_BEGIN_ALLOW_THREADS |
| 3345 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3346 | NULL, OPEN_EXISTING, |
| 3347 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3348 | Py_END_ALLOW_THREADS |
| 3349 | if (hFile == INVALID_HANDLE_VALUE) |
| 3350 | return win32_error_unicode("utime", wpath); |
| 3351 | } else |
| 3352 | /* Drop the argument parsing error as narrow strings |
| 3353 | are also valid. */ |
| 3354 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3355 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3356 | if (!wpath) { |
| 3357 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3358 | PyUnicode_FSConverter, &oapath, &arg)) |
| 3359 | return NULL; |
| 3360 | apath = PyBytes_AsString(oapath); |
| 3361 | Py_BEGIN_ALLOW_THREADS |
| 3362 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3363 | NULL, OPEN_EXISTING, |
| 3364 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3365 | Py_END_ALLOW_THREADS |
| 3366 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3367 | win32_error("utime", apath); |
| 3368 | Py_DECREF(oapath); |
| 3369 | return NULL; |
| 3370 | } |
| 3371 | Py_DECREF(oapath); |
| 3372 | } |
| 3373 | |
| 3374 | if (arg == Py_None) { |
| 3375 | SYSTEMTIME now; |
| 3376 | GetSystemTime(&now); |
| 3377 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3378 | !SystemTimeToFileTime(&now, &atime)) { |
| 3379 | win32_error("utime", NULL); |
| 3380 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3381 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3382 | } |
| 3383 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3384 | PyErr_SetString(PyExc_TypeError, |
| 3385 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3386 | goto done; |
| 3387 | } |
| 3388 | else { |
| 3389 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3390 | &atimesec, &ausec) == -1) |
| 3391 | goto done; |
| 3392 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3393 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3394 | &mtimesec, &musec) == -1) |
| 3395 | goto done; |
| 3396 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3397 | } |
| 3398 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3399 | /* Avoid putting the file name into the error here, |
| 3400 | as that may confuse the user into believing that |
| 3401 | something is wrong with the file, when it also |
| 3402 | could be the time stamp that gives a problem. */ |
| 3403 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3404 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3405 | } |
| 3406 | Py_INCREF(Py_None); |
| 3407 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3408 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3409 | CloseHandle(hFile); |
| 3410 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3411 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3413 | PyObject *opath; |
| 3414 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3415 | time_t atime, mtime; |
| 3416 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3417 | int res; |
| 3418 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3419 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3420 | #if defined(HAVE_UTIMES) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3421 | struct timeval buf[2]; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3422 | #define ATIME buf[0].tv_sec |
| 3423 | #define MTIME buf[1].tv_sec |
| 3424 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3425 | /* XXX should define struct utimbuf instead, above */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3426 | struct utimbuf buf; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3427 | #define ATIME buf.actime |
| 3428 | #define MTIME buf.modtime |
| 3429 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3430 | #else /* HAVE_UTIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3431 | time_t buf[2]; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3432 | #define ATIME buf[0] |
| 3433 | #define MTIME buf[1] |
| 3434 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3435 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3436 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3437 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3438 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3439 | PyUnicode_FSConverter, &opath, &arg)) |
| 3440 | return NULL; |
| 3441 | path = PyBytes_AsString(opath); |
| 3442 | if (arg == Py_None) { |
| 3443 | /* optional time values not given */ |
| 3444 | Py_BEGIN_ALLOW_THREADS |
| 3445 | res = utime(path, NULL); |
| 3446 | Py_END_ALLOW_THREADS |
| 3447 | } |
| 3448 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3449 | PyErr_SetString(PyExc_TypeError, |
| 3450 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3451 | Py_DECREF(opath); |
| 3452 | return NULL; |
| 3453 | } |
| 3454 | else { |
| 3455 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3456 | &atime, &ausec) == -1) { |
| 3457 | Py_DECREF(opath); |
| 3458 | return NULL; |
| 3459 | } |
| 3460 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3461 | &mtime, &musec) == -1) { |
| 3462 | Py_DECREF(opath); |
| 3463 | return NULL; |
| 3464 | } |
| 3465 | ATIME = atime; |
| 3466 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3467 | #ifdef HAVE_UTIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3468 | buf[0].tv_usec = ausec; |
| 3469 | buf[1].tv_usec = musec; |
| 3470 | Py_BEGIN_ALLOW_THREADS |
| 3471 | res = utimes(path, buf); |
| 3472 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3473 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3474 | Py_BEGIN_ALLOW_THREADS |
| 3475 | res = utime(path, UTIME_ARG); |
| 3476 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3477 | #endif /* HAVE_UTIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3478 | } |
| 3479 | if (res < 0) { |
| 3480 | return posix_error_with_allocated_filename(opath); |
| 3481 | } |
| 3482 | Py_DECREF(opath); |
| 3483 | Py_INCREF(Py_None); |
| 3484 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3485 | #undef UTIME_ARG |
| 3486 | #undef ATIME |
| 3487 | #undef MTIME |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3488 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3491 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3492 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3493 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3494 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3495 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3496 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3497 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3498 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3499 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3500 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3501 | int sts; |
| 3502 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3503 | return NULL; |
| 3504 | _exit(sts); |
| 3505 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3508 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3509 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3510 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3511 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3512 | Py_ssize_t i; |
| 3513 | for (i = 0; i < count; i++) |
| 3514 | PyMem_Free(array[i]); |
| 3515 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3516 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3517 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3518 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3519 | int fsconvert_strdup(PyObject *o, char**out) |
| 3520 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3521 | PyObject *bytes; |
| 3522 | Py_ssize_t size; |
| 3523 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3524 | return 0; |
| 3525 | size = PyBytes_GET_SIZE(bytes); |
| 3526 | *out = PyMem_Malloc(size+1); |
| 3527 | if (!*out) |
| 3528 | return 0; |
| 3529 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3530 | Py_DECREF(bytes); |
| 3531 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3532 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3533 | #endif |
| 3534 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3535 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3536 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3537 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3538 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3539 | Execute an executable path with arguments, replacing current process.\n\ |
| 3540 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3541 | path: path of executable file\n\ |
| 3542 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3543 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3544 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3545 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3546 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3547 | PyObject *opath; |
| 3548 | char *path; |
| 3549 | PyObject *argv; |
| 3550 | char **argvlist; |
| 3551 | Py_ssize_t i, argc; |
| 3552 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3553 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3554 | /* execv has two arguments: (path, argv), where |
| 3555 | argv is a list or tuple of strings. */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3556 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3557 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3558 | PyUnicode_FSConverter, |
| 3559 | &opath, &argv)) |
| 3560 | return NULL; |
| 3561 | path = PyBytes_AsString(opath); |
| 3562 | if (PyList_Check(argv)) { |
| 3563 | argc = PyList_Size(argv); |
| 3564 | getitem = PyList_GetItem; |
| 3565 | } |
| 3566 | else if (PyTuple_Check(argv)) { |
| 3567 | argc = PyTuple_Size(argv); |
| 3568 | getitem = PyTuple_GetItem; |
| 3569 | } |
| 3570 | else { |
| 3571 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
| 3572 | Py_DECREF(opath); |
| 3573 | return NULL; |
| 3574 | } |
| 3575 | if (argc < 1) { |
| 3576 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 3577 | Py_DECREF(opath); |
| 3578 | return NULL; |
| 3579 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3580 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3581 | argvlist = PyMem_NEW(char *, argc+1); |
| 3582 | if (argvlist == NULL) { |
| 3583 | Py_DECREF(opath); |
| 3584 | return PyErr_NoMemory(); |
| 3585 | } |
| 3586 | for (i = 0; i < argc; i++) { |
| 3587 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3588 | &argvlist[i])) { |
| 3589 | free_string_array(argvlist, i); |
| 3590 | PyErr_SetString(PyExc_TypeError, |
| 3591 | "execv() arg 2 must contain only strings"); |
| 3592 | Py_DECREF(opath); |
| 3593 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3594 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3595 | } |
| 3596 | } |
| 3597 | argvlist[argc] = NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3598 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3599 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3600 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3601 | /* If we get here it's definitely an error */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3602 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3603 | free_string_array(argvlist, argc); |
| 3604 | Py_DECREF(opath); |
| 3605 | return posix_error(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3608 | static char** |
| 3609 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3610 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3611 | char **envlist; |
| 3612 | Py_ssize_t i, pos, envc; |
| 3613 | PyObject *keys=NULL, *vals=NULL; |
| 3614 | PyObject *key, *val, *key2, *val2; |
| 3615 | char *p, *k, *v; |
| 3616 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3617 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3618 | i = PyMapping_Size(env); |
| 3619 | if (i < 0) |
| 3620 | return NULL; |
| 3621 | envlist = PyMem_NEW(char *, i + 1); |
| 3622 | if (envlist == NULL) { |
| 3623 | PyErr_NoMemory(); |
| 3624 | return NULL; |
| 3625 | } |
| 3626 | envc = 0; |
| 3627 | keys = PyMapping_Keys(env); |
| 3628 | vals = PyMapping_Values(env); |
| 3629 | if (!keys || !vals) |
| 3630 | goto error; |
| 3631 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3632 | PyErr_Format(PyExc_TypeError, |
| 3633 | "env.keys() or env.values() is not a list"); |
| 3634 | goto error; |
| 3635 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3636 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3637 | for (pos = 0; pos < i; pos++) { |
| 3638 | key = PyList_GetItem(keys, pos); |
| 3639 | val = PyList_GetItem(vals, pos); |
| 3640 | if (!key || !val) |
| 3641 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3642 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3643 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3644 | goto error; |
| 3645 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3646 | Py_DECREF(key2); |
| 3647 | goto error; |
| 3648 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3649 | |
| 3650 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3651 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3652 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3653 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3654 | k = PyBytes_AsString(key2); |
| 3655 | v = PyBytes_AsString(val2); |
| 3656 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3657 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3658 | p = PyMem_NEW(char, len); |
| 3659 | if (p == NULL) { |
| 3660 | PyErr_NoMemory(); |
| 3661 | Py_DECREF(key2); |
| 3662 | Py_DECREF(val2); |
| 3663 | goto error; |
| 3664 | } |
| 3665 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3666 | envlist[envc++] = p; |
| 3667 | Py_DECREF(key2); |
| 3668 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3669 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3670 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3671 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3672 | } |
| 3673 | Py_DECREF(vals); |
| 3674 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3675 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3676 | envlist[envc] = 0; |
| 3677 | *envc_ptr = envc; |
| 3678 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3679 | |
| 3680 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3681 | Py_XDECREF(keys); |
| 3682 | Py_XDECREF(vals); |
| 3683 | while (--envc >= 0) |
| 3684 | PyMem_DEL(envlist[envc]); |
| 3685 | PyMem_DEL(envlist); |
| 3686 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3687 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3689 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3690 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3691 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3692 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3693 | path: path of executable file\n\ |
| 3694 | args: tuple or list of arguments\n\ |
| 3695 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3696 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3697 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3698 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3699 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3700 | PyObject *opath; |
| 3701 | char *path; |
| 3702 | PyObject *argv, *env; |
| 3703 | char **argvlist; |
| 3704 | char **envlist; |
| 3705 | Py_ssize_t i, argc, envc; |
| 3706 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3707 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3708 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3709 | /* execve has three arguments: (path, argv, env), where |
| 3710 | argv is a list or tuple of strings and env is a dictionary |
| 3711 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3712 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3713 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3714 | PyUnicode_FSConverter, |
| 3715 | &opath, &argv, &env)) |
| 3716 | return NULL; |
| 3717 | path = PyBytes_AsString(opath); |
| 3718 | if (PyList_Check(argv)) { |
| 3719 | argc = PyList_Size(argv); |
| 3720 | getitem = PyList_GetItem; |
| 3721 | } |
| 3722 | else if (PyTuple_Check(argv)) { |
| 3723 | argc = PyTuple_Size(argv); |
| 3724 | getitem = PyTuple_GetItem; |
| 3725 | } |
| 3726 | else { |
| 3727 | PyErr_SetString(PyExc_TypeError, |
| 3728 | "execve() arg 2 must be a tuple or list"); |
| 3729 | goto fail_0; |
| 3730 | } |
| 3731 | if (!PyMapping_Check(env)) { |
| 3732 | PyErr_SetString(PyExc_TypeError, |
| 3733 | "execve() arg 3 must be a mapping object"); |
| 3734 | goto fail_0; |
| 3735 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3736 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3737 | argvlist = PyMem_NEW(char *, argc+1); |
| 3738 | if (argvlist == NULL) { |
| 3739 | PyErr_NoMemory(); |
| 3740 | goto fail_0; |
| 3741 | } |
| 3742 | for (i = 0; i < argc; i++) { |
| 3743 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3744 | &argvlist[i])) |
| 3745 | { |
| 3746 | lastarg = i; |
| 3747 | goto fail_1; |
| 3748 | } |
| 3749 | } |
| 3750 | lastarg = argc; |
| 3751 | argvlist[argc] = NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3752 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3753 | envlist = parse_envlist(env, &envc); |
| 3754 | if (envlist == NULL) |
| 3755 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3756 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3757 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3758 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3759 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3760 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3761 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3762 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3763 | while (--envc >= 0) |
| 3764 | PyMem_DEL(envlist[envc]); |
| 3765 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3766 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3767 | free_string_array(argvlist, lastarg); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3768 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3769 | Py_DECREF(opath); |
| 3770 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3771 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3772 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3773 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3774 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3775 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3776 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3777 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3778 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3779 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3780 | mode: mode of process creation\n\ |
| 3781 | path: path of executable file\n\ |
| 3782 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3783 | |
| 3784 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3785 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3786 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3787 | PyObject *opath; |
| 3788 | char *path; |
| 3789 | PyObject *argv; |
| 3790 | char **argvlist; |
| 3791 | int mode, i; |
| 3792 | Py_ssize_t argc; |
| 3793 | Py_intptr_t spawnval; |
| 3794 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3795 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3796 | /* spawnv has three arguments: (mode, path, argv), where |
| 3797 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3798 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3799 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 3800 | PyUnicode_FSConverter, |
| 3801 | &opath, &argv)) |
| 3802 | return NULL; |
| 3803 | path = PyBytes_AsString(opath); |
| 3804 | if (PyList_Check(argv)) { |
| 3805 | argc = PyList_Size(argv); |
| 3806 | getitem = PyList_GetItem; |
| 3807 | } |
| 3808 | else if (PyTuple_Check(argv)) { |
| 3809 | argc = PyTuple_Size(argv); |
| 3810 | getitem = PyTuple_GetItem; |
| 3811 | } |
| 3812 | else { |
| 3813 | PyErr_SetString(PyExc_TypeError, |
| 3814 | "spawnv() arg 2 must be a tuple or list"); |
| 3815 | Py_DECREF(opath); |
| 3816 | return NULL; |
| 3817 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3818 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3819 | argvlist = PyMem_NEW(char *, argc+1); |
| 3820 | if (argvlist == NULL) { |
| 3821 | Py_DECREF(opath); |
| 3822 | return PyErr_NoMemory(); |
| 3823 | } |
| 3824 | for (i = 0; i < argc; i++) { |
| 3825 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3826 | &argvlist[i])) { |
| 3827 | free_string_array(argvlist, i); |
| 3828 | PyErr_SetString( |
| 3829 | PyExc_TypeError, |
| 3830 | "spawnv() arg 2 must contain only strings"); |
| 3831 | Py_DECREF(opath); |
| 3832 | return NULL; |
| 3833 | } |
| 3834 | } |
| 3835 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3836 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3837 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3838 | Py_BEGIN_ALLOW_THREADS |
| 3839 | spawnval = spawnv(mode, path, argvlist); |
| 3840 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3841 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3842 | if (mode == _OLD_P_OVERLAY) |
| 3843 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3844 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3845 | Py_BEGIN_ALLOW_THREADS |
| 3846 | spawnval = _spawnv(mode, path, argvlist); |
| 3847 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3848 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3849 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3850 | free_string_array(argvlist, argc); |
| 3851 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3852 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3853 | if (spawnval == -1) |
| 3854 | return posix_error(); |
| 3855 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3856 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3857 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3858 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3859 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3860 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3864 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3865 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3866 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3867 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3868 | mode: mode of process creation\n\ |
| 3869 | path: path of executable file\n\ |
| 3870 | args: tuple or list of arguments\n\ |
| 3871 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3872 | |
| 3873 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3874 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3875 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3876 | PyObject *opath; |
| 3877 | char *path; |
| 3878 | PyObject *argv, *env; |
| 3879 | char **argvlist; |
| 3880 | char **envlist; |
| 3881 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 3882 | int mode; |
| 3883 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3884 | Py_intptr_t spawnval; |
| 3885 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3886 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3887 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3888 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3889 | argv is a list or tuple of strings and env is a dictionary |
| 3890 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3891 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3892 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 3893 | PyUnicode_FSConverter, |
| 3894 | &opath, &argv, &env)) |
| 3895 | return NULL; |
| 3896 | path = PyBytes_AsString(opath); |
| 3897 | if (PyList_Check(argv)) { |
| 3898 | argc = PyList_Size(argv); |
| 3899 | getitem = PyList_GetItem; |
| 3900 | } |
| 3901 | else if (PyTuple_Check(argv)) { |
| 3902 | argc = PyTuple_Size(argv); |
| 3903 | getitem = PyTuple_GetItem; |
| 3904 | } |
| 3905 | else { |
| 3906 | PyErr_SetString(PyExc_TypeError, |
| 3907 | "spawnve() arg 2 must be a tuple or list"); |
| 3908 | goto fail_0; |
| 3909 | } |
| 3910 | if (!PyMapping_Check(env)) { |
| 3911 | PyErr_SetString(PyExc_TypeError, |
| 3912 | "spawnve() arg 3 must be a mapping object"); |
| 3913 | goto fail_0; |
| 3914 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3915 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3916 | argvlist = PyMem_NEW(char *, argc+1); |
| 3917 | if (argvlist == NULL) { |
| 3918 | PyErr_NoMemory(); |
| 3919 | goto fail_0; |
| 3920 | } |
| 3921 | for (i = 0; i < argc; i++) { |
| 3922 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3923 | &argvlist[i])) |
| 3924 | { |
| 3925 | lastarg = i; |
| 3926 | goto fail_1; |
| 3927 | } |
| 3928 | } |
| 3929 | lastarg = argc; |
| 3930 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3931 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3932 | envlist = parse_envlist(env, &envc); |
| 3933 | if (envlist == NULL) |
| 3934 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3935 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3936 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3937 | Py_BEGIN_ALLOW_THREADS |
| 3938 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3939 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3940 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3941 | if (mode == _OLD_P_OVERLAY) |
| 3942 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3943 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3944 | Py_BEGIN_ALLOW_THREADS |
| 3945 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3946 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3947 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3948 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3949 | if (spawnval == -1) |
| 3950 | (void) posix_error(); |
| 3951 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3952 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3953 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3954 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3955 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3956 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3957 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3958 | while (--envc >= 0) |
| 3959 | PyMem_DEL(envlist[envc]); |
| 3960 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3961 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3962 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3963 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3964 | Py_DECREF(opath); |
| 3965 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3966 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3967 | |
| 3968 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3969 | #if defined(PYOS_OS2) |
| 3970 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3971 | "spawnvp(mode, file, args)\n\n\ |
| 3972 | Execute the program 'file' in a new process, using the environment\n\ |
| 3973 | search path to find the file.\n\ |
| 3974 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3975 | mode: mode of process creation\n\ |
| 3976 | file: executable file name\n\ |
| 3977 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3978 | |
| 3979 | static PyObject * |
| 3980 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3981 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3982 | PyObject *opath; |
| 3983 | char *path; |
| 3984 | PyObject *argv; |
| 3985 | char **argvlist; |
| 3986 | int mode, i, argc; |
| 3987 | Py_intptr_t spawnval; |
| 3988 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3989 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3990 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3991 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3992 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3993 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 3994 | PyUnicode_FSConverter, |
| 3995 | &opath, &argv)) |
| 3996 | return NULL; |
| 3997 | path = PyBytes_AsString(opath); |
| 3998 | if (PyList_Check(argv)) { |
| 3999 | argc = PyList_Size(argv); |
| 4000 | getitem = PyList_GetItem; |
| 4001 | } |
| 4002 | else if (PyTuple_Check(argv)) { |
| 4003 | argc = PyTuple_Size(argv); |
| 4004 | getitem = PyTuple_GetItem; |
| 4005 | } |
| 4006 | else { |
| 4007 | PyErr_SetString(PyExc_TypeError, |
| 4008 | "spawnvp() arg 2 must be a tuple or list"); |
| 4009 | Py_DECREF(opath); |
| 4010 | return NULL; |
| 4011 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4012 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4013 | argvlist = PyMem_NEW(char *, argc+1); |
| 4014 | if (argvlist == NULL) { |
| 4015 | Py_DECREF(opath); |
| 4016 | return PyErr_NoMemory(); |
| 4017 | } |
| 4018 | for (i = 0; i < argc; i++) { |
| 4019 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4020 | &argvlist[i])) { |
| 4021 | free_string_array(argvlist, i); |
| 4022 | PyErr_SetString( |
| 4023 | PyExc_TypeError, |
| 4024 | "spawnvp() arg 2 must contain only strings"); |
| 4025 | Py_DECREF(opath); |
| 4026 | return NULL; |
| 4027 | } |
| 4028 | } |
| 4029 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4030 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4031 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4032 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4033 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4034 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4035 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4036 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4037 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4038 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4039 | free_string_array(argvlist, argc); |
| 4040 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4041 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4042 | if (spawnval == -1) |
| 4043 | return posix_error(); |
| 4044 | else |
| 4045 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | |
| 4049 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4050 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4051 | Execute the program 'file' in a new process, using the environment\n\ |
| 4052 | search path to find the file.\n\ |
| 4053 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4054 | mode: mode of process creation\n\ |
| 4055 | file: executable file name\n\ |
| 4056 | args: tuple or list of arguments\n\ |
| 4057 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4058 | |
| 4059 | static PyObject * |
| 4060 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4061 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4062 | PyObject *opath |
| 4063 | char *path; |
| 4064 | PyObject *argv, *env; |
| 4065 | char **argvlist; |
| 4066 | char **envlist; |
| 4067 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4068 | int mode; |
| 4069 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4070 | Py_intptr_t spawnval; |
| 4071 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4072 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4073 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4074 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4075 | argv is a list or tuple of strings and env is a dictionary |
| 4076 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4077 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4078 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4079 | PyUnicode_FSConverter, |
| 4080 | &opath, &argv, &env)) |
| 4081 | return NULL; |
| 4082 | path = PyBytes_AsString(opath); |
| 4083 | if (PyList_Check(argv)) { |
| 4084 | argc = PyList_Size(argv); |
| 4085 | getitem = PyList_GetItem; |
| 4086 | } |
| 4087 | else if (PyTuple_Check(argv)) { |
| 4088 | argc = PyTuple_Size(argv); |
| 4089 | getitem = PyTuple_GetItem; |
| 4090 | } |
| 4091 | else { |
| 4092 | PyErr_SetString(PyExc_TypeError, |
| 4093 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4094 | goto fail_0; |
| 4095 | } |
| 4096 | if (!PyMapping_Check(env)) { |
| 4097 | PyErr_SetString(PyExc_TypeError, |
| 4098 | "spawnvpe() arg 3 must be a mapping object"); |
| 4099 | goto fail_0; |
| 4100 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4101 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4102 | argvlist = PyMem_NEW(char *, argc+1); |
| 4103 | if (argvlist == NULL) { |
| 4104 | PyErr_NoMemory(); |
| 4105 | goto fail_0; |
| 4106 | } |
| 4107 | for (i = 0; i < argc; i++) { |
| 4108 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4109 | &argvlist[i])) |
| 4110 | { |
| 4111 | lastarg = i; |
| 4112 | goto fail_1; |
| 4113 | } |
| 4114 | } |
| 4115 | lastarg = argc; |
| 4116 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4117 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4118 | envlist = parse_envlist(env, &envc); |
| 4119 | if (envlist == NULL) |
| 4120 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4121 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4122 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4123 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4124 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4125 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4126 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4127 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4128 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4129 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4130 | if (spawnval == -1) |
| 4131 | (void) posix_error(); |
| 4132 | else |
| 4133 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4134 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4135 | while (--envc >= 0) |
| 4136 | PyMem_DEL(envlist[envc]); |
| 4137 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4138 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4139 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4140 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4141 | Py_DECREF(opath); |
| 4142 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4143 | } |
| 4144 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4145 | #endif /* HAVE_SPAWNV */ |
| 4146 | |
| 4147 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4148 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4149 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4150 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4151 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4152 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4153 | 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] | 4154 | |
| 4155 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4156 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4157 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4158 | pid_t pid; |
| 4159 | int result = 0; |
| 4160 | _PyImport_AcquireLock(); |
| 4161 | pid = fork1(); |
| 4162 | if (pid == 0) { |
| 4163 | /* child: this clobbers and resets the import lock. */ |
| 4164 | PyOS_AfterFork(); |
| 4165 | } else { |
| 4166 | /* parent: release the import lock. */ |
| 4167 | result = _PyImport_ReleaseLock(); |
| 4168 | } |
| 4169 | if (pid == -1) |
| 4170 | return posix_error(); |
| 4171 | if (result < 0) { |
| 4172 | /* Don't clobber the OSError if the fork failed. */ |
| 4173 | PyErr_SetString(PyExc_RuntimeError, |
| 4174 | "not holding the import lock"); |
| 4175 | return NULL; |
| 4176 | } |
| 4177 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4178 | } |
| 4179 | #endif |
| 4180 | |
| 4181 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4182 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4183 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4184 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4185 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4186 | 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] | 4187 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4188 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4189 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4190 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4191 | pid_t pid; |
| 4192 | int result = 0; |
| 4193 | _PyImport_AcquireLock(); |
| 4194 | pid = fork(); |
| 4195 | if (pid == 0) { |
| 4196 | /* child: this clobbers and resets the import lock. */ |
| 4197 | PyOS_AfterFork(); |
| 4198 | } else { |
| 4199 | /* parent: release the import lock. */ |
| 4200 | result = _PyImport_ReleaseLock(); |
| 4201 | } |
| 4202 | if (pid == -1) |
| 4203 | return posix_error(); |
| 4204 | if (result < 0) { |
| 4205 | /* Don't clobber the OSError if the fork failed. */ |
| 4206 | PyErr_SetString(PyExc_RuntimeError, |
| 4207 | "not holding the import lock"); |
| 4208 | return NULL; |
| 4209 | } |
| 4210 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4211 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4212 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4213 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4214 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 4215 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 4216 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4217 | #define DEV_PTY_FILE "/dev/ptc" |
| 4218 | #define HAVE_DEV_PTMX |
| 4219 | #else |
| 4220 | #define DEV_PTY_FILE "/dev/ptmx" |
| 4221 | #endif |
| 4222 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4223 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4224 | #ifdef HAVE_PTY_H |
| 4225 | #include <pty.h> |
| 4226 | #else |
| 4227 | #ifdef HAVE_LIBUTIL_H |
| 4228 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 4229 | #else |
| 4230 | #ifdef HAVE_UTIL_H |
| 4231 | #include <util.h> |
| 4232 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4233 | #endif /* HAVE_LIBUTIL_H */ |
| 4234 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 4235 | #ifdef HAVE_STROPTS_H |
| 4236 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4237 | #endif |
| 4238 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4239 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4240 | #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] | 4241 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4242 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4243 | 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] | 4244 | |
| 4245 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4246 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4247 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4248 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4249 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4250 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4251 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4252 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4253 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4254 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4255 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4256 | #endif |
| 4257 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4258 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4259 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4260 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 4261 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4262 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4263 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 4264 | if (slave_name == NULL) |
| 4265 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4266 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4267 | slave_fd = open(slave_name, O_RDWR); |
| 4268 | if (slave_fd < 0) |
| 4269 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4270 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4271 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 4272 | if (master_fd < 0) |
| 4273 | return posix_error(); |
| 4274 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 4275 | /* change permission of slave */ |
| 4276 | if (grantpt(master_fd) < 0) { |
| 4277 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4278 | return posix_error(); |
| 4279 | } |
| 4280 | /* unlock slave */ |
| 4281 | if (unlockpt(master_fd) < 0) { |
| 4282 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4283 | return posix_error(); |
| 4284 | } |
| 4285 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4286 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 4287 | if (slave_name == NULL) |
| 4288 | return posix_error(); |
| 4289 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 4290 | if (slave_fd < 0) |
| 4291 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4292 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4293 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 4294 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 4295 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4296 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 4297 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4298 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4299 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4301 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4302 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4303 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4304 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4305 | |
| 4306 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4307 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4308 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4309 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 4310 | 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] | 4311 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4312 | |
| 4313 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4314 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4315 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4316 | int master_fd = -1, result = 0; |
| 4317 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4318 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4319 | _PyImport_AcquireLock(); |
| 4320 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 4321 | if (pid == 0) { |
| 4322 | /* child: this clobbers and resets the import lock. */ |
| 4323 | PyOS_AfterFork(); |
| 4324 | } else { |
| 4325 | /* parent: release the import lock. */ |
| 4326 | result = _PyImport_ReleaseLock(); |
| 4327 | } |
| 4328 | if (pid == -1) |
| 4329 | return posix_error(); |
| 4330 | if (result < 0) { |
| 4331 | /* Don't clobber the OSError if the fork failed. */ |
| 4332 | PyErr_SetString(PyExc_RuntimeError, |
| 4333 | "not holding the import lock"); |
| 4334 | return NULL; |
| 4335 | } |
| 4336 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4337 | } |
| 4338 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4339 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4340 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4341 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4342 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4343 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4344 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4345 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4346 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4347 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4348 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4349 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4350 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4351 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4352 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4353 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4354 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4355 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4356 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4357 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4358 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4359 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4360 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4361 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4362 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4363 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4364 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4365 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4366 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4367 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4368 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4369 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4370 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4371 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4372 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4373 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4374 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4375 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4376 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4377 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4378 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4379 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4380 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4381 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4382 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4383 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4384 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4385 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4386 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4389 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4390 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4391 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4392 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4393 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4394 | |
| 4395 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4396 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4397 | { |
| 4398 | PyObject *result = NULL; |
| 4399 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4400 | #ifdef NGROUPS_MAX |
| 4401 | #define MAX_GROUPS NGROUPS_MAX |
| 4402 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4403 | /* 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] | 4404 | #define MAX_GROUPS 64 |
| 4405 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4406 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4407 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4408 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4409 | * This is a helper variable to store the intermediate result when |
| 4410 | * that happens. |
| 4411 | * |
| 4412 | * To keep the code readable the OSX behaviour is unconditional, |
| 4413 | * according to the POSIX spec this should be safe on all unix-y |
| 4414 | * systems. |
| 4415 | */ |
| 4416 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4417 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4419 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4420 | if (n < 0) { |
| 4421 | if (errno == EINVAL) { |
| 4422 | n = getgroups(0, NULL); |
| 4423 | if (n == -1) { |
| 4424 | return posix_error(); |
| 4425 | } |
| 4426 | if (n == 0) { |
| 4427 | /* Avoid malloc(0) */ |
| 4428 | alt_grouplist = grouplist; |
| 4429 | } else { |
| 4430 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 4431 | if (alt_grouplist == NULL) { |
| 4432 | errno = EINVAL; |
| 4433 | return posix_error(); |
| 4434 | } |
| 4435 | n = getgroups(n, alt_grouplist); |
| 4436 | if (n == -1) { |
| 4437 | PyMem_Free(alt_grouplist); |
| 4438 | return posix_error(); |
| 4439 | } |
| 4440 | } |
| 4441 | } else { |
| 4442 | return posix_error(); |
| 4443 | } |
| 4444 | } |
| 4445 | result = PyList_New(n); |
| 4446 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4447 | int i; |
| 4448 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4449 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4450 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4451 | Py_DECREF(result); |
| 4452 | result = NULL; |
| 4453 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4454 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4455 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4456 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | if (alt_grouplist != grouplist) { |
| 4460 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4461 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4462 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4463 | return result; |
| 4464 | } |
| 4465 | #endif |
| 4466 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4467 | #ifdef HAVE_INITGROUPS |
| 4468 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 4469 | "initgroups(username, gid) -> None\n\n\ |
| 4470 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 4471 | the groups of which the specified username is a member, plus the specified\n\ |
| 4472 | group id."); |
| 4473 | |
| 4474 | static PyObject * |
| 4475 | posix_initgroups(PyObject *self, PyObject *args) |
| 4476 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4477 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4478 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4479 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4480 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4481 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4482 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 4483 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4484 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4485 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4486 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4487 | res = initgroups(username, (gid_t) gid); |
| 4488 | Py_DECREF(oname); |
| 4489 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4490 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4492 | Py_INCREF(Py_None); |
| 4493 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4494 | } |
| 4495 | #endif |
| 4496 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4497 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4498 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4499 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4500 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4501 | |
| 4502 | static PyObject * |
| 4503 | posix_getpgid(PyObject *self, PyObject *args) |
| 4504 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4505 | pid_t pid, pgid; |
| 4506 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 4507 | return NULL; |
| 4508 | pgid = getpgid(pid); |
| 4509 | if (pgid < 0) |
| 4510 | return posix_error(); |
| 4511 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4512 | } |
| 4513 | #endif /* HAVE_GETPGID */ |
| 4514 | |
| 4515 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4516 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4517 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4518 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4519 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4520 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4521 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4522 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4523 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4524 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4525 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4526 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4527 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4528 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4529 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4530 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4531 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4532 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4533 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4534 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4535 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 4536 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4537 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4538 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4539 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4540 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4541 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4542 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4543 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4544 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4545 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4546 | return posix_error(); |
| 4547 | Py_INCREF(Py_None); |
| 4548 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4551 | #endif /* HAVE_SETPGRP */ |
| 4552 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4553 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4554 | |
| 4555 | #ifdef MS_WINDOWS |
| 4556 | #include <tlhelp32.h> |
| 4557 | |
| 4558 | static PyObject* |
| 4559 | win32_getppid() |
| 4560 | { |
| 4561 | HANDLE snapshot; |
| 4562 | pid_t mypid; |
| 4563 | PyObject* result = NULL; |
| 4564 | BOOL have_record; |
| 4565 | PROCESSENTRY32 pe; |
| 4566 | |
| 4567 | mypid = getpid(); /* This function never fails */ |
| 4568 | |
| 4569 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 4570 | if (snapshot == INVALID_HANDLE_VALUE) |
| 4571 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 4572 | |
| 4573 | pe.dwSize = sizeof(pe); |
| 4574 | have_record = Process32First(snapshot, &pe); |
| 4575 | while (have_record) { |
| 4576 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 4577 | /* We could cache the ulong value in a static variable. */ |
| 4578 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 4579 | break; |
| 4580 | } |
| 4581 | |
| 4582 | have_record = Process32Next(snapshot, &pe); |
| 4583 | } |
| 4584 | |
| 4585 | /* If our loop exits and our pid was not found (result will be NULL) |
| 4586 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 4587 | * error anyway, so let's raise it. */ |
| 4588 | if (!result) |
| 4589 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 4590 | |
| 4591 | CloseHandle(snapshot); |
| 4592 | |
| 4593 | return result; |
| 4594 | } |
| 4595 | #endif /*MS_WINDOWS*/ |
| 4596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4597 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4598 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4599 | Return the parent's process id. If the parent process has already exited,\n\ |
| 4600 | Windows machines will still return its id; others systems will return the id\n\ |
| 4601 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4602 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4603 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4604 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4605 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4606 | #ifdef MS_WINDOWS |
| 4607 | return win32_getppid(); |
| 4608 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4609 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4610 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4611 | } |
| 4612 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4613 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4614 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4615 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4616 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4617 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4618 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4619 | |
| 4620 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4621 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4622 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4623 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4624 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4625 | wchar_t user_name[UNLEN + 1]; |
| 4626 | DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]); |
| 4627 | |
| 4628 | if (GetUserNameW(user_name, &num_chars)) { |
| 4629 | /* num_chars is the number of unicode chars plus null terminator */ |
| 4630 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4631 | } |
| 4632 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4633 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 4634 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4635 | char *name; |
| 4636 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4637 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4638 | errno = 0; |
| 4639 | name = getlogin(); |
| 4640 | if (name == NULL) { |
| 4641 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4642 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4643 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4644 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4645 | } |
| 4646 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4647 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4648 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4649 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4650 | return result; |
| 4651 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4652 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4653 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4654 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4655 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4656 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4657 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4658 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4659 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4660 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4661 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4662 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4663 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4664 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4665 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4666 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4667 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4668 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4669 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4670 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4671 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4672 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4673 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4674 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4675 | pid_t pid; |
| 4676 | int sig; |
| 4677 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 4678 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4679 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4680 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4681 | APIRET rc; |
| 4682 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4683 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4684 | |
| 4685 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4686 | APIRET rc; |
| 4687 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4688 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4689 | |
| 4690 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4691 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4692 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4693 | if (kill(pid, sig) == -1) |
| 4694 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4695 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4696 | Py_INCREF(Py_None); |
| 4697 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4698 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4699 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4700 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4701 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4702 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4703 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4704 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4705 | |
| 4706 | static PyObject * |
| 4707 | posix_killpg(PyObject *self, PyObject *args) |
| 4708 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4709 | int sig; |
| 4710 | pid_t pgid; |
| 4711 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4712 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4713 | take the same type. Moreover, pid_t is always at least as wide as |
| 4714 | int (else compilation of this module fails), which is safe. */ |
| 4715 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 4716 | return NULL; |
| 4717 | if (killpg(pgid, sig) == -1) |
| 4718 | return posix_error(); |
| 4719 | Py_INCREF(Py_None); |
| 4720 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4721 | } |
| 4722 | #endif |
| 4723 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4724 | #ifdef MS_WINDOWS |
| 4725 | PyDoc_STRVAR(win32_kill__doc__, |
| 4726 | "kill(pid, sig)\n\n\ |
| 4727 | Kill a process with a signal."); |
| 4728 | |
| 4729 | static PyObject * |
| 4730 | win32_kill(PyObject *self, PyObject *args) |
| 4731 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 4732 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4733 | DWORD pid, sig, err; |
| 4734 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4735 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4736 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 4737 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4739 | /* Console processes which share a common console can be sent CTRL+C or |
| 4740 | CTRL+BREAK events, provided they handle said events. */ |
| 4741 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 4742 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 4743 | err = GetLastError(); |
| 4744 | PyErr_SetFromWindowsErr(err); |
| 4745 | } |
| 4746 | else |
| 4747 | Py_RETURN_NONE; |
| 4748 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4749 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4750 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 4751 | attempt to open and terminate the process. */ |
| 4752 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 4753 | if (handle == NULL) { |
| 4754 | err = GetLastError(); |
| 4755 | return PyErr_SetFromWindowsErr(err); |
| 4756 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4757 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4758 | if (TerminateProcess(handle, sig) == 0) { |
| 4759 | err = GetLastError(); |
| 4760 | result = PyErr_SetFromWindowsErr(err); |
| 4761 | } else { |
| 4762 | Py_INCREF(Py_None); |
| 4763 | result = Py_None; |
| 4764 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4765 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4766 | CloseHandle(handle); |
| 4767 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4768 | } |
| 4769 | #endif /* MS_WINDOWS */ |
| 4770 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4771 | #ifdef HAVE_PLOCK |
| 4772 | |
| 4773 | #ifdef HAVE_SYS_LOCK_H |
| 4774 | #include <sys/lock.h> |
| 4775 | #endif |
| 4776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4777 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4778 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4779 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4780 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4781 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4782 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4783 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4784 | int op; |
| 4785 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 4786 | return NULL; |
| 4787 | if (plock(op) == -1) |
| 4788 | return posix_error(); |
| 4789 | Py_INCREF(Py_None); |
| 4790 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4791 | } |
| 4792 | #endif |
| 4793 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4794 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4795 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4796 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4797 | Set the current process's user id."); |
| 4798 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4799 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4800 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4801 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4802 | long uid_arg; |
| 4803 | uid_t uid; |
| 4804 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 4805 | return NULL; |
| 4806 | uid = uid_arg; |
| 4807 | if (uid != uid_arg) { |
| 4808 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4809 | return NULL; |
| 4810 | } |
| 4811 | if (setuid(uid) < 0) |
| 4812 | return posix_error(); |
| 4813 | Py_INCREF(Py_None); |
| 4814 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4815 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4816 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4817 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4818 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4819 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4820 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4821 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4822 | Set the current process's effective user id."); |
| 4823 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4824 | static PyObject * |
| 4825 | posix_seteuid (PyObject *self, PyObject *args) |
| 4826 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4827 | long euid_arg; |
| 4828 | uid_t euid; |
| 4829 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 4830 | return NULL; |
| 4831 | euid = euid_arg; |
| 4832 | if (euid != euid_arg) { |
| 4833 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4834 | return NULL; |
| 4835 | } |
| 4836 | if (seteuid(euid) < 0) { |
| 4837 | return posix_error(); |
| 4838 | } else { |
| 4839 | Py_INCREF(Py_None); |
| 4840 | return Py_None; |
| 4841 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4842 | } |
| 4843 | #endif /* HAVE_SETEUID */ |
| 4844 | |
| 4845 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4846 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4847 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4848 | Set the current process's effective group id."); |
| 4849 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4850 | static PyObject * |
| 4851 | posix_setegid (PyObject *self, PyObject *args) |
| 4852 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4853 | long egid_arg; |
| 4854 | gid_t egid; |
| 4855 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 4856 | return NULL; |
| 4857 | egid = egid_arg; |
| 4858 | if (egid != egid_arg) { |
| 4859 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4860 | return NULL; |
| 4861 | } |
| 4862 | if (setegid(egid) < 0) { |
| 4863 | return posix_error(); |
| 4864 | } else { |
| 4865 | Py_INCREF(Py_None); |
| 4866 | return Py_None; |
| 4867 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4868 | } |
| 4869 | #endif /* HAVE_SETEGID */ |
| 4870 | |
| 4871 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4872 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4873 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4874 | Set the current process's real and effective user ids."); |
| 4875 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4876 | static PyObject * |
| 4877 | posix_setreuid (PyObject *self, PyObject *args) |
| 4878 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4879 | long ruid_arg, euid_arg; |
| 4880 | uid_t ruid, euid; |
| 4881 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 4882 | return NULL; |
| 4883 | if (ruid_arg == -1) |
| 4884 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 4885 | else |
| 4886 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 4887 | if (euid_arg == -1) |
| 4888 | euid = (uid_t)-1; |
| 4889 | else |
| 4890 | euid = euid_arg; |
| 4891 | if ((euid_arg != -1 && euid != euid_arg) || |
| 4892 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 4893 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4894 | return NULL; |
| 4895 | } |
| 4896 | if (setreuid(ruid, euid) < 0) { |
| 4897 | return posix_error(); |
| 4898 | } else { |
| 4899 | Py_INCREF(Py_None); |
| 4900 | return Py_None; |
| 4901 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4902 | } |
| 4903 | #endif /* HAVE_SETREUID */ |
| 4904 | |
| 4905 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4906 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4907 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4908 | Set the current process's real and effective group ids."); |
| 4909 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4910 | static PyObject * |
| 4911 | posix_setregid (PyObject *self, PyObject *args) |
| 4912 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4913 | long rgid_arg, egid_arg; |
| 4914 | gid_t rgid, egid; |
| 4915 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 4916 | return NULL; |
| 4917 | if (rgid_arg == -1) |
| 4918 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 4919 | else |
| 4920 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 4921 | if (egid_arg == -1) |
| 4922 | egid = (gid_t)-1; |
| 4923 | else |
| 4924 | egid = egid_arg; |
| 4925 | if ((egid_arg != -1 && egid != egid_arg) || |
| 4926 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 4927 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4928 | return NULL; |
| 4929 | } |
| 4930 | if (setregid(rgid, egid) < 0) { |
| 4931 | return posix_error(); |
| 4932 | } else { |
| 4933 | Py_INCREF(Py_None); |
| 4934 | return Py_None; |
| 4935 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4936 | } |
| 4937 | #endif /* HAVE_SETREGID */ |
| 4938 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4939 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4940 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4941 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4942 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4943 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4944 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4945 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4946 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4947 | long gid_arg; |
| 4948 | gid_t gid; |
| 4949 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 4950 | return NULL; |
| 4951 | gid = gid_arg; |
| 4952 | if (gid != gid_arg) { |
| 4953 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4954 | return NULL; |
| 4955 | } |
| 4956 | if (setgid(gid) < 0) |
| 4957 | return posix_error(); |
| 4958 | Py_INCREF(Py_None); |
| 4959 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4960 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4961 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4962 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4963 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4964 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4965 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4966 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4967 | |
| 4968 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4969 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4970 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4971 | int i, len; |
| 4972 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4973 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4974 | if (!PySequence_Check(groups)) { |
| 4975 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4976 | return NULL; |
| 4977 | } |
| 4978 | len = PySequence_Size(groups); |
| 4979 | if (len > MAX_GROUPS) { |
| 4980 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4981 | return NULL; |
| 4982 | } |
| 4983 | for(i = 0; i < len; i++) { |
| 4984 | PyObject *elem; |
| 4985 | elem = PySequence_GetItem(groups, i); |
| 4986 | if (!elem) |
| 4987 | return NULL; |
| 4988 | if (!PyLong_Check(elem)) { |
| 4989 | PyErr_SetString(PyExc_TypeError, |
| 4990 | "groups must be integers"); |
| 4991 | Py_DECREF(elem); |
| 4992 | return NULL; |
| 4993 | } else { |
| 4994 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4995 | if (PyErr_Occurred()) { |
| 4996 | PyErr_SetString(PyExc_TypeError, |
| 4997 | "group id too big"); |
| 4998 | Py_DECREF(elem); |
| 4999 | return NULL; |
| 5000 | } |
| 5001 | grouplist[i] = x; |
| 5002 | /* read back the value to see if it fitted in gid_t */ |
| 5003 | if (grouplist[i] != x) { |
| 5004 | PyErr_SetString(PyExc_TypeError, |
| 5005 | "group id too big"); |
| 5006 | Py_DECREF(elem); |
| 5007 | return NULL; |
| 5008 | } |
| 5009 | } |
| 5010 | Py_DECREF(elem); |
| 5011 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5012 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5013 | if (setgroups(len, grouplist) < 0) |
| 5014 | return posix_error(); |
| 5015 | Py_INCREF(Py_None); |
| 5016 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5017 | } |
| 5018 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5019 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5020 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 5021 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 5022 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5023 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5024 | PyObject *result; |
| 5025 | static PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5027 | if (pid == -1) |
| 5028 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5029 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5030 | if (struct_rusage == NULL) { |
| 5031 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 5032 | if (m == NULL) |
| 5033 | return NULL; |
| 5034 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5035 | Py_DECREF(m); |
| 5036 | if (struct_rusage == NULL) |
| 5037 | return NULL; |
| 5038 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5039 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5040 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5041 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5042 | if (!result) |
| 5043 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5044 | |
| 5045 | #ifndef doubletime |
| 5046 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5047 | #endif |
| 5048 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5049 | PyStructSequence_SET_ITEM(result, 0, |
| 5050 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5051 | PyStructSequence_SET_ITEM(result, 1, |
| 5052 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5053 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5054 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 5055 | SET_INT(result, 2, ru->ru_maxrss); |
| 5056 | SET_INT(result, 3, ru->ru_ixrss); |
| 5057 | SET_INT(result, 4, ru->ru_idrss); |
| 5058 | SET_INT(result, 5, ru->ru_isrss); |
| 5059 | SET_INT(result, 6, ru->ru_minflt); |
| 5060 | SET_INT(result, 7, ru->ru_majflt); |
| 5061 | SET_INT(result, 8, ru->ru_nswap); |
| 5062 | SET_INT(result, 9, ru->ru_inblock); |
| 5063 | SET_INT(result, 10, ru->ru_oublock); |
| 5064 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5065 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5066 | SET_INT(result, 13, ru->ru_nsignals); |
| 5067 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5068 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5069 | #undef SET_INT |
| 5070 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5071 | if (PyErr_Occurred()) { |
| 5072 | Py_DECREF(result); |
| 5073 | return NULL; |
| 5074 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5075 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5076 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5077 | } |
| 5078 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 5079 | |
| 5080 | #ifdef HAVE_WAIT3 |
| 5081 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5082 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5083 | Wait for completion of a child process."); |
| 5084 | |
| 5085 | static PyObject * |
| 5086 | posix_wait3(PyObject *self, PyObject *args) |
| 5087 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5088 | pid_t pid; |
| 5089 | int options; |
| 5090 | struct rusage ru; |
| 5091 | WAIT_TYPE status; |
| 5092 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5093 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5094 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5095 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5096 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5097 | Py_BEGIN_ALLOW_THREADS |
| 5098 | pid = wait3(&status, options, &ru); |
| 5099 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5100 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5101 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5102 | } |
| 5103 | #endif /* HAVE_WAIT3 */ |
| 5104 | |
| 5105 | #ifdef HAVE_WAIT4 |
| 5106 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5107 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5108 | Wait for completion of a given child process."); |
| 5109 | |
| 5110 | static PyObject * |
| 5111 | posix_wait4(PyObject *self, PyObject *args) |
| 5112 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5113 | pid_t pid; |
| 5114 | int options; |
| 5115 | struct rusage ru; |
| 5116 | WAIT_TYPE status; |
| 5117 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5118 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 5120 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5121 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5122 | Py_BEGIN_ALLOW_THREADS |
| 5123 | pid = wait4(pid, &status, options, &ru); |
| 5124 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5125 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5126 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5127 | } |
| 5128 | #endif /* HAVE_WAIT4 */ |
| 5129 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5130 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5131 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5132 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5133 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5134 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5135 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5136 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5137 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5138 | pid_t pid; |
| 5139 | int options; |
| 5140 | WAIT_TYPE status; |
| 5141 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5142 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5143 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 5144 | return NULL; |
| 5145 | Py_BEGIN_ALLOW_THREADS |
| 5146 | pid = waitpid(pid, &status, options); |
| 5147 | Py_END_ALLOW_THREADS |
| 5148 | if (pid == -1) |
| 5149 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5151 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5154 | #elif defined(HAVE_CWAIT) |
| 5155 | |
| 5156 | /* 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] | 5157 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5158 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5159 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5160 | |
| 5161 | static PyObject * |
| 5162 | posix_waitpid(PyObject *self, PyObject *args) |
| 5163 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5164 | Py_intptr_t pid; |
| 5165 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5166 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5167 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 5168 | return NULL; |
| 5169 | Py_BEGIN_ALLOW_THREADS |
| 5170 | pid = _cwait(&status, pid, options); |
| 5171 | Py_END_ALLOW_THREADS |
| 5172 | if (pid == -1) |
| 5173 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5174 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5175 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5176 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5177 | } |
| 5178 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5179 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5180 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5181 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5182 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5183 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5185 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5186 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5187 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5188 | pid_t pid; |
| 5189 | WAIT_TYPE status; |
| 5190 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5192 | Py_BEGIN_ALLOW_THREADS |
| 5193 | pid = wait(&status); |
| 5194 | Py_END_ALLOW_THREADS |
| 5195 | if (pid == -1) |
| 5196 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5197 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5198 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5199 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5200 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5201 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5203 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5204 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5205 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5206 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5207 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5208 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5209 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5210 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5211 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5212 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5213 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5214 | return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", |
| 5215 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5216 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5217 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5218 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5219 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5222 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5223 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5224 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5225 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5226 | 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] | 5227 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5229 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5230 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5231 | PyObject* v; |
| 5232 | char buf[MAXPATHLEN]; |
| 5233 | PyObject *opath; |
| 5234 | char *path; |
| 5235 | int n; |
| 5236 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5237 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5238 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 5239 | PyUnicode_FSConverter, &opath)) |
| 5240 | return NULL; |
| 5241 | path = PyBytes_AsString(opath); |
| 5242 | v = PySequence_GetItem(args, 0); |
| 5243 | if (v == NULL) { |
| 5244 | Py_DECREF(opath); |
| 5245 | return NULL; |
| 5246 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5247 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5248 | if (PyUnicode_Check(v)) { |
| 5249 | arg_is_unicode = 1; |
| 5250 | } |
| 5251 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5252 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5253 | Py_BEGIN_ALLOW_THREADS |
| 5254 | n = readlink(path, buf, (int) sizeof buf); |
| 5255 | Py_END_ALLOW_THREADS |
| 5256 | if (n < 0) |
| 5257 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5258 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5259 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 5260 | if (arg_is_unicode) |
| 5261 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 5262 | else |
| 5263 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5264 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5265 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5266 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5267 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5268 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5269 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5270 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5271 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5272 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5273 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5274 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5275 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5276 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5277 | } |
| 5278 | #endif /* HAVE_SYMLINK */ |
| 5279 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5280 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 5281 | |
| 5282 | PyDoc_STRVAR(win_readlink__doc__, |
| 5283 | "readlink(path) -> path\n\n\ |
| 5284 | Return a string representing the path to which the symbolic link points."); |
| 5285 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5286 | /* Windows readlink implementation */ |
| 5287 | static PyObject * |
| 5288 | win_readlink(PyObject *self, PyObject *args) |
| 5289 | { |
| 5290 | wchar_t *path; |
| 5291 | DWORD n_bytes_returned; |
| 5292 | DWORD io_result; |
| 5293 | PyObject *result; |
| 5294 | HANDLE reparse_point_handle; |
| 5295 | |
| 5296 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 5297 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 5298 | wchar_t *print_name; |
| 5299 | |
| 5300 | if (!PyArg_ParseTuple(args, |
| 5301 | "u:readlink", |
| 5302 | &path)) |
| 5303 | return NULL; |
| 5304 | |
| 5305 | /* First get a handle to the reparse point */ |
| 5306 | Py_BEGIN_ALLOW_THREADS |
| 5307 | reparse_point_handle = CreateFileW( |
| 5308 | path, |
| 5309 | 0, |
| 5310 | 0, |
| 5311 | 0, |
| 5312 | OPEN_EXISTING, |
| 5313 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 5314 | 0); |
| 5315 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5316 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5317 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 5318 | { |
| 5319 | return win32_error_unicode("readlink", path); |
| 5320 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5321 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5322 | Py_BEGIN_ALLOW_THREADS |
| 5323 | /* New call DeviceIoControl to read the reparse point */ |
| 5324 | io_result = DeviceIoControl( |
| 5325 | reparse_point_handle, |
| 5326 | FSCTL_GET_REPARSE_POINT, |
| 5327 | 0, 0, /* in buffer */ |
| 5328 | target_buffer, sizeof(target_buffer), |
| 5329 | &n_bytes_returned, |
| 5330 | 0 /* we're not using OVERLAPPED_IO */ |
| 5331 | ); |
| 5332 | CloseHandle(reparse_point_handle); |
| 5333 | Py_END_ALLOW_THREADS |
| 5334 | |
| 5335 | if (io_result==0) |
| 5336 | { |
| 5337 | return win32_error_unicode("readlink", path); |
| 5338 | } |
| 5339 | |
| 5340 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 5341 | { |
| 5342 | PyErr_SetString(PyExc_ValueError, |
| 5343 | "not a symbolic link"); |
| 5344 | return NULL; |
| 5345 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 5346 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 5347 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 5348 | |
| 5349 | result = PyUnicode_FromWideChar(print_name, |
| 5350 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5351 | return result; |
| 5352 | } |
| 5353 | |
| 5354 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 5355 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5356 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5357 | |
| 5358 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 5359 | static int has_CreateSymbolicLinkW = 0; |
| 5360 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 5361 | static int |
| 5362 | check_CreateSymbolicLinkW() |
| 5363 | { |
| 5364 | HINSTANCE hKernel32; |
| 5365 | /* only recheck */ |
| 5366 | if (has_CreateSymbolicLinkW) |
| 5367 | return has_CreateSymbolicLinkW; |
| 5368 | hKernel32 = GetModuleHandle("KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 5369 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 5370 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5371 | if (Py_CreateSymbolicLinkW) |
| 5372 | has_CreateSymbolicLinkW = 1; |
| 5373 | return has_CreateSymbolicLinkW; |
| 5374 | } |
| 5375 | |
| 5376 | PyDoc_STRVAR(win_symlink__doc__, |
| 5377 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 5378 | Create a symbolic link pointing to src named dst.\n\ |
| 5379 | target_is_directory is required if the target is to be interpreted as\n\ |
| 5380 | a directory.\n\ |
| 5381 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 5382 | NotImplementedError otherwise."); |
| 5383 | |
| 5384 | static PyObject * |
| 5385 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 5386 | { |
| 5387 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
| 5388 | PyObject *src, *dest; |
| 5389 | int target_is_directory = 0; |
| 5390 | DWORD res; |
| 5391 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5392 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5393 | if (!check_CreateSymbolicLinkW()) |
| 5394 | { |
| 5395 | /* raise NotImplementedError */ |
| 5396 | return PyErr_Format(PyExc_NotImplementedError, |
| 5397 | "CreateSymbolicLinkW not found"); |
| 5398 | } |
| 5399 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink", |
| 5400 | kwlist, &src, &dest, &target_is_directory)) |
| 5401 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 5402 | |
| 5403 | if (win32_can_symlink == 0) |
| 5404 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 5405 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5406 | if (!convert_to_unicode(&src)) { return NULL; } |
| 5407 | if (!convert_to_unicode(&dest)) { |
| 5408 | Py_DECREF(src); |
| 5409 | return NULL; |
| 5410 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5411 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5412 | /* if src is a directory, ensure target_is_directory==1 */ |
| 5413 | if( |
| 5414 | GetFileAttributesExW( |
| 5415 | PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info |
| 5416 | )) |
| 5417 | { |
| 5418 | target_is_directory = target_is_directory || |
| 5419 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 5420 | } |
| 5421 | |
| 5422 | Py_BEGIN_ALLOW_THREADS |
| 5423 | res = Py_CreateSymbolicLinkW( |
| 5424 | PyUnicode_AsUnicode(dest), |
| 5425 | PyUnicode_AsUnicode(src), |
| 5426 | target_is_directory); |
| 5427 | Py_END_ALLOW_THREADS |
| 5428 | Py_DECREF(src); |
| 5429 | Py_DECREF(dest); |
| 5430 | if (!res) |
| 5431 | { |
| 5432 | return win32_error_unicode("symlink", PyUnicode_AsUnicode(src)); |
| 5433 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5434 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5435 | Py_INCREF(Py_None); |
| 5436 | return Py_None; |
| 5437 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5438 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5439 | |
| 5440 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5441 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5442 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5443 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5444 | { |
| 5445 | ULONG value = 0; |
| 5446 | |
| 5447 | Py_BEGIN_ALLOW_THREADS |
| 5448 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5449 | Py_END_ALLOW_THREADS |
| 5450 | |
| 5451 | return value; |
| 5452 | } |
| 5453 | |
| 5454 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5455 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5456 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5457 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5458 | return Py_BuildValue("ddddd", |
| 5459 | (double)0 /* t.tms_utime / HZ */, |
| 5460 | (double)0 /* t.tms_stime / HZ */, |
| 5461 | (double)0 /* t.tms_cutime / HZ */, |
| 5462 | (double)0 /* t.tms_cstime / HZ */, |
| 5463 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5464 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5465 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 5466 | #define NEED_TICKS_PER_SECOND |
| 5467 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5468 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5469 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5470 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5471 | struct tms t; |
| 5472 | clock_t c; |
| 5473 | errno = 0; |
| 5474 | c = times(&t); |
| 5475 | if (c == (clock_t) -1) |
| 5476 | return posix_error(); |
| 5477 | return Py_BuildValue("ddddd", |
| 5478 | (double)t.tms_utime / ticks_per_second, |
| 5479 | (double)t.tms_stime / ticks_per_second, |
| 5480 | (double)t.tms_cutime / ticks_per_second, |
| 5481 | (double)t.tms_cstime / ticks_per_second, |
| 5482 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5483 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5484 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5485 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5486 | |
| 5487 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5488 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5489 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5490 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5491 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5492 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5493 | FILETIME create, exit, kernel, user; |
| 5494 | HANDLE hProc; |
| 5495 | hProc = GetCurrentProcess(); |
| 5496 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5497 | /* The fields of a FILETIME structure are the hi and lo part |
| 5498 | of a 64-bit value expressed in 100 nanosecond units. |
| 5499 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5500 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5501 | */ |
| 5502 | return Py_BuildValue( |
| 5503 | "ddddd", |
| 5504 | (double)(user.dwHighDateTime*429.4967296 + |
| 5505 | user.dwLowDateTime*1e-7), |
| 5506 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5507 | kernel.dwLowDateTime*1e-7), |
| 5508 | (double)0, |
| 5509 | (double)0, |
| 5510 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5511 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5512 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5513 | |
| 5514 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5515 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5516 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5517 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5518 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5519 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5520 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5521 | #ifdef HAVE_GETSID |
| 5522 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5523 | "getsid(pid) -> sid\n\n\ |
| 5524 | Call the system call getsid()."); |
| 5525 | |
| 5526 | static PyObject * |
| 5527 | posix_getsid(PyObject *self, PyObject *args) |
| 5528 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5529 | pid_t pid; |
| 5530 | int sid; |
| 5531 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 5532 | return NULL; |
| 5533 | sid = getsid(pid); |
| 5534 | if (sid < 0) |
| 5535 | return posix_error(); |
| 5536 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5537 | } |
| 5538 | #endif /* HAVE_GETSID */ |
| 5539 | |
| 5540 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5541 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5542 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5543 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5544 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5545 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5546 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5547 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5548 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5549 | if (setsid() < 0) |
| 5550 | return posix_error(); |
| 5551 | Py_INCREF(Py_None); |
| 5552 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5553 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5554 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5555 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5556 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5557 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5558 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5559 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5560 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5561 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5562 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5563 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5564 | pid_t pid; |
| 5565 | int pgrp; |
| 5566 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 5567 | return NULL; |
| 5568 | if (setpgid(pid, pgrp) < 0) |
| 5569 | return posix_error(); |
| 5570 | Py_INCREF(Py_None); |
| 5571 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5572 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5573 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5574 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5575 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5576 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5577 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5578 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5579 | 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] | 5580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5581 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5582 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5584 | int fd; |
| 5585 | pid_t pgid; |
| 5586 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 5587 | return NULL; |
| 5588 | pgid = tcgetpgrp(fd); |
| 5589 | if (pgid < 0) |
| 5590 | return posix_error(); |
| 5591 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5592 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5593 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5594 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5595 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5596 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5597 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5598 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5599 | 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] | 5600 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5601 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5602 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5603 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5604 | int fd; |
| 5605 | pid_t pgid; |
| 5606 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 5607 | return NULL; |
| 5608 | if (tcsetpgrp(fd, pgid) < 0) |
| 5609 | return posix_error(); |
| 5610 | Py_INCREF(Py_None); |
| 5611 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5612 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5613 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5614 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5615 | /* Functions acting on file descriptors */ |
| 5616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5617 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5618 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5619 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5620 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5621 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5622 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5623 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5624 | PyObject *ofile; |
| 5625 | char *file; |
| 5626 | int flag; |
| 5627 | int mode = 0777; |
| 5628 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5629 | |
| 5630 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5631 | PyUnicodeObject *po; |
| 5632 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5633 | Py_BEGIN_ALLOW_THREADS |
| 5634 | /* PyUnicode_AS_UNICODE OK without thread |
| 5635 | lock as it is a simple dereference. */ |
| 5636 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5637 | Py_END_ALLOW_THREADS |
| 5638 | if (fd < 0) |
| 5639 | return posix_error(); |
| 5640 | return PyLong_FromLong((long)fd); |
| 5641 | } |
| 5642 | /* Drop the argument parsing error as narrow strings |
| 5643 | are also valid. */ |
| 5644 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5645 | #endif |
| 5646 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5647 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 5648 | PyUnicode_FSConverter, &ofile, |
| 5649 | &flag, &mode)) |
| 5650 | return NULL; |
| 5651 | file = PyBytes_AsString(ofile); |
| 5652 | Py_BEGIN_ALLOW_THREADS |
| 5653 | fd = open(file, flag, mode); |
| 5654 | Py_END_ALLOW_THREADS |
| 5655 | if (fd < 0) |
| 5656 | return posix_error_with_allocated_filename(ofile); |
| 5657 | Py_DECREF(ofile); |
| 5658 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5662 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5663 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5664 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5665 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5666 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5667 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5668 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5669 | int fd, res; |
| 5670 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 5671 | return NULL; |
| 5672 | if (!_PyVerify_fd(fd)) |
| 5673 | return posix_error(); |
| 5674 | Py_BEGIN_ALLOW_THREADS |
| 5675 | res = close(fd); |
| 5676 | Py_END_ALLOW_THREADS |
| 5677 | if (res < 0) |
| 5678 | return posix_error(); |
| 5679 | Py_INCREF(Py_None); |
| 5680 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5681 | } |
| 5682 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5683 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5684 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5685 | "closerange(fd_low, fd_high)\n\n\ |
| 5686 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 5687 | |
| 5688 | static PyObject * |
| 5689 | posix_closerange(PyObject *self, PyObject *args) |
| 5690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5691 | int fd_from, fd_to, i; |
| 5692 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 5693 | return NULL; |
| 5694 | Py_BEGIN_ALLOW_THREADS |
| 5695 | for (i = fd_from; i < fd_to; i++) |
| 5696 | if (_PyVerify_fd(i)) |
| 5697 | close(i); |
| 5698 | Py_END_ALLOW_THREADS |
| 5699 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5703 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5704 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5705 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5706 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5707 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5708 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5709 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5710 | int fd; |
| 5711 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 5712 | return NULL; |
| 5713 | if (!_PyVerify_fd(fd)) |
| 5714 | return posix_error(); |
| 5715 | Py_BEGIN_ALLOW_THREADS |
| 5716 | fd = dup(fd); |
| 5717 | Py_END_ALLOW_THREADS |
| 5718 | if (fd < 0) |
| 5719 | return posix_error(); |
| 5720 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5721 | } |
| 5722 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5723 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5724 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5725 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5726 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5727 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5728 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5729 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5730 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5731 | int fd, fd2, res; |
| 5732 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 5733 | return NULL; |
| 5734 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 5735 | return posix_error(); |
| 5736 | Py_BEGIN_ALLOW_THREADS |
| 5737 | res = dup2(fd, fd2); |
| 5738 | Py_END_ALLOW_THREADS |
| 5739 | if (res < 0) |
| 5740 | return posix_error(); |
| 5741 | Py_INCREF(Py_None); |
| 5742 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5746 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5747 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5748 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5749 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5750 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5751 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5752 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5753 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5754 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5755 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5756 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5757 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5758 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5759 | PyObject *posobj; |
| 5760 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
| 5761 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5762 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5763 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5764 | switch (how) { |
| 5765 | case 0: how = SEEK_SET; break; |
| 5766 | case 1: how = SEEK_CUR; break; |
| 5767 | case 2: how = SEEK_END; break; |
| 5768 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5769 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5770 | |
| 5771 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5772 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5773 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 5774 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5775 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5776 | if (PyErr_Occurred()) |
| 5777 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5778 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5779 | if (!_PyVerify_fd(fd)) |
| 5780 | return posix_error(); |
| 5781 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5782 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5783 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5784 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5785 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5786 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5787 | Py_END_ALLOW_THREADS |
| 5788 | if (res < 0) |
| 5789 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5790 | |
| 5791 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5792 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5793 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5794 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5795 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5799 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5800 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5801 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5802 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5803 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5804 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5805 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5806 | int fd, size; |
| 5807 | Py_ssize_t n; |
| 5808 | PyObject *buffer; |
| 5809 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 5810 | return NULL; |
| 5811 | if (size < 0) { |
| 5812 | errno = EINVAL; |
| 5813 | return posix_error(); |
| 5814 | } |
| 5815 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 5816 | if (buffer == NULL) |
| 5817 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5818 | if (!_PyVerify_fd(fd)) { |
| 5819 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5820 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5821 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5822 | Py_BEGIN_ALLOW_THREADS |
| 5823 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 5824 | Py_END_ALLOW_THREADS |
| 5825 | if (n < 0) { |
| 5826 | Py_DECREF(buffer); |
| 5827 | return posix_error(); |
| 5828 | } |
| 5829 | if (n != size) |
| 5830 | _PyBytes_Resize(&buffer, n); |
| 5831 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5832 | } |
| 5833 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5834 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5835 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5836 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5837 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5838 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5839 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5840 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5841 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5842 | Py_buffer pbuf; |
| 5843 | int fd; |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 5844 | Py_ssize_t size, len; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5846 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 5847 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5848 | if (!_PyVerify_fd(fd)) { |
| 5849 | PyBuffer_Release(&pbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5850 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5851 | } |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 5852 | len = pbuf.len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5853 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 5854 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 5855 | if (len > INT_MAX) |
| 5856 | len = INT_MAX; |
| 5857 | size = write(fd, pbuf.buf, (int)len); |
| 5858 | #else |
Victor Stinner | 7234479 | 2011-01-11 00:04:12 +0000 | [diff] [blame] | 5859 | size = write(fd, pbuf.buf, len); |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 5860 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5861 | Py_END_ALLOW_THREADS |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5862 | PyBuffer_Release(&pbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5863 | if (size < 0) |
| 5864 | return posix_error(); |
| 5865 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5868 | #ifdef HAVE_SENDFILE |
| 5869 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 5870 | static int |
| 5871 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 5872 | { |
| 5873 | int i, j; |
| 5874 | *iov = PyMem_New(struct iovec, cnt); |
| 5875 | if (*iov == NULL) { |
| 5876 | PyErr_NoMemory(); |
| 5877 | return 0; |
| 5878 | } |
| 5879 | *buf = PyMem_New(Py_buffer, cnt); |
| 5880 | if (*buf == NULL) { |
| 5881 | PyMem_Del(*iov); |
| 5882 | PyErr_NoMemory(); |
| 5883 | return 0; |
| 5884 | } |
| 5885 | |
| 5886 | for (i = 0; i < cnt; i++) { |
| 5887 | if (PyObject_GetBuffer(PySequence_GetItem(seq, i), &(*buf)[i], |
| 5888 | type) == -1) { |
| 5889 | PyMem_Del(*iov); |
| 5890 | for (j = 0; j < i; j++) { |
| 5891 | PyBuffer_Release(&(*buf)[j]); |
| 5892 | } |
| 5893 | PyMem_Del(*buf); |
| 5894 | return 0; |
| 5895 | } |
| 5896 | (*iov)[i].iov_base = (*buf)[i].buf; |
| 5897 | (*iov)[i].iov_len = (*buf)[i].len; |
| 5898 | } |
| 5899 | return 1; |
| 5900 | } |
| 5901 | |
| 5902 | static void |
| 5903 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 5904 | { |
| 5905 | int i; |
| 5906 | PyMem_Del(iov); |
| 5907 | for (i = 0; i < cnt; i++) { |
| 5908 | PyBuffer_Release(&buf[i]); |
| 5909 | } |
| 5910 | PyMem_Del(buf); |
| 5911 | } |
| 5912 | #endif |
| 5913 | |
| 5914 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 5915 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 5916 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 5917 | -> byteswritten\n\ |
| 5918 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 5919 | |
| 5920 | static PyObject * |
| 5921 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 5922 | { |
| 5923 | int in, out; |
| 5924 | Py_ssize_t ret; |
| 5925 | off_t offset; |
| 5926 | |
| 5927 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 5928 | #ifndef __APPLE__ |
| 5929 | Py_ssize_t len; |
| 5930 | #endif |
| 5931 | PyObject *headers = NULL, *trailers = NULL; |
| 5932 | Py_buffer *hbuf, *tbuf; |
| 5933 | off_t sbytes; |
| 5934 | struct sf_hdtr sf; |
| 5935 | int flags = 0; |
| 5936 | sf.headers = NULL; |
| 5937 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 5938 | static char *keywords[] = {"out", "in", |
| 5939 | "offset", "count", |
| 5940 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5941 | |
| 5942 | #ifdef __APPLE__ |
| 5943 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 5944 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5945 | #else |
| 5946 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 5947 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5948 | #endif |
| 5949 | &headers, &trailers, &flags)) |
| 5950 | return NULL; |
| 5951 | if (headers != NULL) { |
| 5952 | if (!PySequence_Check(headers)) { |
| 5953 | PyErr_SetString(PyExc_TypeError, |
| 5954 | "sendfile() headers must be a sequence or None"); |
| 5955 | return NULL; |
| 5956 | } else { |
| 5957 | sf.hdr_cnt = PySequence_Size(headers); |
| 5958 | if (sf.hdr_cnt > 0 && !iov_setup(&(sf.headers), &hbuf, |
| 5959 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) |
| 5960 | return NULL; |
| 5961 | } |
| 5962 | } |
| 5963 | if (trailers != NULL) { |
| 5964 | if (!PySequence_Check(trailers)) { |
| 5965 | PyErr_SetString(PyExc_TypeError, |
| 5966 | "sendfile() trailers must be a sequence or None"); |
| 5967 | return NULL; |
| 5968 | } else { |
| 5969 | sf.trl_cnt = PySequence_Size(trailers); |
| 5970 | if (sf.trl_cnt > 0 && !iov_setup(&(sf.trailers), &tbuf, |
| 5971 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) |
| 5972 | return NULL; |
| 5973 | } |
| 5974 | } |
| 5975 | |
| 5976 | Py_BEGIN_ALLOW_THREADS |
| 5977 | #ifdef __APPLE__ |
| 5978 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 5979 | #else |
| 5980 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 5981 | #endif |
| 5982 | Py_END_ALLOW_THREADS |
| 5983 | |
| 5984 | if (sf.headers != NULL) |
| 5985 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 5986 | if (sf.trailers != NULL) |
| 5987 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 5988 | |
| 5989 | if (ret < 0) { |
| 5990 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 5991 | if (sbytes != 0) { |
| 5992 | // some data has been sent |
| 5993 | goto done; |
| 5994 | } |
| 5995 | else { |
| 5996 | // no data has been sent; upper application is supposed |
| 5997 | // to retry on EAGAIN or EBUSY |
| 5998 | return posix_error(); |
| 5999 | } |
| 6000 | } |
| 6001 | return posix_error(); |
| 6002 | } |
| 6003 | goto done; |
| 6004 | |
| 6005 | done: |
| 6006 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6007 | return Py_BuildValue("l", sbytes); |
| 6008 | #else |
| 6009 | return Py_BuildValue("L", sbytes); |
| 6010 | #endif |
| 6011 | |
| 6012 | #else |
| 6013 | Py_ssize_t count; |
| 6014 | PyObject *offobj; |
| 6015 | static char *keywords[] = {"out", "in", |
| 6016 | "offset", "count", NULL}; |
| 6017 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 6018 | keywords, &out, &in, &offobj, &count)) |
| 6019 | return NULL; |
| 6020 | #ifdef linux |
| 6021 | if (offobj == Py_None) { |
| 6022 | Py_BEGIN_ALLOW_THREADS |
| 6023 | ret = sendfile(out, in, NULL, count); |
| 6024 | Py_END_ALLOW_THREADS |
| 6025 | if (ret < 0) |
| 6026 | return posix_error(); |
| 6027 | Py_INCREF(Py_None); |
| 6028 | return Py_BuildValue("nO", ret, Py_None); |
| 6029 | } |
| 6030 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 6031 | if (!_parse_off_t(offobj, &offset)) |
| 6032 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6033 | Py_BEGIN_ALLOW_THREADS |
| 6034 | ret = sendfile(out, in, &offset, count); |
| 6035 | Py_END_ALLOW_THREADS |
| 6036 | if (ret < 0) |
| 6037 | return posix_error(); |
| 6038 | return Py_BuildValue("n", ret); |
| 6039 | #endif |
| 6040 | } |
| 6041 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6042 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6043 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6044 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6045 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6046 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6047 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6048 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6049 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6050 | int fd; |
| 6051 | STRUCT_STAT st; |
| 6052 | int res; |
| 6053 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 6054 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6055 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6056 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 6057 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6058 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6059 | if (!_PyVerify_fd(fd)) |
| 6060 | return posix_error(); |
| 6061 | Py_BEGIN_ALLOW_THREADS |
| 6062 | res = FSTAT(fd, &st); |
| 6063 | Py_END_ALLOW_THREADS |
| 6064 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6065 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6066 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6067 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6068 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6069 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6070 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6071 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6072 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6073 | } |
| 6074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6075 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6076 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6077 | 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] | 6078 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6079 | |
| 6080 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 6081 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6083 | int fd; |
| 6084 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 6085 | return NULL; |
| 6086 | if (!_PyVerify_fd(fd)) |
| 6087 | return PyBool_FromLong(0); |
| 6088 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6089 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6090 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6091 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6092 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6093 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6094 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6095 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6096 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6097 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6098 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6099 | #if defined(PYOS_OS2) |
| 6100 | HFILE read, write; |
| 6101 | APIRET rc; |
| 6102 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6103 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6104 | rc = DosCreatePipe( &read, &write, 4096); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6105 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6106 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6107 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6108 | |
| 6109 | return Py_BuildValue("(ii)", read, write); |
| 6110 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6111 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6112 | int fds[2]; |
| 6113 | int res; |
| 6114 | Py_BEGIN_ALLOW_THREADS |
| 6115 | res = pipe(fds); |
| 6116 | Py_END_ALLOW_THREADS |
| 6117 | if (res != 0) |
| 6118 | return posix_error(); |
| 6119 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6120 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6121 | HANDLE read, write; |
| 6122 | int read_fd, write_fd; |
| 6123 | BOOL ok; |
| 6124 | Py_BEGIN_ALLOW_THREADS |
| 6125 | ok = CreatePipe(&read, &write, NULL, 0); |
| 6126 | Py_END_ALLOW_THREADS |
| 6127 | if (!ok) |
| 6128 | return win32_error("CreatePipe", NULL); |
| 6129 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 6130 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 6131 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6132 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6133 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6134 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6135 | #endif /* HAVE_PIPE */ |
| 6136 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6137 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6138 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6139 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6140 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6141 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6142 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6143 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6144 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6145 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6146 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6147 | char *filename; |
| 6148 | int mode = 0666; |
| 6149 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6150 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 6151 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6152 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6153 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | Py_BEGIN_ALLOW_THREADS |
| 6155 | res = mkfifo(filename, mode); |
| 6156 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6157 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6158 | if (res < 0) |
| 6159 | return posix_error(); |
| 6160 | Py_INCREF(Py_None); |
| 6161 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6162 | } |
| 6163 | #endif |
| 6164 | |
| 6165 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6166 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6167 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6168 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6169 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 6170 | named filename. mode specifies both the permissions to use and the\n\ |
| 6171 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 6172 | 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] | 6173 | device defines the newly created device special file (probably using\n\ |
| 6174 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6175 | |
| 6176 | |
| 6177 | static PyObject * |
| 6178 | posix_mknod(PyObject *self, PyObject *args) |
| 6179 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6180 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6181 | char *filename; |
| 6182 | int mode = 0600; |
| 6183 | int device = 0; |
| 6184 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6185 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 6186 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6187 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6188 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6189 | Py_BEGIN_ALLOW_THREADS |
| 6190 | res = mknod(filename, mode, device); |
| 6191 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6192 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6193 | if (res < 0) |
| 6194 | return posix_error(); |
| 6195 | Py_INCREF(Py_None); |
| 6196 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6197 | } |
| 6198 | #endif |
| 6199 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6200 | #ifdef HAVE_DEVICE_MACROS |
| 6201 | PyDoc_STRVAR(posix_major__doc__, |
| 6202 | "major(device) -> major number\n\ |
| 6203 | Extracts a device major number from a raw device number."); |
| 6204 | |
| 6205 | static PyObject * |
| 6206 | posix_major(PyObject *self, PyObject *args) |
| 6207 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6208 | int device; |
| 6209 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6210 | return NULL; |
| 6211 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6212 | } |
| 6213 | |
| 6214 | PyDoc_STRVAR(posix_minor__doc__, |
| 6215 | "minor(device) -> minor number\n\ |
| 6216 | Extracts a device minor number from a raw device number."); |
| 6217 | |
| 6218 | static PyObject * |
| 6219 | posix_minor(PyObject *self, PyObject *args) |
| 6220 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6221 | int device; |
| 6222 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6223 | return NULL; |
| 6224 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
| 6227 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6228 | "makedev(major, minor) -> device number\n\ |
| 6229 | Composes a raw device number from the major and minor device numbers."); |
| 6230 | |
| 6231 | static PyObject * |
| 6232 | posix_makedev(PyObject *self, PyObject *args) |
| 6233 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6234 | int major, minor; |
| 6235 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 6236 | return NULL; |
| 6237 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6238 | } |
| 6239 | #endif /* device macros */ |
| 6240 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6241 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6242 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6243 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6244 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6245 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6246 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6247 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6248 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6249 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6250 | int fd; |
| 6251 | off_t length; |
| 6252 | int res; |
| 6253 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6254 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6255 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
| 6256 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6257 | |
| 6258 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6259 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6260 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6261 | length = PyLong_Check(lenobj) ? |
| 6262 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6263 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6264 | if (PyErr_Occurred()) |
| 6265 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6266 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6267 | Py_BEGIN_ALLOW_THREADS |
| 6268 | res = ftruncate(fd, length); |
| 6269 | Py_END_ALLOW_THREADS |
| 6270 | if (res < 0) |
| 6271 | return posix_error(); |
| 6272 | Py_INCREF(Py_None); |
| 6273 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6274 | } |
| 6275 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6276 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6277 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6278 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6279 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6280 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6281 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6282 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6283 | * get re-set with another call for the same key. */ |
| 6284 | static PyObject *posix_putenv_garbage; |
| 6285 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6286 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6287 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6288 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6289 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6290 | wchar_t *s1, *s2; |
| 6291 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6292 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6293 | PyObject *os1, *os2; |
| 6294 | char *s1, *s2; |
| 6295 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6296 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6297 | PyObject *newstr = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6298 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6299 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6300 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6301 | if (!PyArg_ParseTuple(args, |
| 6302 | "uu:putenv", |
| 6303 | &s1, &s2)) |
| 6304 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6305 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6306 | if (!PyArg_ParseTuple(args, |
| 6307 | "O&O&:putenv", |
| 6308 | PyUnicode_FSConverter, &os1, |
| 6309 | PyUnicode_FSConverter, &os2)) |
| 6310 | return NULL; |
| 6311 | s1 = PyBytes_AsString(os1); |
| 6312 | s2 = PyBytes_AsString(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6313 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6314 | |
| 6315 | #if defined(PYOS_OS2) |
| 6316 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6317 | APIRET rc; |
| 6318 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6319 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6320 | if (rc != NO_ERROR) { |
| 6321 | os2_error(rc); |
| 6322 | goto error; |
| 6323 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6324 | |
| 6325 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6326 | APIRET rc; |
| 6327 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6328 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6329 | if (rc != NO_ERROR) { |
| 6330 | os2_error(rc); |
| 6331 | goto error; |
| 6332 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6333 | } else { |
| 6334 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6335 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 6336 | /* len includes space for a trailing \0; the size arg to |
| 6337 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6338 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6339 | len = wcslen(s1) + wcslen(s2) + 2; |
| 6340 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6341 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6342 | len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6343 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6344 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6345 | if (newstr == NULL) { |
| 6346 | PyErr_NoMemory(); |
| 6347 | goto error; |
| 6348 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6349 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6350 | newenv = PyUnicode_AsUnicode(newstr); |
| 6351 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 6352 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6353 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6354 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6355 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6356 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6357 | newenv = PyBytes_AS_STRING(newstr); |
| 6358 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6359 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6360 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6361 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6362 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6363 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6364 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6365 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6366 | * this will cause previous value to be collected. This has to |
| 6367 | * happen after the real putenv() call because the old value |
| 6368 | * was still accessible until then. */ |
| 6369 | if (PyDict_SetItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6370 | #ifdef MS_WINDOWS |
| 6371 | PyTuple_GET_ITEM(args, 0), |
| 6372 | #else |
| 6373 | os1, |
| 6374 | #endif |
| 6375 | newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6376 | /* really not much we can do; just leak */ |
| 6377 | PyErr_Clear(); |
| 6378 | } |
| 6379 | else { |
| 6380 | Py_DECREF(newstr); |
| 6381 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6382 | |
| 6383 | #if defined(PYOS_OS2) |
| 6384 | } |
| 6385 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6386 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6387 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6388 | Py_DECREF(os1); |
| 6389 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6390 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6391 | Py_RETURN_NONE; |
| 6392 | |
| 6393 | error: |
| 6394 | #ifndef MS_WINDOWS |
| 6395 | Py_DECREF(os1); |
| 6396 | Py_DECREF(os2); |
| 6397 | #endif |
| 6398 | Py_XDECREF(newstr); |
| 6399 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6400 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6401 | #endif /* putenv */ |
| 6402 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6403 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6404 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6405 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6406 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6407 | |
| 6408 | static PyObject * |
| 6409 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6410 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6411 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6412 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6413 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6414 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6415 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6416 | #else |
| 6417 | PyObject *os1; |
| 6418 | char *s1; |
| 6419 | |
| 6420 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
| 6421 | PyUnicode_FSConverter, &os1)) |
| 6422 | return NULL; |
| 6423 | s1 = PyBytes_AsString(os1); |
| 6424 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6425 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6426 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6427 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6428 | /* Remove the key from posix_putenv_garbage; |
| 6429 | * this will cause it to be collected. This has to |
| 6430 | * happen after the real unsetenv() call because the |
| 6431 | * old value was still accessible until then. |
| 6432 | */ |
| 6433 | if (PyDict_DelItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6434 | #ifdef MS_WINDOWS |
| 6435 | PyTuple_GET_ITEM(args, 0) |
| 6436 | #else |
| 6437 | os1 |
| 6438 | #endif |
| 6439 | )) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6440 | /* really not much we can do; just leak */ |
| 6441 | PyErr_Clear(); |
| 6442 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6443 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6444 | #ifndef MS_WINDOWS |
| 6445 | Py_DECREF(os1); |
| 6446 | #endif |
| 6447 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6448 | } |
| 6449 | #endif /* unsetenv */ |
| 6450 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6451 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6452 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6453 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6454 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6455 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6456 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6457 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6458 | int code; |
| 6459 | char *message; |
| 6460 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 6461 | return NULL; |
| 6462 | message = strerror(code); |
| 6463 | if (message == NULL) { |
| 6464 | PyErr_SetString(PyExc_ValueError, |
| 6465 | "strerror() argument out of range"); |
| 6466 | return NULL; |
| 6467 | } |
| 6468 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6469 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6470 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6471 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6472 | #ifdef HAVE_SYS_WAIT_H |
| 6473 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6474 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6475 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6476 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6477 | 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] | 6478 | |
| 6479 | static PyObject * |
| 6480 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6481 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6482 | WAIT_TYPE status; |
| 6483 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6484 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6485 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 6486 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6487 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6488 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6489 | } |
| 6490 | #endif /* WCOREDUMP */ |
| 6491 | |
| 6492 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6493 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6494 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6495 | 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] | 6496 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6497 | |
| 6498 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6499 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6500 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6501 | WAIT_TYPE status; |
| 6502 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6503 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6504 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 6505 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6506 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6507 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6508 | } |
| 6509 | #endif /* WIFCONTINUED */ |
| 6510 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6511 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6512 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6513 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6514 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6515 | |
| 6516 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6517 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6518 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6519 | WAIT_TYPE status; |
| 6520 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6521 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6522 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 6523 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6524 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6525 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6526 | } |
| 6527 | #endif /* WIFSTOPPED */ |
| 6528 | |
| 6529 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6530 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6531 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6532 | 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] | 6533 | |
| 6534 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6535 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6536 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6537 | WAIT_TYPE status; |
| 6538 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6539 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6540 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 6541 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6542 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6543 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6544 | } |
| 6545 | #endif /* WIFSIGNALED */ |
| 6546 | |
| 6547 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6548 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6549 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6550 | 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] | 6551 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6552 | |
| 6553 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6554 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6555 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6556 | WAIT_TYPE status; |
| 6557 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6558 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6559 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 6560 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6561 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6562 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6563 | } |
| 6564 | #endif /* WIFEXITED */ |
| 6565 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6566 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6567 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6568 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6569 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6570 | |
| 6571 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6572 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6573 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6574 | WAIT_TYPE status; |
| 6575 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6576 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6577 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 6578 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6579 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6580 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6581 | } |
| 6582 | #endif /* WEXITSTATUS */ |
| 6583 | |
| 6584 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6585 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6586 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6587 | 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] | 6588 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6589 | |
| 6590 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6591 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6592 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6593 | WAIT_TYPE status; |
| 6594 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6595 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6596 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 6597 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6598 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6599 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6600 | } |
| 6601 | #endif /* WTERMSIG */ |
| 6602 | |
| 6603 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6604 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6605 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6606 | Return the signal that stopped the process that provided\n\ |
| 6607 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6608 | |
| 6609 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6610 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6611 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6612 | WAIT_TYPE status; |
| 6613 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6614 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6615 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 6616 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6617 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6618 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6619 | } |
| 6620 | #endif /* WSTOPSIG */ |
| 6621 | |
| 6622 | #endif /* HAVE_SYS_WAIT_H */ |
| 6623 | |
| 6624 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6625 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6626 | #ifdef _SCO_DS |
| 6627 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6628 | needed definitions in sys/statvfs.h */ |
| 6629 | #define _SVID3 |
| 6630 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6631 | #include <sys/statvfs.h> |
| 6632 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6633 | static PyObject* |
| 6634 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6635 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6636 | if (v == NULL) |
| 6637 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6638 | |
| 6639 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6640 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 6641 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 6642 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 6643 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 6644 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 6645 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 6646 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 6647 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 6648 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 6649 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6650 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6651 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 6652 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 6653 | PyStructSequence_SET_ITEM(v, 2, |
| 6654 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 6655 | PyStructSequence_SET_ITEM(v, 3, |
| 6656 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 6657 | PyStructSequence_SET_ITEM(v, 4, |
| 6658 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 6659 | PyStructSequence_SET_ITEM(v, 5, |
| 6660 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 6661 | PyStructSequence_SET_ITEM(v, 6, |
| 6662 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 6663 | PyStructSequence_SET_ITEM(v, 7, |
| 6664 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 6665 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 6666 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6667 | #endif |
| 6668 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6669 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6672 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6673 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6674 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6675 | |
| 6676 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6677 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6678 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6679 | int fd, res; |
| 6680 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6681 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6682 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 6683 | return NULL; |
| 6684 | Py_BEGIN_ALLOW_THREADS |
| 6685 | res = fstatvfs(fd, &st); |
| 6686 | Py_END_ALLOW_THREADS |
| 6687 | if (res != 0) |
| 6688 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6689 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6690 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6691 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6692 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6693 | |
| 6694 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6695 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6696 | #include <sys/statvfs.h> |
| 6697 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6698 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6699 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6700 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6701 | |
| 6702 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6703 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6704 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6705 | char *path; |
| 6706 | int res; |
| 6707 | struct statvfs st; |
| 6708 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
| 6709 | return NULL; |
| 6710 | Py_BEGIN_ALLOW_THREADS |
| 6711 | res = statvfs(path, &st); |
| 6712 | Py_END_ALLOW_THREADS |
| 6713 | if (res != 0) |
| 6714 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6715 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6716 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6717 | } |
| 6718 | #endif /* HAVE_STATVFS */ |
| 6719 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6720 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6721 | * It maps strings representing configuration variable names to |
| 6722 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6723 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6724 | * rarely-used constants. There are three separate tables that use |
| 6725 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6726 | * |
| 6727 | * This code is always included, even if none of the interfaces that |
| 6728 | * need it are included. The #if hackery needed to avoid it would be |
| 6729 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6730 | */ |
| 6731 | struct constdef { |
| 6732 | char *name; |
| 6733 | long value; |
| 6734 | }; |
| 6735 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6736 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6737 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 6738 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6739 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6740 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6741 | *valuep = PyLong_AS_LONG(arg); |
| 6742 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6743 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 6744 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6745 | /* look up the value in the table using a binary search */ |
| 6746 | size_t lo = 0; |
| 6747 | size_t mid; |
| 6748 | size_t hi = tablesize; |
| 6749 | int cmp; |
| 6750 | const char *confname; |
| 6751 | if (!PyUnicode_Check(arg)) { |
| 6752 | PyErr_SetString(PyExc_TypeError, |
| 6753 | "configuration names must be strings or integers"); |
| 6754 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6755 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6756 | confname = _PyUnicode_AsString(arg); |
| 6757 | if (confname == NULL) |
| 6758 | return 0; |
| 6759 | while (lo < hi) { |
| 6760 | mid = (lo + hi) / 2; |
| 6761 | cmp = strcmp(confname, table[mid].name); |
| 6762 | if (cmp < 0) |
| 6763 | hi = mid; |
| 6764 | else if (cmp > 0) |
| 6765 | lo = mid + 1; |
| 6766 | else { |
| 6767 | *valuep = table[mid].value; |
| 6768 | return 1; |
| 6769 | } |
| 6770 | } |
| 6771 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6772 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | |
| 6777 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6778 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6779 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6780 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6781 | #endif |
| 6782 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6783 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6784 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6785 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6786 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6787 | #endif |
| 6788 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6789 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6790 | #endif |
| 6791 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6792 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6793 | #endif |
| 6794 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6795 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6796 | #endif |
| 6797 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6798 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6799 | #endif |
| 6800 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6801 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6802 | #endif |
| 6803 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6804 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6805 | #endif |
| 6806 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6807 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6808 | #endif |
| 6809 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6810 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6811 | #endif |
| 6812 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6813 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6814 | #endif |
| 6815 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6816 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6817 | #endif |
| 6818 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6819 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6820 | #endif |
| 6821 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6822 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6823 | #endif |
| 6824 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6825 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6826 | #endif |
| 6827 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6828 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6829 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 6830 | #ifdef _PC_ACL_ENABLED |
| 6831 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 6832 | #endif |
| 6833 | #ifdef _PC_MIN_HOLE_SIZE |
| 6834 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 6835 | #endif |
| 6836 | #ifdef _PC_ALLOC_SIZE_MIN |
| 6837 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 6838 | #endif |
| 6839 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 6840 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 6841 | #endif |
| 6842 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 6843 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 6844 | #endif |
| 6845 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 6846 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 6847 | #endif |
| 6848 | #ifdef _PC_REC_XFER_ALIGN |
| 6849 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 6850 | #endif |
| 6851 | #ifdef _PC_SYMLINK_MAX |
| 6852 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 6853 | #endif |
| 6854 | #ifdef _PC_XATTR_ENABLED |
| 6855 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 6856 | #endif |
| 6857 | #ifdef _PC_XATTR_EXISTS |
| 6858 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 6859 | #endif |
| 6860 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 6861 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 6862 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6863 | }; |
| 6864 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6865 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6866 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6867 | { |
| 6868 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6869 | sizeof(posix_constants_pathconf) |
| 6870 | / sizeof(struct constdef)); |
| 6871 | } |
| 6872 | #endif |
| 6873 | |
| 6874 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6875 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6876 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6877 | 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] | 6878 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6879 | |
| 6880 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6881 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6882 | { |
| 6883 | PyObject *result = NULL; |
| 6884 | int name, fd; |
| 6885 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6886 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6887 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6888 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6889 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6890 | errno = 0; |
| 6891 | limit = fpathconf(fd, name); |
| 6892 | if (limit == -1 && errno != 0) |
| 6893 | posix_error(); |
| 6894 | else |
| 6895 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6896 | } |
| 6897 | return result; |
| 6898 | } |
| 6899 | #endif |
| 6900 | |
| 6901 | |
| 6902 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6903 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6904 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6905 | 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] | 6906 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6907 | |
| 6908 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6909 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6910 | { |
| 6911 | PyObject *result = NULL; |
| 6912 | int name; |
| 6913 | char *path; |
| 6914 | |
| 6915 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6916 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6917 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6918 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6919 | errno = 0; |
| 6920 | limit = pathconf(path, name); |
| 6921 | if (limit == -1 && errno != 0) { |
| 6922 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6923 | /* could be a path or name problem */ |
| 6924 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6925 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6926 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6927 | } |
| 6928 | else |
| 6929 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6930 | } |
| 6931 | return result; |
| 6932 | } |
| 6933 | #endif |
| 6934 | |
| 6935 | #ifdef HAVE_CONFSTR |
| 6936 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6937 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6938 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6939 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 6940 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6941 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 6942 | #endif |
| 6943 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6944 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 6945 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6946 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6947 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6948 | #endif |
| 6949 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6950 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6951 | #endif |
| 6952 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6953 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6954 | #endif |
| 6955 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6956 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6957 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6958 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6959 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6960 | #endif |
| 6961 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6962 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6963 | #endif |
| 6964 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6965 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6966 | #endif |
| 6967 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6968 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6969 | #endif |
| 6970 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6971 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6972 | #endif |
| 6973 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6974 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6975 | #endif |
| 6976 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6977 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6978 | #endif |
| 6979 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6980 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6981 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6982 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6983 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6984 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6985 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6986 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6987 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6988 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6989 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6990 | #endif |
| 6991 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6992 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6993 | #endif |
| 6994 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6995 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6996 | #endif |
| 6997 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6998 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6999 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7000 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7001 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7002 | #endif |
| 7003 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7004 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7005 | #endif |
| 7006 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7007 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7008 | #endif |
| 7009 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7010 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7011 | #endif |
| 7012 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7013 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7014 | #endif |
| 7015 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7016 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7017 | #endif |
| 7018 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7019 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7020 | #endif |
| 7021 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7022 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7023 | #endif |
| 7024 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7025 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7026 | #endif |
| 7027 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7028 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7029 | #endif |
| 7030 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7031 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7032 | #endif |
| 7033 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7034 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7035 | #endif |
| 7036 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7037 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7038 | #endif |
| 7039 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7040 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7041 | #endif |
| 7042 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7043 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7044 | #endif |
| 7045 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7046 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7047 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7048 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7049 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7050 | #endif |
| 7051 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7052 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7053 | #endif |
| 7054 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7055 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7056 | #endif |
| 7057 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7058 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7059 | #endif |
| 7060 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7061 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7062 | #endif |
| 7063 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7064 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7065 | #endif |
| 7066 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7067 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7068 | #endif |
| 7069 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7070 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7071 | #endif |
| 7072 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7073 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7074 | #endif |
| 7075 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7076 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7077 | #endif |
| 7078 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7079 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7080 | #endif |
| 7081 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7082 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7083 | #endif |
| 7084 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7085 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7086 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7087 | }; |
| 7088 | |
| 7089 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7090 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7091 | { |
| 7092 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 7093 | sizeof(posix_constants_confstr) |
| 7094 | / sizeof(struct constdef)); |
| 7095 | } |
| 7096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7097 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7098 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7099 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7100 | |
| 7101 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7102 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7103 | { |
| 7104 | PyObject *result = NULL; |
| 7105 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7106 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7107 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7108 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7109 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 7110 | return NULL; |
| 7111 | |
| 7112 | errno = 0; |
| 7113 | len = confstr(name, buffer, sizeof(buffer)); |
| 7114 | if (len == 0) { |
| 7115 | if (errno) { |
| 7116 | posix_error(); |
| 7117 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7118 | } |
| 7119 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7120 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7121 | } |
| 7122 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7123 | |
| 7124 | if ((unsigned int)len >= sizeof(buffer)) { |
| 7125 | char *buf = PyMem_Malloc(len); |
| 7126 | if (buf == NULL) |
| 7127 | return PyErr_NoMemory(); |
| 7128 | confstr(name, buf, len); |
| 7129 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 7130 | PyMem_Free(buf); |
| 7131 | } |
| 7132 | else |
| 7133 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7134 | return result; |
| 7135 | } |
| 7136 | #endif |
| 7137 | |
| 7138 | |
| 7139 | #ifdef HAVE_SYSCONF |
| 7140 | static struct constdef posix_constants_sysconf[] = { |
| 7141 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7142 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7143 | #endif |
| 7144 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7145 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7146 | #endif |
| 7147 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7148 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7149 | #endif |
| 7150 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7151 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7152 | #endif |
| 7153 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7154 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7155 | #endif |
| 7156 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7157 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7158 | #endif |
| 7159 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7160 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7161 | #endif |
| 7162 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7163 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7164 | #endif |
| 7165 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7166 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7167 | #endif |
| 7168 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7169 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7170 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7171 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7172 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7173 | #endif |
| 7174 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7175 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7176 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7177 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7178 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7179 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7180 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7181 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7182 | #endif |
| 7183 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7184 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7185 | #endif |
| 7186 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7187 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7188 | #endif |
| 7189 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7190 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7191 | #endif |
| 7192 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7193 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7194 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7195 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7196 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7197 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7198 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7199 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7200 | #endif |
| 7201 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7202 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7203 | #endif |
| 7204 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7205 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7206 | #endif |
| 7207 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7208 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7209 | #endif |
| 7210 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7211 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7212 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7213 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7214 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7215 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7216 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7217 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7218 | #endif |
| 7219 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7220 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7221 | #endif |
| 7222 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7223 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7224 | #endif |
| 7225 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7226 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7227 | #endif |
| 7228 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7229 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7230 | #endif |
| 7231 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7232 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7233 | #endif |
| 7234 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7235 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7236 | #endif |
| 7237 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7238 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7239 | #endif |
| 7240 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7241 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7242 | #endif |
| 7243 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7244 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7245 | #endif |
| 7246 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7247 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7248 | #endif |
| 7249 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7250 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7251 | #endif |
| 7252 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7253 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7254 | #endif |
| 7255 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7256 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7257 | #endif |
| 7258 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7259 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7260 | #endif |
| 7261 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7262 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7263 | #endif |
| 7264 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7265 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7266 | #endif |
| 7267 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7268 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7269 | #endif |
| 7270 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7271 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7272 | #endif |
| 7273 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7274 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7275 | #endif |
| 7276 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7277 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7278 | #endif |
| 7279 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7280 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7281 | #endif |
| 7282 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7283 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7284 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7285 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7286 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7287 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7288 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7289 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7290 | #endif |
| 7291 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7292 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7293 | #endif |
| 7294 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7295 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7296 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7297 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7298 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7299 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7300 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7301 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7302 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7303 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7304 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7305 | #endif |
| 7306 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7307 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7308 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7309 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7310 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7311 | #endif |
| 7312 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7313 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7314 | #endif |
| 7315 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7316 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7317 | #endif |
| 7318 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7319 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7320 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7321 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7322 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7323 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7324 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7325 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7326 | #endif |
| 7327 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7328 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7329 | #endif |
| 7330 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7331 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7332 | #endif |
| 7333 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7334 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7335 | #endif |
| 7336 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7337 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7338 | #endif |
| 7339 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7340 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7341 | #endif |
| 7342 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7343 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7344 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7345 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7346 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7347 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7348 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7349 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7350 | #endif |
| 7351 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7352 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7353 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7354 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7355 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7356 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7357 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7358 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7359 | #endif |
| 7360 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7361 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7362 | #endif |
| 7363 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7364 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7365 | #endif |
| 7366 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7367 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7368 | #endif |
| 7369 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7370 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7371 | #endif |
| 7372 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7373 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7374 | #endif |
| 7375 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7376 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7377 | #endif |
| 7378 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7379 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7380 | #endif |
| 7381 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7382 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7383 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7384 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7385 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7386 | #endif |
| 7387 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7388 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7389 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7390 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7391 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7392 | #endif |
| 7393 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7394 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7395 | #endif |
| 7396 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7397 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7398 | #endif |
| 7399 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7400 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7401 | #endif |
| 7402 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7403 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7404 | #endif |
| 7405 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7406 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7407 | #endif |
| 7408 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7409 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7410 | #endif |
| 7411 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7412 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7413 | #endif |
| 7414 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7415 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7416 | #endif |
| 7417 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7418 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7419 | #endif |
| 7420 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7421 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7422 | #endif |
| 7423 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7424 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7425 | #endif |
| 7426 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7427 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7428 | #endif |
| 7429 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7430 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7431 | #endif |
| 7432 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7433 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7434 | #endif |
| 7435 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7436 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7437 | #endif |
| 7438 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7439 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7440 | #endif |
| 7441 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7442 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7443 | #endif |
| 7444 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7445 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7446 | #endif |
| 7447 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7448 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7449 | #endif |
| 7450 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7451 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7452 | #endif |
| 7453 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7454 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7455 | #endif |
| 7456 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7457 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7458 | #endif |
| 7459 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7460 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7461 | #endif |
| 7462 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7463 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7464 | #endif |
| 7465 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7466 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7467 | #endif |
| 7468 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7469 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7470 | #endif |
| 7471 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7472 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7473 | #endif |
| 7474 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7475 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7476 | #endif |
| 7477 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7478 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7479 | #endif |
| 7480 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7481 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7482 | #endif |
| 7483 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7484 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7485 | #endif |
| 7486 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7487 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7488 | #endif |
| 7489 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7490 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7491 | #endif |
| 7492 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7493 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7494 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7495 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7496 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7497 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7498 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7499 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7500 | #endif |
| 7501 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7502 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7503 | #endif |
| 7504 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7505 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7506 | #endif |
| 7507 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7508 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7509 | #endif |
| 7510 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7511 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7512 | #endif |
| 7513 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7514 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7515 | #endif |
| 7516 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7517 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7518 | #endif |
| 7519 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7520 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7521 | #endif |
| 7522 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7523 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7524 | #endif |
| 7525 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7526 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7527 | #endif |
| 7528 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7529 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7530 | #endif |
| 7531 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7532 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7533 | #endif |
| 7534 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7535 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7536 | #endif |
| 7537 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7538 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7539 | #endif |
| 7540 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7541 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7542 | #endif |
| 7543 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7544 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7545 | #endif |
| 7546 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7547 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7548 | #endif |
| 7549 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7550 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7551 | #endif |
| 7552 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7553 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7554 | #endif |
| 7555 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7556 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7557 | #endif |
| 7558 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7559 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7560 | #endif |
| 7561 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7562 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7563 | #endif |
| 7564 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7565 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7566 | #endif |
| 7567 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7568 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7569 | #endif |
| 7570 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7571 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7572 | #endif |
| 7573 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7574 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7575 | #endif |
| 7576 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7577 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7578 | #endif |
| 7579 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7580 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7581 | #endif |
| 7582 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7583 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7584 | #endif |
| 7585 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7586 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7587 | #endif |
| 7588 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7589 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7590 | #endif |
| 7591 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7592 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7593 | #endif |
| 7594 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7595 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7596 | #endif |
| 7597 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7598 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7599 | #endif |
| 7600 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7601 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7602 | #endif |
| 7603 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7604 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7605 | #endif |
| 7606 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7607 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7608 | #endif |
| 7609 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7610 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7611 | #endif |
| 7612 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7613 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7614 | #endif |
| 7615 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7616 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7617 | #endif |
| 7618 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7619 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7620 | #endif |
| 7621 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7622 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7623 | #endif |
| 7624 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7625 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7626 | #endif |
| 7627 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7628 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7629 | #endif |
| 7630 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7631 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7632 | #endif |
| 7633 | }; |
| 7634 | |
| 7635 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7636 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7637 | { |
| 7638 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7639 | sizeof(posix_constants_sysconf) |
| 7640 | / sizeof(struct constdef)); |
| 7641 | } |
| 7642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7643 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7644 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7645 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7646 | |
| 7647 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7648 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7649 | { |
| 7650 | PyObject *result = NULL; |
| 7651 | int name; |
| 7652 | |
| 7653 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7654 | int value; |
| 7655 | |
| 7656 | errno = 0; |
| 7657 | value = sysconf(name); |
| 7658 | if (value == -1 && errno != 0) |
| 7659 | posix_error(); |
| 7660 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7661 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7662 | } |
| 7663 | return result; |
| 7664 | } |
| 7665 | #endif |
| 7666 | |
| 7667 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7668 | /* This code is used to ensure that the tables of configuration value names |
| 7669 | * are in sorted order as required by conv_confname(), and also to build the |
| 7670 | * the exported dictionaries that are used to publish information about the |
| 7671 | * names available on the host platform. |
| 7672 | * |
| 7673 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7674 | * when used, even for platforms we're not able to test on. It also makes |
| 7675 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7676 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7677 | |
| 7678 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7679 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7680 | { |
| 7681 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7682 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7683 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7684 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7685 | |
| 7686 | return strcmp(c1->name, c2->name); |
| 7687 | } |
| 7688 | |
| 7689 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7690 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7691 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7692 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7693 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7694 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7695 | |
| 7696 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7697 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7698 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7699 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7700 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7701 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7702 | PyObject *o = PyLong_FromLong(table[i].value); |
| 7703 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7704 | Py_XDECREF(o); |
| 7705 | Py_DECREF(d); |
| 7706 | return -1; |
| 7707 | } |
| 7708 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7709 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7710 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7711 | } |
| 7712 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7713 | /* Return -1 on failure, 0 on success. */ |
| 7714 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7715 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7716 | { |
| 7717 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7718 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7719 | sizeof(posix_constants_pathconf) |
| 7720 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7721 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7722 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7723 | #endif |
| 7724 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7725 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7726 | sizeof(posix_constants_confstr) |
| 7727 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7728 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7729 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7730 | #endif |
| 7731 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7732 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7733 | sizeof(posix_constants_sysconf) |
| 7734 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7735 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7736 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7737 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7738 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7739 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7740 | |
| 7741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7742 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7743 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7744 | 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] | 7745 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7746 | |
| 7747 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7748 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7749 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7750 | abort(); |
| 7751 | /*NOTREACHED*/ |
| 7752 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7753 | return NULL; |
| 7754 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7755 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7756 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7757 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7758 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7759 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7760 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7761 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7762 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7763 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7764 | application (if any) its extension is associated.\n\ |
| 7765 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7766 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7767 | \n\ |
| 7768 | startfile returns as soon as the associated application is launched.\n\ |
| 7769 | There is no option to wait for the application to close, and no way\n\ |
| 7770 | to retrieve the application's exit status.\n\ |
| 7771 | \n\ |
| 7772 | The filepath is relative to the current directory. If you want to use\n\ |
| 7773 | 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] | 7774 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7775 | |
| 7776 | static PyObject * |
| 7777 | win32_startfile(PyObject *self, PyObject *args) |
| 7778 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7779 | PyObject *ofilepath; |
| 7780 | char *filepath; |
| 7781 | char *operation = NULL; |
| 7782 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 7783 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | PyObject *unipath, *woperation = NULL; |
| 7785 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7786 | &unipath, &operation)) { |
| 7787 | PyErr_Clear(); |
| 7788 | goto normal; |
| 7789 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 7790 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7791 | if (operation) { |
| 7792 | woperation = PyUnicode_DecodeASCII(operation, |
| 7793 | strlen(operation), NULL); |
| 7794 | if (!woperation) { |
| 7795 | PyErr_Clear(); |
| 7796 | operation = NULL; |
| 7797 | goto normal; |
| 7798 | } |
| 7799 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 7800 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7801 | Py_BEGIN_ALLOW_THREADS |
| 7802 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 7803 | PyUnicode_AS_UNICODE(unipath), |
| 7804 | NULL, NULL, SW_SHOWNORMAL); |
| 7805 | Py_END_ALLOW_THREADS |
| 7806 | |
| 7807 | Py_XDECREF(woperation); |
| 7808 | if (rc <= (HINSTANCE)32) { |
| 7809 | PyObject *errval = win32_error_unicode("startfile", |
| 7810 | PyUnicode_AS_UNICODE(unipath)); |
| 7811 | return errval; |
| 7812 | } |
| 7813 | Py_INCREF(Py_None); |
| 7814 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7815 | |
| 7816 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7817 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 7818 | PyUnicode_FSConverter, &ofilepath, |
| 7819 | &operation)) |
| 7820 | return NULL; |
| 7821 | filepath = PyBytes_AsString(ofilepath); |
| 7822 | Py_BEGIN_ALLOW_THREADS |
| 7823 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7824 | NULL, NULL, SW_SHOWNORMAL); |
| 7825 | Py_END_ALLOW_THREADS |
| 7826 | if (rc <= (HINSTANCE)32) { |
| 7827 | PyObject *errval = win32_error("startfile", filepath); |
| 7828 | Py_DECREF(ofilepath); |
| 7829 | return errval; |
| 7830 | } |
| 7831 | Py_DECREF(ofilepath); |
| 7832 | Py_INCREF(Py_None); |
| 7833 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7834 | } |
| 7835 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7836 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7837 | #ifdef HAVE_GETLOADAVG |
| 7838 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7839 | "getloadavg() -> (float, float, float)\n\n\ |
| 7840 | Return the number of processes in the system run queue averaged over\n\ |
| 7841 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7842 | was unobtainable"); |
| 7843 | |
| 7844 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7845 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7846 | { |
| 7847 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7848 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7849 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7850 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7851 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7852 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7853 | } |
| 7854 | #endif |
| 7855 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7856 | #ifdef MS_WINDOWS |
| 7857 | |
| 7858 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7859 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 7860 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7861 | |
| 7862 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7863 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7864 | DWORD dwFlags ); |
| 7865 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7866 | BYTE *pbBuffer ); |
| 7867 | |
| 7868 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 7869 | /* This handle is never explicitly released. Instead, the operating |
| 7870 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7871 | static HCRYPTPROV hCryptProv = 0; |
| 7872 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7873 | static PyObject* |
| 7874 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7875 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7876 | int howMany; |
| 7877 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7878 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7879 | /* Read arguments */ |
| 7880 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7881 | return NULL; |
| 7882 | if (howMany < 0) |
| 7883 | return PyErr_Format(PyExc_ValueError, |
| 7884 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7885 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7886 | if (hCryptProv == 0) { |
| 7887 | HINSTANCE hAdvAPI32 = NULL; |
| 7888 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7889 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7890 | /* Obtain handle to the DLL containing CryptoAPI |
| 7891 | This should not fail */ |
| 7892 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7893 | if(hAdvAPI32 == NULL) |
| 7894 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7895 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7896 | /* Obtain pointers to the CryptoAPI functions |
| 7897 | This will fail on some early versions of Win95 */ |
| 7898 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7899 | hAdvAPI32, |
| 7900 | "CryptAcquireContextA"); |
| 7901 | if (pCryptAcquireContext == NULL) |
| 7902 | return PyErr_Format(PyExc_NotImplementedError, |
| 7903 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7904 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7905 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7906 | hAdvAPI32, "CryptGenRandom"); |
| 7907 | if (pCryptGenRandom == NULL) |
| 7908 | return PyErr_Format(PyExc_NotImplementedError, |
| 7909 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7910 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7911 | /* Acquire context */ |
| 7912 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7913 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7914 | return win32_error("CryptAcquireContext", NULL); |
| 7915 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7916 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7917 | /* Allocate bytes */ |
| 7918 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 7919 | if (result != NULL) { |
| 7920 | /* Get random data */ |
| 7921 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 7922 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7923 | PyBytes_AS_STRING(result))) { |
| 7924 | Py_DECREF(result); |
| 7925 | return win32_error("CryptGenRandom", NULL); |
| 7926 | } |
| 7927 | } |
| 7928 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7929 | } |
| 7930 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7931 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7932 | PyDoc_STRVAR(device_encoding__doc__, |
| 7933 | "device_encoding(fd) -> str\n\n\ |
| 7934 | Return a string describing the encoding of the device\n\ |
| 7935 | if the output is a terminal; else return None."); |
| 7936 | |
| 7937 | static PyObject * |
| 7938 | device_encoding(PyObject *self, PyObject *args) |
| 7939 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7940 | int fd; |
| 7941 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 7942 | return NULL; |
| 7943 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 7944 | Py_INCREF(Py_None); |
| 7945 | return Py_None; |
| 7946 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7947 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7948 | if (fd == 0) { |
| 7949 | char buf[100]; |
| 7950 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 7951 | return PyUnicode_FromString(buf); |
| 7952 | } |
| 7953 | if (fd == 1 || fd == 2) { |
| 7954 | char buf[100]; |
| 7955 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 7956 | return PyUnicode_FromString(buf); |
| 7957 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7958 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7959 | { |
| 7960 | char *codeset = nl_langinfo(CODESET); |
| 7961 | if (codeset != NULL && codeset[0] != 0) |
| 7962 | return PyUnicode_FromString(codeset); |
| 7963 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7964 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7965 | Py_INCREF(Py_None); |
| 7966 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7967 | } |
| 7968 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7969 | #ifdef __VMS |
| 7970 | /* Use openssl random routine */ |
| 7971 | #include <openssl/rand.h> |
| 7972 | PyDoc_STRVAR(vms_urandom__doc__, |
| 7973 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 7974 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7975 | |
| 7976 | static PyObject* |
| 7977 | vms_urandom(PyObject *self, PyObject *args) |
| 7978 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7979 | int howMany; |
| 7980 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7981 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7982 | /* Read arguments */ |
| 7983 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7984 | return NULL; |
| 7985 | if (howMany < 0) |
| 7986 | return PyErr_Format(PyExc_ValueError, |
| 7987 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7988 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7989 | /* Allocate bytes */ |
| 7990 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 7991 | if (result != NULL) { |
| 7992 | /* Get random data */ |
| 7993 | if (RAND_pseudo_bytes((unsigned char*) |
| 7994 | PyBytes_AS_STRING(result), |
| 7995 | howMany) < 0) { |
| 7996 | Py_DECREF(result); |
| 7997 | return PyErr_Format(PyExc_ValueError, |
| 7998 | "RAND_pseudo_bytes"); |
| 7999 | } |
| 8000 | } |
| 8001 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8002 | } |
| 8003 | #endif |
| 8004 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8005 | #ifdef HAVE_SETRESUID |
| 8006 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 8007 | "setresuid(ruid, euid, suid)\n\n\ |
| 8008 | Set the current process's real, effective, and saved user ids."); |
| 8009 | |
| 8010 | static PyObject* |
| 8011 | posix_setresuid (PyObject *self, PyObject *args) |
| 8012 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8013 | /* We assume uid_t is no larger than a long. */ |
| 8014 | long ruid, euid, suid; |
| 8015 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 8016 | return NULL; |
| 8017 | if (setresuid(ruid, euid, suid) < 0) |
| 8018 | return posix_error(); |
| 8019 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8020 | } |
| 8021 | #endif |
| 8022 | |
| 8023 | #ifdef HAVE_SETRESGID |
| 8024 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 8025 | "setresgid(rgid, egid, sgid)\n\n\ |
| 8026 | Set the current process's real, effective, and saved group ids."); |
| 8027 | |
| 8028 | static PyObject* |
| 8029 | posix_setresgid (PyObject *self, PyObject *args) |
| 8030 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8031 | /* We assume uid_t is no larger than a long. */ |
| 8032 | long rgid, egid, sgid; |
| 8033 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 8034 | return NULL; |
| 8035 | if (setresgid(rgid, egid, sgid) < 0) |
| 8036 | return posix_error(); |
| 8037 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8038 | } |
| 8039 | #endif |
| 8040 | |
| 8041 | #ifdef HAVE_GETRESUID |
| 8042 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 8043 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 8044 | Get tuple of the current process's real, effective, and saved user ids."); |
| 8045 | |
| 8046 | static PyObject* |
| 8047 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 8048 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8049 | uid_t ruid, euid, suid; |
| 8050 | long l_ruid, l_euid, l_suid; |
| 8051 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 8052 | return posix_error(); |
| 8053 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 8054 | l_ruid = ruid; |
| 8055 | l_euid = euid; |
| 8056 | l_suid = suid; |
| 8057 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8058 | } |
| 8059 | #endif |
| 8060 | |
| 8061 | #ifdef HAVE_GETRESGID |
| 8062 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 8063 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 8064 | 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] | 8065 | |
| 8066 | static PyObject* |
| 8067 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 8068 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8069 | uid_t rgid, egid, sgid; |
| 8070 | long l_rgid, l_egid, l_sgid; |
| 8071 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 8072 | return posix_error(); |
| 8073 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 8074 | l_rgid = rgid; |
| 8075 | l_egid = egid; |
| 8076 | l_sgid = sgid; |
| 8077 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8078 | } |
| 8079 | #endif |
| 8080 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 8081 | /* Posix *at family of functions: |
| 8082 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 8083 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 8084 | unlinkat, utimensat, mkfifoat */ |
| 8085 | |
| 8086 | #ifdef HAVE_FACCESSAT |
| 8087 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 8088 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 8089 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8090 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 8091 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 8092 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8093 | is interpreted relative to the current working directory."); |
| 8094 | |
| 8095 | static PyObject * |
| 8096 | posix_faccessat(PyObject *self, PyObject *args) |
| 8097 | { |
| 8098 | PyObject *opath; |
| 8099 | char *path; |
| 8100 | int mode; |
| 8101 | int res; |
| 8102 | int dirfd, flags = 0; |
| 8103 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 8104 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 8105 | return NULL; |
| 8106 | path = PyBytes_AsString(opath); |
| 8107 | Py_BEGIN_ALLOW_THREADS |
| 8108 | res = faccessat(dirfd, path, mode, flags); |
| 8109 | Py_END_ALLOW_THREADS |
| 8110 | Py_DECREF(opath); |
| 8111 | return PyBool_FromLong(res == 0); |
| 8112 | } |
| 8113 | #endif |
| 8114 | |
| 8115 | #ifdef HAVE_FCHMODAT |
| 8116 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 8117 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 8118 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8119 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 8120 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8121 | is interpreted relative to the current working directory."); |
| 8122 | |
| 8123 | static PyObject * |
| 8124 | posix_fchmodat(PyObject *self, PyObject *args) |
| 8125 | { |
| 8126 | int dirfd, mode, res; |
| 8127 | int flags = 0; |
| 8128 | PyObject *opath; |
| 8129 | char *path; |
| 8130 | |
| 8131 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 8132 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 8133 | return NULL; |
| 8134 | |
| 8135 | path = PyBytes_AsString(opath); |
| 8136 | |
| 8137 | Py_BEGIN_ALLOW_THREADS |
| 8138 | res = fchmodat(dirfd, path, mode, flags); |
| 8139 | Py_END_ALLOW_THREADS |
| 8140 | Py_DECREF(opath); |
| 8141 | if (res < 0) |
| 8142 | return posix_error(); |
| 8143 | Py_RETURN_NONE; |
| 8144 | } |
| 8145 | #endif /* HAVE_FCHMODAT */ |
| 8146 | |
| 8147 | #ifdef HAVE_FCHOWNAT |
| 8148 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 8149 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 8150 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8151 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 8152 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8153 | is interpreted relative to the current working directory."); |
| 8154 | |
| 8155 | static PyObject * |
| 8156 | posix_fchownat(PyObject *self, PyObject *args) |
| 8157 | { |
| 8158 | PyObject *opath; |
| 8159 | int dirfd, res; |
| 8160 | long uid, gid; |
| 8161 | int flags = 0; |
| 8162 | char *path; |
| 8163 | |
| 8164 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 8165 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 8166 | return NULL; |
| 8167 | |
| 8168 | path = PyBytes_AsString(opath); |
| 8169 | |
| 8170 | Py_BEGIN_ALLOW_THREADS |
| 8171 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 8172 | Py_END_ALLOW_THREADS |
| 8173 | Py_DECREF(opath); |
| 8174 | if (res < 0) |
| 8175 | return posix_error(); |
| 8176 | Py_RETURN_NONE; |
| 8177 | } |
| 8178 | #endif /* HAVE_FCHOWNAT */ |
| 8179 | |
| 8180 | #ifdef HAVE_FSTATAT |
| 8181 | PyDoc_STRVAR(posix_fstatat__doc__, |
| 8182 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
| 8183 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8184 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 8185 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8186 | is interpreted relative to the current working directory."); |
| 8187 | |
| 8188 | static PyObject * |
| 8189 | posix_fstatat(PyObject *self, PyObject *args) |
| 8190 | { |
| 8191 | PyObject *opath; |
| 8192 | char *path; |
| 8193 | STRUCT_STAT st; |
| 8194 | int dirfd, res, flags = 0; |
| 8195 | |
| 8196 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 8197 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 8198 | return NULL; |
| 8199 | path = PyBytes_AsString(opath); |
| 8200 | |
| 8201 | Py_BEGIN_ALLOW_THREADS |
| 8202 | res = fstatat(dirfd, path, &st, flags); |
| 8203 | Py_END_ALLOW_THREADS |
| 8204 | Py_DECREF(opath); |
| 8205 | if (res != 0) |
| 8206 | return posix_error(); |
| 8207 | |
| 8208 | return _pystat_fromstructstat(&st); |
| 8209 | } |
| 8210 | #endif |
| 8211 | |
| 8212 | #ifdef HAVE_FUTIMESAT |
| 8213 | PyDoc_STRVAR(posix_futimesat__doc__, |
| 8214 | "futimesat(dirfd, path, (atime, mtime))\n\ |
| 8215 | futimesat(dirfd, path, None)\n\n\ |
| 8216 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8217 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8218 | is interpreted relative to the current working directory."); |
| 8219 | |
| 8220 | static PyObject * |
| 8221 | posix_futimesat(PyObject *self, PyObject *args) |
| 8222 | { |
| 8223 | PyObject *opath; |
| 8224 | char *path; |
| 8225 | int res, dirfd; |
| 8226 | PyObject* arg; |
| 8227 | |
| 8228 | struct timeval buf[2]; |
| 8229 | |
| 8230 | if (!PyArg_ParseTuple(args, "iO&O:futimesat", |
| 8231 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 8232 | return NULL; |
| 8233 | path = PyBytes_AsString(opath); |
| 8234 | if (arg == Py_None) { |
| 8235 | /* optional time values not given */ |
| 8236 | Py_BEGIN_ALLOW_THREADS |
| 8237 | res = futimesat(dirfd, path, NULL); |
| 8238 | Py_END_ALLOW_THREADS |
| 8239 | } |
| 8240 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 8241 | PyErr_SetString(PyExc_TypeError, |
| 8242 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 8243 | Py_DECREF(opath); |
| 8244 | return NULL; |
| 8245 | } |
| 8246 | else { |
| 8247 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 8248 | &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) { |
| 8249 | Py_DECREF(opath); |
| 8250 | return NULL; |
| 8251 | } |
| 8252 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 8253 | &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) { |
| 8254 | Py_DECREF(opath); |
| 8255 | return NULL; |
| 8256 | } |
| 8257 | Py_BEGIN_ALLOW_THREADS |
| 8258 | res = futimesat(dirfd, path, buf); |
| 8259 | Py_END_ALLOW_THREADS |
| 8260 | } |
| 8261 | Py_DECREF(opath); |
| 8262 | if (res < 0) { |
| 8263 | return posix_error(); |
| 8264 | } |
| 8265 | Py_RETURN_NONE; |
| 8266 | } |
| 8267 | #endif |
| 8268 | |
| 8269 | #ifdef HAVE_LINKAT |
| 8270 | PyDoc_STRVAR(posix_linkat__doc__, |
| 8271 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 8272 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 8273 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 8274 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 8275 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 8276 | srcpath is interpreted relative to the current working directory. This\n\ |
| 8277 | also applies for dstpath."); |
| 8278 | |
| 8279 | static PyObject * |
| 8280 | posix_linkat(PyObject *self, PyObject *args) |
| 8281 | { |
| 8282 | PyObject *osrc, *odst; |
| 8283 | char *src, *dst; |
| 8284 | int res, srcfd, dstfd; |
| 8285 | int flags = 0; |
| 8286 | |
| 8287 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 8288 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 8289 | return NULL; |
| 8290 | src = PyBytes_AsString(osrc); |
| 8291 | dst = PyBytes_AsString(odst); |
| 8292 | Py_BEGIN_ALLOW_THREADS |
| 8293 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 8294 | Py_END_ALLOW_THREADS |
| 8295 | Py_DECREF(osrc); |
| 8296 | Py_DECREF(odst); |
| 8297 | if (res < 0) |
| 8298 | return posix_error(); |
| 8299 | Py_RETURN_NONE; |
| 8300 | } |
| 8301 | #endif /* HAVE_LINKAT */ |
| 8302 | |
| 8303 | #ifdef HAVE_MKDIRAT |
| 8304 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 8305 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 8306 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8307 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8308 | is interpreted relative to the current working directory."); |
| 8309 | |
| 8310 | static PyObject * |
| 8311 | posix_mkdirat(PyObject *self, PyObject *args) |
| 8312 | { |
| 8313 | int res, dirfd; |
| 8314 | PyObject *opath; |
| 8315 | char *path; |
| 8316 | int mode = 0777; |
| 8317 | |
| 8318 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 8319 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 8320 | return NULL; |
| 8321 | path = PyBytes_AsString(opath); |
| 8322 | Py_BEGIN_ALLOW_THREADS |
| 8323 | res = mkdirat(dirfd, path, mode); |
| 8324 | Py_END_ALLOW_THREADS |
| 8325 | Py_DECREF(opath); |
| 8326 | if (res < 0) |
| 8327 | return posix_error(); |
| 8328 | Py_RETURN_NONE; |
| 8329 | } |
| 8330 | #endif |
| 8331 | |
| 8332 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 8333 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 8334 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 8335 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8336 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8337 | is interpreted relative to the current working directory."); |
| 8338 | |
| 8339 | static PyObject * |
| 8340 | posix_mknodat(PyObject *self, PyObject *args) |
| 8341 | { |
| 8342 | PyObject *opath; |
| 8343 | char *filename; |
| 8344 | int mode = 0600; |
| 8345 | int device = 0; |
| 8346 | int res, dirfd; |
| 8347 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 8348 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 8349 | return NULL; |
| 8350 | filename = PyBytes_AS_STRING(opath); |
| 8351 | Py_BEGIN_ALLOW_THREADS |
| 8352 | res = mknodat(dirfd, filename, mode, device); |
| 8353 | Py_END_ALLOW_THREADS |
| 8354 | Py_DECREF(opath); |
| 8355 | if (res < 0) |
| 8356 | return posix_error(); |
| 8357 | Py_RETURN_NONE; |
| 8358 | } |
| 8359 | #endif |
| 8360 | |
| 8361 | #ifdef HAVE_OPENAT |
| 8362 | PyDoc_STRVAR(posix_openat__doc__, |
| 8363 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 8364 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8365 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8366 | is interpreted relative to the current working directory."); |
| 8367 | |
| 8368 | static PyObject * |
| 8369 | posix_openat(PyObject *self, PyObject *args) |
| 8370 | { |
| 8371 | PyObject *ofile; |
| 8372 | char *file; |
| 8373 | int flag, dirfd, fd; |
| 8374 | int mode = 0777; |
| 8375 | |
| 8376 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 8377 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 8378 | &flag, &mode)) |
| 8379 | return NULL; |
| 8380 | file = PyBytes_AsString(ofile); |
| 8381 | Py_BEGIN_ALLOW_THREADS |
| 8382 | fd = openat(dirfd, file, flag, mode); |
| 8383 | Py_END_ALLOW_THREADS |
| 8384 | Py_DECREF(ofile); |
| 8385 | if (fd < 0) |
| 8386 | return posix_error(); |
| 8387 | return PyLong_FromLong((long)fd); |
| 8388 | } |
| 8389 | #endif |
| 8390 | |
| 8391 | #ifdef HAVE_READLINKAT |
| 8392 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 8393 | "readlinkat(dirfd, path) -> path\n\n\ |
| 8394 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8395 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8396 | is interpreted relative to the current working directory."); |
| 8397 | |
| 8398 | static PyObject * |
| 8399 | posix_readlinkat(PyObject *self, PyObject *args) |
| 8400 | { |
| 8401 | PyObject *v, *opath; |
| 8402 | char buf[MAXPATHLEN]; |
| 8403 | char *path; |
| 8404 | int n, dirfd; |
| 8405 | int arg_is_unicode = 0; |
| 8406 | |
| 8407 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 8408 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 8409 | return NULL; |
| 8410 | path = PyBytes_AsString(opath); |
| 8411 | v = PySequence_GetItem(args, 1); |
| 8412 | if (v == NULL) { |
| 8413 | Py_DECREF(opath); |
| 8414 | return NULL; |
| 8415 | } |
| 8416 | |
| 8417 | if (PyUnicode_Check(v)) { |
| 8418 | arg_is_unicode = 1; |
| 8419 | } |
| 8420 | Py_DECREF(v); |
| 8421 | |
| 8422 | Py_BEGIN_ALLOW_THREADS |
| 8423 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 8424 | Py_END_ALLOW_THREADS |
| 8425 | Py_DECREF(opath); |
| 8426 | if (n < 0) |
| 8427 | return posix_error(); |
| 8428 | |
| 8429 | if (arg_is_unicode) |
| 8430 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 8431 | else |
| 8432 | return PyBytes_FromStringAndSize(buf, n); |
| 8433 | } |
| 8434 | #endif /* HAVE_READLINKAT */ |
| 8435 | |
| 8436 | #ifdef HAVE_RENAMEAT |
| 8437 | PyDoc_STRVAR(posix_renameat__doc__, |
| 8438 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 8439 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 8440 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 8441 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 8442 | oldpath is interpreted relative to the current working directory. This\n\ |
| 8443 | also applies for newpath."); |
| 8444 | |
| 8445 | static PyObject * |
| 8446 | posix_renameat(PyObject *self, PyObject *args) |
| 8447 | { |
| 8448 | int res; |
| 8449 | PyObject *opathold, *opathnew; |
| 8450 | char *opath, *npath; |
| 8451 | int oldfd, newfd; |
| 8452 | |
| 8453 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 8454 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 8455 | return NULL; |
| 8456 | opath = PyBytes_AsString(opathold); |
| 8457 | npath = PyBytes_AsString(opathnew); |
| 8458 | Py_BEGIN_ALLOW_THREADS |
| 8459 | res = renameat(oldfd, opath, newfd, npath); |
| 8460 | Py_END_ALLOW_THREADS |
| 8461 | Py_DECREF(opathold); |
| 8462 | Py_DECREF(opathnew); |
| 8463 | if (res < 0) |
| 8464 | return posix_error(); |
| 8465 | Py_RETURN_NONE; |
| 8466 | } |
| 8467 | #endif |
| 8468 | |
| 8469 | #if HAVE_SYMLINKAT |
| 8470 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 8471 | "symlinkat(src, dstfd, dst)\n\n\ |
| 8472 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 8473 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 8474 | is interpreted relative to the current working directory."); |
| 8475 | |
| 8476 | static PyObject * |
| 8477 | posix_symlinkat(PyObject *self, PyObject *args) |
| 8478 | { |
| 8479 | int res, dstfd; |
| 8480 | PyObject *osrc, *odst; |
| 8481 | char *src, *dst; |
| 8482 | |
| 8483 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 8484 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 8485 | return NULL; |
| 8486 | src = PyBytes_AsString(osrc); |
| 8487 | dst = PyBytes_AsString(odst); |
| 8488 | Py_BEGIN_ALLOW_THREADS |
| 8489 | res = symlinkat(src, dstfd, dst); |
| 8490 | Py_END_ALLOW_THREADS |
| 8491 | Py_DECREF(osrc); |
| 8492 | Py_DECREF(odst); |
| 8493 | if (res < 0) |
| 8494 | return posix_error(); |
| 8495 | Py_RETURN_NONE; |
| 8496 | } |
| 8497 | #endif /* HAVE_SYMLINKAT */ |
| 8498 | |
| 8499 | #ifdef HAVE_UNLINKAT |
| 8500 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 8501 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 8502 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8503 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 8504 | specified, unlinkat() behaves like rmdir().\n\ |
| 8505 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8506 | is interpreted relative to the current working directory."); |
| 8507 | |
| 8508 | static PyObject * |
| 8509 | posix_unlinkat(PyObject *self, PyObject *args) |
| 8510 | { |
| 8511 | int dirfd, res, flags = 0; |
| 8512 | PyObject *opath; |
| 8513 | char *path; |
| 8514 | |
| 8515 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 8516 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 8517 | return NULL; |
| 8518 | path = PyBytes_AsString(opath); |
| 8519 | Py_BEGIN_ALLOW_THREADS |
| 8520 | res = unlinkat(dirfd, path, flags); |
| 8521 | Py_END_ALLOW_THREADS |
| 8522 | Py_DECREF(opath); |
| 8523 | if (res < 0) |
| 8524 | return posix_error(); |
| 8525 | Py_RETURN_NONE; |
| 8526 | } |
| 8527 | #endif |
| 8528 | |
| 8529 | #ifdef HAVE_UTIMENSAT |
| 8530 | PyDoc_STRVAR(posix_utimensat__doc__, |
| 8531 | "utimensat(dirfd, path, (atime_sec, atime_nsec),\n\ |
| 8532 | (mtime_sec, mtime_nsec), flags)\n\ |
| 8533 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 8534 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 8535 | relative, it is taken as relative to dirfd.\n\ |
| 8536 | The second form sets atime and mtime to the current time.\n\ |
| 8537 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 8538 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8539 | is interpreted relative to the current working directory.\n\ |
| 8540 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 8541 | current time.\n\ |
| 8542 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 8543 | |
| 8544 | static PyObject * |
| 8545 | posix_utimensat(PyObject *self, PyObject *args) |
| 8546 | { |
| 8547 | PyObject *opath; |
| 8548 | char *path; |
| 8549 | int res, dirfd, flags = 0; |
| 8550 | PyObject *atime, *mtime; |
| 8551 | |
| 8552 | struct timespec buf[2]; |
| 8553 | |
| 8554 | if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat", |
| 8555 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 8556 | return NULL; |
| 8557 | path = PyBytes_AsString(opath); |
| 8558 | if (atime == Py_None && mtime == Py_None) { |
| 8559 | /* optional time values not given */ |
| 8560 | Py_BEGIN_ALLOW_THREADS |
| 8561 | res = utimensat(dirfd, path, NULL, flags); |
| 8562 | Py_END_ALLOW_THREADS |
| 8563 | } |
| 8564 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 8565 | PyErr_SetString(PyExc_TypeError, |
| 8566 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 8567 | Py_DECREF(opath); |
| 8568 | return NULL; |
| 8569 | } |
| 8570 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 8571 | PyErr_SetString(PyExc_TypeError, |
| 8572 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 8573 | Py_DECREF(opath); |
| 8574 | return NULL; |
| 8575 | } |
| 8576 | else { |
| 8577 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 8578 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 8579 | Py_DECREF(opath); |
| 8580 | return NULL; |
| 8581 | } |
| 8582 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 8583 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 8584 | Py_DECREF(opath); |
| 8585 | return NULL; |
| 8586 | } |
| 8587 | Py_BEGIN_ALLOW_THREADS |
| 8588 | res = utimensat(dirfd, path, buf, flags); |
| 8589 | Py_END_ALLOW_THREADS |
| 8590 | } |
| 8591 | Py_DECREF(opath); |
| 8592 | if (res < 0) { |
| 8593 | return posix_error(); |
| 8594 | } |
| 8595 | Py_RETURN_NONE; |
| 8596 | } |
| 8597 | #endif |
| 8598 | |
| 8599 | #ifdef HAVE_MKFIFOAT |
| 8600 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 8601 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 8602 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8603 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8604 | is interpreted relative to the current working directory."); |
| 8605 | |
| 8606 | static PyObject * |
| 8607 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 8608 | { |
| 8609 | PyObject *opath; |
| 8610 | char *filename; |
| 8611 | int mode = 0666; |
| 8612 | int res, dirfd; |
| 8613 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 8614 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 8615 | return NULL; |
| 8616 | filename = PyBytes_AS_STRING(opath); |
| 8617 | Py_BEGIN_ALLOW_THREADS |
| 8618 | res = mkfifoat(dirfd, filename, mode); |
| 8619 | Py_END_ALLOW_THREADS |
| 8620 | Py_DECREF(opath); |
| 8621 | if (res < 0) |
| 8622 | return posix_error(); |
| 8623 | Py_RETURN_NONE; |
| 8624 | } |
| 8625 | #endif |
| 8626 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8627 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8628 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8629 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8630 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8631 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8632 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 8633 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8634 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 8635 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8636 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8637 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8638 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8639 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8640 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8641 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8642 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8643 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8644 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8645 | #endif /* HAVE_LCHMOD */ |
| 8646 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8647 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8648 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 8649 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8650 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 8651 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8652 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8653 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8654 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8655 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8656 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8657 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8658 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8659 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8660 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8661 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8662 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 8663 | METH_NOARGS, posix_getcwd__doc__}, |
| 8664 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 8665 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8666 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8667 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8668 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8669 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8670 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 8671 | #ifdef HAVE_FDOPENDIR |
| 8672 | {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__}, |
| 8673 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8674 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 8675 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8676 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8677 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8678 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 8679 | #ifdef HAVE_GETPRIORITY |
| 8680 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 8681 | #endif /* HAVE_GETPRIORITY */ |
| 8682 | #ifdef HAVE_SETPRIORITY |
| 8683 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 8684 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8685 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8686 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8687 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8688 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 8689 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8690 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 8691 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 8692 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 8693 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8695 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8696 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8697 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8698 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 8699 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8700 | win_symlink__doc__}, |
| 8701 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8702 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8704 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8705 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8706 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8707 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8708 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8709 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 8710 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 8711 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8712 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8713 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8714 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8716 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8717 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 8718 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8719 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8720 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8721 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 8722 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8723 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8724 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 8725 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8726 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8727 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8728 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8729 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8730 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8731 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8732 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8733 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8734 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8735 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8736 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8737 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8738 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8739 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8740 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8741 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8742 | #endif /* HAVE_GETEGID */ |
| 8743 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8744 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8745 | #endif /* HAVE_GETEUID */ |
| 8746 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8747 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8748 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8749 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8750 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8751 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8752 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8753 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8754 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8755 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8756 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8757 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8758 | #endif /* HAVE_GETPPID */ |
| 8759 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8760 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8761 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8762 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8764 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8765 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8766 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8767 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8768 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8770 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8771 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8772 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8773 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 8774 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8775 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 8776 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 8777 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 8778 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8779 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8780 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8781 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8782 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8783 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8784 | #endif /* HAVE_SETEUID */ |
| 8785 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8786 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8787 | #endif /* HAVE_SETEGID */ |
| 8788 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8789 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8790 | #endif /* HAVE_SETREUID */ |
| 8791 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8792 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8793 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8794 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8795 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8796 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8797 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8798 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8799 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8800 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8801 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8802 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8803 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8804 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8805 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8806 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8807 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8808 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8809 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8810 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8811 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8812 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8813 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8814 | #endif /* HAVE_WAIT3 */ |
| 8815 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8816 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8817 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8818 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8819 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8820 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8821 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8822 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8823 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8824 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8825 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8826 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8827 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8828 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8829 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8830 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8831 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8832 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8833 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8834 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8835 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8836 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 8837 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 8838 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 8839 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 8840 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 8841 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 8842 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 8843 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 8844 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8845 | #ifdef HAVE_SENDFILE |
| 8846 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 8847 | posix_sendfile__doc__}, |
| 8848 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8849 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 8850 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8851 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8852 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8853 | #endif |
| 8854 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8855 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8856 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8857 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8858 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8859 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8860 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8861 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 8862 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 8863 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8864 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8865 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8866 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8867 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8868 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8869 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8870 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8871 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8872 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8873 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8874 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8875 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8876 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8877 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8878 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8879 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8880 | #endif |
| 8881 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8882 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8883 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8884 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8885 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8886 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8887 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8888 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8889 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8890 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8891 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8892 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8893 | #endif /* WIFSTOPPED */ |
| 8894 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8895 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8896 | #endif /* WIFSIGNALED */ |
| 8897 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8898 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8899 | #endif /* WIFEXITED */ |
| 8900 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8901 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8902 | #endif /* WEXITSTATUS */ |
| 8903 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8904 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8905 | #endif /* WTERMSIG */ |
| 8906 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8907 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8908 | #endif /* WSTOPSIG */ |
| 8909 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8910 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8911 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8912 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8913 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8914 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8915 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8916 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8917 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8918 | #endif |
| 8919 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8920 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8921 | #endif |
| 8922 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8923 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8924 | #endif |
| 8925 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8926 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8927 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8928 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8929 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8930 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8931 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 8932 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 8933 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8934 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8935 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8936 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8937 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8938 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8939 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8940 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8941 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8942 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8943 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8944 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8945 | #endif |
| 8946 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8947 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8948 | #endif |
| 8949 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8950 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8951 | #endif |
| 8952 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8953 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8954 | #endif |
| 8955 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 8956 | /* posix *at family of functions */ |
| 8957 | #ifdef HAVE_FACCESSAT |
| 8958 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 8959 | #endif |
| 8960 | #ifdef HAVE_FCHMODAT |
| 8961 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 8962 | #endif /* HAVE_FCHMODAT */ |
| 8963 | #ifdef HAVE_FCHOWNAT |
| 8964 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 8965 | #endif /* HAVE_FCHOWNAT */ |
| 8966 | #ifdef HAVE_FSTATAT |
| 8967 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
| 8968 | #endif |
| 8969 | #ifdef HAVE_FUTIMESAT |
| 8970 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 8971 | #endif |
| 8972 | #ifdef HAVE_LINKAT |
| 8973 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 8974 | #endif /* HAVE_LINKAT */ |
| 8975 | #ifdef HAVE_MKDIRAT |
| 8976 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 8977 | #endif |
| 8978 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 8979 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 8980 | #endif |
| 8981 | #ifdef HAVE_OPENAT |
| 8982 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 8983 | #endif |
| 8984 | #ifdef HAVE_READLINKAT |
| 8985 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 8986 | #endif /* HAVE_READLINKAT */ |
| 8987 | #ifdef HAVE_RENAMEAT |
| 8988 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 8989 | #endif |
| 8990 | #if HAVE_SYMLINKAT |
| 8991 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 8992 | #endif /* HAVE_SYMLINKAT */ |
| 8993 | #ifdef HAVE_UNLINKAT |
| 8994 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 8995 | #endif |
| 8996 | #ifdef HAVE_UTIMENSAT |
| 8997 | {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__}, |
| 8998 | #endif |
| 8999 | #ifdef HAVE_MKFIFOAT |
| 9000 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 9001 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9002 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9003 | }; |
| 9004 | |
| 9005 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9006 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9007 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9008 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9009 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9010 | } |
| 9011 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9012 | #if defined(PYOS_OS2) |
| 9013 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9014 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9015 | { |
| 9016 | APIRET rc; |
| 9017 | ULONG values[QSV_MAX+1]; |
| 9018 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 9019 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9020 | |
| 9021 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 9022 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9023 | Py_END_ALLOW_THREADS |
| 9024 | |
| 9025 | if (rc != NO_ERROR) { |
| 9026 | os2_error(rc); |
| 9027 | return -1; |
| 9028 | } |
| 9029 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9030 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 9031 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 9032 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 9033 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 9034 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 9035 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 9036 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9037 | |
| 9038 | switch (values[QSV_VERSION_MINOR]) { |
| 9039 | case 0: ver = "2.00"; break; |
| 9040 | case 10: ver = "2.10"; break; |
| 9041 | case 11: ver = "2.11"; break; |
| 9042 | case 30: ver = "3.00"; break; |
| 9043 | case 40: ver = "4.00"; break; |
| 9044 | case 50: ver = "5.00"; break; |
| 9045 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 9046 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9047 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 9048 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9049 | ver = &tmp[0]; |
| 9050 | } |
| 9051 | |
| 9052 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9053 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9054 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9055 | |
| 9056 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 9057 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 9058 | tmp[1] = ':'; |
| 9059 | tmp[2] = '\0'; |
| 9060 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9061 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9062 | } |
| 9063 | #endif |
| 9064 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9065 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9066 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9067 | enable_symlink() |
| 9068 | { |
| 9069 | HANDLE tok; |
| 9070 | TOKEN_PRIVILEGES tok_priv; |
| 9071 | LUID luid; |
| 9072 | int meth_idx = 0; |
| 9073 | |
| 9074 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9075 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9076 | |
| 9077 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9078 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9079 | |
| 9080 | tok_priv.PrivilegeCount = 1; |
| 9081 | tok_priv.Privileges[0].Luid = luid; |
| 9082 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 9083 | |
| 9084 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 9085 | sizeof(TOKEN_PRIVILEGES), |
| 9086 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9087 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9088 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9089 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 9090 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9091 | } |
| 9092 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 9093 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9094 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 9095 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9096 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9097 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9099 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9100 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9102 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9103 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9104 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9105 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9106 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9107 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9108 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9109 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9110 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9111 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9112 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9113 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9114 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9115 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9116 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9117 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9118 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9119 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9120 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9121 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9123 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9124 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9125 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9126 | #endif |
| 9127 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9128 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9129 | #endif |
| 9130 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9131 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9132 | #endif |
| 9133 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9134 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9135 | #endif |
| 9136 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9137 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9138 | #endif |
| 9139 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9140 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9141 | #endif |
| 9142 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9143 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9144 | #endif |
| 9145 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9147 | #endif |
| 9148 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9149 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9150 | #endif |
| 9151 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9152 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9153 | #endif |
| 9154 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9155 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9156 | #endif |
| 9157 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9158 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9159 | #endif |
| 9160 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9162 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9163 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9164 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9165 | #endif |
| 9166 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9167 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9168 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9169 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9170 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9171 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9172 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9173 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9174 | #endif |
| 9175 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9176 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9177 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 9178 | #ifdef PRIO_PROCESS |
| 9179 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 9180 | #endif |
| 9181 | #ifdef PRIO_PGRP |
| 9182 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 9183 | #endif |
| 9184 | #ifdef PRIO_USER |
| 9185 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 9186 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9187 | /* posix - constants for *at functions */ |
| 9188 | #ifdef AT_SYMLINK_NOFOLLOW |
| 9189 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 9190 | #endif |
| 9191 | #ifdef AT_EACCESS |
| 9192 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 9193 | #endif |
| 9194 | #ifdef AT_FDCWD |
| 9195 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 9196 | #endif |
| 9197 | #ifdef AT_REMOVEDIR |
| 9198 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 9199 | #endif |
| 9200 | #ifdef AT_SYMLINK_FOLLOW |
| 9201 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 9202 | #endif |
| 9203 | #ifdef UTIME_NOW |
| 9204 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 9205 | #endif |
| 9206 | #ifdef UTIME_OMIT |
| 9207 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 9208 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 9209 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9210 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9211 | /* MS Windows */ |
| 9212 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9213 | /* Don't inherit in child processes. */ |
| 9214 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9215 | #endif |
| 9216 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9217 | /* Optimize for short life (keep in memory). */ |
| 9218 | /* MS forgot to define this one with a non-underscore form too. */ |
| 9219 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9220 | #endif |
| 9221 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9222 | /* Automatically delete when last handle is closed. */ |
| 9223 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9224 | #endif |
| 9225 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9226 | /* Optimize for random access. */ |
| 9227 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9228 | #endif |
| 9229 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | /* Optimize for sequential access. */ |
| 9231 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9232 | #endif |
| 9233 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9234 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 9235 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9236 | /* Send a SIGIO signal whenever input or output |
| 9237 | becomes available on file descriptor */ |
| 9238 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 9239 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9240 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9241 | /* Direct disk access. */ |
| 9242 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9243 | #endif |
| 9244 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9245 | /* Must be a directory. */ |
| 9246 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9247 | #endif |
| 9248 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9249 | /* Do not follow links. */ |
| 9250 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9251 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 9252 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9253 | /* Do not update the access time. */ |
| 9254 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 9255 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9256 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9258 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9259 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9260 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9261 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9262 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9263 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9264 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9265 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9266 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9267 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9268 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9269 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9270 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9271 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9272 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9273 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9274 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9275 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9276 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9277 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9278 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9279 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9280 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9281 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9282 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9283 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9284 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9285 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9286 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9287 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9288 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9289 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9290 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9291 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9292 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9293 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9294 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9295 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9296 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9297 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9298 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9299 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9300 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9301 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9302 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9303 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9304 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9305 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9306 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9307 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9308 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9309 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 9310 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 9311 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 9312 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 9313 | #endif /* ST_RDONLY */ |
| 9314 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 9315 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 9316 | #endif /* ST_NOSUID */ |
| 9317 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9318 | /* FreeBSD sendfile() constants */ |
| 9319 | #ifdef SF_NODISKIO |
| 9320 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 9321 | #endif |
| 9322 | #ifdef SF_MNOWAIT |
| 9323 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 9324 | #endif |
| 9325 | #ifdef SF_SYNC |
| 9326 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 9327 | #endif |
| 9328 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9329 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9330 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9331 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 9332 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 9333 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 9334 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 9335 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 9336 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 9337 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 9338 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 9339 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 9340 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 9341 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 9342 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 9343 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 9344 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 9345 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 9346 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 9347 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 9348 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 9349 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 9350 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9351 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9352 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 9353 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 9354 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 9355 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 9356 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9357 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9358 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9359 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9360 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9361 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9362 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9363 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9364 | } |
| 9365 | |
| 9366 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9367 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 9368 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 9369 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 9370 | |
| 9371 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 9372 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 9373 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 9374 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 9375 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 9376 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 9377 | #define MODNAME "posix" |
| 9378 | #endif |
| 9379 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 9380 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9381 | PyModuleDef_HEAD_INIT, |
| 9382 | MODNAME, |
| 9383 | posix__doc__, |
| 9384 | -1, |
| 9385 | posix_methods, |
| 9386 | NULL, |
| 9387 | NULL, |
| 9388 | NULL, |
| 9389 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 9390 | }; |
| 9391 | |
| 9392 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 9393 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 9394 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9395 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9396 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9397 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9398 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9399 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9400 | #endif |
| 9401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9402 | m = PyModule_Create(&posixmodule); |
| 9403 | if (m == NULL) |
| 9404 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9405 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9406 | /* Initialize environ dictionary */ |
| 9407 | v = convertenviron(); |
| 9408 | Py_XINCREF(v); |
| 9409 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 9410 | return NULL; |
| 9411 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9413 | if (all_ins(m)) |
| 9414 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9415 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9416 | if (setup_confname_tables(m)) |
| 9417 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9419 | Py_INCREF(PyExc_OSError); |
| 9420 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9421 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 9422 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9423 | if (posix_putenv_garbage == NULL) |
| 9424 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 9425 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9426 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9427 | if (!initialized) { |
| 9428 | stat_result_desc.name = MODNAME ".stat_result"; |
| 9429 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 9430 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 9431 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 9432 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 9433 | structseq_new = StatResultType.tp_new; |
| 9434 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9435 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9436 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 9437 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 9438 | #ifdef NEED_TICKS_PER_SECOND |
| 9439 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9440 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 9441 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9442 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 9443 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9444 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 9445 | # endif |
| 9446 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9447 | } |
| 9448 | Py_INCREF((PyObject*) &StatResultType); |
| 9449 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 9450 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 9451 | PyModule_AddObject(m, "statvfs_result", |
| 9452 | (PyObject*) &StatVFSResultType); |
| 9453 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9454 | |
| 9455 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9456 | /* |
| 9457 | * Step 2 of weak-linking support on Mac OS X. |
| 9458 | * |
| 9459 | * The code below removes functions that are not available on the |
| 9460 | * currently active platform. |
| 9461 | * |
| 9462 | * This block allow one to use a python binary that was build on |
| 9463 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 9464 | * OSX 10.4. |
| 9465 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9466 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9467 | if (fstatvfs == NULL) { |
| 9468 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 9469 | return NULL; |
| 9470 | } |
| 9471 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9472 | #endif /* HAVE_FSTATVFS */ |
| 9473 | |
| 9474 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9475 | if (statvfs == NULL) { |
| 9476 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 9477 | return NULL; |
| 9478 | } |
| 9479 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9480 | #endif /* HAVE_STATVFS */ |
| 9481 | |
| 9482 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9483 | if (lchown == NULL) { |
| 9484 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 9485 | return NULL; |
| 9486 | } |
| 9487 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9488 | #endif /* HAVE_LCHOWN */ |
| 9489 | |
| 9490 | |
| 9491 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9492 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9493 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9494 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9495 | |
| 9496 | #ifdef __cplusplus |
| 9497 | } |
| 9498 | #endif |