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 |
| 372 | *((off_t*)addr) = PyLong_Check(arg) ? PyLong_AsLongLong(arg) |
| 373 | : PyLong_AsLong(arg); |
| 374 | #endif |
| 375 | if (PyErr_Occurred()) |
| 376 | return 0; |
| 377 | return 1; |
| 378 | } |
| 379 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 380 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 381 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 382 | * valid and throw an assertion if it isn't. |
| 383 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 384 | * an assertion can be useful, but it does contradict the POSIX standard |
| 385 | * which for write(2) states: |
| 386 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 387 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 388 | * writing." |
| 389 | * Furthermore, python allows the user to enter any old integer |
| 390 | * as a fd and should merely raise a python exception on error. |
| 391 | * The Microsoft CRT doesn't provide an official way to check for the |
| 392 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 393 | * 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] | 394 | * internal structures involved. |
| 395 | * The structures below must be updated for each version of visual studio |
| 396 | * according to the file internal.h in the CRT source, until MS comes |
| 397 | * up with a less hacky way to do this. |
| 398 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 399 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 400 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 401 | /* The actual size of the structure is determined at runtime. |
| 402 | * Only the first items must be present. |
| 403 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 404 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 405 | intptr_t osfhnd; |
| 406 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 407 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 408 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 409 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 410 | #define IOINFO_L2E 5 |
| 411 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 412 | #define IOINFO_ARRAYS 64 |
| 413 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 414 | #define FOPEN 0x01 |
| 415 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 416 | |
| 417 | /* This function emulates what the windows CRT does to validate file handles */ |
| 418 | int |
| 419 | _PyVerify_fd(int fd) |
| 420 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 421 | const int i1 = fd >> IOINFO_L2E; |
| 422 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 423 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 424 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 425 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 426 | /* Determine the actual size of the ioinfo structure, |
| 427 | * as used by the CRT loaded in memory |
| 428 | */ |
| 429 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 430 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 431 | } |
| 432 | if (sizeof_ioinfo == 0) { |
| 433 | /* This should not happen... */ |
| 434 | goto fail; |
| 435 | } |
| 436 | |
| 437 | /* See that it isn't a special CLEAR fileno */ |
| 438 | if (fd != _NO_CONSOLE_FILENO) { |
| 439 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 440 | * we check pointer validity and other info |
| 441 | */ |
| 442 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 443 | /* finally, check that the file is open */ |
| 444 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 445 | if (info->osfile & FOPEN) { |
| 446 | return 1; |
| 447 | } |
| 448 | } |
| 449 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 450 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 451 | errno = EBADF; |
| 452 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 456 | static int |
| 457 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 458 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 459 | if (!_PyVerify_fd(fd1)) |
| 460 | return 0; |
| 461 | if (fd2 == _NO_CONSOLE_FILENO) |
| 462 | return 0; |
| 463 | if ((unsigned)fd2 < _NHANDLE_) |
| 464 | return 1; |
| 465 | else |
| 466 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 467 | } |
| 468 | #else |
| 469 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 470 | #define _PyVerify_fd_dup2(A, B) (1) |
| 471 | #endif |
| 472 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 473 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 474 | /* The following structure was copied from |
| 475 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 476 | include doesn't seem to be present in the Windows SDK (at least as included |
| 477 | with Visual Studio Express). */ |
| 478 | typedef struct _REPARSE_DATA_BUFFER { |
| 479 | ULONG ReparseTag; |
| 480 | USHORT ReparseDataLength; |
| 481 | USHORT Reserved; |
| 482 | union { |
| 483 | struct { |
| 484 | USHORT SubstituteNameOffset; |
| 485 | USHORT SubstituteNameLength; |
| 486 | USHORT PrintNameOffset; |
| 487 | USHORT PrintNameLength; |
| 488 | ULONG Flags; |
| 489 | WCHAR PathBuffer[1]; |
| 490 | } SymbolicLinkReparseBuffer; |
| 491 | |
| 492 | struct { |
| 493 | USHORT SubstituteNameOffset; |
| 494 | USHORT SubstituteNameLength; |
| 495 | USHORT PrintNameOffset; |
| 496 | USHORT PrintNameLength; |
| 497 | WCHAR PathBuffer[1]; |
| 498 | } MountPointReparseBuffer; |
| 499 | |
| 500 | struct { |
| 501 | UCHAR DataBuffer[1]; |
| 502 | } GenericReparseBuffer; |
| 503 | }; |
| 504 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 505 | |
| 506 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 507 | GenericReparseBuffer) |
| 508 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 509 | |
| 510 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 511 | 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] | 512 | { |
| 513 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 514 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 515 | DWORD n_bytes_returned; |
| 516 | const wchar_t *ptr; |
| 517 | wchar_t *buf; |
| 518 | size_t len; |
| 519 | |
| 520 | if (0 == DeviceIoControl( |
| 521 | reparse_point_handle, |
| 522 | FSCTL_GET_REPARSE_POINT, |
| 523 | NULL, 0, /* in buffer */ |
| 524 | target_buffer, sizeof(target_buffer), |
| 525 | &n_bytes_returned, |
| 526 | NULL)) /* we're not using OVERLAPPED_IO */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 527 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 528 | |
| 529 | if (reparse_tag) |
| 530 | *reparse_tag = rdb->ReparseTag; |
| 531 | |
| 532 | if (target_path) { |
| 533 | switch (rdb->ReparseTag) { |
| 534 | case IO_REPARSE_TAG_SYMLINK: |
| 535 | /* XXX: Maybe should use SubstituteName? */ |
| 536 | ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 537 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR); |
| 538 | len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR); |
| 539 | break; |
| 540 | case IO_REPARSE_TAG_MOUNT_POINT: |
| 541 | ptr = rdb->MountPointReparseBuffer.PathBuffer + |
| 542 | rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR); |
| 543 | len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR); |
| 544 | break; |
| 545 | default: |
| 546 | SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 547 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 548 | } |
| 549 | buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1)); |
| 550 | if (!buf) { |
| 551 | SetLastError(ERROR_OUTOFMEMORY); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 552 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 553 | } |
| 554 | wcsncpy(buf, ptr, len); |
| 555 | buf[len] = L'\0'; |
| 556 | if (wcsncmp(buf, L"\\??\\", 4) == 0) |
| 557 | buf[1] = L'\\'; |
| 558 | *target_path = buf; |
| 559 | } |
| 560 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 561 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 562 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 563 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 564 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 565 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 566 | #ifdef WITH_NEXT_FRAMEWORK |
| 567 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 568 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 569 | */ |
| 570 | #include <crt_externs.h> |
| 571 | static char **environ; |
| 572 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 573 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 574 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 575 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 576 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 577 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 578 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 579 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 580 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 581 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 582 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 583 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 584 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 585 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 586 | APIRET rc; |
| 587 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 588 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 590 | d = PyDict_New(); |
| 591 | if (d == NULL) |
| 592 | return NULL; |
| 593 | #ifdef WITH_NEXT_FRAMEWORK |
| 594 | if (environ == NULL) |
| 595 | environ = *_NSGetEnviron(); |
| 596 | #endif |
| 597 | #ifdef MS_WINDOWS |
| 598 | /* _wenviron must be initialized in this way if the program is started |
| 599 | through main() instead of wmain(). */ |
| 600 | _wgetenv(L""); |
| 601 | if (_wenviron == NULL) |
| 602 | return d; |
| 603 | /* This part ignores errors */ |
| 604 | for (e = _wenviron; *e != NULL; e++) { |
| 605 | PyObject *k; |
| 606 | PyObject *v; |
| 607 | wchar_t *p = wcschr(*e, L'='); |
| 608 | if (p == NULL) |
| 609 | continue; |
| 610 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 611 | if (k == NULL) { |
| 612 | PyErr_Clear(); |
| 613 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 614 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 615 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 616 | if (v == NULL) { |
| 617 | PyErr_Clear(); |
| 618 | Py_DECREF(k); |
| 619 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 620 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 621 | if (PyDict_GetItem(d, k) == NULL) { |
| 622 | if (PyDict_SetItem(d, k, v) != 0) |
| 623 | PyErr_Clear(); |
| 624 | } |
| 625 | Py_DECREF(k); |
| 626 | Py_DECREF(v); |
| 627 | } |
| 628 | #else |
| 629 | if (environ == NULL) |
| 630 | return d; |
| 631 | /* This part ignores errors */ |
| 632 | for (e = environ; *e != NULL; e++) { |
| 633 | PyObject *k; |
| 634 | PyObject *v; |
| 635 | char *p = strchr(*e, '='); |
| 636 | if (p == NULL) |
| 637 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 638 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 639 | if (k == NULL) { |
| 640 | PyErr_Clear(); |
| 641 | continue; |
| 642 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 643 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 644 | if (v == NULL) { |
| 645 | PyErr_Clear(); |
| 646 | Py_DECREF(k); |
| 647 | continue; |
| 648 | } |
| 649 | if (PyDict_GetItem(d, k) == NULL) { |
| 650 | if (PyDict_SetItem(d, k, v) != 0) |
| 651 | PyErr_Clear(); |
| 652 | } |
| 653 | Py_DECREF(k); |
| 654 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 655 | } |
| 656 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 657 | #if defined(PYOS_OS2) |
| 658 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 659 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 660 | PyObject *v = PyBytes_FromString(buffer); |
| 661 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 662 | Py_DECREF(v); |
| 663 | } |
| 664 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 665 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 666 | PyObject *v = PyBytes_FromString(buffer); |
| 667 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 668 | Py_DECREF(v); |
| 669 | } |
| 670 | #endif |
| 671 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 674 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 675 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 676 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 677 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 678 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 679 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 680 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 681 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 682 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 683 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 684 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 687 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 688 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 689 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 690 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 691 | PyObject *name_str, *rc; |
| 692 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 693 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 694 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 695 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 696 | name_str); |
| 697 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 698 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 701 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 702 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 703 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 704 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 705 | /* XXX We should pass the function name along in the future. |
| 706 | (winreg.c also wants to pass the function name.) |
| 707 | This would however require an additional param to the |
| 708 | Windows error object, which is non-trivial. |
| 709 | */ |
| 710 | errno = GetLastError(); |
| 711 | if (filename) |
| 712 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 713 | else |
| 714 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 715 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 716 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 717 | static PyObject * |
| 718 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 719 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 720 | /* XXX - see win32_error for comments on 'function' */ |
| 721 | errno = GetLastError(); |
| 722 | if (filename) |
| 723 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 724 | else |
| 725 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 728 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 729 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 730 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 731 | if (PyUnicode_CheckExact(*param)) |
| 732 | Py_INCREF(*param); |
| 733 | else if (PyUnicode_Check(*param)) |
| 734 | /* For a Unicode subtype that's not a Unicode object, |
| 735 | return a true Unicode object with the same data. */ |
| 736 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 737 | PyUnicode_GET_SIZE(*param)); |
| 738 | else |
| 739 | *param = PyUnicode_FromEncodedObject(*param, |
| 740 | Py_FileSystemDefaultEncoding, |
| 741 | "strict"); |
| 742 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 745 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 746 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 747 | #if defined(PYOS_OS2) |
| 748 | /********************************************************************** |
| 749 | * Helper Function to Trim and Format OS/2 Messages |
| 750 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 751 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 752 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 753 | { |
| 754 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 755 | |
| 756 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 757 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 758 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 759 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 760 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 761 | } |
| 762 | |
| 763 | /* Add Optional Reason Text */ |
| 764 | if (reason) { |
| 765 | strcat(msgbuf, " : "); |
| 766 | strcat(msgbuf, reason); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /********************************************************************** |
| 771 | * Decode an OS/2 Operating System Error Code |
| 772 | * |
| 773 | * A convenience function to lookup an OS/2 error code and return a |
| 774 | * text message we can use to raise a Python exception. |
| 775 | * |
| 776 | * Notes: |
| 777 | * The messages for errors returned from the OS/2 kernel reside in |
| 778 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 779 | * |
| 780 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 781 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 782 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 783 | { |
| 784 | APIRET rc; |
| 785 | ULONG msglen; |
| 786 | |
| 787 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 788 | Py_BEGIN_ALLOW_THREADS |
| 789 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 790 | errorcode, "oso001.msg", &msglen); |
| 791 | Py_END_ALLOW_THREADS |
| 792 | |
| 793 | if (rc == NO_ERROR) |
| 794 | os2_formatmsg(msgbuf, msglen, reason); |
| 795 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 796 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 797 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 798 | |
| 799 | return msgbuf; |
| 800 | } |
| 801 | |
| 802 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 803 | errors are not in a global variable e.g. 'errno' nor are |
| 804 | they congruent with posix error numbers. */ |
| 805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 806 | static PyObject * |
| 807 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 808 | { |
| 809 | char text[1024]; |
| 810 | PyObject *v; |
| 811 | |
| 812 | os2_strerror(text, sizeof(text), code, ""); |
| 813 | |
| 814 | v = Py_BuildValue("(is)", code, text); |
| 815 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 816 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 817 | Py_DECREF(v); |
| 818 | } |
| 819 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 820 | } |
| 821 | |
| 822 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 823 | |
| 824 | /* POSIX generic methods */ |
| 825 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 826 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 827 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 828 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 829 | int fd; |
| 830 | int res; |
| 831 | fd = PyObject_AsFileDescriptor(fdobj); |
| 832 | if (fd < 0) |
| 833 | return NULL; |
| 834 | if (!_PyVerify_fd(fd)) |
| 835 | return posix_error(); |
| 836 | Py_BEGIN_ALLOW_THREADS |
| 837 | res = (*func)(fd); |
| 838 | Py_END_ALLOW_THREADS |
| 839 | if (res < 0) |
| 840 | return posix_error(); |
| 841 | Py_INCREF(Py_None); |
| 842 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 843 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 844 | |
| 845 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 846 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 847 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 848 | PyObject *opath1 = NULL; |
| 849 | char *path1; |
| 850 | int res; |
| 851 | if (!PyArg_ParseTuple(args, format, |
| 852 | PyUnicode_FSConverter, &opath1)) |
| 853 | return NULL; |
| 854 | path1 = PyBytes_AsString(opath1); |
| 855 | Py_BEGIN_ALLOW_THREADS |
| 856 | res = (*func)(path1); |
| 857 | Py_END_ALLOW_THREADS |
| 858 | if (res < 0) |
| 859 | return posix_error_with_allocated_filename(opath1); |
| 860 | Py_DECREF(opath1); |
| 861 | Py_INCREF(Py_None); |
| 862 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 865 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 866 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 867 | char *format, |
| 868 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 869 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 870 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 871 | char *path1, *path2; |
| 872 | int res; |
| 873 | if (!PyArg_ParseTuple(args, format, |
| 874 | PyUnicode_FSConverter, &opath1, |
| 875 | PyUnicode_FSConverter, &opath2)) { |
| 876 | return NULL; |
| 877 | } |
| 878 | path1 = PyBytes_AsString(opath1); |
| 879 | path2 = PyBytes_AsString(opath2); |
| 880 | Py_BEGIN_ALLOW_THREADS |
| 881 | res = (*func)(path1, path2); |
| 882 | Py_END_ALLOW_THREADS |
| 883 | Py_DECREF(opath1); |
| 884 | Py_DECREF(opath2); |
| 885 | if (res != 0) |
| 886 | /* XXX how to report both path1 and path2??? */ |
| 887 | return posix_error(); |
| 888 | Py_INCREF(Py_None); |
| 889 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 892 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 893 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 894 | win32_1str(PyObject* args, char* func, |
| 895 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 896 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 897 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 898 | PyObject *uni; |
| 899 | char *ansi; |
| 900 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 901 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 902 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 903 | PyErr_Clear(); |
| 904 | else { |
| 905 | Py_BEGIN_ALLOW_THREADS |
| 906 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 907 | Py_END_ALLOW_THREADS |
| 908 | if (!result) |
| 909 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 910 | Py_INCREF(Py_None); |
| 911 | return Py_None; |
| 912 | } |
| 913 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 914 | return NULL; |
| 915 | Py_BEGIN_ALLOW_THREADS |
| 916 | result = funcA(ansi); |
| 917 | Py_END_ALLOW_THREADS |
| 918 | if (!result) |
| 919 | return win32_error(func, ansi); |
| 920 | Py_INCREF(Py_None); |
| 921 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 922 | |
| 923 | } |
| 924 | |
| 925 | /* This is a reimplementation of the C library's chdir function, |
| 926 | but one that produces Win32 errors instead of DOS error codes. |
| 927 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 928 | it also needs to set "magic" environment variables indicating |
| 929 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 930 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 931 | win32_chdir(LPCSTR path) |
| 932 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 933 | char new_path[MAX_PATH+1]; |
| 934 | int result; |
| 935 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 936 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 937 | if(!SetCurrentDirectoryA(path)) |
| 938 | return FALSE; |
| 939 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 940 | if (!result) |
| 941 | return FALSE; |
| 942 | /* In the ANSI API, there should not be any paths longer |
| 943 | than MAX_PATH. */ |
| 944 | assert(result <= MAX_PATH+1); |
| 945 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 946 | strncmp(new_path, "//", 2) == 0) |
| 947 | /* UNC path, nothing to do. */ |
| 948 | return TRUE; |
| 949 | env[1] = new_path[0]; |
| 950 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* The Unicode version differs from the ANSI version |
| 954 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 955 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 956 | win32_wchdir(LPCWSTR path) |
| 957 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 958 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 959 | int result; |
| 960 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 962 | if(!SetCurrentDirectoryW(path)) |
| 963 | return FALSE; |
| 964 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 965 | if (!result) |
| 966 | return FALSE; |
| 967 | if (result > MAX_PATH+1) { |
| 968 | new_path = malloc(result * sizeof(wchar_t)); |
| 969 | if (!new_path) { |
| 970 | SetLastError(ERROR_OUTOFMEMORY); |
| 971 | return FALSE; |
| 972 | } |
| 973 | result = GetCurrentDirectoryW(result, new_path); |
| 974 | if (!result) { |
| 975 | free(new_path); |
| 976 | return FALSE; |
| 977 | } |
| 978 | } |
| 979 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 980 | wcsncmp(new_path, L"//", 2) == 0) |
| 981 | /* UNC path, nothing to do. */ |
| 982 | return TRUE; |
| 983 | env[1] = new_path[0]; |
| 984 | result = SetEnvironmentVariableW(env, new_path); |
| 985 | if (new_path != _new_path) |
| 986 | free(new_path); |
| 987 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 988 | } |
| 989 | #endif |
| 990 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 991 | #ifdef MS_WINDOWS |
| 992 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 993 | - time stamps are restricted to second resolution |
| 994 | - file modification times suffer from forth-and-back conversions between |
| 995 | UTC and local time |
| 996 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 997 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 998 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 999 | |
| 1000 | struct win32_stat{ |
| 1001 | int st_dev; |
| 1002 | __int64 st_ino; |
| 1003 | unsigned short st_mode; |
| 1004 | int st_nlink; |
| 1005 | int st_uid; |
| 1006 | int st_gid; |
| 1007 | int st_rdev; |
| 1008 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1009 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1010 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1011 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1012 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1013 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1014 | int st_ctime_nsec; |
| 1015 | }; |
| 1016 | |
| 1017 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1018 | |
| 1019 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1020 | 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] | 1021 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1022 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1023 | /* Cannot simply cast and dereference in_ptr, |
| 1024 | since it might not be aligned properly */ |
| 1025 | __int64 in; |
| 1026 | memcpy(&in, in_ptr, sizeof(in)); |
| 1027 | *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] | 1028 | *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] | 1029 | } |
| 1030 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1031 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1032 | 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] | 1033 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1034 | /* XXX endianness */ |
| 1035 | __int64 out; |
| 1036 | out = time_in + secs_between_epochs; |
| 1037 | out = out * 10000000 + nsec_in / 100; |
| 1038 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1041 | /* Below, we *know* that ugo+r is 0444 */ |
| 1042 | #if _S_IREAD != 0400 |
| 1043 | #error Unsupported C library |
| 1044 | #endif |
| 1045 | static int |
| 1046 | attributes_to_mode(DWORD attr) |
| 1047 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1048 | int m = 0; |
| 1049 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1050 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1051 | else |
| 1052 | m |= _S_IFREG; |
| 1053 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1054 | m |= 0444; |
| 1055 | else |
| 1056 | m |= 0666; |
| 1057 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1061 | 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] | 1062 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1063 | memset(result, 0, sizeof(*result)); |
| 1064 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1065 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1066 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1067 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1068 | 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] | 1069 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1070 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1071 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1072 | /* first clear the S_IFMT bits */ |
| 1073 | result->st_mode ^= (result->st_mode & 0170000); |
| 1074 | /* now set the bits that make this a symlink */ |
| 1075 | result->st_mode |= 0120000; |
| 1076 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1077 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1078 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1081 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1082 | 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] | 1083 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1084 | HANDLE hFindFile; |
| 1085 | WIN32_FIND_DATAA FileData; |
| 1086 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1087 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1088 | return FALSE; |
| 1089 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1090 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1091 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1092 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1093 | info->ftCreationTime = FileData.ftCreationTime; |
| 1094 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1095 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1096 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1097 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1098 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1099 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1100 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1101 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1105 | 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] | 1106 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1107 | HANDLE hFindFile; |
| 1108 | WIN32_FIND_DATAW FileData; |
| 1109 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1110 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1111 | return FALSE; |
| 1112 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1113 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1114 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1115 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1116 | info->ftCreationTime = FileData.ftCreationTime; |
| 1117 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1118 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1119 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1120 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1121 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1122 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1123 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1124 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1127 | #ifndef SYMLOOP_MAX |
| 1128 | #define SYMLOOP_MAX ( 88 ) |
| 1129 | #endif |
| 1130 | |
| 1131 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1132 | 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] | 1133 | |
| 1134 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1135 | 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] | 1136 | { |
| 1137 | int code; |
| 1138 | HANDLE hFile; |
| 1139 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1140 | ULONG reparse_tag = 0; |
Hirokazu Yamamoto | 7467351 | 2010-12-05 02:48:08 +0000 | [diff] [blame] | 1141 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1142 | const char *dot; |
| 1143 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1144 | if (depth > SYMLOOP_MAX) { |
| 1145 | SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */ |
| 1146 | return -1; |
| 1147 | } |
| 1148 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1149 | hFile = CreateFileA( |
| 1150 | path, |
| 1151 | 0, /* desired access */ |
| 1152 | 0, /* share mode */ |
| 1153 | NULL, /* security attributes */ |
| 1154 | OPEN_EXISTING, |
| 1155 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 1156 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT, |
| 1157 | NULL); |
| 1158 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1159 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1160 | /* Either the target doesn't exist, or we don't have access to |
| 1161 | get a handle to it. If the former, we need to return an error. |
| 1162 | If the latter, we can use attributes_from_dir. */ |
| 1163 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1164 | return -1; |
| 1165 | /* Could not get attributes on open file. Fall back to |
| 1166 | reading the directory. */ |
| 1167 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1168 | /* Very strange. This should not fail now */ |
| 1169 | return -1; |
| 1170 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1171 | if (traverse) { |
| 1172 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1173 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1174 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1175 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1176 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1177 | } else { |
| 1178 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1179 | CloseHandle(hFile); |
| 1180 | return -1;; |
| 1181 | } |
| 1182 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1183 | code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL); |
| 1184 | CloseHandle(hFile); |
| 1185 | if (code < 0) |
| 1186 | return code; |
| 1187 | if (traverse) { |
| 1188 | code = win32_xstat_impl_w(target_path, result, traverse, depth + 1); |
| 1189 | free(target_path); |
| 1190 | return code; |
| 1191 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1192 | } else |
| 1193 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1194 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1195 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1196 | |
| 1197 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1198 | dot = strrchr(path, '.'); |
| 1199 | if (dot) { |
| 1200 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1201 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1202 | result->st_mode |= 0111; |
| 1203 | } |
| 1204 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1208 | 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] | 1209 | { |
| 1210 | int code; |
| 1211 | HANDLE hFile; |
| 1212 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1213 | ULONG reparse_tag = 0; |
| 1214 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1215 | const wchar_t *dot; |
| 1216 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1217 | if (depth > SYMLOOP_MAX) { |
| 1218 | SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */ |
| 1219 | return -1; |
| 1220 | } |
| 1221 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1222 | hFile = CreateFileW( |
| 1223 | path, |
| 1224 | 0, /* desired access */ |
| 1225 | 0, /* share mode */ |
| 1226 | NULL, /* security attributes */ |
| 1227 | OPEN_EXISTING, |
| 1228 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 1229 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT, |
| 1230 | NULL); |
| 1231 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1232 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1233 | /* Either the target doesn't exist, or we don't have access to |
| 1234 | get a handle to it. If the former, we need to return an error. |
| 1235 | If the latter, we can use attributes_from_dir. */ |
| 1236 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1237 | return -1; |
| 1238 | /* Could not get attributes on open file. Fall back to |
| 1239 | reading the directory. */ |
| 1240 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1241 | /* Very strange. This should not fail now */ |
| 1242 | return -1; |
| 1243 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1244 | if (traverse) { |
| 1245 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1246 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1247 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1248 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1249 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1250 | } else { |
| 1251 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1252 | CloseHandle(hFile); |
| 1253 | return -1;; |
| 1254 | } |
| 1255 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1256 | code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL); |
| 1257 | CloseHandle(hFile); |
| 1258 | if (code < 0) |
| 1259 | return code; |
| 1260 | if (traverse) { |
| 1261 | code = win32_xstat_impl_w(target_path, result, traverse, depth + 1); |
| 1262 | free(target_path); |
| 1263 | return code; |
| 1264 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1265 | } else |
| 1266 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1267 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1268 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1271 | dot = wcsrchr(path, '.'); |
| 1272 | if (dot) { |
| 1273 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1274 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1275 | result->st_mode |= 0111; |
| 1276 | } |
| 1277 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1281 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1282 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1283 | /* Protocol violation: we explicitly clear errno, instead of |
| 1284 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1285 | int code = win32_xstat_impl(path, result, traverse, 0); |
| 1286 | errno = 0; |
| 1287 | return code; |
| 1288 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1289 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1290 | static int |
| 1291 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1292 | { |
| 1293 | /* Protocol violation: we explicitly clear errno, instead of |
| 1294 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1295 | int code = win32_xstat_impl_w(path, result, traverse, 0); |
| 1296 | errno = 0; |
| 1297 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1300 | /* About the following functions: win32_lstat, win32_lstat_w, win32_stat, |
| 1301 | win32_stat_w |
| 1302 | |
| 1303 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1304 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1305 | default does not traverse symlinks and instead returns attributes for |
| 1306 | the symlink. |
| 1307 | |
| 1308 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1309 | win32_stat will first explicitly resolve the symlink target and then will |
| 1310 | call win32_lstat on that result. |
| 1311 | |
| 1312 | The _w represent Unicode equivalents of the aformentioned ANSI functions. */ |
| 1313 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1314 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1315 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1316 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1317 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1320 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1321 | 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] | 1322 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1323 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | static int |
| 1327 | win32_stat(const char* path, struct win32_stat *result) |
| 1328 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1329 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1332 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1333 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1334 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1335 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | static int |
| 1339 | win32_fstat(int file_number, struct win32_stat *result) |
| 1340 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1341 | BY_HANDLE_FILE_INFORMATION info; |
| 1342 | HANDLE h; |
| 1343 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1344 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1345 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1347 | /* Protocol violation: we explicitly clear errno, instead of |
| 1348 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1349 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1351 | if (h == INVALID_HANDLE_VALUE) { |
| 1352 | /* This is really a C library error (invalid file handle). |
| 1353 | We set the Win32 error to the closes one matching. */ |
| 1354 | SetLastError(ERROR_INVALID_HANDLE); |
| 1355 | return -1; |
| 1356 | } |
| 1357 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1359 | type = GetFileType(h); |
| 1360 | if (type == FILE_TYPE_UNKNOWN) { |
| 1361 | DWORD error = GetLastError(); |
| 1362 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1363 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1364 | } |
| 1365 | /* else: valid but unknown file */ |
| 1366 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1367 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1368 | if (type != FILE_TYPE_DISK) { |
| 1369 | if (type == FILE_TYPE_CHAR) |
| 1370 | result->st_mode = _S_IFCHR; |
| 1371 | else if (type == FILE_TYPE_PIPE) |
| 1372 | result->st_mode = _S_IFIFO; |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | if (!GetFileInformationByHandle(h, &info)) { |
| 1377 | return -1; |
| 1378 | } |
| 1379 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1380 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1381 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1382 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1383 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | #endif /* MS_WINDOWS */ |
| 1387 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1388 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1389 | "stat_result: Result from stat or lstat.\n\n\ |
| 1390 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1391 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1392 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1393 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1394 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1395 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1396 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1397 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1398 | |
| 1399 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1400 | {"st_mode", "protection bits"}, |
| 1401 | {"st_ino", "inode"}, |
| 1402 | {"st_dev", "device"}, |
| 1403 | {"st_nlink", "number of hard links"}, |
| 1404 | {"st_uid", "user ID of owner"}, |
| 1405 | {"st_gid", "group ID of owner"}, |
| 1406 | {"st_size", "total size, in bytes"}, |
| 1407 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1408 | {NULL, "integer time of last access"}, |
| 1409 | {NULL, "integer time of last modification"}, |
| 1410 | {NULL, "integer time of last change"}, |
| 1411 | {"st_atime", "time of last access"}, |
| 1412 | {"st_mtime", "time of last modification"}, |
| 1413 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1414 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1415 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1416 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1417 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1418 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1419 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1420 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1421 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1422 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1423 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1424 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1425 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1426 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1427 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1428 | #endif |
| 1429 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1430 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1431 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1432 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1433 | }; |
| 1434 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1435 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1436 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1437 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1438 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1439 | #endif |
| 1440 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1441 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1442 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1443 | #else |
| 1444 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1445 | #endif |
| 1446 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1447 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1448 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1449 | #else |
| 1450 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1451 | #endif |
| 1452 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1453 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1454 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1455 | #else |
| 1456 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1457 | #endif |
| 1458 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1459 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1460 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1461 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1462 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1463 | #endif |
| 1464 | |
| 1465 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1466 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1467 | #else |
| 1468 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1469 | #endif |
| 1470 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1471 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1472 | "stat_result", /* name */ |
| 1473 | stat_result__doc__, /* doc */ |
| 1474 | stat_result_fields, |
| 1475 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1476 | }; |
| 1477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1478 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1479 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1480 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1481 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1482 | 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] | 1483 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1484 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1485 | |
| 1486 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1487 | {"f_bsize", }, |
| 1488 | {"f_frsize", }, |
| 1489 | {"f_blocks", }, |
| 1490 | {"f_bfree", }, |
| 1491 | {"f_bavail", }, |
| 1492 | {"f_files", }, |
| 1493 | {"f_ffree", }, |
| 1494 | {"f_favail", }, |
| 1495 | {"f_flag", }, |
| 1496 | {"f_namemax",}, |
| 1497 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1498 | }; |
| 1499 | |
| 1500 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1501 | "statvfs_result", /* name */ |
| 1502 | statvfs_result__doc__, /* doc */ |
| 1503 | statvfs_result_fields, |
| 1504 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1505 | }; |
| 1506 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1507 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1508 | static PyTypeObject StatResultType; |
| 1509 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1510 | static newfunc structseq_new; |
| 1511 | |
| 1512 | static PyObject * |
| 1513 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1514 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1515 | PyStructSequence *result; |
| 1516 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1517 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1518 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1519 | if (!result) |
| 1520 | return NULL; |
| 1521 | /* If we have been initialized from a tuple, |
| 1522 | st_?time might be set to None. Initialize it |
| 1523 | from the int slots. */ |
| 1524 | for (i = 7; i <= 9; i++) { |
| 1525 | if (result->ob_item[i+3] == Py_None) { |
| 1526 | Py_DECREF(Py_None); |
| 1527 | Py_INCREF(result->ob_item[i]); |
| 1528 | result->ob_item[i+3] = result->ob_item[i]; |
| 1529 | } |
| 1530 | } |
| 1531 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1537 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1538 | |
| 1539 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1540 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1541 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1542 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1543 | future calls return ints. \n\ |
| 1544 | If newval is omitted, return the current setting.\n"); |
| 1545 | |
| 1546 | static PyObject* |
| 1547 | stat_float_times(PyObject* self, PyObject *args) |
| 1548 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1549 | int newval = -1; |
| 1550 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1551 | return NULL; |
| 1552 | if (newval == -1) |
| 1553 | /* Return old value */ |
| 1554 | return PyBool_FromLong(_stat_float_times); |
| 1555 | _stat_float_times = newval; |
| 1556 | Py_INCREF(Py_None); |
| 1557 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1558 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1559 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1560 | static void |
| 1561 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1562 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1563 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1564 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1565 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1566 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1567 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1568 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1569 | if (!ival) |
| 1570 | return; |
| 1571 | if (_stat_float_times) { |
| 1572 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1573 | } else { |
| 1574 | fval = ival; |
| 1575 | Py_INCREF(fval); |
| 1576 | } |
| 1577 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1578 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1581 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1582 | (used by posix_stat() and posix_fstat()) */ |
| 1583 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1584 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1585 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1586 | unsigned long ansec, mnsec, cnsec; |
| 1587 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1588 | if (v == NULL) |
| 1589 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1590 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1591 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1592 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1593 | PyStructSequence_SET_ITEM(v, 1, |
| 1594 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1595 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1596 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1597 | #endif |
| 1598 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1599 | PyStructSequence_SET_ITEM(v, 2, |
| 1600 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1601 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1602 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1603 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1604 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1605 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1606 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1607 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1608 | PyStructSequence_SET_ITEM(v, 6, |
| 1609 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1610 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1611 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1612 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1613 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1614 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1615 | ansec = st->st_atim.tv_nsec; |
| 1616 | mnsec = st->st_mtim.tv_nsec; |
| 1617 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1618 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1619 | ansec = st->st_atimespec.tv_nsec; |
| 1620 | mnsec = st->st_mtimespec.tv_nsec; |
| 1621 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1622 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1623 | ansec = st->st_atime_nsec; |
| 1624 | mnsec = st->st_mtime_nsec; |
| 1625 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1626 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1627 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1628 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1629 | fill_time(v, 7, st->st_atime, ansec); |
| 1630 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1631 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1632 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1633 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1634 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1635 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1636 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1637 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1638 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1639 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1640 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1641 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1642 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1643 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1644 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1645 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1646 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1647 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1648 | #endif |
| 1649 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1650 | { |
| 1651 | PyObject *val; |
| 1652 | unsigned long bsec,bnsec; |
| 1653 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1654 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1655 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1656 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1657 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1658 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1659 | if (_stat_float_times) { |
| 1660 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1661 | } else { |
| 1662 | val = PyLong_FromLong((long)bsec); |
| 1663 | } |
| 1664 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1665 | val); |
| 1666 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1667 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1668 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1669 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1670 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1671 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1672 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1673 | if (PyErr_Occurred()) { |
| 1674 | Py_DECREF(v); |
| 1675 | return NULL; |
| 1676 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1677 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1678 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1681 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1682 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1683 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1684 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1685 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1686 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1687 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1688 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1689 | char *wformat, |
| 1690 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1691 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1692 | STRUCT_STAT st; |
| 1693 | PyObject *opath; |
| 1694 | char *path; |
| 1695 | int res; |
| 1696 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1697 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1698 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1699 | PyUnicodeObject *po; |
| 1700 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1701 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1702 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1703 | Py_BEGIN_ALLOW_THREADS |
| 1704 | /* PyUnicode_AS_UNICODE result OK without |
| 1705 | thread lock as it is a simple dereference. */ |
| 1706 | res = wstatfunc(wpath, &st); |
| 1707 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1708 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1709 | if (res != 0) |
| 1710 | return win32_error_unicode("stat", wpath); |
| 1711 | return _pystat_fromstructstat(&st); |
| 1712 | } |
| 1713 | /* Drop the argument parsing error as narrow strings |
| 1714 | are also valid. */ |
| 1715 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1716 | #endif |
| 1717 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1718 | if (!PyArg_ParseTuple(args, format, |
| 1719 | PyUnicode_FSConverter, &opath)) |
| 1720 | return NULL; |
| 1721 | path = PyBytes_AsString(opath); |
| 1722 | Py_BEGIN_ALLOW_THREADS |
| 1723 | res = (*statfunc)(path, &st); |
| 1724 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1725 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1726 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1727 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1728 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1729 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1730 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1731 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1732 | } |
| 1733 | else |
| 1734 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1735 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1736 | Py_DECREF(opath); |
| 1737 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1740 | /* POSIX methods */ |
| 1741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1742 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1743 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1744 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1745 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1746 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1747 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1748 | 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] | 1749 | |
| 1750 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1751 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1752 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1753 | PyObject *opath; |
| 1754 | char *path; |
| 1755 | int mode; |
| 1756 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1757 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | DWORD attr; |
| 1759 | PyUnicodeObject *po; |
| 1760 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1761 | Py_BEGIN_ALLOW_THREADS |
| 1762 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1763 | it is a simple dereference. */ |
| 1764 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1765 | Py_END_ALLOW_THREADS |
| 1766 | goto finish; |
| 1767 | } |
| 1768 | /* Drop the argument parsing error as narrow strings |
| 1769 | are also valid. */ |
| 1770 | PyErr_Clear(); |
| 1771 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1772 | PyUnicode_FSConverter, &opath, &mode)) |
| 1773 | return NULL; |
| 1774 | path = PyBytes_AsString(opath); |
| 1775 | Py_BEGIN_ALLOW_THREADS |
| 1776 | attr = GetFileAttributesA(path); |
| 1777 | Py_END_ALLOW_THREADS |
| 1778 | Py_DECREF(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1779 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1780 | if (attr == 0xFFFFFFFF) |
| 1781 | /* File does not exist, or cannot read attributes */ |
| 1782 | return PyBool_FromLong(0); |
| 1783 | /* Access is possible if either write access wasn't requested, or |
| 1784 | the file isn't read-only, or if it's a directory, as there are |
| 1785 | no read-only directories on Windows. */ |
| 1786 | return PyBool_FromLong(!(mode & 2) |
| 1787 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1788 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1789 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1790 | int res; |
| 1791 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1792 | PyUnicode_FSConverter, &opath, &mode)) |
| 1793 | return NULL; |
| 1794 | path = PyBytes_AsString(opath); |
| 1795 | Py_BEGIN_ALLOW_THREADS |
| 1796 | res = access(path, mode); |
| 1797 | Py_END_ALLOW_THREADS |
| 1798 | Py_DECREF(opath); |
| 1799 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1800 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1803 | #ifndef F_OK |
| 1804 | #define F_OK 0 |
| 1805 | #endif |
| 1806 | #ifndef R_OK |
| 1807 | #define R_OK 4 |
| 1808 | #endif |
| 1809 | #ifndef W_OK |
| 1810 | #define W_OK 2 |
| 1811 | #endif |
| 1812 | #ifndef X_OK |
| 1813 | #define X_OK 1 |
| 1814 | #endif |
| 1815 | |
| 1816 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1817 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1818 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1819 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1820 | |
| 1821 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1822 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1823 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1824 | int id; |
| 1825 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1826 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1827 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1828 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1829 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1830 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1831 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1832 | if (id == 0) { |
| 1833 | ret = ttyname(); |
| 1834 | } |
| 1835 | else { |
| 1836 | ret = NULL; |
| 1837 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1838 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1839 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1840 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1841 | if (ret == NULL) |
| 1842 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1843 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1844 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1845 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1846 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1847 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1848 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1849 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1850 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1851 | |
| 1852 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1853 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1854 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1855 | char *ret; |
| 1856 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1857 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1858 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1859 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1860 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1861 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1862 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1863 | if (ret == NULL) |
| 1864 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1865 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1866 | } |
| 1867 | #endif |
| 1868 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1869 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1870 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1871 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1872 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1873 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1874 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1875 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1876 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1877 | 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] | 1878 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1879 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1880 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1881 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1882 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1883 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1884 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1887 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1888 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1889 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1890 | 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] | 1891 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1892 | |
| 1893 | static PyObject * |
| 1894 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1895 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1896 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1897 | } |
| 1898 | #endif /* HAVE_FCHDIR */ |
| 1899 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1901 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1902 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1903 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1904 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1905 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1906 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1907 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1908 | PyObject *opath = NULL; |
| 1909 | char *path = NULL; |
| 1910 | int i; |
| 1911 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1912 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1913 | DWORD attr; |
| 1914 | PyUnicodeObject *po; |
| 1915 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1916 | Py_BEGIN_ALLOW_THREADS |
| 1917 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1918 | if (attr != 0xFFFFFFFF) { |
| 1919 | if (i & _S_IWRITE) |
| 1920 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1921 | else |
| 1922 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1923 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1924 | } |
| 1925 | else |
| 1926 | res = 0; |
| 1927 | Py_END_ALLOW_THREADS |
| 1928 | if (!res) |
| 1929 | return win32_error_unicode("chmod", |
| 1930 | PyUnicode_AS_UNICODE(po)); |
| 1931 | Py_INCREF(Py_None); |
| 1932 | return Py_None; |
| 1933 | } |
| 1934 | /* Drop the argument parsing error as narrow strings |
| 1935 | are also valid. */ |
| 1936 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1937 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1938 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1939 | &opath, &i)) |
| 1940 | return NULL; |
| 1941 | path = PyBytes_AsString(opath); |
| 1942 | Py_BEGIN_ALLOW_THREADS |
| 1943 | attr = GetFileAttributesA(path); |
| 1944 | if (attr != 0xFFFFFFFF) { |
| 1945 | if (i & _S_IWRITE) |
| 1946 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1947 | else |
| 1948 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1949 | res = SetFileAttributesA(path, attr); |
| 1950 | } |
| 1951 | else |
| 1952 | res = 0; |
| 1953 | Py_END_ALLOW_THREADS |
| 1954 | if (!res) { |
| 1955 | win32_error("chmod", path); |
| 1956 | Py_DECREF(opath); |
| 1957 | return NULL; |
| 1958 | } |
| 1959 | Py_DECREF(opath); |
| 1960 | Py_INCREF(Py_None); |
| 1961 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1962 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1963 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1964 | &opath, &i)) |
| 1965 | return NULL; |
| 1966 | path = PyBytes_AsString(opath); |
| 1967 | Py_BEGIN_ALLOW_THREADS |
| 1968 | res = chmod(path, i); |
| 1969 | Py_END_ALLOW_THREADS |
| 1970 | if (res < 0) |
| 1971 | return posix_error_with_allocated_filename(opath); |
| 1972 | Py_DECREF(opath); |
| 1973 | Py_INCREF(Py_None); |
| 1974 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1975 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1978 | #ifdef HAVE_FCHMOD |
| 1979 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1980 | "fchmod(fd, mode)\n\n\ |
| 1981 | Change the access permissions of the file given by file\n\ |
| 1982 | descriptor fd."); |
| 1983 | |
| 1984 | static PyObject * |
| 1985 | posix_fchmod(PyObject *self, PyObject *args) |
| 1986 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1987 | int fd, mode, res; |
| 1988 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1989 | return NULL; |
| 1990 | Py_BEGIN_ALLOW_THREADS |
| 1991 | res = fchmod(fd, mode); |
| 1992 | Py_END_ALLOW_THREADS |
| 1993 | if (res < 0) |
| 1994 | return posix_error(); |
| 1995 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1996 | } |
| 1997 | #endif /* HAVE_FCHMOD */ |
| 1998 | |
| 1999 | #ifdef HAVE_LCHMOD |
| 2000 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2001 | "lchmod(path, mode)\n\n\ |
| 2002 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2003 | affects the link itself rather than the target."); |
| 2004 | |
| 2005 | static PyObject * |
| 2006 | posix_lchmod(PyObject *self, PyObject *args) |
| 2007 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2008 | PyObject *opath; |
| 2009 | char *path; |
| 2010 | int i; |
| 2011 | int res; |
| 2012 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2013 | &opath, &i)) |
| 2014 | return NULL; |
| 2015 | path = PyBytes_AsString(opath); |
| 2016 | Py_BEGIN_ALLOW_THREADS |
| 2017 | res = lchmod(path, i); |
| 2018 | Py_END_ALLOW_THREADS |
| 2019 | if (res < 0) |
| 2020 | return posix_error_with_allocated_filename(opath); |
| 2021 | Py_DECREF(opath); |
| 2022 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2023 | } |
| 2024 | #endif /* HAVE_LCHMOD */ |
| 2025 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2026 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2027 | #ifdef HAVE_CHFLAGS |
| 2028 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2029 | "chflags(path, flags)\n\n\ |
| 2030 | Set file flags."); |
| 2031 | |
| 2032 | static PyObject * |
| 2033 | posix_chflags(PyObject *self, PyObject *args) |
| 2034 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2035 | PyObject *opath; |
| 2036 | char *path; |
| 2037 | unsigned long flags; |
| 2038 | int res; |
| 2039 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2040 | PyUnicode_FSConverter, &opath, &flags)) |
| 2041 | return NULL; |
| 2042 | path = PyBytes_AsString(opath); |
| 2043 | Py_BEGIN_ALLOW_THREADS |
| 2044 | res = chflags(path, flags); |
| 2045 | Py_END_ALLOW_THREADS |
| 2046 | if (res < 0) |
| 2047 | return posix_error_with_allocated_filename(opath); |
| 2048 | Py_DECREF(opath); |
| 2049 | Py_INCREF(Py_None); |
| 2050 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2051 | } |
| 2052 | #endif /* HAVE_CHFLAGS */ |
| 2053 | |
| 2054 | #ifdef HAVE_LCHFLAGS |
| 2055 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2056 | "lchflags(path, flags)\n\n\ |
| 2057 | Set file flags.\n\ |
| 2058 | This function will not follow symbolic links."); |
| 2059 | |
| 2060 | static PyObject * |
| 2061 | posix_lchflags(PyObject *self, PyObject *args) |
| 2062 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2063 | PyObject *opath; |
| 2064 | char *path; |
| 2065 | unsigned long flags; |
| 2066 | int res; |
| 2067 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2068 | PyUnicode_FSConverter, &opath, &flags)) |
| 2069 | return NULL; |
| 2070 | path = PyBytes_AsString(opath); |
| 2071 | Py_BEGIN_ALLOW_THREADS |
| 2072 | res = lchflags(path, flags); |
| 2073 | Py_END_ALLOW_THREADS |
| 2074 | if (res < 0) |
| 2075 | return posix_error_with_allocated_filename(opath); |
| 2076 | Py_DECREF(opath); |
| 2077 | Py_INCREF(Py_None); |
| 2078 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2079 | } |
| 2080 | #endif /* HAVE_LCHFLAGS */ |
| 2081 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2082 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2083 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2084 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2085 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2086 | |
| 2087 | static PyObject * |
| 2088 | posix_chroot(PyObject *self, PyObject *args) |
| 2089 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2090 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2091 | } |
| 2092 | #endif |
| 2093 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2094 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2095 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2096 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2097 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2098 | |
| 2099 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2100 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2101 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2102 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2103 | } |
| 2104 | #endif /* HAVE_FSYNC */ |
| 2105 | |
| 2106 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2107 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2108 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2109 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2110 | #endif |
| 2111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2112 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2113 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2114 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2115 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2116 | |
| 2117 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2118 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2119 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2120 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2121 | } |
| 2122 | #endif /* HAVE_FDATASYNC */ |
| 2123 | |
| 2124 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2125 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2126 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2127 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2128 | 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] | 2129 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2130 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2131 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2132 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2133 | PyObject *opath; |
| 2134 | char *path; |
| 2135 | long uid, gid; |
| 2136 | int res; |
| 2137 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2138 | PyUnicode_FSConverter, &opath, |
| 2139 | &uid, &gid)) |
| 2140 | return NULL; |
| 2141 | path = PyBytes_AsString(opath); |
| 2142 | Py_BEGIN_ALLOW_THREADS |
| 2143 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2144 | Py_END_ALLOW_THREADS |
| 2145 | if (res < 0) |
| 2146 | return posix_error_with_allocated_filename(opath); |
| 2147 | Py_DECREF(opath); |
| 2148 | Py_INCREF(Py_None); |
| 2149 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2150 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2151 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2152 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2153 | #ifdef HAVE_FCHOWN |
| 2154 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2155 | "fchown(fd, uid, gid)\n\n\ |
| 2156 | Change the owner and group id of the file given by file descriptor\n\ |
| 2157 | fd to the numeric uid and gid."); |
| 2158 | |
| 2159 | static PyObject * |
| 2160 | posix_fchown(PyObject *self, PyObject *args) |
| 2161 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2162 | int fd; |
| 2163 | long uid, gid; |
| 2164 | int res; |
| 2165 | if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) |
| 2166 | return NULL; |
| 2167 | Py_BEGIN_ALLOW_THREADS |
| 2168 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2169 | Py_END_ALLOW_THREADS |
| 2170 | if (res < 0) |
| 2171 | return posix_error(); |
| 2172 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2173 | } |
| 2174 | #endif /* HAVE_FCHOWN */ |
| 2175 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2176 | #ifdef HAVE_LCHOWN |
| 2177 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2178 | "lchown(path, uid, gid)\n\n\ |
| 2179 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2180 | This function will not follow symbolic links."); |
| 2181 | |
| 2182 | static PyObject * |
| 2183 | posix_lchown(PyObject *self, PyObject *args) |
| 2184 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2185 | PyObject *opath; |
| 2186 | char *path; |
| 2187 | long uid, gid; |
| 2188 | int res; |
| 2189 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2190 | PyUnicode_FSConverter, &opath, |
| 2191 | &uid, &gid)) |
| 2192 | return NULL; |
| 2193 | path = PyBytes_AsString(opath); |
| 2194 | Py_BEGIN_ALLOW_THREADS |
| 2195 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2196 | Py_END_ALLOW_THREADS |
| 2197 | if (res < 0) |
| 2198 | return posix_error_with_allocated_filename(opath); |
| 2199 | Py_DECREF(opath); |
| 2200 | Py_INCREF(Py_None); |
| 2201 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2202 | } |
| 2203 | #endif /* HAVE_LCHOWN */ |
| 2204 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2205 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2206 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2207 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2208 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2209 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2210 | char buf[1026]; |
| 2211 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2212 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2213 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2214 | if (!use_bytes) { |
| 2215 | wchar_t wbuf[1026]; |
| 2216 | wchar_t *wbuf2 = wbuf; |
| 2217 | PyObject *resobj; |
| 2218 | DWORD len; |
| 2219 | Py_BEGIN_ALLOW_THREADS |
| 2220 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2221 | /* If the buffer is large enough, len does not include the |
| 2222 | terminating \0. If the buffer is too small, len includes |
| 2223 | the space needed for the terminator. */ |
| 2224 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2225 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2226 | if (wbuf2) |
| 2227 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2228 | } |
| 2229 | Py_END_ALLOW_THREADS |
| 2230 | if (!wbuf2) { |
| 2231 | PyErr_NoMemory(); |
| 2232 | return NULL; |
| 2233 | } |
| 2234 | if (!len) { |
| 2235 | if (wbuf2 != wbuf) free(wbuf2); |
| 2236 | return win32_error("getcwdu", NULL); |
| 2237 | } |
| 2238 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2239 | if (wbuf2 != wbuf) free(wbuf2); |
| 2240 | return resobj; |
| 2241 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2242 | #endif |
| 2243 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2244 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2245 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2246 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2247 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2248 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2249 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2250 | Py_END_ALLOW_THREADS |
| 2251 | if (res == NULL) |
| 2252 | return posix_error(); |
| 2253 | if (use_bytes) |
| 2254 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2255 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2256 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2257 | |
| 2258 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2259 | "getcwd() -> path\n\n\ |
| 2260 | Return a unicode string representing the current working directory."); |
| 2261 | |
| 2262 | static PyObject * |
| 2263 | posix_getcwd_unicode(PyObject *self) |
| 2264 | { |
| 2265 | return posix_getcwd(0); |
| 2266 | } |
| 2267 | |
| 2268 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2269 | "getcwdb() -> path\n\n\ |
| 2270 | Return a bytes string representing the current working directory."); |
| 2271 | |
| 2272 | static PyObject * |
| 2273 | posix_getcwd_bytes(PyObject *self) |
| 2274 | { |
| 2275 | return posix_getcwd(1); |
| 2276 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2277 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2278 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2279 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2280 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2281 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2282 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2283 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2284 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2285 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2286 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2287 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2288 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2289 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2290 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2291 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2292 | #ifdef MS_WINDOWS |
| 2293 | PyDoc_STRVAR(win32_link__doc__, |
| 2294 | "link(src, dst)\n\n\ |
| 2295 | Create a hard link to a file."); |
| 2296 | |
| 2297 | static PyObject * |
| 2298 | win32_link(PyObject *self, PyObject *args) |
| 2299 | { |
| 2300 | PyObject *osrc, *odst; |
| 2301 | char *src, *dst; |
| 2302 | BOOL rslt; |
| 2303 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2304 | PyUnicodeObject *usrc, *udst; |
| 2305 | if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) { |
| 2306 | Py_BEGIN_ALLOW_THREADS |
| 2307 | rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst), |
| 2308 | PyUnicode_AS_UNICODE(usrc), NULL); |
| 2309 | Py_END_ALLOW_THREADS |
| 2310 | |
| 2311 | if (rslt == 0) |
| 2312 | return win32_error("link", NULL); |
| 2313 | |
| 2314 | Py_RETURN_NONE; |
| 2315 | } |
| 2316 | |
| 2317 | /* Narrow strings also valid. */ |
| 2318 | PyErr_Clear(); |
| 2319 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2320 | if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, |
| 2321 | PyUnicode_FSConverter, &odst)) |
| 2322 | return NULL; |
| 2323 | |
| 2324 | src = PyBytes_AsString(osrc); |
| 2325 | dst = PyBytes_AsString(odst); |
| 2326 | |
| 2327 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2328 | rslt = CreateHardLinkA(dst, src, NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2329 | Py_END_ALLOW_THREADS |
| 2330 | |
Stefan Krah | 30b341f | 2010-11-27 11:44:18 +0000 | [diff] [blame] | 2331 | Py_DECREF(osrc); |
| 2332 | Py_DECREF(odst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2333 | if (rslt == 0) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2334 | return win32_error("link", NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2335 | |
| 2336 | Py_RETURN_NONE; |
| 2337 | } |
| 2338 | #endif /* MS_WINDOWS */ |
| 2339 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2340 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2341 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2342 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2343 | Return a list containing the names of the entries in the directory.\n\ |
| 2344 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2345 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2346 | \n\ |
| 2347 | 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] | 2348 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2349 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2350 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2351 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2352 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2353 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2354 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2355 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2357 | PyObject *d, *v; |
| 2358 | HANDLE hFindFile; |
| 2359 | BOOL result; |
| 2360 | WIN32_FIND_DATA FileData; |
| 2361 | PyObject *opath; |
| 2362 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2363 | char *bufptr = namebuf; |
| 2364 | 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] | 2365 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2366 | PyObject *po = NULL; |
| 2367 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2368 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2369 | Py_UNICODE *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2370 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2371 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2372 | po_wchars = L"."; |
| 2373 | len = 1; |
| 2374 | } else { |
| 2375 | po_wchars = PyUnicode_AS_UNICODE(po); |
| 2376 | len = PyUnicode_GET_SIZE(po); |
| 2377 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2378 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2379 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2380 | if (!wnamebuf) { |
| 2381 | PyErr_NoMemory(); |
| 2382 | return NULL; |
| 2383 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2384 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2385 | if (len > 0) { |
| 2386 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2387 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2388 | wnamebuf[len++] = L'\\'; |
| 2389 | wcscpy(wnamebuf + len, L"*.*"); |
| 2390 | } |
| 2391 | if ((d = PyList_New(0)) == NULL) { |
| 2392 | free(wnamebuf); |
| 2393 | return NULL; |
| 2394 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2395 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2396 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2397 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2398 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2399 | int error = GetLastError(); |
| 2400 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2401 | free(wnamebuf); |
| 2402 | return d; |
| 2403 | } |
| 2404 | Py_DECREF(d); |
| 2405 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2406 | free(wnamebuf); |
| 2407 | return NULL; |
| 2408 | } |
| 2409 | do { |
| 2410 | /* Skip over . and .. */ |
| 2411 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2412 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2413 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2414 | if (v == NULL) { |
| 2415 | Py_DECREF(d); |
| 2416 | d = NULL; |
| 2417 | break; |
| 2418 | } |
| 2419 | if (PyList_Append(d, v) != 0) { |
| 2420 | Py_DECREF(v); |
| 2421 | Py_DECREF(d); |
| 2422 | d = NULL; |
| 2423 | break; |
| 2424 | } |
| 2425 | Py_DECREF(v); |
| 2426 | } |
| 2427 | Py_BEGIN_ALLOW_THREADS |
| 2428 | result = FindNextFileW(hFindFile, &wFileData); |
| 2429 | Py_END_ALLOW_THREADS |
| 2430 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2431 | it got to the end of the directory. */ |
| 2432 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2433 | Py_DECREF(d); |
| 2434 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2435 | FindClose(hFindFile); |
| 2436 | free(wnamebuf); |
| 2437 | return NULL; |
| 2438 | } |
| 2439 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2440 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2441 | if (FindClose(hFindFile) == FALSE) { |
| 2442 | Py_DECREF(d); |
| 2443 | win32_error_unicode("FindClose", wnamebuf); |
| 2444 | free(wnamebuf); |
| 2445 | return NULL; |
| 2446 | } |
| 2447 | free(wnamebuf); |
| 2448 | return d; |
| 2449 | } |
| 2450 | /* Drop the argument parsing error as narrow strings |
| 2451 | are also valid. */ |
| 2452 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2453 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2454 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2455 | PyUnicode_FSConverter, &opath)) |
| 2456 | return NULL; |
| 2457 | if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { |
| 2458 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2459 | Py_DECREF(opath); |
| 2460 | return NULL; |
| 2461 | } |
| 2462 | strcpy(namebuf, PyBytes_AsString(opath)); |
| 2463 | len = PyObject_Size(opath); |
Stefan Krah | 2a7feee | 2010-11-27 22:06:49 +0000 | [diff] [blame] | 2464 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2465 | if (len > 0) { |
| 2466 | char ch = namebuf[len-1]; |
| 2467 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2468 | namebuf[len++] = '/'; |
| 2469 | strcpy(namebuf + len, "*.*"); |
| 2470 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2471 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2472 | if ((d = PyList_New(0)) == NULL) |
| 2473 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2474 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2475 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2476 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2477 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2478 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2479 | int error = GetLastError(); |
| 2480 | if (error == ERROR_FILE_NOT_FOUND) |
| 2481 | return d; |
| 2482 | Py_DECREF(d); |
| 2483 | return win32_error("FindFirstFile", namebuf); |
| 2484 | } |
| 2485 | do { |
| 2486 | /* Skip over . and .. */ |
| 2487 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2488 | strcmp(FileData.cFileName, "..") != 0) { |
| 2489 | v = PyBytes_FromString(FileData.cFileName); |
| 2490 | if (v == NULL) { |
| 2491 | Py_DECREF(d); |
| 2492 | d = NULL; |
| 2493 | break; |
| 2494 | } |
| 2495 | if (PyList_Append(d, v) != 0) { |
| 2496 | Py_DECREF(v); |
| 2497 | Py_DECREF(d); |
| 2498 | d = NULL; |
| 2499 | break; |
| 2500 | } |
| 2501 | Py_DECREF(v); |
| 2502 | } |
| 2503 | Py_BEGIN_ALLOW_THREADS |
| 2504 | result = FindNextFile(hFindFile, &FileData); |
| 2505 | Py_END_ALLOW_THREADS |
| 2506 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2507 | it got to the end of the directory. */ |
| 2508 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2509 | Py_DECREF(d); |
| 2510 | win32_error("FindNextFile", namebuf); |
| 2511 | FindClose(hFindFile); |
| 2512 | return NULL; |
| 2513 | } |
| 2514 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2515 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2516 | if (FindClose(hFindFile) == FALSE) { |
| 2517 | Py_DECREF(d); |
| 2518 | return win32_error("FindClose", namebuf); |
| 2519 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2520 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2521 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2522 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2523 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2524 | |
| 2525 | #ifndef MAX_PATH |
| 2526 | #define MAX_PATH CCHMAXPATH |
| 2527 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2528 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2529 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2530 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2531 | PyObject *d, *v; |
| 2532 | char namebuf[MAX_PATH+5]; |
| 2533 | HDIR hdir = 1; |
| 2534 | ULONG srchcnt = 1; |
| 2535 | FILEFINDBUF3 ep; |
| 2536 | APIRET rc; |
| 2537 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2538 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2539 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2540 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2541 | name = PyBytes_AsString(oname); |
| 2542 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2543 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2544 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2545 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2546 | return NULL; |
| 2547 | } |
| 2548 | strcpy(namebuf, name); |
| 2549 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2550 | if (*pt == ALTSEP) |
| 2551 | *pt = SEP; |
| 2552 | if (namebuf[len-1] != SEP) |
| 2553 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2554 | strcpy(namebuf + len, "*.*"); |
| 2555 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2556 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2557 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2558 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2559 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2560 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2561 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2562 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2563 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2564 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2565 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2566 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2567 | |
| 2568 | if (rc != NO_ERROR) { |
| 2569 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2570 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2573 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2574 | do { |
| 2575 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2576 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2577 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2578 | |
| 2579 | strcpy(namebuf, ep.achName); |
| 2580 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2581 | /* Leave Case of Name Alone -- In Native Form */ |
| 2582 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2583 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2584 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2585 | if (v == NULL) { |
| 2586 | Py_DECREF(d); |
| 2587 | d = NULL; |
| 2588 | break; |
| 2589 | } |
| 2590 | if (PyList_Append(d, v) != 0) { |
| 2591 | Py_DECREF(v); |
| 2592 | Py_DECREF(d); |
| 2593 | d = NULL; |
| 2594 | break; |
| 2595 | } |
| 2596 | Py_DECREF(v); |
| 2597 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2598 | } |
| 2599 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2600 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2601 | return d; |
| 2602 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2603 | PyObject *oname; |
| 2604 | char *name; |
| 2605 | PyObject *d, *v; |
| 2606 | DIR *dirp; |
| 2607 | struct dirent *ep; |
| 2608 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2609 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2610 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2611 | /* v is never read, so it does not need to be initialized yet. */ |
| 2612 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2613 | arg_is_unicode = 0; |
| 2614 | PyErr_Clear(); |
| 2615 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2616 | oname = NULL; |
| 2617 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2618 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2619 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2620 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2621 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2622 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2623 | Py_BEGIN_ALLOW_THREADS |
| 2624 | dirp = opendir(name); |
| 2625 | Py_END_ALLOW_THREADS |
| 2626 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2627 | return posix_error_with_allocated_filename(oname); |
| 2628 | } |
| 2629 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2630 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2631 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2632 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2633 | Py_DECREF(oname); |
| 2634 | return NULL; |
| 2635 | } |
| 2636 | for (;;) { |
| 2637 | errno = 0; |
| 2638 | Py_BEGIN_ALLOW_THREADS |
| 2639 | ep = readdir(dirp); |
| 2640 | Py_END_ALLOW_THREADS |
| 2641 | if (ep == NULL) { |
| 2642 | if (errno == 0) { |
| 2643 | break; |
| 2644 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2645 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2646 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2647 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2648 | Py_DECREF(d); |
| 2649 | return posix_error_with_allocated_filename(oname); |
| 2650 | } |
| 2651 | } |
| 2652 | if (ep->d_name[0] == '.' && |
| 2653 | (NAMLEN(ep) == 1 || |
| 2654 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2655 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2656 | if (arg_is_unicode) |
| 2657 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2658 | else |
| 2659 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2660 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2661 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2662 | break; |
| 2663 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2664 | if (PyList_Append(d, v) != 0) { |
| 2665 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2666 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2667 | break; |
| 2668 | } |
| 2669 | Py_DECREF(v); |
| 2670 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2671 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2672 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2673 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2674 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2675 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2676 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2677 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2678 | #endif /* which OS */ |
| 2679 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2680 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2681 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2682 | /* A helper function for abspath on win32 */ |
| 2683 | static PyObject * |
| 2684 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2685 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2686 | PyObject *opath; |
| 2687 | char *path; |
| 2688 | char outbuf[MAX_PATH*2]; |
| 2689 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2690 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2691 | PyUnicodeObject *po; |
| 2692 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2693 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2694 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2695 | Py_UNICODE *wtemp; |
| 2696 | DWORD result; |
| 2697 | PyObject *v; |
| 2698 | result = GetFullPathNameW(wpath, |
| 2699 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2700 | woutbuf, &wtemp); |
| 2701 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2702 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2703 | if (!woutbufp) |
| 2704 | return PyErr_NoMemory(); |
| 2705 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2706 | } |
| 2707 | if (result) |
| 2708 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2709 | else |
| 2710 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2711 | if (woutbufp != woutbuf) |
| 2712 | free(woutbufp); |
| 2713 | return v; |
| 2714 | } |
| 2715 | /* Drop the argument parsing error as narrow strings |
| 2716 | are also valid. */ |
| 2717 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2718 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2719 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2720 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2721 | PyUnicode_FSConverter, &opath)) |
| 2722 | return NULL; |
| 2723 | path = PyBytes_AsString(opath); |
| 2724 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2725 | outbuf, &temp)) { |
| 2726 | win32_error("GetFullPathName", path); |
| 2727 | Py_DECREF(opath); |
| 2728 | return NULL; |
| 2729 | } |
| 2730 | Py_DECREF(opath); |
| 2731 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2732 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2733 | Py_FileSystemDefaultEncoding, NULL); |
| 2734 | } |
| 2735 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2736 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2737 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2738 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 2739 | static int has_GetFinalPathNameByHandle = 0; |
| 2740 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 2741 | DWORD); |
| 2742 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 2743 | DWORD); |
| 2744 | static int |
| 2745 | check_GetFinalPathNameByHandle() |
| 2746 | { |
| 2747 | HINSTANCE hKernel32; |
| 2748 | /* only recheck */ |
| 2749 | if (!has_GetFinalPathNameByHandle) |
| 2750 | { |
| 2751 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 2752 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 2753 | "GetFinalPathNameByHandleA"); |
| 2754 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 2755 | "GetFinalPathNameByHandleW"); |
| 2756 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 2757 | Py_GetFinalPathNameByHandleW; |
| 2758 | } |
| 2759 | return has_GetFinalPathNameByHandle; |
| 2760 | } |
| 2761 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2762 | /* A helper function for samepath on windows */ |
| 2763 | static PyObject * |
| 2764 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 2765 | { |
| 2766 | HANDLE hFile; |
| 2767 | int buf_size; |
| 2768 | wchar_t *target_path; |
| 2769 | int result_length; |
| 2770 | PyObject *result; |
| 2771 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2772 | |
Brian Curtin | 94622b0 | 2010-09-24 00:03:39 +0000 | [diff] [blame] | 2773 | if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) { |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2774 | return NULL; |
| 2775 | } |
| 2776 | |
| 2777 | if(!check_GetFinalPathNameByHandle()) { |
| 2778 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 2779 | NotImplementedError. */ |
| 2780 | return PyErr_Format(PyExc_NotImplementedError, |
| 2781 | "GetFinalPathNameByHandle not available on this platform"); |
| 2782 | } |
| 2783 | |
| 2784 | hFile = CreateFileW( |
| 2785 | path, |
| 2786 | 0, /* desired access */ |
| 2787 | 0, /* share mode */ |
| 2788 | NULL, /* security attributes */ |
| 2789 | OPEN_EXISTING, |
| 2790 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 2791 | FILE_FLAG_BACKUP_SEMANTICS, |
| 2792 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2793 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2794 | if(hFile == INVALID_HANDLE_VALUE) { |
| 2795 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 2796 | return PyErr_Format(PyExc_RuntimeError, |
| 2797 | "Could not get a handle to file."); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | /* We have a good handle to the target, use it to determine the |
| 2801 | target path name. */ |
| 2802 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 2803 | |
| 2804 | if(!buf_size) |
| 2805 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2806 | |
| 2807 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 2808 | if(!target_path) |
| 2809 | return PyErr_NoMemory(); |
| 2810 | |
| 2811 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 2812 | buf_size, VOLUME_NAME_DOS); |
| 2813 | if(!result_length) |
| 2814 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
| 2815 | |
| 2816 | if(!CloseHandle(hFile)) |
| 2817 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2818 | |
| 2819 | target_path[result_length] = 0; |
| 2820 | result = PyUnicode_FromUnicode(target_path, result_length); |
| 2821 | free(target_path); |
| 2822 | return result; |
| 2823 | |
| 2824 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2825 | |
| 2826 | static PyObject * |
| 2827 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 2828 | { |
| 2829 | HANDLE hFile; |
| 2830 | BY_HANDLE_FILE_INFORMATION info; |
| 2831 | int fd; |
| 2832 | |
| 2833 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 2834 | return NULL; |
| 2835 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 2836 | if (!_PyVerify_fd(fd)) |
| 2837 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2838 | |
| 2839 | hFile = (HANDLE)_get_osfhandle(fd); |
| 2840 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 2841 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2842 | |
| 2843 | if (!GetFileInformationByHandle(hFile, &info)) |
| 2844 | return win32_error("_getfileinformation", NULL); |
| 2845 | |
| 2846 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 2847 | info.nFileIndexHigh, |
| 2848 | info.nFileIndexLow); |
| 2849 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2850 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2851 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2852 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2853 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2854 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2855 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2856 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2857 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2858 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2859 | int res; |
| 2860 | PyObject *opath; |
| 2861 | char *path; |
| 2862 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2863 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2864 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2865 | PyUnicodeObject *po; |
| 2866 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 2867 | Py_BEGIN_ALLOW_THREADS |
| 2868 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2869 | it is a simple dereference. */ |
| 2870 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 2871 | Py_END_ALLOW_THREADS |
| 2872 | if (!res) |
| 2873 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 2874 | Py_INCREF(Py_None); |
| 2875 | return Py_None; |
| 2876 | } |
| 2877 | /* Drop the argument parsing error as narrow strings |
| 2878 | are also valid. */ |
| 2879 | PyErr_Clear(); |
| 2880 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2881 | PyUnicode_FSConverter, &opath, &mode)) |
| 2882 | return NULL; |
| 2883 | path = PyBytes_AsString(opath); |
| 2884 | Py_BEGIN_ALLOW_THREADS |
| 2885 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2886 | it is a simple dereference. */ |
| 2887 | res = CreateDirectoryA(path, NULL); |
| 2888 | Py_END_ALLOW_THREADS |
| 2889 | if (!res) { |
| 2890 | win32_error("mkdir", path); |
| 2891 | Py_DECREF(opath); |
| 2892 | return NULL; |
| 2893 | } |
| 2894 | Py_DECREF(opath); |
| 2895 | Py_INCREF(Py_None); |
| 2896 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2897 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2898 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2899 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2900 | PyUnicode_FSConverter, &opath, &mode)) |
| 2901 | return NULL; |
| 2902 | path = PyBytes_AsString(opath); |
| 2903 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2904 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2905 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2906 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2907 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2908 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2909 | Py_END_ALLOW_THREADS |
| 2910 | if (res < 0) |
| 2911 | return posix_error_with_allocated_filename(opath); |
| 2912 | Py_DECREF(opath); |
| 2913 | Py_INCREF(Py_None); |
| 2914 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2915 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2918 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2919 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2920 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2921 | #include <sys/resource.h> |
| 2922 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2923 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2924 | |
| 2925 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2926 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2927 | "nice(inc) -> new_priority\n\n\ |
| 2928 | 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] | 2929 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2930 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2931 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2932 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2933 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2934 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2935 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 2936 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2937 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2938 | /* There are two flavours of 'nice': one that returns the new |
| 2939 | priority (as required by almost all standards out there) and the |
| 2940 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2941 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2942 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2943 | If we are of the nice family that returns the new priority, we |
| 2944 | need to clear errno before the call, and check if errno is filled |
| 2945 | before calling posix_error() on a returnvalue of -1, because the |
| 2946 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2948 | errno = 0; |
| 2949 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2950 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2951 | if (value == 0) |
| 2952 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2953 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2954 | if (value == -1 && errno != 0) |
| 2955 | /* either nice() or getpriority() returned an error */ |
| 2956 | return posix_error(); |
| 2957 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2958 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2959 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2960 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 2961 | |
| 2962 | #ifdef HAVE_GETPRIORITY |
| 2963 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 2964 | "getpriority(which, who) -> current_priority\n\n\ |
| 2965 | Get program scheduling priority."); |
| 2966 | |
| 2967 | static PyObject * |
| 2968 | posix_getpriority(PyObject *self, PyObject *args) |
| 2969 | { |
| 2970 | int which, who, retval; |
| 2971 | |
| 2972 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 2973 | return NULL; |
| 2974 | errno = 0; |
| 2975 | Py_BEGIN_ALLOW_THREADS |
| 2976 | retval = getpriority(which, who); |
| 2977 | Py_END_ALLOW_THREADS |
| 2978 | if (errno != 0) |
| 2979 | return posix_error(); |
| 2980 | return PyLong_FromLong((long)retval); |
| 2981 | } |
| 2982 | #endif /* HAVE_GETPRIORITY */ |
| 2983 | |
| 2984 | |
| 2985 | #ifdef HAVE_SETPRIORITY |
| 2986 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 2987 | "setpriority(which, who, prio) -> None\n\n\ |
| 2988 | Set program scheduling priority."); |
| 2989 | |
| 2990 | static PyObject * |
| 2991 | posix_setpriority(PyObject *self, PyObject *args) |
| 2992 | { |
| 2993 | int which, who, prio, retval; |
| 2994 | |
| 2995 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 2996 | return NULL; |
| 2997 | Py_BEGIN_ALLOW_THREADS |
| 2998 | retval = setpriority(which, who, prio); |
| 2999 | Py_END_ALLOW_THREADS |
| 3000 | if (retval == -1) |
| 3001 | return posix_error(); |
| 3002 | Py_RETURN_NONE; |
| 3003 | } |
| 3004 | #endif /* HAVE_SETPRIORITY */ |
| 3005 | |
| 3006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3007 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3008 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3009 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3010 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3011 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3012 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3013 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3014 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3015 | PyObject *o1, *o2; |
| 3016 | char *p1, *p2; |
| 3017 | BOOL result; |
| 3018 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 3019 | goto error; |
| 3020 | if (!convert_to_unicode(&o1)) |
| 3021 | goto error; |
| 3022 | if (!convert_to_unicode(&o2)) { |
| 3023 | Py_DECREF(o1); |
| 3024 | goto error; |
| 3025 | } |
| 3026 | Py_BEGIN_ALLOW_THREADS |
| 3027 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 3028 | PyUnicode_AsUnicode(o2)); |
| 3029 | Py_END_ALLOW_THREADS |
| 3030 | Py_DECREF(o1); |
| 3031 | Py_DECREF(o2); |
| 3032 | if (!result) |
| 3033 | return win32_error("rename", NULL); |
| 3034 | Py_INCREF(Py_None); |
| 3035 | return Py_None; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3036 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3037 | PyErr_Clear(); |
| 3038 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 3039 | return NULL; |
| 3040 | Py_BEGIN_ALLOW_THREADS |
| 3041 | result = MoveFileA(p1, p2); |
| 3042 | Py_END_ALLOW_THREADS |
| 3043 | if (!result) |
| 3044 | return win32_error("rename", NULL); |
| 3045 | Py_INCREF(Py_None); |
| 3046 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3047 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3048 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3049 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3053 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3054 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3055 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3056 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3057 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3058 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3059 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3060 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3061 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3062 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3063 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3064 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3068 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3069 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3070 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3071 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3072 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3073 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3074 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3075 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3076 | 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] | 3077 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3078 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3079 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3082 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3083 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3084 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3085 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3086 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3087 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3088 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3089 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3090 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3091 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3092 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3093 | wchar_t *command; |
| 3094 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3095 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3096 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3097 | Py_BEGIN_ALLOW_THREADS |
| 3098 | sts = _wsystem(command); |
| 3099 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3100 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3101 | PyObject *command_obj; |
| 3102 | char *command; |
| 3103 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3104 | PyUnicode_FSConverter, &command_obj)) |
| 3105 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3106 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3107 | command = PyBytes_AsString(command_obj); |
| 3108 | Py_BEGIN_ALLOW_THREADS |
| 3109 | sts = system(command); |
| 3110 | Py_END_ALLOW_THREADS |
| 3111 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3112 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3113 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3114 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3115 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3116 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3117 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3118 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3119 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3120 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3121 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3122 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3123 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3124 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3125 | int i; |
| 3126 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3127 | return NULL; |
| 3128 | i = (int)umask(i); |
| 3129 | if (i < 0) |
| 3130 | return posix_error(); |
| 3131 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3134 | #ifdef MS_WINDOWS |
| 3135 | |
| 3136 | /* override the default DeleteFileW behavior so that directory |
| 3137 | symlinks can be removed with this function, the same as with |
| 3138 | Unix symlinks */ |
| 3139 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3140 | { |
| 3141 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3142 | WIN32_FIND_DATAW find_data; |
| 3143 | HANDLE find_data_handle; |
| 3144 | int is_directory = 0; |
| 3145 | int is_link = 0; |
| 3146 | |
| 3147 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3148 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3149 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3150 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3151 | it is a symlink */ |
| 3152 | if(is_directory && |
| 3153 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3154 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3155 | |
| 3156 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3157 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3158 | FindClose(find_data_handle); |
| 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | if (is_directory && is_link) |
| 3164 | return RemoveDirectoryW(lpFileName); |
| 3165 | |
| 3166 | return DeleteFileW(lpFileName); |
| 3167 | } |
| 3168 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3169 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3170 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3171 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3172 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3173 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3174 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3175 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3176 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3177 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3178 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3179 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3180 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3181 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3182 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3183 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3184 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3185 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3186 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3189 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3190 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3191 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3192 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3193 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3194 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3195 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3196 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3197 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3198 | struct utsname u; |
| 3199 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3200 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3201 | Py_BEGIN_ALLOW_THREADS |
| 3202 | res = uname(&u); |
| 3203 | Py_END_ALLOW_THREADS |
| 3204 | if (res < 0) |
| 3205 | return posix_error(); |
| 3206 | return Py_BuildValue("(sssss)", |
| 3207 | u.sysname, |
| 3208 | u.nodename, |
| 3209 | u.release, |
| 3210 | u.version, |
| 3211 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3212 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3213 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3214 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3215 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3216 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3217 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3218 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3219 | if (PyFloat_Check(t)) { |
| 3220 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3221 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3222 | if (!intobj) |
| 3223 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3224 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3225 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3226 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3227 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3228 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3229 | Py_DECREF(intobj); |
| 3230 | if (intval == -1 && PyErr_Occurred()) |
| 3231 | return -1; |
| 3232 | *sec = intval; |
| 3233 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
| 3234 | if (*usec < 0) |
| 3235 | /* If rounding gave us a negative number, |
| 3236 | truncate. */ |
| 3237 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3238 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3239 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3240 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3241 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3242 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3243 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3244 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3245 | if (intval == -1 && PyErr_Occurred()) |
| 3246 | return -1; |
| 3247 | *sec = intval; |
| 3248 | *usec = 0; |
| 3249 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3250 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3251 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3252 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3253 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3254 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3255 | 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] | 3256 | 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] | 3257 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3258 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3259 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3260 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3261 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3262 | PyObject *arg; |
| 3263 | PyUnicodeObject *obwpath; |
| 3264 | wchar_t *wpath = NULL; |
| 3265 | PyObject *oapath; |
| 3266 | char *apath; |
| 3267 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3268 | time_t atimesec, mtimesec; |
| 3269 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3270 | FILETIME atime, mtime; |
| 3271 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3272 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3273 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 3274 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 3275 | Py_BEGIN_ALLOW_THREADS |
| 3276 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3277 | NULL, OPEN_EXISTING, |
| 3278 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3279 | Py_END_ALLOW_THREADS |
| 3280 | if (hFile == INVALID_HANDLE_VALUE) |
| 3281 | return win32_error_unicode("utime", wpath); |
| 3282 | } else |
| 3283 | /* Drop the argument parsing error as narrow strings |
| 3284 | are also valid. */ |
| 3285 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3286 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3287 | if (!wpath) { |
| 3288 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3289 | PyUnicode_FSConverter, &oapath, &arg)) |
| 3290 | return NULL; |
| 3291 | apath = PyBytes_AsString(oapath); |
| 3292 | Py_BEGIN_ALLOW_THREADS |
| 3293 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3294 | NULL, OPEN_EXISTING, |
| 3295 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3296 | Py_END_ALLOW_THREADS |
| 3297 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3298 | win32_error("utime", apath); |
| 3299 | Py_DECREF(oapath); |
| 3300 | return NULL; |
| 3301 | } |
| 3302 | Py_DECREF(oapath); |
| 3303 | } |
| 3304 | |
| 3305 | if (arg == Py_None) { |
| 3306 | SYSTEMTIME now; |
| 3307 | GetSystemTime(&now); |
| 3308 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3309 | !SystemTimeToFileTime(&now, &atime)) { |
| 3310 | win32_error("utime", NULL); |
| 3311 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3312 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3313 | } |
| 3314 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3315 | PyErr_SetString(PyExc_TypeError, |
| 3316 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3317 | goto done; |
| 3318 | } |
| 3319 | else { |
| 3320 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3321 | &atimesec, &ausec) == -1) |
| 3322 | goto done; |
| 3323 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3324 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3325 | &mtimesec, &musec) == -1) |
| 3326 | goto done; |
| 3327 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3328 | } |
| 3329 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3330 | /* Avoid putting the file name into the error here, |
| 3331 | as that may confuse the user into believing that |
| 3332 | something is wrong with the file, when it also |
| 3333 | could be the time stamp that gives a problem. */ |
| 3334 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3335 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3336 | } |
| 3337 | Py_INCREF(Py_None); |
| 3338 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3339 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3340 | CloseHandle(hFile); |
| 3341 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3342 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3343 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3344 | PyObject *opath; |
| 3345 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3346 | time_t atime, mtime; |
| 3347 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3348 | int res; |
| 3349 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3350 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3351 | #if defined(HAVE_UTIMES) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3352 | struct timeval buf[2]; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3353 | #define ATIME buf[0].tv_sec |
| 3354 | #define MTIME buf[1].tv_sec |
| 3355 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3356 | /* XXX should define struct utimbuf instead, above */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3357 | struct utimbuf buf; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3358 | #define ATIME buf.actime |
| 3359 | #define MTIME buf.modtime |
| 3360 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3361 | #else /* HAVE_UTIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3362 | time_t buf[2]; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3363 | #define ATIME buf[0] |
| 3364 | #define MTIME buf[1] |
| 3365 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3366 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3367 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3368 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3369 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3370 | PyUnicode_FSConverter, &opath, &arg)) |
| 3371 | return NULL; |
| 3372 | path = PyBytes_AsString(opath); |
| 3373 | if (arg == Py_None) { |
| 3374 | /* optional time values not given */ |
| 3375 | Py_BEGIN_ALLOW_THREADS |
| 3376 | res = utime(path, NULL); |
| 3377 | Py_END_ALLOW_THREADS |
| 3378 | } |
| 3379 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3380 | PyErr_SetString(PyExc_TypeError, |
| 3381 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3382 | Py_DECREF(opath); |
| 3383 | return NULL; |
| 3384 | } |
| 3385 | else { |
| 3386 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3387 | &atime, &ausec) == -1) { |
| 3388 | Py_DECREF(opath); |
| 3389 | return NULL; |
| 3390 | } |
| 3391 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3392 | &mtime, &musec) == -1) { |
| 3393 | Py_DECREF(opath); |
| 3394 | return NULL; |
| 3395 | } |
| 3396 | ATIME = atime; |
| 3397 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3398 | #ifdef HAVE_UTIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3399 | buf[0].tv_usec = ausec; |
| 3400 | buf[1].tv_usec = musec; |
| 3401 | Py_BEGIN_ALLOW_THREADS |
| 3402 | res = utimes(path, buf); |
| 3403 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3404 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3405 | Py_BEGIN_ALLOW_THREADS |
| 3406 | res = utime(path, UTIME_ARG); |
| 3407 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3408 | #endif /* HAVE_UTIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3409 | } |
| 3410 | if (res < 0) { |
| 3411 | return posix_error_with_allocated_filename(opath); |
| 3412 | } |
| 3413 | Py_DECREF(opath); |
| 3414 | Py_INCREF(Py_None); |
| 3415 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3416 | #undef UTIME_ARG |
| 3417 | #undef ATIME |
| 3418 | #undef MTIME |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3419 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3422 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3423 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3424 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3425 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3426 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3427 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3428 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3429 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3430 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3431 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3432 | int sts; |
| 3433 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3434 | return NULL; |
| 3435 | _exit(sts); |
| 3436 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3439 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3440 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3441 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3442 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3443 | Py_ssize_t i; |
| 3444 | for (i = 0; i < count; i++) |
| 3445 | PyMem_Free(array[i]); |
| 3446 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3447 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3448 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3449 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3450 | int fsconvert_strdup(PyObject *o, char**out) |
| 3451 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3452 | PyObject *bytes; |
| 3453 | Py_ssize_t size; |
| 3454 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3455 | return 0; |
| 3456 | size = PyBytes_GET_SIZE(bytes); |
| 3457 | *out = PyMem_Malloc(size+1); |
| 3458 | if (!*out) |
| 3459 | return 0; |
| 3460 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3461 | Py_DECREF(bytes); |
| 3462 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3463 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3464 | #endif |
| 3465 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3466 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3467 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3468 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3469 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3470 | Execute an executable path with arguments, replacing current process.\n\ |
| 3471 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3472 | path: path of executable file\n\ |
| 3473 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3474 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3475 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3476 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3477 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3478 | PyObject *opath; |
| 3479 | char *path; |
| 3480 | PyObject *argv; |
| 3481 | char **argvlist; |
| 3482 | Py_ssize_t i, argc; |
| 3483 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3484 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3485 | /* execv has two arguments: (path, argv), where |
| 3486 | argv is a list or tuple of strings. */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3487 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3488 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3489 | PyUnicode_FSConverter, |
| 3490 | &opath, &argv)) |
| 3491 | return NULL; |
| 3492 | path = PyBytes_AsString(opath); |
| 3493 | if (PyList_Check(argv)) { |
| 3494 | argc = PyList_Size(argv); |
| 3495 | getitem = PyList_GetItem; |
| 3496 | } |
| 3497 | else if (PyTuple_Check(argv)) { |
| 3498 | argc = PyTuple_Size(argv); |
| 3499 | getitem = PyTuple_GetItem; |
| 3500 | } |
| 3501 | else { |
| 3502 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
| 3503 | Py_DECREF(opath); |
| 3504 | return NULL; |
| 3505 | } |
| 3506 | if (argc < 1) { |
| 3507 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 3508 | Py_DECREF(opath); |
| 3509 | return NULL; |
| 3510 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3511 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3512 | argvlist = PyMem_NEW(char *, argc+1); |
| 3513 | if (argvlist == NULL) { |
| 3514 | Py_DECREF(opath); |
| 3515 | return PyErr_NoMemory(); |
| 3516 | } |
| 3517 | for (i = 0; i < argc; i++) { |
| 3518 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3519 | &argvlist[i])) { |
| 3520 | free_string_array(argvlist, i); |
| 3521 | PyErr_SetString(PyExc_TypeError, |
| 3522 | "execv() arg 2 must contain only strings"); |
| 3523 | Py_DECREF(opath); |
| 3524 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3525 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3526 | } |
| 3527 | } |
| 3528 | argvlist[argc] = NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3529 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3530 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3531 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3532 | /* If we get here it's definitely an error */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3533 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3534 | free_string_array(argvlist, argc); |
| 3535 | Py_DECREF(opath); |
| 3536 | return posix_error(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3539 | static char** |
| 3540 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3541 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3542 | char **envlist; |
| 3543 | Py_ssize_t i, pos, envc; |
| 3544 | PyObject *keys=NULL, *vals=NULL; |
| 3545 | PyObject *key, *val, *key2, *val2; |
| 3546 | char *p, *k, *v; |
| 3547 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3548 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3549 | i = PyMapping_Size(env); |
| 3550 | if (i < 0) |
| 3551 | return NULL; |
| 3552 | envlist = PyMem_NEW(char *, i + 1); |
| 3553 | if (envlist == NULL) { |
| 3554 | PyErr_NoMemory(); |
| 3555 | return NULL; |
| 3556 | } |
| 3557 | envc = 0; |
| 3558 | keys = PyMapping_Keys(env); |
| 3559 | vals = PyMapping_Values(env); |
| 3560 | if (!keys || !vals) |
| 3561 | goto error; |
| 3562 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3563 | PyErr_Format(PyExc_TypeError, |
| 3564 | "env.keys() or env.values() is not a list"); |
| 3565 | goto error; |
| 3566 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3567 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3568 | for (pos = 0; pos < i; pos++) { |
| 3569 | key = PyList_GetItem(keys, pos); |
| 3570 | val = PyList_GetItem(vals, pos); |
| 3571 | if (!key || !val) |
| 3572 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3573 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3574 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3575 | goto error; |
| 3576 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3577 | Py_DECREF(key2); |
| 3578 | goto error; |
| 3579 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3580 | |
| 3581 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3582 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3583 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3584 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3585 | k = PyBytes_AsString(key2); |
| 3586 | v = PyBytes_AsString(val2); |
| 3587 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3588 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3589 | p = PyMem_NEW(char, len); |
| 3590 | if (p == NULL) { |
| 3591 | PyErr_NoMemory(); |
| 3592 | Py_DECREF(key2); |
| 3593 | Py_DECREF(val2); |
| 3594 | goto error; |
| 3595 | } |
| 3596 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3597 | envlist[envc++] = p; |
| 3598 | Py_DECREF(key2); |
| 3599 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3600 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3601 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3602 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3603 | } |
| 3604 | Py_DECREF(vals); |
| 3605 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3606 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3607 | envlist[envc] = 0; |
| 3608 | *envc_ptr = envc; |
| 3609 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3610 | |
| 3611 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3612 | Py_XDECREF(keys); |
| 3613 | Py_XDECREF(vals); |
| 3614 | while (--envc >= 0) |
| 3615 | PyMem_DEL(envlist[envc]); |
| 3616 | PyMem_DEL(envlist); |
| 3617 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3618 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3620 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3621 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3622 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3623 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3624 | path: path of executable file\n\ |
| 3625 | args: tuple or list of arguments\n\ |
| 3626 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3627 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3628 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3629 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3630 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3631 | PyObject *opath; |
| 3632 | char *path; |
| 3633 | PyObject *argv, *env; |
| 3634 | char **argvlist; |
| 3635 | char **envlist; |
| 3636 | Py_ssize_t i, argc, envc; |
| 3637 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3638 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3639 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3640 | /* execve has three arguments: (path, argv, env), where |
| 3641 | argv is a list or tuple of strings and env is a dictionary |
| 3642 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3643 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3644 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3645 | PyUnicode_FSConverter, |
| 3646 | &opath, &argv, &env)) |
| 3647 | return NULL; |
| 3648 | path = PyBytes_AsString(opath); |
| 3649 | if (PyList_Check(argv)) { |
| 3650 | argc = PyList_Size(argv); |
| 3651 | getitem = PyList_GetItem; |
| 3652 | } |
| 3653 | else if (PyTuple_Check(argv)) { |
| 3654 | argc = PyTuple_Size(argv); |
| 3655 | getitem = PyTuple_GetItem; |
| 3656 | } |
| 3657 | else { |
| 3658 | PyErr_SetString(PyExc_TypeError, |
| 3659 | "execve() arg 2 must be a tuple or list"); |
| 3660 | goto fail_0; |
| 3661 | } |
| 3662 | if (!PyMapping_Check(env)) { |
| 3663 | PyErr_SetString(PyExc_TypeError, |
| 3664 | "execve() arg 3 must be a mapping object"); |
| 3665 | goto fail_0; |
| 3666 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3667 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3668 | argvlist = PyMem_NEW(char *, argc+1); |
| 3669 | if (argvlist == NULL) { |
| 3670 | PyErr_NoMemory(); |
| 3671 | goto fail_0; |
| 3672 | } |
| 3673 | for (i = 0; i < argc; i++) { |
| 3674 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3675 | &argvlist[i])) |
| 3676 | { |
| 3677 | lastarg = i; |
| 3678 | goto fail_1; |
| 3679 | } |
| 3680 | } |
| 3681 | lastarg = argc; |
| 3682 | argvlist[argc] = NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3683 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3684 | envlist = parse_envlist(env, &envc); |
| 3685 | if (envlist == NULL) |
| 3686 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3687 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3688 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3689 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3690 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3691 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3692 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3693 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3694 | while (--envc >= 0) |
| 3695 | PyMem_DEL(envlist[envc]); |
| 3696 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3697 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3698 | free_string_array(argvlist, lastarg); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3699 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3700 | Py_DECREF(opath); |
| 3701 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3702 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3703 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3704 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3705 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3706 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3707 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3708 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3709 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3710 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3711 | mode: mode of process creation\n\ |
| 3712 | path: path of executable file\n\ |
| 3713 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3714 | |
| 3715 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3716 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3717 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3718 | PyObject *opath; |
| 3719 | char *path; |
| 3720 | PyObject *argv; |
| 3721 | char **argvlist; |
| 3722 | int mode, i; |
| 3723 | Py_ssize_t argc; |
| 3724 | Py_intptr_t spawnval; |
| 3725 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3726 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3727 | /* spawnv has three arguments: (mode, path, argv), where |
| 3728 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3729 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3730 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 3731 | PyUnicode_FSConverter, |
| 3732 | &opath, &argv)) |
| 3733 | return NULL; |
| 3734 | path = PyBytes_AsString(opath); |
| 3735 | if (PyList_Check(argv)) { |
| 3736 | argc = PyList_Size(argv); |
| 3737 | getitem = PyList_GetItem; |
| 3738 | } |
| 3739 | else if (PyTuple_Check(argv)) { |
| 3740 | argc = PyTuple_Size(argv); |
| 3741 | getitem = PyTuple_GetItem; |
| 3742 | } |
| 3743 | else { |
| 3744 | PyErr_SetString(PyExc_TypeError, |
| 3745 | "spawnv() arg 2 must be a tuple or list"); |
| 3746 | Py_DECREF(opath); |
| 3747 | return NULL; |
| 3748 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3749 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3750 | argvlist = PyMem_NEW(char *, argc+1); |
| 3751 | if (argvlist == NULL) { |
| 3752 | Py_DECREF(opath); |
| 3753 | return PyErr_NoMemory(); |
| 3754 | } |
| 3755 | for (i = 0; i < argc; i++) { |
| 3756 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3757 | &argvlist[i])) { |
| 3758 | free_string_array(argvlist, i); |
| 3759 | PyErr_SetString( |
| 3760 | PyExc_TypeError, |
| 3761 | "spawnv() arg 2 must contain only strings"); |
| 3762 | Py_DECREF(opath); |
| 3763 | return NULL; |
| 3764 | } |
| 3765 | } |
| 3766 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3767 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3768 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3769 | Py_BEGIN_ALLOW_THREADS |
| 3770 | spawnval = spawnv(mode, path, argvlist); |
| 3771 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3772 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3773 | if (mode == _OLD_P_OVERLAY) |
| 3774 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3775 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3776 | Py_BEGIN_ALLOW_THREADS |
| 3777 | spawnval = _spawnv(mode, path, argvlist); |
| 3778 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3779 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3780 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3781 | free_string_array(argvlist, argc); |
| 3782 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3783 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3784 | if (spawnval == -1) |
| 3785 | return posix_error(); |
| 3786 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3787 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3788 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3789 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3790 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3791 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3795 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3796 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3797 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3798 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3799 | mode: mode of process creation\n\ |
| 3800 | path: path of executable file\n\ |
| 3801 | args: tuple or list of arguments\n\ |
| 3802 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3803 | |
| 3804 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3805 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3806 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3807 | PyObject *opath; |
| 3808 | char *path; |
| 3809 | PyObject *argv, *env; |
| 3810 | char **argvlist; |
| 3811 | char **envlist; |
| 3812 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 3813 | int mode; |
| 3814 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3815 | Py_intptr_t spawnval; |
| 3816 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3817 | Py_ssize_t lastarg = 0; |
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 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3820 | argv is a list or tuple of strings and env is a dictionary |
| 3821 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3823 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 3824 | PyUnicode_FSConverter, |
| 3825 | &opath, &argv, &env)) |
| 3826 | return NULL; |
| 3827 | path = PyBytes_AsString(opath); |
| 3828 | if (PyList_Check(argv)) { |
| 3829 | argc = PyList_Size(argv); |
| 3830 | getitem = PyList_GetItem; |
| 3831 | } |
| 3832 | else if (PyTuple_Check(argv)) { |
| 3833 | argc = PyTuple_Size(argv); |
| 3834 | getitem = PyTuple_GetItem; |
| 3835 | } |
| 3836 | else { |
| 3837 | PyErr_SetString(PyExc_TypeError, |
| 3838 | "spawnve() arg 2 must be a tuple or list"); |
| 3839 | goto fail_0; |
| 3840 | } |
| 3841 | if (!PyMapping_Check(env)) { |
| 3842 | PyErr_SetString(PyExc_TypeError, |
| 3843 | "spawnve() arg 3 must be a mapping object"); |
| 3844 | goto fail_0; |
| 3845 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3846 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3847 | argvlist = PyMem_NEW(char *, argc+1); |
| 3848 | if (argvlist == NULL) { |
| 3849 | PyErr_NoMemory(); |
| 3850 | goto fail_0; |
| 3851 | } |
| 3852 | for (i = 0; i < argc; i++) { |
| 3853 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3854 | &argvlist[i])) |
| 3855 | { |
| 3856 | lastarg = i; |
| 3857 | goto fail_1; |
| 3858 | } |
| 3859 | } |
| 3860 | lastarg = argc; |
| 3861 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3862 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3863 | envlist = parse_envlist(env, &envc); |
| 3864 | if (envlist == NULL) |
| 3865 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3866 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3867 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3868 | Py_BEGIN_ALLOW_THREADS |
| 3869 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3870 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3871 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3872 | if (mode == _OLD_P_OVERLAY) |
| 3873 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3874 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3875 | Py_BEGIN_ALLOW_THREADS |
| 3876 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3877 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3878 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3879 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3880 | if (spawnval == -1) |
| 3881 | (void) posix_error(); |
| 3882 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3883 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3884 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3885 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3886 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3887 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3888 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3889 | while (--envc >= 0) |
| 3890 | PyMem_DEL(envlist[envc]); |
| 3891 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3892 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3893 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3894 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3895 | Py_DECREF(opath); |
| 3896 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3897 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3898 | |
| 3899 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3900 | #if defined(PYOS_OS2) |
| 3901 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3902 | "spawnvp(mode, file, args)\n\n\ |
| 3903 | Execute the program 'file' in a new process, using the environment\n\ |
| 3904 | search path to find the file.\n\ |
| 3905 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3906 | mode: mode of process creation\n\ |
| 3907 | file: executable file name\n\ |
| 3908 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3909 | |
| 3910 | static PyObject * |
| 3911 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3912 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3913 | PyObject *opath; |
| 3914 | char *path; |
| 3915 | PyObject *argv; |
| 3916 | char **argvlist; |
| 3917 | int mode, i, argc; |
| 3918 | Py_intptr_t spawnval; |
| 3919 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3920 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3921 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3922 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3923 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3924 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 3925 | PyUnicode_FSConverter, |
| 3926 | &opath, &argv)) |
| 3927 | return NULL; |
| 3928 | path = PyBytes_AsString(opath); |
| 3929 | if (PyList_Check(argv)) { |
| 3930 | argc = PyList_Size(argv); |
| 3931 | getitem = PyList_GetItem; |
| 3932 | } |
| 3933 | else if (PyTuple_Check(argv)) { |
| 3934 | argc = PyTuple_Size(argv); |
| 3935 | getitem = PyTuple_GetItem; |
| 3936 | } |
| 3937 | else { |
| 3938 | PyErr_SetString(PyExc_TypeError, |
| 3939 | "spawnvp() arg 2 must be a tuple or list"); |
| 3940 | Py_DECREF(opath); |
| 3941 | return NULL; |
| 3942 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3943 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3944 | argvlist = PyMem_NEW(char *, argc+1); |
| 3945 | if (argvlist == NULL) { |
| 3946 | Py_DECREF(opath); |
| 3947 | return PyErr_NoMemory(); |
| 3948 | } |
| 3949 | for (i = 0; i < argc; i++) { |
| 3950 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3951 | &argvlist[i])) { |
| 3952 | free_string_array(argvlist, i); |
| 3953 | PyErr_SetString( |
| 3954 | PyExc_TypeError, |
| 3955 | "spawnvp() arg 2 must contain only strings"); |
| 3956 | Py_DECREF(opath); |
| 3957 | return NULL; |
| 3958 | } |
| 3959 | } |
| 3960 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3962 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3963 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3964 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3965 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3966 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3967 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3968 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3969 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3970 | free_string_array(argvlist, argc); |
| 3971 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3972 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3973 | if (spawnval == -1) |
| 3974 | return posix_error(); |
| 3975 | else |
| 3976 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
| 3979 | |
| 3980 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3981 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3982 | Execute the program 'file' in a new process, using the environment\n\ |
| 3983 | search path to find the file.\n\ |
| 3984 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3985 | mode: mode of process creation\n\ |
| 3986 | file: executable file name\n\ |
| 3987 | args: tuple or list of arguments\n\ |
| 3988 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3989 | |
| 3990 | static PyObject * |
| 3991 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3992 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3993 | PyObject *opath |
| 3994 | char *path; |
| 3995 | PyObject *argv, *env; |
| 3996 | char **argvlist; |
| 3997 | char **envlist; |
| 3998 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 3999 | int mode; |
| 4000 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4001 | Py_intptr_t spawnval; |
| 4002 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4003 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4005 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4006 | argv is a list or tuple of strings and env is a dictionary |
| 4007 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4008 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4009 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4010 | PyUnicode_FSConverter, |
| 4011 | &opath, &argv, &env)) |
| 4012 | return NULL; |
| 4013 | path = PyBytes_AsString(opath); |
| 4014 | if (PyList_Check(argv)) { |
| 4015 | argc = PyList_Size(argv); |
| 4016 | getitem = PyList_GetItem; |
| 4017 | } |
| 4018 | else if (PyTuple_Check(argv)) { |
| 4019 | argc = PyTuple_Size(argv); |
| 4020 | getitem = PyTuple_GetItem; |
| 4021 | } |
| 4022 | else { |
| 4023 | PyErr_SetString(PyExc_TypeError, |
| 4024 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4025 | goto fail_0; |
| 4026 | } |
| 4027 | if (!PyMapping_Check(env)) { |
| 4028 | PyErr_SetString(PyExc_TypeError, |
| 4029 | "spawnvpe() arg 3 must be a mapping object"); |
| 4030 | goto fail_0; |
| 4031 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4032 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4033 | argvlist = PyMem_NEW(char *, argc+1); |
| 4034 | if (argvlist == NULL) { |
| 4035 | PyErr_NoMemory(); |
| 4036 | goto fail_0; |
| 4037 | } |
| 4038 | for (i = 0; i < argc; i++) { |
| 4039 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4040 | &argvlist[i])) |
| 4041 | { |
| 4042 | lastarg = i; |
| 4043 | goto fail_1; |
| 4044 | } |
| 4045 | } |
| 4046 | lastarg = argc; |
| 4047 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4048 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4049 | envlist = parse_envlist(env, &envc); |
| 4050 | if (envlist == NULL) |
| 4051 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4052 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4053 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4054 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4055 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4056 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4057 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4058 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4059 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4060 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4061 | if (spawnval == -1) |
| 4062 | (void) posix_error(); |
| 4063 | else |
| 4064 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4065 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4066 | while (--envc >= 0) |
| 4067 | PyMem_DEL(envlist[envc]); |
| 4068 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4069 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4070 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4071 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4072 | Py_DECREF(opath); |
| 4073 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4074 | } |
| 4075 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4076 | #endif /* HAVE_SPAWNV */ |
| 4077 | |
| 4078 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4079 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4080 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4081 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4082 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4083 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4084 | 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] | 4085 | |
| 4086 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4087 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4088 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4089 | pid_t pid; |
| 4090 | int result = 0; |
| 4091 | _PyImport_AcquireLock(); |
| 4092 | pid = fork1(); |
| 4093 | if (pid == 0) { |
| 4094 | /* child: this clobbers and resets the import lock. */ |
| 4095 | PyOS_AfterFork(); |
| 4096 | } else { |
| 4097 | /* parent: release the import lock. */ |
| 4098 | result = _PyImport_ReleaseLock(); |
| 4099 | } |
| 4100 | if (pid == -1) |
| 4101 | return posix_error(); |
| 4102 | if (result < 0) { |
| 4103 | /* Don't clobber the OSError if the fork failed. */ |
| 4104 | PyErr_SetString(PyExc_RuntimeError, |
| 4105 | "not holding the import lock"); |
| 4106 | return NULL; |
| 4107 | } |
| 4108 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4109 | } |
| 4110 | #endif |
| 4111 | |
| 4112 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4113 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4114 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4115 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4116 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4117 | 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] | 4118 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4119 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4120 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4121 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4122 | pid_t pid; |
| 4123 | int result = 0; |
| 4124 | _PyImport_AcquireLock(); |
| 4125 | pid = fork(); |
| 4126 | if (pid == 0) { |
| 4127 | /* child: this clobbers and resets the import lock. */ |
| 4128 | PyOS_AfterFork(); |
| 4129 | } else { |
| 4130 | /* parent: release the import lock. */ |
| 4131 | result = _PyImport_ReleaseLock(); |
| 4132 | } |
| 4133 | if (pid == -1) |
| 4134 | return posix_error(); |
| 4135 | if (result < 0) { |
| 4136 | /* Don't clobber the OSError if the fork failed. */ |
| 4137 | PyErr_SetString(PyExc_RuntimeError, |
| 4138 | "not holding the import lock"); |
| 4139 | return NULL; |
| 4140 | } |
| 4141 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4142 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4143 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4144 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4145 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 4146 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 4147 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4148 | #define DEV_PTY_FILE "/dev/ptc" |
| 4149 | #define HAVE_DEV_PTMX |
| 4150 | #else |
| 4151 | #define DEV_PTY_FILE "/dev/ptmx" |
| 4152 | #endif |
| 4153 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4154 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4155 | #ifdef HAVE_PTY_H |
| 4156 | #include <pty.h> |
| 4157 | #else |
| 4158 | #ifdef HAVE_LIBUTIL_H |
| 4159 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 4160 | #else |
| 4161 | #ifdef HAVE_UTIL_H |
| 4162 | #include <util.h> |
| 4163 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4164 | #endif /* HAVE_LIBUTIL_H */ |
| 4165 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 4166 | #ifdef HAVE_STROPTS_H |
| 4167 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4168 | #endif |
| 4169 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4170 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4171 | #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] | 4172 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4173 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4174 | 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] | 4175 | |
| 4176 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4177 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4178 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4179 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4180 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4181 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4182 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4183 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4184 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4185 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4186 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4187 | #endif |
| 4188 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4189 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4190 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4191 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 4192 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4193 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4194 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 4195 | if (slave_name == NULL) |
| 4196 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4197 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4198 | slave_fd = open(slave_name, O_RDWR); |
| 4199 | if (slave_fd < 0) |
| 4200 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4201 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4202 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 4203 | if (master_fd < 0) |
| 4204 | return posix_error(); |
| 4205 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 4206 | /* change permission of slave */ |
| 4207 | if (grantpt(master_fd) < 0) { |
| 4208 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4209 | return posix_error(); |
| 4210 | } |
| 4211 | /* unlock slave */ |
| 4212 | if (unlockpt(master_fd) < 0) { |
| 4213 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4214 | return posix_error(); |
| 4215 | } |
| 4216 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4217 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 4218 | if (slave_name == NULL) |
| 4219 | return posix_error(); |
| 4220 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 4221 | if (slave_fd < 0) |
| 4222 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4223 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4224 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 4225 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 4226 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4227 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 4228 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4229 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4230 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4231 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4232 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4233 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4234 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4235 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4236 | |
| 4237 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4238 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4239 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4240 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 4241 | 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] | 4242 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4243 | |
| 4244 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4245 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4246 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4247 | int master_fd = -1, result = 0; |
| 4248 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4249 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4250 | _PyImport_AcquireLock(); |
| 4251 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 4252 | if (pid == 0) { |
| 4253 | /* child: this clobbers and resets the import lock. */ |
| 4254 | PyOS_AfterFork(); |
| 4255 | } else { |
| 4256 | /* parent: release the import lock. */ |
| 4257 | result = _PyImport_ReleaseLock(); |
| 4258 | } |
| 4259 | if (pid == -1) |
| 4260 | return posix_error(); |
| 4261 | if (result < 0) { |
| 4262 | /* Don't clobber the OSError if the fork failed. */ |
| 4263 | PyErr_SetString(PyExc_RuntimeError, |
| 4264 | "not holding the import lock"); |
| 4265 | return NULL; |
| 4266 | } |
| 4267 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4268 | } |
| 4269 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4270 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4271 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4272 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4273 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4274 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4275 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4276 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4277 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4278 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4279 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4280 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4281 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4282 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4283 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4284 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4285 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4286 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4287 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4288 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4289 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4290 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4291 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4292 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4293 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4294 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4295 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4296 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4297 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4298 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4299 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4300 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4301 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4302 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4303 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4304 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4305 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4306 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4307 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4308 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4310 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4311 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4312 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4313 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4314 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4315 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4316 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4317 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4320 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4321 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4322 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4323 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4324 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4325 | |
| 4326 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4327 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4328 | { |
| 4329 | PyObject *result = NULL; |
| 4330 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4331 | #ifdef NGROUPS_MAX |
| 4332 | #define MAX_GROUPS NGROUPS_MAX |
| 4333 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4334 | /* 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] | 4335 | #define MAX_GROUPS 64 |
| 4336 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4337 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4338 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4339 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4340 | * This is a helper variable to store the intermediate result when |
| 4341 | * that happens. |
| 4342 | * |
| 4343 | * To keep the code readable the OSX behaviour is unconditional, |
| 4344 | * according to the POSIX spec this should be safe on all unix-y |
| 4345 | * systems. |
| 4346 | */ |
| 4347 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4348 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4349 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4350 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4351 | if (n < 0) { |
| 4352 | if (errno == EINVAL) { |
| 4353 | n = getgroups(0, NULL); |
| 4354 | if (n == -1) { |
| 4355 | return posix_error(); |
| 4356 | } |
| 4357 | if (n == 0) { |
| 4358 | /* Avoid malloc(0) */ |
| 4359 | alt_grouplist = grouplist; |
| 4360 | } else { |
| 4361 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 4362 | if (alt_grouplist == NULL) { |
| 4363 | errno = EINVAL; |
| 4364 | return posix_error(); |
| 4365 | } |
| 4366 | n = getgroups(n, alt_grouplist); |
| 4367 | if (n == -1) { |
| 4368 | PyMem_Free(alt_grouplist); |
| 4369 | return posix_error(); |
| 4370 | } |
| 4371 | } |
| 4372 | } else { |
| 4373 | return posix_error(); |
| 4374 | } |
| 4375 | } |
| 4376 | result = PyList_New(n); |
| 4377 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4378 | int i; |
| 4379 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4380 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4381 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4382 | Py_DECREF(result); |
| 4383 | result = NULL; |
| 4384 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4385 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4386 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4387 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | if (alt_grouplist != grouplist) { |
| 4391 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4392 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4393 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4394 | return result; |
| 4395 | } |
| 4396 | #endif |
| 4397 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4398 | #ifdef HAVE_INITGROUPS |
| 4399 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 4400 | "initgroups(username, gid) -> None\n\n\ |
| 4401 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 4402 | the groups of which the specified username is a member, plus the specified\n\ |
| 4403 | group id."); |
| 4404 | |
| 4405 | static PyObject * |
| 4406 | posix_initgroups(PyObject *self, PyObject *args) |
| 4407 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4408 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4409 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4410 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4411 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4412 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4413 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 4414 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4415 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4416 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4417 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4418 | res = initgroups(username, (gid_t) gid); |
| 4419 | Py_DECREF(oname); |
| 4420 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4421 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4422 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4423 | Py_INCREF(Py_None); |
| 4424 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4425 | } |
| 4426 | #endif |
| 4427 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4428 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4429 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4430 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4431 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4432 | |
| 4433 | static PyObject * |
| 4434 | posix_getpgid(PyObject *self, PyObject *args) |
| 4435 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4436 | pid_t pid, pgid; |
| 4437 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 4438 | return NULL; |
| 4439 | pgid = getpgid(pid); |
| 4440 | if (pgid < 0) |
| 4441 | return posix_error(); |
| 4442 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4443 | } |
| 4444 | #endif /* HAVE_GETPGID */ |
| 4445 | |
| 4446 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4447 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4448 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4449 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4450 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4451 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4452 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4453 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4454 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4455 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4456 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4457 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4458 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4459 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4460 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4461 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4462 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4463 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4464 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4465 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4466 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 4467 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4468 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4469 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4470 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4471 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4472 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4473 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4474 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4475 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4476 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4477 | return posix_error(); |
| 4478 | Py_INCREF(Py_None); |
| 4479 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4482 | #endif /* HAVE_SETPGRP */ |
| 4483 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4484 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4485 | |
| 4486 | #ifdef MS_WINDOWS |
| 4487 | #include <tlhelp32.h> |
| 4488 | |
| 4489 | static PyObject* |
| 4490 | win32_getppid() |
| 4491 | { |
| 4492 | HANDLE snapshot; |
| 4493 | pid_t mypid; |
| 4494 | PyObject* result = NULL; |
| 4495 | BOOL have_record; |
| 4496 | PROCESSENTRY32 pe; |
| 4497 | |
| 4498 | mypid = getpid(); /* This function never fails */ |
| 4499 | |
| 4500 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 4501 | if (snapshot == INVALID_HANDLE_VALUE) |
| 4502 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 4503 | |
| 4504 | pe.dwSize = sizeof(pe); |
| 4505 | have_record = Process32First(snapshot, &pe); |
| 4506 | while (have_record) { |
| 4507 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 4508 | /* We could cache the ulong value in a static variable. */ |
| 4509 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 4510 | break; |
| 4511 | } |
| 4512 | |
| 4513 | have_record = Process32Next(snapshot, &pe); |
| 4514 | } |
| 4515 | |
| 4516 | /* If our loop exits and our pid was not found (result will be NULL) |
| 4517 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 4518 | * error anyway, so let's raise it. */ |
| 4519 | if (!result) |
| 4520 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 4521 | |
| 4522 | CloseHandle(snapshot); |
| 4523 | |
| 4524 | return result; |
| 4525 | } |
| 4526 | #endif /*MS_WINDOWS*/ |
| 4527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4528 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4529 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4530 | Return the parent's process id. If the parent process has already exited,\n\ |
| 4531 | Windows machines will still return its id; others systems will return the id\n\ |
| 4532 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4533 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4534 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4535 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4536 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4537 | #ifdef MS_WINDOWS |
| 4538 | return win32_getppid(); |
| 4539 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4540 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4541 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4542 | } |
| 4543 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4544 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4545 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4546 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4547 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4548 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4549 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4550 | |
| 4551 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4552 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4553 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4554 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4555 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4556 | wchar_t user_name[UNLEN + 1]; |
| 4557 | DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]); |
| 4558 | |
| 4559 | if (GetUserNameW(user_name, &num_chars)) { |
| 4560 | /* num_chars is the number of unicode chars plus null terminator */ |
| 4561 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4562 | } |
| 4563 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4564 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 4565 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4566 | char *name; |
| 4567 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4568 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4569 | errno = 0; |
| 4570 | name = getlogin(); |
| 4571 | if (name == NULL) { |
| 4572 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4573 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4574 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4575 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4576 | } |
| 4577 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4578 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4579 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4580 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4581 | return result; |
| 4582 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4583 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4584 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4585 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4586 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4587 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4588 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4590 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4591 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4592 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4593 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4594 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4595 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4596 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4597 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4598 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4599 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4600 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4601 | Kill a process with a signal."); |
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 * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4604 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4605 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4606 | pid_t pid; |
| 4607 | int sig; |
| 4608 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 4609 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4610 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4611 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4612 | APIRET rc; |
| 4613 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4614 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4615 | |
| 4616 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4617 | APIRET rc; |
| 4618 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4619 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4620 | |
| 4621 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4622 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4623 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4624 | if (kill(pid, sig) == -1) |
| 4625 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4626 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4627 | Py_INCREF(Py_None); |
| 4628 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4629 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4630 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4631 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4632 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4633 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4634 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4635 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4636 | |
| 4637 | static PyObject * |
| 4638 | posix_killpg(PyObject *self, PyObject *args) |
| 4639 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4640 | int sig; |
| 4641 | pid_t pgid; |
| 4642 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4643 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4644 | take the same type. Moreover, pid_t is always at least as wide as |
| 4645 | int (else compilation of this module fails), which is safe. */ |
| 4646 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 4647 | return NULL; |
| 4648 | if (killpg(pgid, sig) == -1) |
| 4649 | return posix_error(); |
| 4650 | Py_INCREF(Py_None); |
| 4651 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4652 | } |
| 4653 | #endif |
| 4654 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4655 | #ifdef MS_WINDOWS |
| 4656 | PyDoc_STRVAR(win32_kill__doc__, |
| 4657 | "kill(pid, sig)\n\n\ |
| 4658 | Kill a process with a signal."); |
| 4659 | |
| 4660 | static PyObject * |
| 4661 | win32_kill(PyObject *self, PyObject *args) |
| 4662 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 4663 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4664 | DWORD pid, sig, err; |
| 4665 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4666 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4667 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 4668 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4669 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4670 | /* Console processes which share a common console can be sent CTRL+C or |
| 4671 | CTRL+BREAK events, provided they handle said events. */ |
| 4672 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 4673 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 4674 | err = GetLastError(); |
| 4675 | PyErr_SetFromWindowsErr(err); |
| 4676 | } |
| 4677 | else |
| 4678 | Py_RETURN_NONE; |
| 4679 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4680 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4681 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 4682 | attempt to open and terminate the process. */ |
| 4683 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 4684 | if (handle == NULL) { |
| 4685 | err = GetLastError(); |
| 4686 | return PyErr_SetFromWindowsErr(err); |
| 4687 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4688 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4689 | if (TerminateProcess(handle, sig) == 0) { |
| 4690 | err = GetLastError(); |
| 4691 | result = PyErr_SetFromWindowsErr(err); |
| 4692 | } else { |
| 4693 | Py_INCREF(Py_None); |
| 4694 | result = Py_None; |
| 4695 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4696 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4697 | CloseHandle(handle); |
| 4698 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4699 | } |
| 4700 | #endif /* MS_WINDOWS */ |
| 4701 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4702 | #ifdef HAVE_PLOCK |
| 4703 | |
| 4704 | #ifdef HAVE_SYS_LOCK_H |
| 4705 | #include <sys/lock.h> |
| 4706 | #endif |
| 4707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4708 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4709 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4710 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4711 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4712 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4713 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4714 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4715 | int op; |
| 4716 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 4717 | return NULL; |
| 4718 | if (plock(op) == -1) |
| 4719 | return posix_error(); |
| 4720 | Py_INCREF(Py_None); |
| 4721 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4722 | } |
| 4723 | #endif |
| 4724 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4725 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4726 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4727 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4728 | Set the current process's user id."); |
| 4729 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4730 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4731 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4732 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4733 | long uid_arg; |
| 4734 | uid_t uid; |
| 4735 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 4736 | return NULL; |
| 4737 | uid = uid_arg; |
| 4738 | if (uid != uid_arg) { |
| 4739 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4740 | return NULL; |
| 4741 | } |
| 4742 | if (setuid(uid) < 0) |
| 4743 | return posix_error(); |
| 4744 | Py_INCREF(Py_None); |
| 4745 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4746 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4747 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4748 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4749 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4750 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4751 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4752 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4753 | Set the current process's effective user id."); |
| 4754 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4755 | static PyObject * |
| 4756 | posix_seteuid (PyObject *self, PyObject *args) |
| 4757 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4758 | long euid_arg; |
| 4759 | uid_t euid; |
| 4760 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 4761 | return NULL; |
| 4762 | euid = euid_arg; |
| 4763 | if (euid != euid_arg) { |
| 4764 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4765 | return NULL; |
| 4766 | } |
| 4767 | if (seteuid(euid) < 0) { |
| 4768 | return posix_error(); |
| 4769 | } else { |
| 4770 | Py_INCREF(Py_None); |
| 4771 | return Py_None; |
| 4772 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4773 | } |
| 4774 | #endif /* HAVE_SETEUID */ |
| 4775 | |
| 4776 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4777 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4778 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4779 | Set the current process's effective group id."); |
| 4780 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4781 | static PyObject * |
| 4782 | posix_setegid (PyObject *self, PyObject *args) |
| 4783 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4784 | long egid_arg; |
| 4785 | gid_t egid; |
| 4786 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 4787 | return NULL; |
| 4788 | egid = egid_arg; |
| 4789 | if (egid != egid_arg) { |
| 4790 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4791 | return NULL; |
| 4792 | } |
| 4793 | if (setegid(egid) < 0) { |
| 4794 | return posix_error(); |
| 4795 | } else { |
| 4796 | Py_INCREF(Py_None); |
| 4797 | return Py_None; |
| 4798 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4799 | } |
| 4800 | #endif /* HAVE_SETEGID */ |
| 4801 | |
| 4802 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4803 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4804 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4805 | Set the current process's real and effective user ids."); |
| 4806 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4807 | static PyObject * |
| 4808 | posix_setreuid (PyObject *self, PyObject *args) |
| 4809 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4810 | long ruid_arg, euid_arg; |
| 4811 | uid_t ruid, euid; |
| 4812 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 4813 | return NULL; |
| 4814 | if (ruid_arg == -1) |
| 4815 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 4816 | else |
| 4817 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 4818 | if (euid_arg == -1) |
| 4819 | euid = (uid_t)-1; |
| 4820 | else |
| 4821 | euid = euid_arg; |
| 4822 | if ((euid_arg != -1 && euid != euid_arg) || |
| 4823 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 4824 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4825 | return NULL; |
| 4826 | } |
| 4827 | if (setreuid(ruid, euid) < 0) { |
| 4828 | return posix_error(); |
| 4829 | } else { |
| 4830 | Py_INCREF(Py_None); |
| 4831 | return Py_None; |
| 4832 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4833 | } |
| 4834 | #endif /* HAVE_SETREUID */ |
| 4835 | |
| 4836 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4837 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4838 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4839 | Set the current process's real and effective group ids."); |
| 4840 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4841 | static PyObject * |
| 4842 | posix_setregid (PyObject *self, PyObject *args) |
| 4843 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4844 | long rgid_arg, egid_arg; |
| 4845 | gid_t rgid, egid; |
| 4846 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 4847 | return NULL; |
| 4848 | if (rgid_arg == -1) |
| 4849 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 4850 | else |
| 4851 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 4852 | if (egid_arg == -1) |
| 4853 | egid = (gid_t)-1; |
| 4854 | else |
| 4855 | egid = egid_arg; |
| 4856 | if ((egid_arg != -1 && egid != egid_arg) || |
| 4857 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 4858 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4859 | return NULL; |
| 4860 | } |
| 4861 | if (setregid(rgid, egid) < 0) { |
| 4862 | return posix_error(); |
| 4863 | } else { |
| 4864 | Py_INCREF(Py_None); |
| 4865 | return Py_None; |
| 4866 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4867 | } |
| 4868 | #endif /* HAVE_SETREGID */ |
| 4869 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4870 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4871 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4872 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4873 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4874 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4875 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4876 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4877 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4878 | long gid_arg; |
| 4879 | gid_t gid; |
| 4880 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 4881 | return NULL; |
| 4882 | gid = gid_arg; |
| 4883 | if (gid != gid_arg) { |
| 4884 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4885 | return NULL; |
| 4886 | } |
| 4887 | if (setgid(gid) < 0) |
| 4888 | return posix_error(); |
| 4889 | Py_INCREF(Py_None); |
| 4890 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4891 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4892 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4893 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4894 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4895 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4896 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4897 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4898 | |
| 4899 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4900 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4901 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4902 | int i, len; |
| 4903 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4904 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4905 | if (!PySequence_Check(groups)) { |
| 4906 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4907 | return NULL; |
| 4908 | } |
| 4909 | len = PySequence_Size(groups); |
| 4910 | if (len > MAX_GROUPS) { |
| 4911 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4912 | return NULL; |
| 4913 | } |
| 4914 | for(i = 0; i < len; i++) { |
| 4915 | PyObject *elem; |
| 4916 | elem = PySequence_GetItem(groups, i); |
| 4917 | if (!elem) |
| 4918 | return NULL; |
| 4919 | if (!PyLong_Check(elem)) { |
| 4920 | PyErr_SetString(PyExc_TypeError, |
| 4921 | "groups must be integers"); |
| 4922 | Py_DECREF(elem); |
| 4923 | return NULL; |
| 4924 | } else { |
| 4925 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4926 | if (PyErr_Occurred()) { |
| 4927 | PyErr_SetString(PyExc_TypeError, |
| 4928 | "group id too big"); |
| 4929 | Py_DECREF(elem); |
| 4930 | return NULL; |
| 4931 | } |
| 4932 | grouplist[i] = x; |
| 4933 | /* read back the value to see if it fitted in gid_t */ |
| 4934 | if (grouplist[i] != x) { |
| 4935 | PyErr_SetString(PyExc_TypeError, |
| 4936 | "group id too big"); |
| 4937 | Py_DECREF(elem); |
| 4938 | return NULL; |
| 4939 | } |
| 4940 | } |
| 4941 | Py_DECREF(elem); |
| 4942 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4943 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4944 | if (setgroups(len, grouplist) < 0) |
| 4945 | return posix_error(); |
| 4946 | Py_INCREF(Py_None); |
| 4947 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4948 | } |
| 4949 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4950 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4951 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4952 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4953 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4954 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4955 | PyObject *result; |
| 4956 | static PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4957 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4958 | if (pid == -1) |
| 4959 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4960 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4961 | if (struct_rusage == NULL) { |
| 4962 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 4963 | if (m == NULL) |
| 4964 | return NULL; |
| 4965 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4966 | Py_DECREF(m); |
| 4967 | if (struct_rusage == NULL) |
| 4968 | return NULL; |
| 4969 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4970 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4971 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4972 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4973 | if (!result) |
| 4974 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4975 | |
| 4976 | #ifndef doubletime |
| 4977 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4978 | #endif |
| 4979 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4980 | PyStructSequence_SET_ITEM(result, 0, |
| 4981 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4982 | PyStructSequence_SET_ITEM(result, 1, |
| 4983 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4984 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4985 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 4986 | SET_INT(result, 2, ru->ru_maxrss); |
| 4987 | SET_INT(result, 3, ru->ru_ixrss); |
| 4988 | SET_INT(result, 4, ru->ru_idrss); |
| 4989 | SET_INT(result, 5, ru->ru_isrss); |
| 4990 | SET_INT(result, 6, ru->ru_minflt); |
| 4991 | SET_INT(result, 7, ru->ru_majflt); |
| 4992 | SET_INT(result, 8, ru->ru_nswap); |
| 4993 | SET_INT(result, 9, ru->ru_inblock); |
| 4994 | SET_INT(result, 10, ru->ru_oublock); |
| 4995 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4996 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4997 | SET_INT(result, 13, ru->ru_nsignals); |
| 4998 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4999 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5000 | #undef SET_INT |
| 5001 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5002 | if (PyErr_Occurred()) { |
| 5003 | Py_DECREF(result); |
| 5004 | return NULL; |
| 5005 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5006 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5007 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5008 | } |
| 5009 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 5010 | |
| 5011 | #ifdef HAVE_WAIT3 |
| 5012 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5013 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5014 | Wait for completion of a child process."); |
| 5015 | |
| 5016 | static PyObject * |
| 5017 | posix_wait3(PyObject *self, PyObject *args) |
| 5018 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5019 | pid_t pid; |
| 5020 | int options; |
| 5021 | struct rusage ru; |
| 5022 | WAIT_TYPE status; |
| 5023 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5024 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5025 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5026 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5027 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5028 | Py_BEGIN_ALLOW_THREADS |
| 5029 | pid = wait3(&status, options, &ru); |
| 5030 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5032 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5033 | } |
| 5034 | #endif /* HAVE_WAIT3 */ |
| 5035 | |
| 5036 | #ifdef HAVE_WAIT4 |
| 5037 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5038 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5039 | Wait for completion of a given child process."); |
| 5040 | |
| 5041 | static PyObject * |
| 5042 | posix_wait4(PyObject *self, PyObject *args) |
| 5043 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5044 | pid_t pid; |
| 5045 | int options; |
| 5046 | struct rusage ru; |
| 5047 | WAIT_TYPE status; |
| 5048 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5049 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5050 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 5051 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5052 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5053 | Py_BEGIN_ALLOW_THREADS |
| 5054 | pid = wait4(pid, &status, options, &ru); |
| 5055 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5056 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5057 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5058 | } |
| 5059 | #endif /* HAVE_WAIT4 */ |
| 5060 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5061 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5062 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5063 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5064 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5065 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5066 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5067 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5068 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5069 | pid_t pid; |
| 5070 | int options; |
| 5071 | WAIT_TYPE status; |
| 5072 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5073 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5074 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 5075 | return NULL; |
| 5076 | Py_BEGIN_ALLOW_THREADS |
| 5077 | pid = waitpid(pid, &status, options); |
| 5078 | Py_END_ALLOW_THREADS |
| 5079 | if (pid == -1) |
| 5080 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5081 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5082 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5085 | #elif defined(HAVE_CWAIT) |
| 5086 | |
| 5087 | /* 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] | 5088 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5089 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5090 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5091 | |
| 5092 | static PyObject * |
| 5093 | posix_waitpid(PyObject *self, PyObject *args) |
| 5094 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5095 | Py_intptr_t pid; |
| 5096 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5098 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 5099 | return NULL; |
| 5100 | Py_BEGIN_ALLOW_THREADS |
| 5101 | pid = _cwait(&status, pid, options); |
| 5102 | Py_END_ALLOW_THREADS |
| 5103 | if (pid == -1) |
| 5104 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5105 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5106 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5107 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5108 | } |
| 5109 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5110 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5111 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5112 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5113 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5114 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5115 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5116 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5117 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5118 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | pid_t pid; |
| 5120 | WAIT_TYPE status; |
| 5121 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5122 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5123 | Py_BEGIN_ALLOW_THREADS |
| 5124 | pid = wait(&status); |
| 5125 | Py_END_ALLOW_THREADS |
| 5126 | if (pid == -1) |
| 5127 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5128 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5129 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5130 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5131 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5132 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5134 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5135 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5136 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5137 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5138 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5139 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5140 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5141 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5142 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5143 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5144 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5145 | return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", |
| 5146 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5147 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5148 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5149 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5150 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5153 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5154 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5155 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5156 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5157 | 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] | 5158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5159 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5160 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5161 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5162 | PyObject* v; |
| 5163 | char buf[MAXPATHLEN]; |
| 5164 | PyObject *opath; |
| 5165 | char *path; |
| 5166 | int n; |
| 5167 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5168 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5169 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 5170 | PyUnicode_FSConverter, &opath)) |
| 5171 | return NULL; |
| 5172 | path = PyBytes_AsString(opath); |
| 5173 | v = PySequence_GetItem(args, 0); |
| 5174 | if (v == NULL) { |
| 5175 | Py_DECREF(opath); |
| 5176 | return NULL; |
| 5177 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5178 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5179 | if (PyUnicode_Check(v)) { |
| 5180 | arg_is_unicode = 1; |
| 5181 | } |
| 5182 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5184 | Py_BEGIN_ALLOW_THREADS |
| 5185 | n = readlink(path, buf, (int) sizeof buf); |
| 5186 | Py_END_ALLOW_THREADS |
| 5187 | if (n < 0) |
| 5188 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5189 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5190 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 5191 | if (arg_is_unicode) |
| 5192 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 5193 | else |
| 5194 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5195 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5196 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5197 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5198 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5199 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5200 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5201 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5202 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5203 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5204 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5205 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5206 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5207 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5208 | } |
| 5209 | #endif /* HAVE_SYMLINK */ |
| 5210 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5211 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 5212 | |
| 5213 | PyDoc_STRVAR(win_readlink__doc__, |
| 5214 | "readlink(path) -> path\n\n\ |
| 5215 | Return a string representing the path to which the symbolic link points."); |
| 5216 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5217 | /* Windows readlink implementation */ |
| 5218 | static PyObject * |
| 5219 | win_readlink(PyObject *self, PyObject *args) |
| 5220 | { |
| 5221 | wchar_t *path; |
| 5222 | DWORD n_bytes_returned; |
| 5223 | DWORD io_result; |
| 5224 | PyObject *result; |
| 5225 | HANDLE reparse_point_handle; |
| 5226 | |
| 5227 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 5228 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 5229 | wchar_t *print_name; |
| 5230 | |
| 5231 | if (!PyArg_ParseTuple(args, |
| 5232 | "u:readlink", |
| 5233 | &path)) |
| 5234 | return NULL; |
| 5235 | |
| 5236 | /* First get a handle to the reparse point */ |
| 5237 | Py_BEGIN_ALLOW_THREADS |
| 5238 | reparse_point_handle = CreateFileW( |
| 5239 | path, |
| 5240 | 0, |
| 5241 | 0, |
| 5242 | 0, |
| 5243 | OPEN_EXISTING, |
| 5244 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 5245 | 0); |
| 5246 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5247 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5248 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 5249 | { |
| 5250 | return win32_error_unicode("readlink", path); |
| 5251 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5252 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5253 | Py_BEGIN_ALLOW_THREADS |
| 5254 | /* New call DeviceIoControl to read the reparse point */ |
| 5255 | io_result = DeviceIoControl( |
| 5256 | reparse_point_handle, |
| 5257 | FSCTL_GET_REPARSE_POINT, |
| 5258 | 0, 0, /* in buffer */ |
| 5259 | target_buffer, sizeof(target_buffer), |
| 5260 | &n_bytes_returned, |
| 5261 | 0 /* we're not using OVERLAPPED_IO */ |
| 5262 | ); |
| 5263 | CloseHandle(reparse_point_handle); |
| 5264 | Py_END_ALLOW_THREADS |
| 5265 | |
| 5266 | if (io_result==0) |
| 5267 | { |
| 5268 | return win32_error_unicode("readlink", path); |
| 5269 | } |
| 5270 | |
| 5271 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 5272 | { |
| 5273 | PyErr_SetString(PyExc_ValueError, |
| 5274 | "not a symbolic link"); |
| 5275 | return NULL; |
| 5276 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 5277 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 5278 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 5279 | |
| 5280 | result = PyUnicode_FromWideChar(print_name, |
| 5281 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5282 | return result; |
| 5283 | } |
| 5284 | |
| 5285 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 5286 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5287 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5288 | |
| 5289 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 5290 | static int has_CreateSymbolicLinkW = 0; |
| 5291 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 5292 | static int |
| 5293 | check_CreateSymbolicLinkW() |
| 5294 | { |
| 5295 | HINSTANCE hKernel32; |
| 5296 | /* only recheck */ |
| 5297 | if (has_CreateSymbolicLinkW) |
| 5298 | return has_CreateSymbolicLinkW; |
| 5299 | hKernel32 = GetModuleHandle("KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 5300 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 5301 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5302 | if (Py_CreateSymbolicLinkW) |
| 5303 | has_CreateSymbolicLinkW = 1; |
| 5304 | return has_CreateSymbolicLinkW; |
| 5305 | } |
| 5306 | |
| 5307 | PyDoc_STRVAR(win_symlink__doc__, |
| 5308 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 5309 | Create a symbolic link pointing to src named dst.\n\ |
| 5310 | target_is_directory is required if the target is to be interpreted as\n\ |
| 5311 | a directory.\n\ |
| 5312 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 5313 | NotImplementedError otherwise."); |
| 5314 | |
| 5315 | static PyObject * |
| 5316 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 5317 | { |
| 5318 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
| 5319 | PyObject *src, *dest; |
| 5320 | int target_is_directory = 0; |
| 5321 | DWORD res; |
| 5322 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5323 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5324 | if (!check_CreateSymbolicLinkW()) |
| 5325 | { |
| 5326 | /* raise NotImplementedError */ |
| 5327 | return PyErr_Format(PyExc_NotImplementedError, |
| 5328 | "CreateSymbolicLinkW not found"); |
| 5329 | } |
| 5330 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink", |
| 5331 | kwlist, &src, &dest, &target_is_directory)) |
| 5332 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 5333 | |
| 5334 | if (win32_can_symlink == 0) |
| 5335 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 5336 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5337 | if (!convert_to_unicode(&src)) { return NULL; } |
| 5338 | if (!convert_to_unicode(&dest)) { |
| 5339 | Py_DECREF(src); |
| 5340 | return NULL; |
| 5341 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5342 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5343 | /* if src is a directory, ensure target_is_directory==1 */ |
| 5344 | if( |
| 5345 | GetFileAttributesExW( |
| 5346 | PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info |
| 5347 | )) |
| 5348 | { |
| 5349 | target_is_directory = target_is_directory || |
| 5350 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 5351 | } |
| 5352 | |
| 5353 | Py_BEGIN_ALLOW_THREADS |
| 5354 | res = Py_CreateSymbolicLinkW( |
| 5355 | PyUnicode_AsUnicode(dest), |
| 5356 | PyUnicode_AsUnicode(src), |
| 5357 | target_is_directory); |
| 5358 | Py_END_ALLOW_THREADS |
| 5359 | Py_DECREF(src); |
| 5360 | Py_DECREF(dest); |
| 5361 | if (!res) |
| 5362 | { |
| 5363 | return win32_error_unicode("symlink", PyUnicode_AsUnicode(src)); |
| 5364 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5365 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5366 | Py_INCREF(Py_None); |
| 5367 | return Py_None; |
| 5368 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5369 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5370 | |
| 5371 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5372 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5373 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5374 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5375 | { |
| 5376 | ULONG value = 0; |
| 5377 | |
| 5378 | Py_BEGIN_ALLOW_THREADS |
| 5379 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5380 | Py_END_ALLOW_THREADS |
| 5381 | |
| 5382 | return value; |
| 5383 | } |
| 5384 | |
| 5385 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5386 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5387 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5388 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5389 | return Py_BuildValue("ddddd", |
| 5390 | (double)0 /* t.tms_utime / HZ */, |
| 5391 | (double)0 /* t.tms_stime / HZ */, |
| 5392 | (double)0 /* t.tms_cutime / HZ */, |
| 5393 | (double)0 /* t.tms_cstime / HZ */, |
| 5394 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5395 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5396 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 5397 | #define NEED_TICKS_PER_SECOND |
| 5398 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5399 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5400 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5401 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5402 | struct tms t; |
| 5403 | clock_t c; |
| 5404 | errno = 0; |
| 5405 | c = times(&t); |
| 5406 | if (c == (clock_t) -1) |
| 5407 | return posix_error(); |
| 5408 | return Py_BuildValue("ddddd", |
| 5409 | (double)t.tms_utime / ticks_per_second, |
| 5410 | (double)t.tms_stime / ticks_per_second, |
| 5411 | (double)t.tms_cutime / ticks_per_second, |
| 5412 | (double)t.tms_cstime / ticks_per_second, |
| 5413 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5414 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5415 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5416 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5417 | |
| 5418 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5419 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5420 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5421 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5422 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5423 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5424 | FILETIME create, exit, kernel, user; |
| 5425 | HANDLE hProc; |
| 5426 | hProc = GetCurrentProcess(); |
| 5427 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5428 | /* The fields of a FILETIME structure are the hi and lo part |
| 5429 | of a 64-bit value expressed in 100 nanosecond units. |
| 5430 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5431 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5432 | */ |
| 5433 | return Py_BuildValue( |
| 5434 | "ddddd", |
| 5435 | (double)(user.dwHighDateTime*429.4967296 + |
| 5436 | user.dwLowDateTime*1e-7), |
| 5437 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5438 | kernel.dwLowDateTime*1e-7), |
| 5439 | (double)0, |
| 5440 | (double)0, |
| 5441 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5442 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5443 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5444 | |
| 5445 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5446 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5447 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5448 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5449 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5450 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5451 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5452 | #ifdef HAVE_GETSID |
| 5453 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5454 | "getsid(pid) -> sid\n\n\ |
| 5455 | Call the system call getsid()."); |
| 5456 | |
| 5457 | static PyObject * |
| 5458 | posix_getsid(PyObject *self, PyObject *args) |
| 5459 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5460 | pid_t pid; |
| 5461 | int sid; |
| 5462 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 5463 | return NULL; |
| 5464 | sid = getsid(pid); |
| 5465 | if (sid < 0) |
| 5466 | return posix_error(); |
| 5467 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5468 | } |
| 5469 | #endif /* HAVE_GETSID */ |
| 5470 | |
| 5471 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5472 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5473 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5474 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5475 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5476 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5477 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5478 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5479 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5480 | if (setsid() < 0) |
| 5481 | return posix_error(); |
| 5482 | Py_INCREF(Py_None); |
| 5483 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5484 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5485 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5486 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5487 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5488 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5489 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5490 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5491 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5492 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5493 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5494 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5495 | pid_t pid; |
| 5496 | int pgrp; |
| 5497 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 5498 | return NULL; |
| 5499 | if (setpgid(pid, pgrp) < 0) |
| 5500 | return posix_error(); |
| 5501 | Py_INCREF(Py_None); |
| 5502 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5503 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5504 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5505 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5506 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5507 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5508 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5509 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5510 | 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] | 5511 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5512 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5513 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5514 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5515 | int fd; |
| 5516 | pid_t pgid; |
| 5517 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 5518 | return NULL; |
| 5519 | pgid = tcgetpgrp(fd); |
| 5520 | if (pgid < 0) |
| 5521 | return posix_error(); |
| 5522 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5523 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5524 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5525 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5526 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5527 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5528 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5529 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5530 | 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] | 5531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5532 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5533 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5534 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5535 | int fd; |
| 5536 | pid_t pgid; |
| 5537 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 5538 | return NULL; |
| 5539 | if (tcsetpgrp(fd, pgid) < 0) |
| 5540 | return posix_error(); |
| 5541 | Py_INCREF(Py_None); |
| 5542 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5543 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5544 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5545 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5546 | /* Functions acting on file descriptors */ |
| 5547 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5548 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5549 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5550 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5551 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5552 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5553 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5554 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5555 | PyObject *ofile; |
| 5556 | char *file; |
| 5557 | int flag; |
| 5558 | int mode = 0777; |
| 5559 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5560 | |
| 5561 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5562 | PyUnicodeObject *po; |
| 5563 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5564 | Py_BEGIN_ALLOW_THREADS |
| 5565 | /* PyUnicode_AS_UNICODE OK without thread |
| 5566 | lock as it is a simple dereference. */ |
| 5567 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5568 | Py_END_ALLOW_THREADS |
| 5569 | if (fd < 0) |
| 5570 | return posix_error(); |
| 5571 | return PyLong_FromLong((long)fd); |
| 5572 | } |
| 5573 | /* Drop the argument parsing error as narrow strings |
| 5574 | are also valid. */ |
| 5575 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5576 | #endif |
| 5577 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5578 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 5579 | PyUnicode_FSConverter, &ofile, |
| 5580 | &flag, &mode)) |
| 5581 | return NULL; |
| 5582 | file = PyBytes_AsString(ofile); |
| 5583 | Py_BEGIN_ALLOW_THREADS |
| 5584 | fd = open(file, flag, mode); |
| 5585 | Py_END_ALLOW_THREADS |
| 5586 | if (fd < 0) |
| 5587 | return posix_error_with_allocated_filename(ofile); |
| 5588 | Py_DECREF(ofile); |
| 5589 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5593 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5594 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5595 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5596 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5597 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5598 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5599 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5600 | int fd, res; |
| 5601 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 5602 | return NULL; |
| 5603 | if (!_PyVerify_fd(fd)) |
| 5604 | return posix_error(); |
| 5605 | Py_BEGIN_ALLOW_THREADS |
| 5606 | res = close(fd); |
| 5607 | Py_END_ALLOW_THREADS |
| 5608 | if (res < 0) |
| 5609 | return posix_error(); |
| 5610 | Py_INCREF(Py_None); |
| 5611 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5614 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5615 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5616 | "closerange(fd_low, fd_high)\n\n\ |
| 5617 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 5618 | |
| 5619 | static PyObject * |
| 5620 | posix_closerange(PyObject *self, PyObject *args) |
| 5621 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5622 | int fd_from, fd_to, i; |
| 5623 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 5624 | return NULL; |
| 5625 | Py_BEGIN_ALLOW_THREADS |
| 5626 | for (i = fd_from; i < fd_to; i++) |
| 5627 | if (_PyVerify_fd(i)) |
| 5628 | close(i); |
| 5629 | Py_END_ALLOW_THREADS |
| 5630 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5634 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5635 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5636 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5637 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5638 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5639 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5640 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5641 | int fd; |
| 5642 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 5643 | return NULL; |
| 5644 | if (!_PyVerify_fd(fd)) |
| 5645 | return posix_error(); |
| 5646 | Py_BEGIN_ALLOW_THREADS |
| 5647 | fd = dup(fd); |
| 5648 | Py_END_ALLOW_THREADS |
| 5649 | if (fd < 0) |
| 5650 | return posix_error(); |
| 5651 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5655 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5656 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5657 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5658 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5659 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5660 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5661 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5662 | int fd, fd2, res; |
| 5663 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 5664 | return NULL; |
| 5665 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 5666 | return posix_error(); |
| 5667 | Py_BEGIN_ALLOW_THREADS |
| 5668 | res = dup2(fd, fd2); |
| 5669 | Py_END_ALLOW_THREADS |
| 5670 | if (res < 0) |
| 5671 | return posix_error(); |
| 5672 | Py_INCREF(Py_None); |
| 5673 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5674 | } |
| 5675 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5676 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5677 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5678 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5679 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5680 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5681 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5682 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5683 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5684 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5685 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5686 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5687 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5688 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5689 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5690 | PyObject *posobj; |
| 5691 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
| 5692 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5693 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5694 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5695 | switch (how) { |
| 5696 | case 0: how = SEEK_SET; break; |
| 5697 | case 1: how = SEEK_CUR; break; |
| 5698 | case 2: how = SEEK_END; break; |
| 5699 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5700 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5701 | |
| 5702 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5703 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5704 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5705 | pos = PyLong_Check(posobj) ? |
| 5706 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5707 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5708 | if (PyErr_Occurred()) |
| 5709 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5710 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5711 | if (!_PyVerify_fd(fd)) |
| 5712 | return posix_error(); |
| 5713 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5714 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5715 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5716 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5717 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5718 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5719 | Py_END_ALLOW_THREADS |
| 5720 | if (res < 0) |
| 5721 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5722 | |
| 5723 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5724 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5725 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5726 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5727 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5731 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5732 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5733 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5734 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5735 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5736 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5737 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5738 | int fd, size; |
| 5739 | Py_ssize_t n; |
| 5740 | PyObject *buffer; |
| 5741 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 5742 | return NULL; |
| 5743 | if (size < 0) { |
| 5744 | errno = EINVAL; |
| 5745 | return posix_error(); |
| 5746 | } |
| 5747 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 5748 | if (buffer == NULL) |
| 5749 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5750 | if (!_PyVerify_fd(fd)) { |
| 5751 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5752 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5753 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5754 | Py_BEGIN_ALLOW_THREADS |
| 5755 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 5756 | Py_END_ALLOW_THREADS |
| 5757 | if (n < 0) { |
| 5758 | Py_DECREF(buffer); |
| 5759 | return posix_error(); |
| 5760 | } |
| 5761 | if (n != size) |
| 5762 | _PyBytes_Resize(&buffer, n); |
| 5763 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5766 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5767 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5768 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5769 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5770 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5771 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5772 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5773 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5774 | Py_buffer pbuf; |
| 5775 | int fd; |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 5776 | Py_ssize_t size, len; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5777 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5778 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 5779 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5780 | if (!_PyVerify_fd(fd)) { |
| 5781 | PyBuffer_Release(&pbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5782 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5783 | } |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 5784 | len = pbuf.len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5785 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 5786 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 5787 | if (len > INT_MAX) |
| 5788 | len = INT_MAX; |
| 5789 | size = write(fd, pbuf.buf, (int)len); |
| 5790 | #else |
Victor Stinner | 7234479 | 2011-01-11 00:04:12 +0000 | [diff] [blame] | 5791 | size = write(fd, pbuf.buf, len); |
Victor Stinner | e6edec2 | 2011-01-04 00:29:35 +0000 | [diff] [blame] | 5792 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5793 | Py_END_ALLOW_THREADS |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 5794 | PyBuffer_Release(&pbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5795 | if (size < 0) |
| 5796 | return posix_error(); |
| 5797 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5798 | } |
| 5799 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5800 | #ifdef HAVE_SENDFILE |
| 5801 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 5802 | static int |
| 5803 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 5804 | { |
| 5805 | int i, j; |
| 5806 | *iov = PyMem_New(struct iovec, cnt); |
| 5807 | if (*iov == NULL) { |
| 5808 | PyErr_NoMemory(); |
| 5809 | return 0; |
| 5810 | } |
| 5811 | *buf = PyMem_New(Py_buffer, cnt); |
| 5812 | if (*buf == NULL) { |
| 5813 | PyMem_Del(*iov); |
| 5814 | PyErr_NoMemory(); |
| 5815 | return 0; |
| 5816 | } |
| 5817 | |
| 5818 | for (i = 0; i < cnt; i++) { |
| 5819 | if (PyObject_GetBuffer(PySequence_GetItem(seq, i), &(*buf)[i], |
| 5820 | type) == -1) { |
| 5821 | PyMem_Del(*iov); |
| 5822 | for (j = 0; j < i; j++) { |
| 5823 | PyBuffer_Release(&(*buf)[j]); |
| 5824 | } |
| 5825 | PyMem_Del(*buf); |
| 5826 | return 0; |
| 5827 | } |
| 5828 | (*iov)[i].iov_base = (*buf)[i].buf; |
| 5829 | (*iov)[i].iov_len = (*buf)[i].len; |
| 5830 | } |
| 5831 | return 1; |
| 5832 | } |
| 5833 | |
| 5834 | static void |
| 5835 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 5836 | { |
| 5837 | int i; |
| 5838 | PyMem_Del(iov); |
| 5839 | for (i = 0; i < cnt; i++) { |
| 5840 | PyBuffer_Release(&buf[i]); |
| 5841 | } |
| 5842 | PyMem_Del(buf); |
| 5843 | } |
| 5844 | #endif |
| 5845 | |
| 5846 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 5847 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 5848 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 5849 | -> byteswritten\n\ |
| 5850 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 5851 | |
| 5852 | static PyObject * |
| 5853 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 5854 | { |
| 5855 | int in, out; |
| 5856 | Py_ssize_t ret; |
| 5857 | off_t offset; |
| 5858 | |
| 5859 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 5860 | #ifndef __APPLE__ |
| 5861 | Py_ssize_t len; |
| 5862 | #endif |
| 5863 | PyObject *headers = NULL, *trailers = NULL; |
| 5864 | Py_buffer *hbuf, *tbuf; |
| 5865 | off_t sbytes; |
| 5866 | struct sf_hdtr sf; |
| 5867 | int flags = 0; |
| 5868 | sf.headers = NULL; |
| 5869 | sf.trailers = NULL; |
| 5870 | static char *keywords[] = {"out", "in", |
| 5871 | "offset", "count", |
| 5872 | "headers", "trailers", "flags", NULL}; |
| 5873 | |
| 5874 | #ifdef __APPLE__ |
| 5875 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 5876 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5877 | #else |
| 5878 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 5879 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5880 | #endif |
| 5881 | &headers, &trailers, &flags)) |
| 5882 | return NULL; |
| 5883 | if (headers != NULL) { |
| 5884 | if (!PySequence_Check(headers)) { |
| 5885 | PyErr_SetString(PyExc_TypeError, |
| 5886 | "sendfile() headers must be a sequence or None"); |
| 5887 | return NULL; |
| 5888 | } else { |
| 5889 | sf.hdr_cnt = PySequence_Size(headers); |
| 5890 | if (sf.hdr_cnt > 0 && !iov_setup(&(sf.headers), &hbuf, |
| 5891 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) |
| 5892 | return NULL; |
| 5893 | } |
| 5894 | } |
| 5895 | if (trailers != NULL) { |
| 5896 | if (!PySequence_Check(trailers)) { |
| 5897 | PyErr_SetString(PyExc_TypeError, |
| 5898 | "sendfile() trailers must be a sequence or None"); |
| 5899 | return NULL; |
| 5900 | } else { |
| 5901 | sf.trl_cnt = PySequence_Size(trailers); |
| 5902 | if (sf.trl_cnt > 0 && !iov_setup(&(sf.trailers), &tbuf, |
| 5903 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) |
| 5904 | return NULL; |
| 5905 | } |
| 5906 | } |
| 5907 | |
| 5908 | Py_BEGIN_ALLOW_THREADS |
| 5909 | #ifdef __APPLE__ |
| 5910 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 5911 | #else |
| 5912 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 5913 | #endif |
| 5914 | Py_END_ALLOW_THREADS |
| 5915 | |
| 5916 | if (sf.headers != NULL) |
| 5917 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 5918 | if (sf.trailers != NULL) |
| 5919 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 5920 | |
| 5921 | if (ret < 0) { |
| 5922 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 5923 | if (sbytes != 0) { |
| 5924 | // some data has been sent |
| 5925 | goto done; |
| 5926 | } |
| 5927 | else { |
| 5928 | // no data has been sent; upper application is supposed |
| 5929 | // to retry on EAGAIN or EBUSY |
| 5930 | return posix_error(); |
| 5931 | } |
| 5932 | } |
| 5933 | return posix_error(); |
| 5934 | } |
| 5935 | goto done; |
| 5936 | |
| 5937 | done: |
| 5938 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5939 | return Py_BuildValue("l", sbytes); |
| 5940 | #else |
| 5941 | return Py_BuildValue("L", sbytes); |
| 5942 | #endif |
| 5943 | |
| 5944 | #else |
| 5945 | Py_ssize_t count; |
| 5946 | PyObject *offobj; |
| 5947 | static char *keywords[] = {"out", "in", |
| 5948 | "offset", "count", NULL}; |
| 5949 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 5950 | keywords, &out, &in, &offobj, &count)) |
| 5951 | return NULL; |
| 5952 | #ifdef linux |
| 5953 | if (offobj == Py_None) { |
| 5954 | Py_BEGIN_ALLOW_THREADS |
| 5955 | ret = sendfile(out, in, NULL, count); |
| 5956 | Py_END_ALLOW_THREADS |
| 5957 | if (ret < 0) |
| 5958 | return posix_error(); |
| 5959 | Py_INCREF(Py_None); |
| 5960 | return Py_BuildValue("nO", ret, Py_None); |
| 5961 | } |
| 5962 | #endif |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 5963 | _parse_off_t(offobj, &offset); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5964 | Py_BEGIN_ALLOW_THREADS |
| 5965 | ret = sendfile(out, in, &offset, count); |
| 5966 | Py_END_ALLOW_THREADS |
| 5967 | if (ret < 0) |
| 5968 | return posix_error(); |
| 5969 | return Py_BuildValue("n", ret); |
| 5970 | #endif |
| 5971 | } |
| 5972 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5973 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5974 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5975 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5976 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5977 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5978 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5979 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5980 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5981 | int fd; |
| 5982 | STRUCT_STAT st; |
| 5983 | int res; |
| 5984 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 5985 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5986 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5987 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5988 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5989 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5990 | if (!_PyVerify_fd(fd)) |
| 5991 | return posix_error(); |
| 5992 | Py_BEGIN_ALLOW_THREADS |
| 5993 | res = FSTAT(fd, &st); |
| 5994 | Py_END_ALLOW_THREADS |
| 5995 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5996 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5997 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5998 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5999 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6000 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6001 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6002 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6003 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6004 | } |
| 6005 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6006 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6007 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6008 | 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] | 6009 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6010 | |
| 6011 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 6012 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6013 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6014 | int fd; |
| 6015 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 6016 | return NULL; |
| 6017 | if (!_PyVerify_fd(fd)) |
| 6018 | return PyBool_FromLong(0); |
| 6019 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6020 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6021 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6022 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6023 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6024 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6025 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6026 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6027 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6028 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6029 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6030 | #if defined(PYOS_OS2) |
| 6031 | HFILE read, write; |
| 6032 | APIRET rc; |
| 6033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6034 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6035 | rc = DosCreatePipe( &read, &write, 4096); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6036 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6037 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6038 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6039 | |
| 6040 | return Py_BuildValue("(ii)", read, write); |
| 6041 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6042 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6043 | int fds[2]; |
| 6044 | int res; |
| 6045 | Py_BEGIN_ALLOW_THREADS |
| 6046 | res = pipe(fds); |
| 6047 | Py_END_ALLOW_THREADS |
| 6048 | if (res != 0) |
| 6049 | return posix_error(); |
| 6050 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6051 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6052 | HANDLE read, write; |
| 6053 | int read_fd, write_fd; |
| 6054 | BOOL ok; |
| 6055 | Py_BEGIN_ALLOW_THREADS |
| 6056 | ok = CreatePipe(&read, &write, NULL, 0); |
| 6057 | Py_END_ALLOW_THREADS |
| 6058 | if (!ok) |
| 6059 | return win32_error("CreatePipe", NULL); |
| 6060 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 6061 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 6062 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6063 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6064 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6065 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6066 | #endif /* HAVE_PIPE */ |
| 6067 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6068 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6069 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6070 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6071 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6072 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6073 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6074 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6075 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6076 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6077 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6078 | char *filename; |
| 6079 | int mode = 0666; |
| 6080 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6081 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 6082 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6083 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6084 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6085 | Py_BEGIN_ALLOW_THREADS |
| 6086 | res = mkfifo(filename, mode); |
| 6087 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6088 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6089 | if (res < 0) |
| 6090 | return posix_error(); |
| 6091 | Py_INCREF(Py_None); |
| 6092 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6093 | } |
| 6094 | #endif |
| 6095 | |
| 6096 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6097 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6098 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6099 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6100 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 6101 | named filename. mode specifies both the permissions to use and the\n\ |
| 6102 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 6103 | 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] | 6104 | device defines the newly created device special file (probably using\n\ |
| 6105 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6106 | |
| 6107 | |
| 6108 | static PyObject * |
| 6109 | posix_mknod(PyObject *self, PyObject *args) |
| 6110 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6111 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6112 | char *filename; |
| 6113 | int mode = 0600; |
| 6114 | int device = 0; |
| 6115 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6116 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 6117 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6118 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6119 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6120 | Py_BEGIN_ALLOW_THREADS |
| 6121 | res = mknod(filename, mode, device); |
| 6122 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6123 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6124 | if (res < 0) |
| 6125 | return posix_error(); |
| 6126 | Py_INCREF(Py_None); |
| 6127 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6128 | } |
| 6129 | #endif |
| 6130 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6131 | #ifdef HAVE_DEVICE_MACROS |
| 6132 | PyDoc_STRVAR(posix_major__doc__, |
| 6133 | "major(device) -> major number\n\ |
| 6134 | Extracts a device major number from a raw device number."); |
| 6135 | |
| 6136 | static PyObject * |
| 6137 | posix_major(PyObject *self, PyObject *args) |
| 6138 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6139 | int device; |
| 6140 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6141 | return NULL; |
| 6142 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
| 6145 | PyDoc_STRVAR(posix_minor__doc__, |
| 6146 | "minor(device) -> minor number\n\ |
| 6147 | Extracts a device minor number from a raw device number."); |
| 6148 | |
| 6149 | static PyObject * |
| 6150 | posix_minor(PyObject *self, PyObject *args) |
| 6151 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6152 | int device; |
| 6153 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6154 | return NULL; |
| 6155 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
| 6158 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6159 | "makedev(major, minor) -> device number\n\ |
| 6160 | Composes a raw device number from the major and minor device numbers."); |
| 6161 | |
| 6162 | static PyObject * |
| 6163 | posix_makedev(PyObject *self, PyObject *args) |
| 6164 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6165 | int major, minor; |
| 6166 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 6167 | return NULL; |
| 6168 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6169 | } |
| 6170 | #endif /* device macros */ |
| 6171 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6172 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6173 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6174 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6175 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6176 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6177 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6178 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6179 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6180 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6181 | int fd; |
| 6182 | off_t length; |
| 6183 | int res; |
| 6184 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6185 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6186 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
| 6187 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6188 | |
| 6189 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6190 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6191 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6192 | length = PyLong_Check(lenobj) ? |
| 6193 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6194 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6195 | if (PyErr_Occurred()) |
| 6196 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6197 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6198 | Py_BEGIN_ALLOW_THREADS |
| 6199 | res = ftruncate(fd, length); |
| 6200 | Py_END_ALLOW_THREADS |
| 6201 | if (res < 0) |
| 6202 | return posix_error(); |
| 6203 | Py_INCREF(Py_None); |
| 6204 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6205 | } |
| 6206 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6207 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6208 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6209 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6210 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6211 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6212 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6213 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6214 | * get re-set with another call for the same key. */ |
| 6215 | static PyObject *posix_putenv_garbage; |
| 6216 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6217 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6218 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6219 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6220 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6221 | wchar_t *s1, *s2; |
| 6222 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6223 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6224 | PyObject *os1, *os2; |
| 6225 | char *s1, *s2; |
| 6226 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6227 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6228 | PyObject *newstr = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6229 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6230 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6231 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6232 | if (!PyArg_ParseTuple(args, |
| 6233 | "uu:putenv", |
| 6234 | &s1, &s2)) |
| 6235 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6236 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6237 | if (!PyArg_ParseTuple(args, |
| 6238 | "O&O&:putenv", |
| 6239 | PyUnicode_FSConverter, &os1, |
| 6240 | PyUnicode_FSConverter, &os2)) |
| 6241 | return NULL; |
| 6242 | s1 = PyBytes_AsString(os1); |
| 6243 | s2 = PyBytes_AsString(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6244 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6245 | |
| 6246 | #if defined(PYOS_OS2) |
| 6247 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6248 | APIRET rc; |
| 6249 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6250 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6251 | if (rc != NO_ERROR) { |
| 6252 | os2_error(rc); |
| 6253 | goto error; |
| 6254 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6255 | |
| 6256 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6257 | APIRET rc; |
| 6258 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6259 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6260 | if (rc != NO_ERROR) { |
| 6261 | os2_error(rc); |
| 6262 | goto error; |
| 6263 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6264 | } else { |
| 6265 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6266 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 6267 | /* len includes space for a trailing \0; the size arg to |
| 6268 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6269 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6270 | len = wcslen(s1) + wcslen(s2) + 2; |
| 6271 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6272 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6273 | len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6274 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6275 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6276 | if (newstr == NULL) { |
| 6277 | PyErr_NoMemory(); |
| 6278 | goto error; |
| 6279 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6280 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6281 | newenv = PyUnicode_AsUnicode(newstr); |
| 6282 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 6283 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6284 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6285 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6286 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6287 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6288 | newenv = PyBytes_AS_STRING(newstr); |
| 6289 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6290 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6291 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6292 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6293 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6294 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6295 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6296 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6297 | * this will cause previous value to be collected. This has to |
| 6298 | * happen after the real putenv() call because the old value |
| 6299 | * was still accessible until then. */ |
| 6300 | if (PyDict_SetItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6301 | #ifdef MS_WINDOWS |
| 6302 | PyTuple_GET_ITEM(args, 0), |
| 6303 | #else |
| 6304 | os1, |
| 6305 | #endif |
| 6306 | newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6307 | /* really not much we can do; just leak */ |
| 6308 | PyErr_Clear(); |
| 6309 | } |
| 6310 | else { |
| 6311 | Py_DECREF(newstr); |
| 6312 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6313 | |
| 6314 | #if defined(PYOS_OS2) |
| 6315 | } |
| 6316 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6317 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6318 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6319 | Py_DECREF(os1); |
| 6320 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6321 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6322 | Py_RETURN_NONE; |
| 6323 | |
| 6324 | error: |
| 6325 | #ifndef MS_WINDOWS |
| 6326 | Py_DECREF(os1); |
| 6327 | Py_DECREF(os2); |
| 6328 | #endif |
| 6329 | Py_XDECREF(newstr); |
| 6330 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6331 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6332 | #endif /* putenv */ |
| 6333 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6334 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6335 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6336 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6337 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6338 | |
| 6339 | static PyObject * |
| 6340 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6341 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6342 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6343 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6344 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6345 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6346 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6347 | #else |
| 6348 | PyObject *os1; |
| 6349 | char *s1; |
| 6350 | |
| 6351 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
| 6352 | PyUnicode_FSConverter, &os1)) |
| 6353 | return NULL; |
| 6354 | s1 = PyBytes_AsString(os1); |
| 6355 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6357 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6359 | /* Remove the key from posix_putenv_garbage; |
| 6360 | * this will cause it to be collected. This has to |
| 6361 | * happen after the real unsetenv() call because the |
| 6362 | * old value was still accessible until then. |
| 6363 | */ |
| 6364 | if (PyDict_DelItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6365 | #ifdef MS_WINDOWS |
| 6366 | PyTuple_GET_ITEM(args, 0) |
| 6367 | #else |
| 6368 | os1 |
| 6369 | #endif |
| 6370 | )) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6371 | /* really not much we can do; just leak */ |
| 6372 | PyErr_Clear(); |
| 6373 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6374 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6375 | #ifndef MS_WINDOWS |
| 6376 | Py_DECREF(os1); |
| 6377 | #endif |
| 6378 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6379 | } |
| 6380 | #endif /* unsetenv */ |
| 6381 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6382 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6383 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6384 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6385 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6386 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6387 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6388 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6389 | int code; |
| 6390 | char *message; |
| 6391 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 6392 | return NULL; |
| 6393 | message = strerror(code); |
| 6394 | if (message == NULL) { |
| 6395 | PyErr_SetString(PyExc_ValueError, |
| 6396 | "strerror() argument out of range"); |
| 6397 | return NULL; |
| 6398 | } |
| 6399 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6400 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6401 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6402 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6403 | #ifdef HAVE_SYS_WAIT_H |
| 6404 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6405 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6406 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6407 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6408 | 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] | 6409 | |
| 6410 | static PyObject * |
| 6411 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6412 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6413 | WAIT_TYPE status; |
| 6414 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6415 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6416 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 6417 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6419 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6420 | } |
| 6421 | #endif /* WCOREDUMP */ |
| 6422 | |
| 6423 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6424 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6425 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6426 | 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] | 6427 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6428 | |
| 6429 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6430 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6431 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6432 | WAIT_TYPE status; |
| 6433 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6434 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6435 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 6436 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6437 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6438 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6439 | } |
| 6440 | #endif /* WIFCONTINUED */ |
| 6441 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6442 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6443 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6444 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6445 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6446 | |
| 6447 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6448 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6449 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6450 | WAIT_TYPE status; |
| 6451 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6452 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6453 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 6454 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6455 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6456 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6457 | } |
| 6458 | #endif /* WIFSTOPPED */ |
| 6459 | |
| 6460 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6461 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6462 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6463 | 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] | 6464 | |
| 6465 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6466 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6467 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6468 | WAIT_TYPE status; |
| 6469 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6470 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6471 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 6472 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6473 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6474 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6475 | } |
| 6476 | #endif /* WIFSIGNALED */ |
| 6477 | |
| 6478 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6479 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6480 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6481 | 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] | 6482 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6483 | |
| 6484 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6485 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6486 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6487 | WAIT_TYPE status; |
| 6488 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6489 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6490 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 6491 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6492 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6493 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6494 | } |
| 6495 | #endif /* WIFEXITED */ |
| 6496 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6497 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6498 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6499 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6500 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6501 | |
| 6502 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6503 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6504 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6505 | WAIT_TYPE status; |
| 6506 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6508 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 6509 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6510 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6511 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6512 | } |
| 6513 | #endif /* WEXITSTATUS */ |
| 6514 | |
| 6515 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6516 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6517 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6518 | 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] | 6519 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6520 | |
| 6521 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6522 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6523 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6524 | WAIT_TYPE status; |
| 6525 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6526 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6527 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 6528 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6529 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6530 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6531 | } |
| 6532 | #endif /* WTERMSIG */ |
| 6533 | |
| 6534 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6535 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6536 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6537 | Return the signal that stopped the process that provided\n\ |
| 6538 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6539 | |
| 6540 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6541 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6542 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6543 | WAIT_TYPE status; |
| 6544 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6545 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6546 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 6547 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6548 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6549 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6550 | } |
| 6551 | #endif /* WSTOPSIG */ |
| 6552 | |
| 6553 | #endif /* HAVE_SYS_WAIT_H */ |
| 6554 | |
| 6555 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6556 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6557 | #ifdef _SCO_DS |
| 6558 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6559 | needed definitions in sys/statvfs.h */ |
| 6560 | #define _SVID3 |
| 6561 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6562 | #include <sys/statvfs.h> |
| 6563 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6564 | static PyObject* |
| 6565 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6566 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6567 | if (v == NULL) |
| 6568 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6569 | |
| 6570 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6571 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 6572 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 6573 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 6574 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 6575 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 6576 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 6577 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 6578 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 6579 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 6580 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6581 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6582 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 6583 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 6584 | PyStructSequence_SET_ITEM(v, 2, |
| 6585 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 6586 | PyStructSequence_SET_ITEM(v, 3, |
| 6587 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 6588 | PyStructSequence_SET_ITEM(v, 4, |
| 6589 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 6590 | PyStructSequence_SET_ITEM(v, 5, |
| 6591 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 6592 | PyStructSequence_SET_ITEM(v, 6, |
| 6593 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 6594 | PyStructSequence_SET_ITEM(v, 7, |
| 6595 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 6596 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 6597 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6598 | #endif |
| 6599 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6600 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6601 | } |
| 6602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6603 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6604 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6605 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6606 | |
| 6607 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6608 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6609 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6610 | int fd, res; |
| 6611 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6612 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6613 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 6614 | return NULL; |
| 6615 | Py_BEGIN_ALLOW_THREADS |
| 6616 | res = fstatvfs(fd, &st); |
| 6617 | Py_END_ALLOW_THREADS |
| 6618 | if (res != 0) |
| 6619 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6620 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6621 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6622 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6623 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6624 | |
| 6625 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6626 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6627 | #include <sys/statvfs.h> |
| 6628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6629 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6630 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6631 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6632 | |
| 6633 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6634 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6635 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6636 | char *path; |
| 6637 | int res; |
| 6638 | struct statvfs st; |
| 6639 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
| 6640 | return NULL; |
| 6641 | Py_BEGIN_ALLOW_THREADS |
| 6642 | res = statvfs(path, &st); |
| 6643 | Py_END_ALLOW_THREADS |
| 6644 | if (res != 0) |
| 6645 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6646 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6647 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6648 | } |
| 6649 | #endif /* HAVE_STATVFS */ |
| 6650 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6651 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6652 | * It maps strings representing configuration variable names to |
| 6653 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6654 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6655 | * rarely-used constants. There are three separate tables that use |
| 6656 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6657 | * |
| 6658 | * This code is always included, even if none of the interfaces that |
| 6659 | * need it are included. The #if hackery needed to avoid it would be |
| 6660 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6661 | */ |
| 6662 | struct constdef { |
| 6663 | char *name; |
| 6664 | long value; |
| 6665 | }; |
| 6666 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6667 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6668 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 6669 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6670 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6671 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6672 | *valuep = PyLong_AS_LONG(arg); |
| 6673 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6674 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 6675 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6676 | /* look up the value in the table using a binary search */ |
| 6677 | size_t lo = 0; |
| 6678 | size_t mid; |
| 6679 | size_t hi = tablesize; |
| 6680 | int cmp; |
| 6681 | const char *confname; |
| 6682 | if (!PyUnicode_Check(arg)) { |
| 6683 | PyErr_SetString(PyExc_TypeError, |
| 6684 | "configuration names must be strings or integers"); |
| 6685 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6686 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6687 | confname = _PyUnicode_AsString(arg); |
| 6688 | if (confname == NULL) |
| 6689 | return 0; |
| 6690 | while (lo < hi) { |
| 6691 | mid = (lo + hi) / 2; |
| 6692 | cmp = strcmp(confname, table[mid].name); |
| 6693 | if (cmp < 0) |
| 6694 | hi = mid; |
| 6695 | else if (cmp > 0) |
| 6696 | lo = mid + 1; |
| 6697 | else { |
| 6698 | *valuep = table[mid].value; |
| 6699 | return 1; |
| 6700 | } |
| 6701 | } |
| 6702 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6703 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6704 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6705 | } |
| 6706 | |
| 6707 | |
| 6708 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6709 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6710 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6711 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6712 | #endif |
| 6713 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6714 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6715 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6716 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6717 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6718 | #endif |
| 6719 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6720 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6721 | #endif |
| 6722 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6723 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6724 | #endif |
| 6725 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6726 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6727 | #endif |
| 6728 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6729 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6730 | #endif |
| 6731 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6732 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6733 | #endif |
| 6734 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6735 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6736 | #endif |
| 6737 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6738 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6739 | #endif |
| 6740 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6741 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6742 | #endif |
| 6743 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6744 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6745 | #endif |
| 6746 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6747 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6748 | #endif |
| 6749 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6750 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6751 | #endif |
| 6752 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6753 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6754 | #endif |
| 6755 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6756 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6757 | #endif |
| 6758 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6759 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6760 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 6761 | #ifdef _PC_ACL_ENABLED |
| 6762 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 6763 | #endif |
| 6764 | #ifdef _PC_MIN_HOLE_SIZE |
| 6765 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 6766 | #endif |
| 6767 | #ifdef _PC_ALLOC_SIZE_MIN |
| 6768 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 6769 | #endif |
| 6770 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 6771 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 6772 | #endif |
| 6773 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 6774 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 6775 | #endif |
| 6776 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 6777 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 6778 | #endif |
| 6779 | #ifdef _PC_REC_XFER_ALIGN |
| 6780 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 6781 | #endif |
| 6782 | #ifdef _PC_SYMLINK_MAX |
| 6783 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 6784 | #endif |
| 6785 | #ifdef _PC_XATTR_ENABLED |
| 6786 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 6787 | #endif |
| 6788 | #ifdef _PC_XATTR_EXISTS |
| 6789 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 6790 | #endif |
| 6791 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 6792 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 6793 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6794 | }; |
| 6795 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6796 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6797 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6798 | { |
| 6799 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6800 | sizeof(posix_constants_pathconf) |
| 6801 | / sizeof(struct constdef)); |
| 6802 | } |
| 6803 | #endif |
| 6804 | |
| 6805 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6806 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6807 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6808 | 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] | 6809 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6810 | |
| 6811 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6812 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6813 | { |
| 6814 | PyObject *result = NULL; |
| 6815 | int name, fd; |
| 6816 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6817 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6818 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6819 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6820 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6821 | errno = 0; |
| 6822 | limit = fpathconf(fd, name); |
| 6823 | if (limit == -1 && errno != 0) |
| 6824 | posix_error(); |
| 6825 | else |
| 6826 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6827 | } |
| 6828 | return result; |
| 6829 | } |
| 6830 | #endif |
| 6831 | |
| 6832 | |
| 6833 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6834 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6835 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6836 | 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] | 6837 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6838 | |
| 6839 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6840 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6841 | { |
| 6842 | PyObject *result = NULL; |
| 6843 | int name; |
| 6844 | char *path; |
| 6845 | |
| 6846 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6847 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6848 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6849 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6850 | errno = 0; |
| 6851 | limit = pathconf(path, name); |
| 6852 | if (limit == -1 && errno != 0) { |
| 6853 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6854 | /* could be a path or name problem */ |
| 6855 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6856 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6857 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6858 | } |
| 6859 | else |
| 6860 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6861 | } |
| 6862 | return result; |
| 6863 | } |
| 6864 | #endif |
| 6865 | |
| 6866 | #ifdef HAVE_CONFSTR |
| 6867 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6868 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6869 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6870 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 6871 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6872 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 6873 | #endif |
| 6874 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6875 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 6876 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6877 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6878 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6879 | #endif |
| 6880 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6881 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6882 | #endif |
| 6883 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6884 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6885 | #endif |
| 6886 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6887 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6888 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6889 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6890 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6891 | #endif |
| 6892 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6893 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6894 | #endif |
| 6895 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6896 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6897 | #endif |
| 6898 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6899 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6900 | #endif |
| 6901 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6902 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6903 | #endif |
| 6904 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6905 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6906 | #endif |
| 6907 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6908 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6909 | #endif |
| 6910 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6911 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6912 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6913 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6914 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6915 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6916 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6917 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6918 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6919 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6920 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6921 | #endif |
| 6922 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6923 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6924 | #endif |
| 6925 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6926 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6927 | #endif |
| 6928 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6929 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6930 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6931 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6932 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6933 | #endif |
| 6934 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6935 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6936 | #endif |
| 6937 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6938 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6939 | #endif |
| 6940 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6941 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6942 | #endif |
| 6943 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6944 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6945 | #endif |
| 6946 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6947 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6948 | #endif |
| 6949 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6950 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6951 | #endif |
| 6952 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6953 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6954 | #endif |
| 6955 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6956 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6957 | #endif |
| 6958 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6959 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6960 | #endif |
| 6961 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6962 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6963 | #endif |
| 6964 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6965 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6966 | #endif |
| 6967 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6968 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6969 | #endif |
| 6970 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6971 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6972 | #endif |
| 6973 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6974 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6975 | #endif |
| 6976 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6977 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6978 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6979 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6980 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6981 | #endif |
| 6982 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6983 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6984 | #endif |
| 6985 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6986 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6987 | #endif |
| 6988 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6989 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6990 | #endif |
| 6991 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6992 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6993 | #endif |
| 6994 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6995 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6996 | #endif |
| 6997 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6998 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6999 | #endif |
| 7000 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7001 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7002 | #endif |
| 7003 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7004 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7005 | #endif |
| 7006 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7007 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7008 | #endif |
| 7009 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7010 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7011 | #endif |
| 7012 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7013 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7014 | #endif |
| 7015 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7016 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7017 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7018 | }; |
| 7019 | |
| 7020 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7021 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7022 | { |
| 7023 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 7024 | sizeof(posix_constants_confstr) |
| 7025 | / sizeof(struct constdef)); |
| 7026 | } |
| 7027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7028 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7029 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7030 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7031 | |
| 7032 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7033 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7034 | { |
| 7035 | PyObject *result = NULL; |
| 7036 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7037 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7038 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7039 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7040 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 7041 | return NULL; |
| 7042 | |
| 7043 | errno = 0; |
| 7044 | len = confstr(name, buffer, sizeof(buffer)); |
| 7045 | if (len == 0) { |
| 7046 | if (errno) { |
| 7047 | posix_error(); |
| 7048 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7049 | } |
| 7050 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7051 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7052 | } |
| 7053 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7054 | |
| 7055 | if ((unsigned int)len >= sizeof(buffer)) { |
| 7056 | char *buf = PyMem_Malloc(len); |
| 7057 | if (buf == NULL) |
| 7058 | return PyErr_NoMemory(); |
| 7059 | confstr(name, buf, len); |
| 7060 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 7061 | PyMem_Free(buf); |
| 7062 | } |
| 7063 | else |
| 7064 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7065 | return result; |
| 7066 | } |
| 7067 | #endif |
| 7068 | |
| 7069 | |
| 7070 | #ifdef HAVE_SYSCONF |
| 7071 | static struct constdef posix_constants_sysconf[] = { |
| 7072 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7073 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7074 | #endif |
| 7075 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7076 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7077 | #endif |
| 7078 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7079 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7080 | #endif |
| 7081 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7082 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7083 | #endif |
| 7084 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7085 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7086 | #endif |
| 7087 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7088 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7089 | #endif |
| 7090 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7091 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7092 | #endif |
| 7093 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7094 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7095 | #endif |
| 7096 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7097 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7098 | #endif |
| 7099 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7100 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7101 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7102 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7103 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7104 | #endif |
| 7105 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7106 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7107 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7108 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7109 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7110 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7111 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7112 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7113 | #endif |
| 7114 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7115 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7116 | #endif |
| 7117 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7118 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7119 | #endif |
| 7120 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7121 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7122 | #endif |
| 7123 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7124 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7125 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7126 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7127 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7128 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7129 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7130 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7131 | #endif |
| 7132 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7133 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7134 | #endif |
| 7135 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7136 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7137 | #endif |
| 7138 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7139 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7140 | #endif |
| 7141 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7142 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7143 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7144 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7145 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7146 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7147 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7148 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7149 | #endif |
| 7150 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7151 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7152 | #endif |
| 7153 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7154 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7155 | #endif |
| 7156 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7157 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7158 | #endif |
| 7159 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7160 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7161 | #endif |
| 7162 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7163 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7164 | #endif |
| 7165 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7166 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7167 | #endif |
| 7168 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7169 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7170 | #endif |
| 7171 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7172 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7173 | #endif |
| 7174 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7175 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7176 | #endif |
| 7177 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7178 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7179 | #endif |
| 7180 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7181 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7182 | #endif |
| 7183 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7184 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7185 | #endif |
| 7186 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7187 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7188 | #endif |
| 7189 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7190 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7191 | #endif |
| 7192 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7193 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7194 | #endif |
| 7195 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7196 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7197 | #endif |
| 7198 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7199 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7200 | #endif |
| 7201 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7202 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7203 | #endif |
| 7204 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7205 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7206 | #endif |
| 7207 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7208 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7209 | #endif |
| 7210 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7211 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7212 | #endif |
| 7213 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7214 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7215 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7216 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7217 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7218 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7219 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7220 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7221 | #endif |
| 7222 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7223 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7224 | #endif |
| 7225 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7226 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7227 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7228 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7229 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7230 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7231 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7232 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7233 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7234 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7235 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7236 | #endif |
| 7237 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7238 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7239 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7240 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7241 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7242 | #endif |
| 7243 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7244 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7245 | #endif |
| 7246 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7247 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7248 | #endif |
| 7249 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7250 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7251 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7252 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7253 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7254 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7255 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7256 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7257 | #endif |
| 7258 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7259 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7260 | #endif |
| 7261 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7262 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7263 | #endif |
| 7264 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7265 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7266 | #endif |
| 7267 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7268 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7269 | #endif |
| 7270 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7271 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7272 | #endif |
| 7273 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7274 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7275 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7276 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7277 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7278 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7279 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7280 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7281 | #endif |
| 7282 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7283 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
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_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7286 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
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_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7289 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7290 | #endif |
| 7291 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7292 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7293 | #endif |
| 7294 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7295 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7296 | #endif |
| 7297 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7298 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7299 | #endif |
| 7300 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7301 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7302 | #endif |
| 7303 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7304 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7305 | #endif |
| 7306 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7307 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7308 | #endif |
| 7309 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7310 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7311 | #endif |
| 7312 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7313 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7314 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7315 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7316 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7317 | #endif |
| 7318 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7319 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7320 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7321 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7322 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7323 | #endif |
| 7324 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7325 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7326 | #endif |
| 7327 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7328 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7329 | #endif |
| 7330 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7331 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7332 | #endif |
| 7333 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7334 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7335 | #endif |
| 7336 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7337 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7338 | #endif |
| 7339 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7340 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7341 | #endif |
| 7342 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7343 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7344 | #endif |
| 7345 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7346 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7347 | #endif |
| 7348 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7349 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7350 | #endif |
| 7351 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7352 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7353 | #endif |
| 7354 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7355 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7356 | #endif |
| 7357 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7358 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7359 | #endif |
| 7360 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7361 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7362 | #endif |
| 7363 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7364 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7365 | #endif |
| 7366 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7367 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7368 | #endif |
| 7369 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7370 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7371 | #endif |
| 7372 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7373 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7374 | #endif |
| 7375 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7376 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7377 | #endif |
| 7378 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7379 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7380 | #endif |
| 7381 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7382 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7383 | #endif |
| 7384 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7385 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7386 | #endif |
| 7387 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7388 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7389 | #endif |
| 7390 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7391 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7392 | #endif |
| 7393 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7394 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7395 | #endif |
| 7396 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7397 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7398 | #endif |
| 7399 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7400 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7401 | #endif |
| 7402 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7403 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7404 | #endif |
| 7405 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7406 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7407 | #endif |
| 7408 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7409 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7410 | #endif |
| 7411 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7412 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7413 | #endif |
| 7414 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7415 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7416 | #endif |
| 7417 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7418 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7419 | #endif |
| 7420 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7421 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7422 | #endif |
| 7423 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7424 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7425 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7426 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7427 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7428 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7429 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7430 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7431 | #endif |
| 7432 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7433 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7434 | #endif |
| 7435 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7436 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7437 | #endif |
| 7438 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7439 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7440 | #endif |
| 7441 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7442 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7443 | #endif |
| 7444 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7445 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7446 | #endif |
| 7447 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7448 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7449 | #endif |
| 7450 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7451 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7452 | #endif |
| 7453 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7454 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7455 | #endif |
| 7456 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7457 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7458 | #endif |
| 7459 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7460 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7461 | #endif |
| 7462 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7463 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7464 | #endif |
| 7465 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7466 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7467 | #endif |
| 7468 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7469 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7470 | #endif |
| 7471 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7472 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7473 | #endif |
| 7474 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7475 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7476 | #endif |
| 7477 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7478 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7479 | #endif |
| 7480 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7481 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7482 | #endif |
| 7483 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7484 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7485 | #endif |
| 7486 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7487 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7488 | #endif |
| 7489 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7490 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7491 | #endif |
| 7492 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7493 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7494 | #endif |
| 7495 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7496 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7497 | #endif |
| 7498 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7499 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7500 | #endif |
| 7501 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7502 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7503 | #endif |
| 7504 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7505 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7506 | #endif |
| 7507 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7508 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7509 | #endif |
| 7510 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7511 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7512 | #endif |
| 7513 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7514 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7515 | #endif |
| 7516 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7517 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7518 | #endif |
| 7519 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7520 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7521 | #endif |
| 7522 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7523 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7524 | #endif |
| 7525 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7526 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7527 | #endif |
| 7528 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7529 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7530 | #endif |
| 7531 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7532 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7533 | #endif |
| 7534 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7535 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7536 | #endif |
| 7537 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7538 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7539 | #endif |
| 7540 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7541 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7542 | #endif |
| 7543 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7544 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7545 | #endif |
| 7546 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7547 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7548 | #endif |
| 7549 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7550 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7551 | #endif |
| 7552 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7553 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7554 | #endif |
| 7555 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7556 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7557 | #endif |
| 7558 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7559 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7560 | #endif |
| 7561 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7562 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7563 | #endif |
| 7564 | }; |
| 7565 | |
| 7566 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7567 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7568 | { |
| 7569 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7570 | sizeof(posix_constants_sysconf) |
| 7571 | / sizeof(struct constdef)); |
| 7572 | } |
| 7573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7574 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7575 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7576 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7577 | |
| 7578 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7579 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7580 | { |
| 7581 | PyObject *result = NULL; |
| 7582 | int name; |
| 7583 | |
| 7584 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7585 | int value; |
| 7586 | |
| 7587 | errno = 0; |
| 7588 | value = sysconf(name); |
| 7589 | if (value == -1 && errno != 0) |
| 7590 | posix_error(); |
| 7591 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7592 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7593 | } |
| 7594 | return result; |
| 7595 | } |
| 7596 | #endif |
| 7597 | |
| 7598 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7599 | /* This code is used to ensure that the tables of configuration value names |
| 7600 | * are in sorted order as required by conv_confname(), and also to build the |
| 7601 | * the exported dictionaries that are used to publish information about the |
| 7602 | * names available on the host platform. |
| 7603 | * |
| 7604 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7605 | * when used, even for platforms we're not able to test on. It also makes |
| 7606 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7607 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7608 | |
| 7609 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7610 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7611 | { |
| 7612 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7613 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7614 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7615 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7616 | |
| 7617 | return strcmp(c1->name, c2->name); |
| 7618 | } |
| 7619 | |
| 7620 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7621 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7622 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7623 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7624 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7625 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7626 | |
| 7627 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7628 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7629 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7630 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7631 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7632 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7633 | PyObject *o = PyLong_FromLong(table[i].value); |
| 7634 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7635 | Py_XDECREF(o); |
| 7636 | Py_DECREF(d); |
| 7637 | return -1; |
| 7638 | } |
| 7639 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7640 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7641 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7642 | } |
| 7643 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7644 | /* Return -1 on failure, 0 on success. */ |
| 7645 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7646 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7647 | { |
| 7648 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7649 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7650 | sizeof(posix_constants_pathconf) |
| 7651 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7652 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7653 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7654 | #endif |
| 7655 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7656 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7657 | sizeof(posix_constants_confstr) |
| 7658 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7659 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7660 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7661 | #endif |
| 7662 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7663 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7664 | sizeof(posix_constants_sysconf) |
| 7665 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7666 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7667 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7668 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7669 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7670 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7671 | |
| 7672 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7673 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7674 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7675 | 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] | 7676 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7677 | |
| 7678 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7679 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7680 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7681 | abort(); |
| 7682 | /*NOTREACHED*/ |
| 7683 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7684 | return NULL; |
| 7685 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7686 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7687 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7688 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7689 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7690 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7691 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7692 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7693 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7694 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7695 | application (if any) its extension is associated.\n\ |
| 7696 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7697 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7698 | \n\ |
| 7699 | startfile returns as soon as the associated application is launched.\n\ |
| 7700 | There is no option to wait for the application to close, and no way\n\ |
| 7701 | to retrieve the application's exit status.\n\ |
| 7702 | \n\ |
| 7703 | The filepath is relative to the current directory. If you want to use\n\ |
| 7704 | 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] | 7705 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7706 | |
| 7707 | static PyObject * |
| 7708 | win32_startfile(PyObject *self, PyObject *args) |
| 7709 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7710 | PyObject *ofilepath; |
| 7711 | char *filepath; |
| 7712 | char *operation = NULL; |
| 7713 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 7714 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7715 | PyObject *unipath, *woperation = NULL; |
| 7716 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7717 | &unipath, &operation)) { |
| 7718 | PyErr_Clear(); |
| 7719 | goto normal; |
| 7720 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 7721 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7722 | if (operation) { |
| 7723 | woperation = PyUnicode_DecodeASCII(operation, |
| 7724 | strlen(operation), NULL); |
| 7725 | if (!woperation) { |
| 7726 | PyErr_Clear(); |
| 7727 | operation = NULL; |
| 7728 | goto normal; |
| 7729 | } |
| 7730 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 7731 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7732 | Py_BEGIN_ALLOW_THREADS |
| 7733 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 7734 | PyUnicode_AS_UNICODE(unipath), |
| 7735 | NULL, NULL, SW_SHOWNORMAL); |
| 7736 | Py_END_ALLOW_THREADS |
| 7737 | |
| 7738 | Py_XDECREF(woperation); |
| 7739 | if (rc <= (HINSTANCE)32) { |
| 7740 | PyObject *errval = win32_error_unicode("startfile", |
| 7741 | PyUnicode_AS_UNICODE(unipath)); |
| 7742 | return errval; |
| 7743 | } |
| 7744 | Py_INCREF(Py_None); |
| 7745 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7746 | |
| 7747 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7748 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 7749 | PyUnicode_FSConverter, &ofilepath, |
| 7750 | &operation)) |
| 7751 | return NULL; |
| 7752 | filepath = PyBytes_AsString(ofilepath); |
| 7753 | Py_BEGIN_ALLOW_THREADS |
| 7754 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7755 | NULL, NULL, SW_SHOWNORMAL); |
| 7756 | Py_END_ALLOW_THREADS |
| 7757 | if (rc <= (HINSTANCE)32) { |
| 7758 | PyObject *errval = win32_error("startfile", filepath); |
| 7759 | Py_DECREF(ofilepath); |
| 7760 | return errval; |
| 7761 | } |
| 7762 | Py_DECREF(ofilepath); |
| 7763 | Py_INCREF(Py_None); |
| 7764 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7765 | } |
| 7766 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7767 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7768 | #ifdef HAVE_GETLOADAVG |
| 7769 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7770 | "getloadavg() -> (float, float, float)\n\n\ |
| 7771 | Return the number of processes in the system run queue averaged over\n\ |
| 7772 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7773 | was unobtainable"); |
| 7774 | |
| 7775 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7776 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7777 | { |
| 7778 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7779 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7780 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7781 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7782 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7783 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7784 | } |
| 7785 | #endif |
| 7786 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7787 | #ifdef MS_WINDOWS |
| 7788 | |
| 7789 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7790 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 7791 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7792 | |
| 7793 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7794 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7795 | DWORD dwFlags ); |
| 7796 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7797 | BYTE *pbBuffer ); |
| 7798 | |
| 7799 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 7800 | /* This handle is never explicitly released. Instead, the operating |
| 7801 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7802 | static HCRYPTPROV hCryptProv = 0; |
| 7803 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7804 | static PyObject* |
| 7805 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7806 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7807 | int howMany; |
| 7808 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7809 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7810 | /* Read arguments */ |
| 7811 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7812 | return NULL; |
| 7813 | if (howMany < 0) |
| 7814 | return PyErr_Format(PyExc_ValueError, |
| 7815 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7816 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7817 | if (hCryptProv == 0) { |
| 7818 | HINSTANCE hAdvAPI32 = NULL; |
| 7819 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7821 | /* Obtain handle to the DLL containing CryptoAPI |
| 7822 | This should not fail */ |
| 7823 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7824 | if(hAdvAPI32 == NULL) |
| 7825 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7826 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7827 | /* Obtain pointers to the CryptoAPI functions |
| 7828 | This will fail on some early versions of Win95 */ |
| 7829 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7830 | hAdvAPI32, |
| 7831 | "CryptAcquireContextA"); |
| 7832 | if (pCryptAcquireContext == NULL) |
| 7833 | return PyErr_Format(PyExc_NotImplementedError, |
| 7834 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7835 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7836 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7837 | hAdvAPI32, "CryptGenRandom"); |
| 7838 | if (pCryptGenRandom == NULL) |
| 7839 | return PyErr_Format(PyExc_NotImplementedError, |
| 7840 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7841 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7842 | /* Acquire context */ |
| 7843 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7844 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7845 | return win32_error("CryptAcquireContext", NULL); |
| 7846 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7847 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7848 | /* Allocate bytes */ |
| 7849 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 7850 | if (result != NULL) { |
| 7851 | /* Get random data */ |
| 7852 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 7853 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7854 | PyBytes_AS_STRING(result))) { |
| 7855 | Py_DECREF(result); |
| 7856 | return win32_error("CryptGenRandom", NULL); |
| 7857 | } |
| 7858 | } |
| 7859 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7860 | } |
| 7861 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7862 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7863 | PyDoc_STRVAR(device_encoding__doc__, |
| 7864 | "device_encoding(fd) -> str\n\n\ |
| 7865 | Return a string describing the encoding of the device\n\ |
| 7866 | if the output is a terminal; else return None."); |
| 7867 | |
| 7868 | static PyObject * |
| 7869 | device_encoding(PyObject *self, PyObject *args) |
| 7870 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7871 | int fd; |
| 7872 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 7873 | return NULL; |
| 7874 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 7875 | Py_INCREF(Py_None); |
| 7876 | return Py_None; |
| 7877 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7878 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7879 | if (fd == 0) { |
| 7880 | char buf[100]; |
| 7881 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 7882 | return PyUnicode_FromString(buf); |
| 7883 | } |
| 7884 | if (fd == 1 || fd == 2) { |
| 7885 | char buf[100]; |
| 7886 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 7887 | return PyUnicode_FromString(buf); |
| 7888 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7889 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7890 | { |
| 7891 | char *codeset = nl_langinfo(CODESET); |
| 7892 | if (codeset != NULL && codeset[0] != 0) |
| 7893 | return PyUnicode_FromString(codeset); |
| 7894 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7895 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7896 | Py_INCREF(Py_None); |
| 7897 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7898 | } |
| 7899 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7900 | #ifdef __VMS |
| 7901 | /* Use openssl random routine */ |
| 7902 | #include <openssl/rand.h> |
| 7903 | PyDoc_STRVAR(vms_urandom__doc__, |
| 7904 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 7905 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7906 | |
| 7907 | static PyObject* |
| 7908 | vms_urandom(PyObject *self, PyObject *args) |
| 7909 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7910 | int howMany; |
| 7911 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7913 | /* Read arguments */ |
| 7914 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7915 | return NULL; |
| 7916 | if (howMany < 0) |
| 7917 | return PyErr_Format(PyExc_ValueError, |
| 7918 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7920 | /* Allocate bytes */ |
| 7921 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 7922 | if (result != NULL) { |
| 7923 | /* Get random data */ |
| 7924 | if (RAND_pseudo_bytes((unsigned char*) |
| 7925 | PyBytes_AS_STRING(result), |
| 7926 | howMany) < 0) { |
| 7927 | Py_DECREF(result); |
| 7928 | return PyErr_Format(PyExc_ValueError, |
| 7929 | "RAND_pseudo_bytes"); |
| 7930 | } |
| 7931 | } |
| 7932 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7933 | } |
| 7934 | #endif |
| 7935 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 7936 | #ifdef HAVE_SETRESUID |
| 7937 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 7938 | "setresuid(ruid, euid, suid)\n\n\ |
| 7939 | Set the current process's real, effective, and saved user ids."); |
| 7940 | |
| 7941 | static PyObject* |
| 7942 | posix_setresuid (PyObject *self, PyObject *args) |
| 7943 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7944 | /* We assume uid_t is no larger than a long. */ |
| 7945 | long ruid, euid, suid; |
| 7946 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 7947 | return NULL; |
| 7948 | if (setresuid(ruid, euid, suid) < 0) |
| 7949 | return posix_error(); |
| 7950 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 7951 | } |
| 7952 | #endif |
| 7953 | |
| 7954 | #ifdef HAVE_SETRESGID |
| 7955 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 7956 | "setresgid(rgid, egid, sgid)\n\n\ |
| 7957 | Set the current process's real, effective, and saved group ids."); |
| 7958 | |
| 7959 | static PyObject* |
| 7960 | posix_setresgid (PyObject *self, PyObject *args) |
| 7961 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7962 | /* We assume uid_t is no larger than a long. */ |
| 7963 | long rgid, egid, sgid; |
| 7964 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 7965 | return NULL; |
| 7966 | if (setresgid(rgid, egid, sgid) < 0) |
| 7967 | return posix_error(); |
| 7968 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 7969 | } |
| 7970 | #endif |
| 7971 | |
| 7972 | #ifdef HAVE_GETRESUID |
| 7973 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 7974 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 7975 | Get tuple of the current process's real, effective, and saved user ids."); |
| 7976 | |
| 7977 | static PyObject* |
| 7978 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 7979 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7980 | uid_t ruid, euid, suid; |
| 7981 | long l_ruid, l_euid, l_suid; |
| 7982 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 7983 | return posix_error(); |
| 7984 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 7985 | l_ruid = ruid; |
| 7986 | l_euid = euid; |
| 7987 | l_suid = suid; |
| 7988 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 7989 | } |
| 7990 | #endif |
| 7991 | |
| 7992 | #ifdef HAVE_GETRESGID |
| 7993 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 7994 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 7995 | 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] | 7996 | |
| 7997 | static PyObject* |
| 7998 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 7999 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8000 | uid_t rgid, egid, sgid; |
| 8001 | long l_rgid, l_egid, l_sgid; |
| 8002 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 8003 | return posix_error(); |
| 8004 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 8005 | l_rgid = rgid; |
| 8006 | l_egid = egid; |
| 8007 | l_sgid = sgid; |
| 8008 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8009 | } |
| 8010 | #endif |
| 8011 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8012 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8013 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8014 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8015 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8016 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8017 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 8018 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8019 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 8020 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8021 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8022 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8023 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8024 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8025 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8026 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8027 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8028 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8029 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8030 | #endif /* HAVE_LCHMOD */ |
| 8031 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8032 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 8033 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 8034 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8035 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 8036 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8037 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8038 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8039 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8040 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8041 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8042 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8043 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8044 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8045 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8046 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8047 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 8048 | METH_NOARGS, posix_getcwd__doc__}, |
| 8049 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 8050 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8051 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8052 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8053 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8054 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8055 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 8056 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 8057 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8058 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8059 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8060 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 8061 | #ifdef HAVE_GETPRIORITY |
| 8062 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 8063 | #endif /* HAVE_GETPRIORITY */ |
| 8064 | #ifdef HAVE_SETPRIORITY |
| 8065 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 8066 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8067 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8068 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8069 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8070 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 8071 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8072 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 8073 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 8074 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 8075 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8076 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8077 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8078 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8079 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8080 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 8081 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8082 | win_symlink__doc__}, |
| 8083 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8084 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8085 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8086 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8087 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8088 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8089 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8090 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8091 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 8092 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 8093 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8094 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8095 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8096 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8097 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8098 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8099 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 8100 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8101 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8102 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8103 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 8104 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8105 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8106 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 8107 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8108 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8109 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8110 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8111 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8112 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8113 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8114 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8115 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8116 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8117 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8118 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8119 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8120 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8121 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8122 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8123 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8124 | #endif /* HAVE_GETEGID */ |
| 8125 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8126 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8127 | #endif /* HAVE_GETEUID */ |
| 8128 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8129 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8130 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8131 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8132 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8133 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8134 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8135 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8136 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8137 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8138 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8139 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8140 | #endif /* HAVE_GETPPID */ |
| 8141 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8142 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8143 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8144 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8145 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8146 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8147 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8148 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8149 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8150 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8151 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8152 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8153 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8154 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8155 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 8156 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8157 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 8158 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 8159 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 8160 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8161 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8162 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8163 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8164 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8165 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8166 | #endif /* HAVE_SETEUID */ |
| 8167 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8168 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8169 | #endif /* HAVE_SETEGID */ |
| 8170 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8171 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8172 | #endif /* HAVE_SETREUID */ |
| 8173 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8174 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8175 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8176 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8177 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8178 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8179 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8180 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8181 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8182 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8183 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8184 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8185 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8186 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8187 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8188 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8189 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8190 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8191 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8192 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8193 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8194 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8195 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8196 | #endif /* HAVE_WAIT3 */ |
| 8197 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8198 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8199 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8200 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8201 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8202 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8203 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8204 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8205 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8206 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8207 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8208 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8209 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8210 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8211 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8212 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8213 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8214 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8215 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8216 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8217 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8218 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 8219 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 8220 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 8221 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 8222 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 8223 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 8224 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 8225 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 8226 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8227 | #ifdef HAVE_SENDFILE |
| 8228 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 8229 | posix_sendfile__doc__}, |
| 8230 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8231 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 8232 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8233 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8234 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8235 | #endif |
| 8236 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8237 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8238 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8239 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8240 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8241 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8242 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8243 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 8244 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 8245 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8246 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8247 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8248 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8249 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8250 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8251 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8252 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8253 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8254 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8255 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8256 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8257 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8258 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8259 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8260 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8261 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8262 | #endif |
| 8263 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8264 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8265 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8266 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8267 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8268 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8269 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8270 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8271 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8272 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8273 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8274 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8275 | #endif /* WIFSTOPPED */ |
| 8276 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8277 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8278 | #endif /* WIFSIGNALED */ |
| 8279 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8280 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8281 | #endif /* WIFEXITED */ |
| 8282 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8283 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8284 | #endif /* WEXITSTATUS */ |
| 8285 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8286 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8287 | #endif /* WTERMSIG */ |
| 8288 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8289 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8290 | #endif /* WSTOPSIG */ |
| 8291 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8292 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8293 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8294 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8295 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8296 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8297 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8298 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8299 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8300 | #endif |
| 8301 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8302 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8303 | #endif |
| 8304 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8305 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8306 | #endif |
| 8307 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8308 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8309 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8310 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8311 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8312 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8313 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 8314 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 8315 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8316 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8317 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8318 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8319 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8320 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8321 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8322 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8323 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8324 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8325 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8326 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8327 | #endif |
| 8328 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8329 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8330 | #endif |
| 8331 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8332 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8333 | #endif |
| 8334 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8335 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8336 | #endif |
| 8337 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8338 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8339 | }; |
| 8340 | |
| 8341 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8342 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8343 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8344 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8345 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8346 | } |
| 8347 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8348 | #if defined(PYOS_OS2) |
| 8349 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8350 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8351 | { |
| 8352 | APIRET rc; |
| 8353 | ULONG values[QSV_MAX+1]; |
| 8354 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 8355 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8356 | |
| 8357 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 8358 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8359 | Py_END_ALLOW_THREADS |
| 8360 | |
| 8361 | if (rc != NO_ERROR) { |
| 8362 | os2_error(rc); |
| 8363 | return -1; |
| 8364 | } |
| 8365 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8366 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 8367 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 8368 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 8369 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 8370 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 8371 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 8372 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8373 | |
| 8374 | switch (values[QSV_VERSION_MINOR]) { |
| 8375 | case 0: ver = "2.00"; break; |
| 8376 | case 10: ver = "2.10"; break; |
| 8377 | case 11: ver = "2.11"; break; |
| 8378 | case 30: ver = "3.00"; break; |
| 8379 | case 40: ver = "4.00"; break; |
| 8380 | case 50: ver = "5.00"; break; |
| 8381 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 8382 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8383 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 8384 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8385 | ver = &tmp[0]; |
| 8386 | } |
| 8387 | |
| 8388 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8389 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8390 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8391 | |
| 8392 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 8393 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 8394 | tmp[1] = ':'; |
| 8395 | tmp[2] = '\0'; |
| 8396 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8397 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8398 | } |
| 8399 | #endif |
| 8400 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8401 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 8402 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8403 | enable_symlink() |
| 8404 | { |
| 8405 | HANDLE tok; |
| 8406 | TOKEN_PRIVILEGES tok_priv; |
| 8407 | LUID luid; |
| 8408 | int meth_idx = 0; |
| 8409 | |
| 8410 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 8411 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8412 | |
| 8413 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 8414 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8415 | |
| 8416 | tok_priv.PrivilegeCount = 1; |
| 8417 | tok_priv.Privileges[0].Luid = luid; |
| 8418 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 8419 | |
| 8420 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 8421 | sizeof(TOKEN_PRIVILEGES), |
| 8422 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 8423 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8424 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 8425 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 8426 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8427 | } |
| 8428 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 8429 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8430 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8431 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8432 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8433 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8434 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8435 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8436 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8437 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8438 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8439 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8440 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8441 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8442 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8443 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8444 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8445 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8446 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8447 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8448 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8449 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8450 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8451 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8452 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8453 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8454 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8455 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8456 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8457 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8458 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8459 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8460 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8461 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8462 | #endif |
| 8463 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8464 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8465 | #endif |
| 8466 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8467 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8468 | #endif |
| 8469 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8470 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8471 | #endif |
| 8472 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8473 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8474 | #endif |
| 8475 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8476 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8477 | #endif |
| 8478 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8479 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8480 | #endif |
| 8481 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8482 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8483 | #endif |
| 8484 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8485 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8486 | #endif |
| 8487 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8488 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8489 | #endif |
| 8490 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8491 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8492 | #endif |
| 8493 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8494 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8495 | #endif |
| 8496 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8497 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8498 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8499 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8500 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8501 | #endif |
| 8502 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8503 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8504 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8505 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8506 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8507 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8508 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8509 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8510 | #endif |
| 8511 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8512 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8513 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 8514 | #ifdef PRIO_PROCESS |
| 8515 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 8516 | #endif |
| 8517 | #ifdef PRIO_PGRP |
| 8518 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 8519 | #endif |
| 8520 | #ifdef PRIO_USER |
| 8521 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 8522 | #endif |
| 8523 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8524 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8525 | /* MS Windows */ |
| 8526 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8527 | /* Don't inherit in child processes. */ |
| 8528 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8529 | #endif |
| 8530 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8531 | /* Optimize for short life (keep in memory). */ |
| 8532 | /* MS forgot to define this one with a non-underscore form too. */ |
| 8533 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8534 | #endif |
| 8535 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8536 | /* Automatically delete when last handle is closed. */ |
| 8537 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8538 | #endif |
| 8539 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8540 | /* Optimize for random access. */ |
| 8541 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8542 | #endif |
| 8543 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8544 | /* Optimize for sequential access. */ |
| 8545 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8546 | #endif |
| 8547 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8548 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 8549 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8550 | /* Send a SIGIO signal whenever input or output |
| 8551 | becomes available on file descriptor */ |
| 8552 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 8553 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8554 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8555 | /* Direct disk access. */ |
| 8556 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8557 | #endif |
| 8558 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8559 | /* Must be a directory. */ |
| 8560 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8561 | #endif |
| 8562 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8563 | /* Do not follow links. */ |
| 8564 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8565 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 8566 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8567 | /* Do not update the access time. */ |
| 8568 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 8569 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8570 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8571 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8572 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8573 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8574 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8575 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8576 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8577 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8578 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8579 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8580 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8581 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8582 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8583 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8584 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8585 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8586 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8587 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8588 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8589 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8590 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8591 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8592 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8593 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8594 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8595 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8596 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8597 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8598 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8599 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8600 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8601 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8602 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8603 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8604 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8605 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8606 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8607 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8608 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8609 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8610 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8611 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8612 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8613 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8614 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8615 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8616 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8617 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8618 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8619 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8620 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8621 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8622 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8623 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 8624 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 8625 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 8626 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 8627 | #endif /* ST_RDONLY */ |
| 8628 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 8629 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 8630 | #endif /* ST_NOSUID */ |
| 8631 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8632 | /* FreeBSD sendfile() constants */ |
| 8633 | #ifdef SF_NODISKIO |
| 8634 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 8635 | #endif |
| 8636 | #ifdef SF_MNOWAIT |
| 8637 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 8638 | #endif |
| 8639 | #ifdef SF_SYNC |
| 8640 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 8641 | #endif |
| 8642 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8643 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8644 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8645 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8646 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8647 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8648 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8649 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8650 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8651 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8652 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8653 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8654 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8655 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8656 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8657 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8658 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8659 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8660 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8661 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8662 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8663 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8664 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8665 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8666 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8667 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8668 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8669 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8670 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8671 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8672 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8673 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8674 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8675 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8676 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8677 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8678 | } |
| 8679 | |
| 8680 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8681 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 8682 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8683 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8684 | |
| 8685 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 8686 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8687 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8688 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8689 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 8690 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8691 | #define MODNAME "posix" |
| 8692 | #endif |
| 8693 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 8694 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8695 | PyModuleDef_HEAD_INIT, |
| 8696 | MODNAME, |
| 8697 | posix__doc__, |
| 8698 | -1, |
| 8699 | posix_methods, |
| 8700 | NULL, |
| 8701 | NULL, |
| 8702 | NULL, |
| 8703 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 8704 | }; |
| 8705 | |
| 8706 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8707 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8708 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8709 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8710 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8711 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8712 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 8713 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 8714 | #endif |
| 8715 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8716 | m = PyModule_Create(&posixmodule); |
| 8717 | if (m == NULL) |
| 8718 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8719 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8720 | /* Initialize environ dictionary */ |
| 8721 | v = convertenviron(); |
| 8722 | Py_XINCREF(v); |
| 8723 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 8724 | return NULL; |
| 8725 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8726 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8727 | if (all_ins(m)) |
| 8728 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8729 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8730 | if (setup_confname_tables(m)) |
| 8731 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8732 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8733 | Py_INCREF(PyExc_OSError); |
| 8734 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8735 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8736 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8737 | if (posix_putenv_garbage == NULL) |
| 8738 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8739 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8740 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8741 | if (!initialized) { |
| 8742 | stat_result_desc.name = MODNAME ".stat_result"; |
| 8743 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8744 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8745 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 8746 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 8747 | structseq_new = StatResultType.tp_new; |
| 8748 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8749 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8750 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 8751 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 8752 | #ifdef NEED_TICKS_PER_SECOND |
| 8753 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8754 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 8755 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8756 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 8757 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8758 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 8759 | # endif |
| 8760 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8761 | } |
| 8762 | Py_INCREF((PyObject*) &StatResultType); |
| 8763 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 8764 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8765 | PyModule_AddObject(m, "statvfs_result", |
| 8766 | (PyObject*) &StatVFSResultType); |
| 8767 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8768 | |
| 8769 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8770 | /* |
| 8771 | * Step 2 of weak-linking support on Mac OS X. |
| 8772 | * |
| 8773 | * The code below removes functions that are not available on the |
| 8774 | * currently active platform. |
| 8775 | * |
| 8776 | * This block allow one to use a python binary that was build on |
| 8777 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 8778 | * OSX 10.4. |
| 8779 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8780 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8781 | if (fstatvfs == NULL) { |
| 8782 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 8783 | return NULL; |
| 8784 | } |
| 8785 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8786 | #endif /* HAVE_FSTATVFS */ |
| 8787 | |
| 8788 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8789 | if (statvfs == NULL) { |
| 8790 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 8791 | return NULL; |
| 8792 | } |
| 8793 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8794 | #endif /* HAVE_STATVFS */ |
| 8795 | |
| 8796 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8797 | if (lchown == NULL) { |
| 8798 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 8799 | return NULL; |
| 8800 | } |
| 8801 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8802 | #endif /* HAVE_LCHOWN */ |
| 8803 | |
| 8804 | |
| 8805 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8806 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8807 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8808 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8809 | |
| 8810 | #ifdef __cplusplus |
| 8811 | } |
| 8812 | #endif |