Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 31 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 33 | # error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #if defined(PYOS_OS2) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 49 | #error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 50 | #define INCL_DOS |
| 51 | #define INCL_DOSERRORS |
| 52 | #define INCL_DOSPROCESS |
| 53 | #define INCL_NOPMAPI |
| 54 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 55 | #if defined(PYCC_GCC) |
| 56 | #include <ctype.h> |
| 57 | #include <io.h> |
| 58 | #include <stdio.h> |
| 59 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 60 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 61 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 64 | #ifdef HAVE_SYS_UIO_H |
| 65 | #include <sys/uio.h> |
| 66 | #endif |
| 67 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 70 | #endif /* HAVE_SYS_TYPES_H */ |
| 71 | |
| 72 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 73 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 77 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 78 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 81 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 82 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 84 | #ifdef HAVE_FCNTL_H |
| 85 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 86 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 88 | #ifdef HAVE_GRP_H |
| 89 | #include <grp.h> |
| 90 | #endif |
| 91 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 92 | #ifdef HAVE_SYSEXITS_H |
| 93 | #include <sysexits.h> |
| 94 | #endif /* HAVE_SYSEXITS_H */ |
| 95 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 96 | #ifdef HAVE_SYS_LOADAVG_H |
| 97 | #include <sys/loadavg.h> |
| 98 | #endif |
| 99 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 100 | #ifdef HAVE_LANGINFO_H |
| 101 | #include <langinfo.h> |
| 102 | #endif |
| 103 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYS_SENDFILE_H |
| 105 | #include <sys/sendfile.h> |
| 106 | #endif |
| 107 | |
| 108 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 109 | #ifdef HAVE_SYS_SOCKET_H |
| 110 | #include <sys/socket.h> |
| 111 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 112 | #endif |
| 113 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 114 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 115 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 116 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 117 | #include <process.h> |
| 118 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 119 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 120 | #define HAVE_GETCWD 1 |
| 121 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 122 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 123 | #if defined(__OS2__) |
| 124 | #define HAVE_EXECV 1 |
| 125 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 126 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #include <process.h> |
| 128 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 129 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #define HAVE_EXECV 1 |
| 131 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 132 | #define HAVE_OPENDIR 1 |
| 133 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 134 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 135 | #define HAVE_WAIT 1 |
| 136 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 137 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 138 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 139 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 140 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 141 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 142 | #define HAVE_EXECV 1 |
| 143 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #define HAVE_SYSTEM 1 |
| 145 | #define HAVE_CWAIT 1 |
| 146 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 147 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 148 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 149 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 150 | /* 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] | 151 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 152 | /* Unix functions that the configure script doesn't check for */ |
| 153 | #define HAVE_EXECV 1 |
| 154 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 155 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 156 | #define HAVE_FORK1 1 |
| 157 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 158 | #define HAVE_GETCWD 1 |
| 159 | #define HAVE_GETEGID 1 |
| 160 | #define HAVE_GETEUID 1 |
| 161 | #define HAVE_GETGID 1 |
| 162 | #define HAVE_GETPPID 1 |
| 163 | #define HAVE_GETUID 1 |
| 164 | #define HAVE_KILL 1 |
| 165 | #define HAVE_OPENDIR 1 |
| 166 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 167 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 168 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 169 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 170 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 171 | #endif /* _MSC_VER */ |
| 172 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 173 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 174 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 176 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 177 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 178 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 179 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 180 | (default) */ |
| 181 | extern char *ctermid_r(char *); |
| 182 | #endif |
| 183 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 184 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 185 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 186 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 187 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 188 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 189 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 190 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 191 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 192 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 193 | #endif |
| 194 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int chdir(char *); |
| 196 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 197 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 198 | extern int chdir(const char *); |
| 199 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 200 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 201 | #ifdef __BORLANDC__ |
| 202 | extern int chmod(const char *, int); |
| 203 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 205 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 206 | /*#ifdef HAVE_FCHMOD |
| 207 | extern int fchmod(int, mode_t); |
| 208 | #endif*/ |
| 209 | /*#ifdef HAVE_LCHMOD |
| 210 | extern int lchmod(const char *, mode_t); |
| 211 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 212 | extern int chown(const char *, uid_t, gid_t); |
| 213 | extern char *getcwd(char *, int); |
| 214 | extern char *strerror(int); |
| 215 | extern int link(const char *, const char *); |
| 216 | extern int rename(const char *, const char *); |
| 217 | extern int stat(const char *, struct stat *); |
| 218 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 220 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 221 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 223 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 224 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 227 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | #ifdef HAVE_UTIME_H |
| 230 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 231 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 233 | #ifdef HAVE_SYS_UTIME_H |
| 234 | #include <sys/utime.h> |
| 235 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 236 | #endif /* HAVE_SYS_UTIME_H */ |
| 237 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | #ifdef HAVE_SYS_TIMES_H |
| 239 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 240 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | |
| 242 | #ifdef HAVE_SYS_PARAM_H |
| 243 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 244 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | |
| 246 | #ifdef HAVE_SYS_UTSNAME_H |
| 247 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 248 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 250 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 252 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 253 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 254 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 255 | #include <direct.h> |
| 256 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 257 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 259 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 260 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 261 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 263 | #endif |
| 264 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 266 | #endif |
| 267 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 269 | #endif |
| 270 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 272 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 273 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 275 | #endif |
| 276 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 277 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 278 | #endif |
| 279 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 281 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 282 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 283 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 284 | #endif |
| 285 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 286 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 287 | #endif |
| 288 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 289 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 290 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 291 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 292 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 293 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 294 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 295 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 296 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 297 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 298 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 299 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 300 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 302 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 303 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 304 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 305 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 306 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 307 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 308 | #define MAXPATHLEN PATH_MAX |
| 309 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 310 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 311 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 312 | #endif /* MAXPATHLEN */ |
| 313 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 314 | #ifdef UNION_WAIT |
| 315 | /* Emulate some macros on systems that have a union instead of macros */ |
| 316 | |
| 317 | #ifndef WIFEXITED |
| 318 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 319 | #endif |
| 320 | |
| 321 | #ifndef WEXITSTATUS |
| 322 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 323 | #endif |
| 324 | |
| 325 | #ifndef WTERMSIG |
| 326 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 327 | #endif |
| 328 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 329 | #define WAIT_TYPE union wait |
| 330 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 331 | |
| 332 | #else /* !UNION_WAIT */ |
| 333 | #define WAIT_TYPE int |
| 334 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 335 | #endif /* UNION_WAIT */ |
| 336 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 337 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 338 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 339 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 340 | #define USE_CTERMID_R |
| 341 | #endif |
| 342 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 343 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 344 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 345 | #undef FSTAT |
| 346 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 347 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 348 | # define STAT win32_stat |
| 349 | # define FSTAT win32_fstat |
| 350 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 351 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 352 | # define STAT stat |
| 353 | # define FSTAT fstat |
| 354 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 355 | #endif |
| 356 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 357 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 358 | #include <sys/mkdev.h> |
| 359 | #else |
| 360 | #if defined(MAJOR_IN_SYSMACROS) |
| 361 | #include <sys/sysmacros.h> |
| 362 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 363 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 364 | #include <sys/mkdev.h> |
| 365 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 366 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 367 | |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 368 | static int |
| 369 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 370 | { |
| 371 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 372 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 373 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 374 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 375 | #endif |
| 376 | if (PyErr_Occurred()) |
| 377 | return 0; |
| 378 | return 1; |
| 379 | } |
| 380 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 381 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 382 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 383 | * valid and throw an assertion if it isn't. |
| 384 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 385 | * an assertion can be useful, but it does contradict the POSIX standard |
| 386 | * which for write(2) states: |
| 387 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 388 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 389 | * writing." |
| 390 | * Furthermore, python allows the user to enter any old integer |
| 391 | * as a fd and should merely raise a python exception on error. |
| 392 | * The Microsoft CRT doesn't provide an official way to check for the |
| 393 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 394 | * 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] | 395 | * internal structures involved. |
| 396 | * The structures below must be updated for each version of visual studio |
| 397 | * according to the file internal.h in the CRT source, until MS comes |
| 398 | * up with a less hacky way to do this. |
| 399 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 400 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 401 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 402 | /* The actual size of the structure is determined at runtime. |
| 403 | * Only the first items must be present. |
| 404 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 405 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 406 | intptr_t osfhnd; |
| 407 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 408 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 409 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 410 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 411 | #define IOINFO_L2E 5 |
| 412 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 413 | #define IOINFO_ARRAYS 64 |
| 414 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 415 | #define FOPEN 0x01 |
| 416 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 417 | |
| 418 | /* This function emulates what the windows CRT does to validate file handles */ |
| 419 | int |
| 420 | _PyVerify_fd(int fd) |
| 421 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 422 | const int i1 = fd >> IOINFO_L2E; |
| 423 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 424 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 425 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 426 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 427 | /* Determine the actual size of the ioinfo structure, |
| 428 | * as used by the CRT loaded in memory |
| 429 | */ |
| 430 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 431 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 432 | } |
| 433 | if (sizeof_ioinfo == 0) { |
| 434 | /* This should not happen... */ |
| 435 | goto fail; |
| 436 | } |
| 437 | |
| 438 | /* See that it isn't a special CLEAR fileno */ |
| 439 | if (fd != _NO_CONSOLE_FILENO) { |
| 440 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 441 | * we check pointer validity and other info |
| 442 | */ |
| 443 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 444 | /* finally, check that the file is open */ |
| 445 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 446 | if (info->osfile & FOPEN) { |
| 447 | return 1; |
| 448 | } |
| 449 | } |
| 450 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 451 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 452 | errno = EBADF; |
| 453 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 457 | static int |
| 458 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 459 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 460 | if (!_PyVerify_fd(fd1)) |
| 461 | return 0; |
| 462 | if (fd2 == _NO_CONSOLE_FILENO) |
| 463 | return 0; |
| 464 | if ((unsigned)fd2 < _NHANDLE_) |
| 465 | return 1; |
| 466 | else |
| 467 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 468 | } |
| 469 | #else |
| 470 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 471 | #define _PyVerify_fd_dup2(A, B) (1) |
| 472 | #endif |
| 473 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 474 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 475 | /* The following structure was copied from |
| 476 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 477 | include doesn't seem to be present in the Windows SDK (at least as included |
| 478 | with Visual Studio Express). */ |
| 479 | typedef struct _REPARSE_DATA_BUFFER { |
| 480 | ULONG ReparseTag; |
| 481 | USHORT ReparseDataLength; |
| 482 | USHORT Reserved; |
| 483 | union { |
| 484 | struct { |
| 485 | USHORT SubstituteNameOffset; |
| 486 | USHORT SubstituteNameLength; |
| 487 | USHORT PrintNameOffset; |
| 488 | USHORT PrintNameLength; |
| 489 | ULONG Flags; |
| 490 | WCHAR PathBuffer[1]; |
| 491 | } SymbolicLinkReparseBuffer; |
| 492 | |
| 493 | struct { |
| 494 | USHORT SubstituteNameOffset; |
| 495 | USHORT SubstituteNameLength; |
| 496 | USHORT PrintNameOffset; |
| 497 | USHORT PrintNameLength; |
| 498 | WCHAR PathBuffer[1]; |
| 499 | } MountPointReparseBuffer; |
| 500 | |
| 501 | struct { |
| 502 | UCHAR DataBuffer[1]; |
| 503 | } GenericReparseBuffer; |
| 504 | }; |
| 505 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 506 | |
| 507 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 508 | GenericReparseBuffer) |
| 509 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 510 | |
| 511 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 512 | 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] | 513 | { |
| 514 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 515 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 516 | DWORD n_bytes_returned; |
| 517 | const wchar_t *ptr; |
| 518 | wchar_t *buf; |
| 519 | size_t len; |
| 520 | |
| 521 | if (0 == DeviceIoControl( |
| 522 | reparse_point_handle, |
| 523 | FSCTL_GET_REPARSE_POINT, |
| 524 | NULL, 0, /* in buffer */ |
| 525 | target_buffer, sizeof(target_buffer), |
| 526 | &n_bytes_returned, |
| 527 | NULL)) /* we're not using OVERLAPPED_IO */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 528 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 529 | |
| 530 | if (reparse_tag) |
| 531 | *reparse_tag = rdb->ReparseTag; |
| 532 | |
| 533 | if (target_path) { |
| 534 | switch (rdb->ReparseTag) { |
| 535 | case IO_REPARSE_TAG_SYMLINK: |
| 536 | /* XXX: Maybe should use SubstituteName? */ |
| 537 | ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 538 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR); |
| 539 | len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR); |
| 540 | break; |
| 541 | case IO_REPARSE_TAG_MOUNT_POINT: |
| 542 | ptr = rdb->MountPointReparseBuffer.PathBuffer + |
| 543 | rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR); |
| 544 | len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR); |
| 545 | break; |
| 546 | default: |
| 547 | SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 548 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 549 | } |
| 550 | buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1)); |
| 551 | if (!buf) { |
| 552 | SetLastError(ERROR_OUTOFMEMORY); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 553 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 554 | } |
| 555 | wcsncpy(buf, ptr, len); |
| 556 | buf[len] = L'\0'; |
| 557 | if (wcsncmp(buf, L"\\??\\", 4) == 0) |
| 558 | buf[1] = L'\\'; |
| 559 | *target_path = buf; |
| 560 | } |
| 561 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 562 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 563 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 564 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 565 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 566 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 567 | #ifdef WITH_NEXT_FRAMEWORK |
| 568 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 569 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 570 | */ |
| 571 | #include <crt_externs.h> |
| 572 | static char **environ; |
| 573 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 574 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 575 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 576 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 577 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 578 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 579 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 580 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 581 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 582 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 583 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 584 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 585 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 586 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 587 | APIRET rc; |
| 588 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 589 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 590 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 591 | d = PyDict_New(); |
| 592 | if (d == NULL) |
| 593 | return NULL; |
| 594 | #ifdef WITH_NEXT_FRAMEWORK |
| 595 | if (environ == NULL) |
| 596 | environ = *_NSGetEnviron(); |
| 597 | #endif |
| 598 | #ifdef MS_WINDOWS |
| 599 | /* _wenviron must be initialized in this way if the program is started |
| 600 | through main() instead of wmain(). */ |
| 601 | _wgetenv(L""); |
| 602 | if (_wenviron == NULL) |
| 603 | return d; |
| 604 | /* This part ignores errors */ |
| 605 | for (e = _wenviron; *e != NULL; e++) { |
| 606 | PyObject *k; |
| 607 | PyObject *v; |
| 608 | wchar_t *p = wcschr(*e, L'='); |
| 609 | if (p == NULL) |
| 610 | continue; |
| 611 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 612 | if (k == NULL) { |
| 613 | PyErr_Clear(); |
| 614 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 615 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 616 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 617 | if (v == NULL) { |
| 618 | PyErr_Clear(); |
| 619 | Py_DECREF(k); |
| 620 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 621 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 622 | if (PyDict_GetItem(d, k) == NULL) { |
| 623 | if (PyDict_SetItem(d, k, v) != 0) |
| 624 | PyErr_Clear(); |
| 625 | } |
| 626 | Py_DECREF(k); |
| 627 | Py_DECREF(v); |
| 628 | } |
| 629 | #else |
| 630 | if (environ == NULL) |
| 631 | return d; |
| 632 | /* This part ignores errors */ |
| 633 | for (e = environ; *e != NULL; e++) { |
| 634 | PyObject *k; |
| 635 | PyObject *v; |
| 636 | char *p = strchr(*e, '='); |
| 637 | if (p == NULL) |
| 638 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 639 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 640 | if (k == NULL) { |
| 641 | PyErr_Clear(); |
| 642 | continue; |
| 643 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 644 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 645 | if (v == NULL) { |
| 646 | PyErr_Clear(); |
| 647 | Py_DECREF(k); |
| 648 | continue; |
| 649 | } |
| 650 | if (PyDict_GetItem(d, k) == NULL) { |
| 651 | if (PyDict_SetItem(d, k, v) != 0) |
| 652 | PyErr_Clear(); |
| 653 | } |
| 654 | Py_DECREF(k); |
| 655 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 656 | } |
| 657 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 658 | #if defined(PYOS_OS2) |
| 659 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 660 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 661 | PyObject *v = PyBytes_FromString(buffer); |
| 662 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 663 | Py_DECREF(v); |
| 664 | } |
| 665 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 666 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 667 | PyObject *v = PyBytes_FromString(buffer); |
| 668 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 669 | Py_DECREF(v); |
| 670 | } |
| 671 | #endif |
| 672 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 675 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 676 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 677 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 678 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 679 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 680 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 681 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 682 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 683 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 684 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 685 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 688 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 689 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 690 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 691 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 692 | PyObject *name_str, *rc; |
| 693 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 694 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 695 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 696 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 697 | name_str); |
| 698 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 699 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 702 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 703 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 704 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 706 | /* XXX We should pass the function name along in the future. |
| 707 | (winreg.c also wants to pass the function name.) |
| 708 | This would however require an additional param to the |
| 709 | Windows error object, which is non-trivial. |
| 710 | */ |
| 711 | errno = GetLastError(); |
| 712 | if (filename) |
| 713 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 714 | else |
| 715 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 716 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 717 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 718 | static PyObject * |
| 719 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 720 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 721 | /* XXX - see win32_error for comments on 'function' */ |
| 722 | errno = GetLastError(); |
| 723 | if (filename) |
| 724 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 725 | else |
| 726 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 729 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 730 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 731 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 732 | if (PyUnicode_CheckExact(*param)) |
| 733 | Py_INCREF(*param); |
| 734 | else if (PyUnicode_Check(*param)) |
| 735 | /* For a Unicode subtype that's not a Unicode object, |
| 736 | return a true Unicode object with the same data. */ |
| 737 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 738 | PyUnicode_GET_SIZE(*param)); |
| 739 | else |
| 740 | *param = PyUnicode_FromEncodedObject(*param, |
| 741 | Py_FileSystemDefaultEncoding, |
| 742 | "strict"); |
| 743 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 746 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 747 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 748 | #if defined(PYOS_OS2) |
| 749 | /********************************************************************** |
| 750 | * Helper Function to Trim and Format OS/2 Messages |
| 751 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 752 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 753 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 754 | { |
| 755 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 756 | |
| 757 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 758 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 759 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 760 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 761 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 762 | } |
| 763 | |
| 764 | /* Add Optional Reason Text */ |
| 765 | if (reason) { |
| 766 | strcat(msgbuf, " : "); |
| 767 | strcat(msgbuf, reason); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /********************************************************************** |
| 772 | * Decode an OS/2 Operating System Error Code |
| 773 | * |
| 774 | * A convenience function to lookup an OS/2 error code and return a |
| 775 | * text message we can use to raise a Python exception. |
| 776 | * |
| 777 | * Notes: |
| 778 | * The messages for errors returned from the OS/2 kernel reside in |
| 779 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 780 | * |
| 781 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 782 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 783 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 784 | { |
| 785 | APIRET rc; |
| 786 | ULONG msglen; |
| 787 | |
| 788 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 789 | Py_BEGIN_ALLOW_THREADS |
| 790 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 791 | errorcode, "oso001.msg", &msglen); |
| 792 | Py_END_ALLOW_THREADS |
| 793 | |
| 794 | if (rc == NO_ERROR) |
| 795 | os2_formatmsg(msgbuf, msglen, reason); |
| 796 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 797 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 798 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 799 | |
| 800 | return msgbuf; |
| 801 | } |
| 802 | |
| 803 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 804 | errors are not in a global variable e.g. 'errno' nor are |
| 805 | they congruent with posix error numbers. */ |
| 806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 807 | static PyObject * |
| 808 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 809 | { |
| 810 | char text[1024]; |
| 811 | PyObject *v; |
| 812 | |
| 813 | os2_strerror(text, sizeof(text), code, ""); |
| 814 | |
| 815 | v = Py_BuildValue("(is)", code, text); |
| 816 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 817 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 818 | Py_DECREF(v); |
| 819 | } |
| 820 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 821 | } |
| 822 | |
| 823 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 824 | |
| 825 | /* POSIX generic methods */ |
| 826 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 827 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 828 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 829 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 830 | int fd; |
| 831 | int res; |
| 832 | fd = PyObject_AsFileDescriptor(fdobj); |
| 833 | if (fd < 0) |
| 834 | return NULL; |
| 835 | if (!_PyVerify_fd(fd)) |
| 836 | return posix_error(); |
| 837 | Py_BEGIN_ALLOW_THREADS |
| 838 | res = (*func)(fd); |
| 839 | Py_END_ALLOW_THREADS |
| 840 | if (res < 0) |
| 841 | return posix_error(); |
| 842 | Py_INCREF(Py_None); |
| 843 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 844 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 845 | |
| 846 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 847 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 848 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 849 | PyObject *opath1 = NULL; |
| 850 | char *path1; |
| 851 | int res; |
| 852 | if (!PyArg_ParseTuple(args, format, |
| 853 | PyUnicode_FSConverter, &opath1)) |
| 854 | return NULL; |
| 855 | path1 = PyBytes_AsString(opath1); |
| 856 | Py_BEGIN_ALLOW_THREADS |
| 857 | res = (*func)(path1); |
| 858 | Py_END_ALLOW_THREADS |
| 859 | if (res < 0) |
| 860 | return posix_error_with_allocated_filename(opath1); |
| 861 | Py_DECREF(opath1); |
| 862 | Py_INCREF(Py_None); |
| 863 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 866 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 867 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 868 | char *format, |
| 869 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 870 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 871 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 872 | char *path1, *path2; |
| 873 | int res; |
| 874 | if (!PyArg_ParseTuple(args, format, |
| 875 | PyUnicode_FSConverter, &opath1, |
| 876 | PyUnicode_FSConverter, &opath2)) { |
| 877 | return NULL; |
| 878 | } |
| 879 | path1 = PyBytes_AsString(opath1); |
| 880 | path2 = PyBytes_AsString(opath2); |
| 881 | Py_BEGIN_ALLOW_THREADS |
| 882 | res = (*func)(path1, path2); |
| 883 | Py_END_ALLOW_THREADS |
| 884 | Py_DECREF(opath1); |
| 885 | Py_DECREF(opath2); |
| 886 | if (res != 0) |
| 887 | /* XXX how to report both path1 and path2??? */ |
| 888 | return posix_error(); |
| 889 | Py_INCREF(Py_None); |
| 890 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 893 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 894 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 895 | win32_1str(PyObject* args, char* func, |
| 896 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 897 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 898 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 899 | PyObject *uni; |
| 900 | char *ansi; |
| 901 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 902 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 903 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 904 | PyErr_Clear(); |
| 905 | else { |
| 906 | Py_BEGIN_ALLOW_THREADS |
| 907 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 908 | Py_END_ALLOW_THREADS |
| 909 | if (!result) |
| 910 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 911 | Py_INCREF(Py_None); |
| 912 | return Py_None; |
| 913 | } |
| 914 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 915 | return NULL; |
| 916 | Py_BEGIN_ALLOW_THREADS |
| 917 | result = funcA(ansi); |
| 918 | Py_END_ALLOW_THREADS |
| 919 | if (!result) |
| 920 | return win32_error(func, ansi); |
| 921 | Py_INCREF(Py_None); |
| 922 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 923 | |
| 924 | } |
| 925 | |
| 926 | /* This is a reimplementation of the C library's chdir function, |
| 927 | but one that produces Win32 errors instead of DOS error codes. |
| 928 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 929 | it also needs to set "magic" environment variables indicating |
| 930 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 931 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 932 | win32_chdir(LPCSTR path) |
| 933 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 934 | char new_path[MAX_PATH+1]; |
| 935 | int result; |
| 936 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 937 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 938 | if(!SetCurrentDirectoryA(path)) |
| 939 | return FALSE; |
| 940 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 941 | if (!result) |
| 942 | return FALSE; |
| 943 | /* In the ANSI API, there should not be any paths longer |
| 944 | than MAX_PATH. */ |
| 945 | assert(result <= MAX_PATH+1); |
| 946 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 947 | strncmp(new_path, "//", 2) == 0) |
| 948 | /* UNC path, nothing to do. */ |
| 949 | return TRUE; |
| 950 | env[1] = new_path[0]; |
| 951 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* The Unicode version differs from the ANSI version |
| 955 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 956 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 957 | win32_wchdir(LPCWSTR path) |
| 958 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 959 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 960 | int result; |
| 961 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 962 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 963 | if(!SetCurrentDirectoryW(path)) |
| 964 | return FALSE; |
| 965 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 966 | if (!result) |
| 967 | return FALSE; |
| 968 | if (result > MAX_PATH+1) { |
| 969 | new_path = malloc(result * sizeof(wchar_t)); |
| 970 | if (!new_path) { |
| 971 | SetLastError(ERROR_OUTOFMEMORY); |
| 972 | return FALSE; |
| 973 | } |
| 974 | result = GetCurrentDirectoryW(result, new_path); |
| 975 | if (!result) { |
| 976 | free(new_path); |
| 977 | return FALSE; |
| 978 | } |
| 979 | } |
| 980 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 981 | wcsncmp(new_path, L"//", 2) == 0) |
| 982 | /* UNC path, nothing to do. */ |
| 983 | return TRUE; |
| 984 | env[1] = new_path[0]; |
| 985 | result = SetEnvironmentVariableW(env, new_path); |
| 986 | if (new_path != _new_path) |
| 987 | free(new_path); |
| 988 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 989 | } |
| 990 | #endif |
| 991 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 992 | #ifdef MS_WINDOWS |
| 993 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 994 | - time stamps are restricted to second resolution |
| 995 | - file modification times suffer from forth-and-back conversions between |
| 996 | UTC and local time |
| 997 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 998 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 999 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1000 | |
| 1001 | struct win32_stat{ |
| 1002 | int st_dev; |
| 1003 | __int64 st_ino; |
| 1004 | unsigned short st_mode; |
| 1005 | int st_nlink; |
| 1006 | int st_uid; |
| 1007 | int st_gid; |
| 1008 | int st_rdev; |
| 1009 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1010 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1011 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1012 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1013 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1014 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1015 | int st_ctime_nsec; |
| 1016 | }; |
| 1017 | |
| 1018 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1019 | |
| 1020 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1021 | 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] | 1022 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1023 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1024 | /* Cannot simply cast and dereference in_ptr, |
| 1025 | since it might not be aligned properly */ |
| 1026 | __int64 in; |
| 1027 | memcpy(&in, in_ptr, sizeof(in)); |
| 1028 | *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] | 1029 | *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] | 1030 | } |
| 1031 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1032 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1033 | 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] | 1034 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1035 | /* XXX endianness */ |
| 1036 | __int64 out; |
| 1037 | out = time_in + secs_between_epochs; |
| 1038 | out = out * 10000000 + nsec_in / 100; |
| 1039 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1042 | /* Below, we *know* that ugo+r is 0444 */ |
| 1043 | #if _S_IREAD != 0400 |
| 1044 | #error Unsupported C library |
| 1045 | #endif |
| 1046 | static int |
| 1047 | attributes_to_mode(DWORD attr) |
| 1048 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1049 | int m = 0; |
| 1050 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1051 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1052 | else |
| 1053 | m |= _S_IFREG; |
| 1054 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1055 | m |= 0444; |
| 1056 | else |
| 1057 | m |= 0666; |
| 1058 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1062 | 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] | 1063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1064 | memset(result, 0, sizeof(*result)); |
| 1065 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1066 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1067 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1068 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1069 | 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] | 1070 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1071 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1072 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1073 | /* first clear the S_IFMT bits */ |
| 1074 | result->st_mode ^= (result->st_mode & 0170000); |
| 1075 | /* now set the bits that make this a symlink */ |
| 1076 | result->st_mode |= 0120000; |
| 1077 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1078 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1079 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1082 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1083 | 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] | 1084 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1085 | HANDLE hFindFile; |
| 1086 | WIN32_FIND_DATAA FileData; |
| 1087 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1088 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1089 | return FALSE; |
| 1090 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1091 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1092 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1093 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1094 | info->ftCreationTime = FileData.ftCreationTime; |
| 1095 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1096 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1097 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1098 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1099 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1100 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1101 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1102 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1106 | 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] | 1107 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1108 | HANDLE hFindFile; |
| 1109 | WIN32_FIND_DATAW FileData; |
| 1110 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1111 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1112 | return FALSE; |
| 1113 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1114 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1115 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1116 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1117 | info->ftCreationTime = FileData.ftCreationTime; |
| 1118 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1119 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1120 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1121 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1122 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1123 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1124 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1125 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1128 | #ifndef SYMLOOP_MAX |
| 1129 | #define SYMLOOP_MAX ( 88 ) |
| 1130 | #endif |
| 1131 | |
| 1132 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1133 | 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] | 1134 | |
| 1135 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1136 | 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] | 1137 | { |
| 1138 | int code; |
| 1139 | HANDLE hFile; |
| 1140 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1141 | ULONG reparse_tag = 0; |
Hirokazu Yamamoto | 7467351 | 2010-12-05 02:48:08 +0000 | [diff] [blame] | 1142 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1143 | const char *dot; |
| 1144 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1145 | if (depth > SYMLOOP_MAX) { |
| 1146 | SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */ |
| 1147 | return -1; |
| 1148 | } |
| 1149 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1150 | hFile = CreateFileA( |
| 1151 | path, |
| 1152 | 0, /* desired access */ |
| 1153 | 0, /* share mode */ |
| 1154 | NULL, /* security attributes */ |
| 1155 | OPEN_EXISTING, |
| 1156 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 1157 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT, |
| 1158 | NULL); |
| 1159 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1160 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1161 | /* Either the target doesn't exist, or we don't have access to |
| 1162 | get a handle to it. If the former, we need to return an error. |
| 1163 | If the latter, we can use attributes_from_dir. */ |
| 1164 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1165 | return -1; |
| 1166 | /* Could not get attributes on open file. Fall back to |
| 1167 | reading the directory. */ |
| 1168 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1169 | /* Very strange. This should not fail now */ |
| 1170 | return -1; |
| 1171 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1172 | if (traverse) { |
| 1173 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1174 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1175 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1176 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1177 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1178 | } else { |
| 1179 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1180 | CloseHandle(hFile); |
| 1181 | return -1;; |
| 1182 | } |
| 1183 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1184 | code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL); |
| 1185 | CloseHandle(hFile); |
| 1186 | if (code < 0) |
| 1187 | return code; |
| 1188 | if (traverse) { |
| 1189 | code = win32_xstat_impl_w(target_path, result, traverse, depth + 1); |
| 1190 | free(target_path); |
| 1191 | return code; |
| 1192 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1193 | } else |
| 1194 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1195 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1196 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1197 | |
| 1198 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1199 | dot = strrchr(path, '.'); |
| 1200 | if (dot) { |
| 1201 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1202 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1203 | result->st_mode |= 0111; |
| 1204 | } |
| 1205 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1209 | 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] | 1210 | { |
| 1211 | int code; |
| 1212 | HANDLE hFile; |
| 1213 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1214 | ULONG reparse_tag = 0; |
| 1215 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1216 | const wchar_t *dot; |
| 1217 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1218 | if (depth > SYMLOOP_MAX) { |
| 1219 | SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */ |
| 1220 | return -1; |
| 1221 | } |
| 1222 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1223 | hFile = CreateFileW( |
| 1224 | path, |
| 1225 | 0, /* desired access */ |
| 1226 | 0, /* share mode */ |
| 1227 | NULL, /* security attributes */ |
| 1228 | OPEN_EXISTING, |
| 1229 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 1230 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT, |
| 1231 | NULL); |
| 1232 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1233 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1234 | /* Either the target doesn't exist, or we don't have access to |
| 1235 | get a handle to it. If the former, we need to return an error. |
| 1236 | If the latter, we can use attributes_from_dir. */ |
| 1237 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1238 | return -1; |
| 1239 | /* Could not get attributes on open file. Fall back to |
| 1240 | reading the directory. */ |
| 1241 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1242 | /* Very strange. This should not fail now */ |
| 1243 | return -1; |
| 1244 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1245 | if (traverse) { |
| 1246 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1247 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1248 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1249 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1250 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1251 | } else { |
| 1252 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1253 | CloseHandle(hFile); |
| 1254 | return -1;; |
| 1255 | } |
| 1256 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1257 | code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL); |
| 1258 | CloseHandle(hFile); |
| 1259 | if (code < 0) |
| 1260 | return code; |
| 1261 | if (traverse) { |
| 1262 | code = win32_xstat_impl_w(target_path, result, traverse, depth + 1); |
| 1263 | free(target_path); |
| 1264 | return code; |
| 1265 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1266 | } else |
| 1267 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1268 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1269 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1270 | |
| 1271 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1272 | dot = wcsrchr(path, '.'); |
| 1273 | if (dot) { |
| 1274 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1275 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1276 | result->st_mode |= 0111; |
| 1277 | } |
| 1278 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1282 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1283 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1284 | /* Protocol violation: we explicitly clear errno, instead of |
| 1285 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1286 | int code = win32_xstat_impl(path, result, traverse, 0); |
| 1287 | errno = 0; |
| 1288 | return code; |
| 1289 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1290 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1291 | static int |
| 1292 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1293 | { |
| 1294 | /* Protocol violation: we explicitly clear errno, instead of |
| 1295 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1296 | int code = win32_xstat_impl_w(path, result, traverse, 0); |
| 1297 | errno = 0; |
| 1298 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1301 | /* About the following functions: win32_lstat, win32_lstat_w, win32_stat, |
| 1302 | win32_stat_w |
| 1303 | |
| 1304 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1305 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1306 | default does not traverse symlinks and instead returns attributes for |
| 1307 | the symlink. |
| 1308 | |
| 1309 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1310 | win32_stat will first explicitly resolve the symlink target and then will |
| 1311 | call win32_lstat on that result. |
| 1312 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1313 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1314 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1315 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1316 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1317 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1318 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1321 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1322 | 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] | 1323 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1324 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | static int |
| 1328 | win32_stat(const char* path, struct win32_stat *result) |
| 1329 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1330 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1333 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1334 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1335 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1336 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | static int |
| 1340 | win32_fstat(int file_number, struct win32_stat *result) |
| 1341 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1342 | BY_HANDLE_FILE_INFORMATION info; |
| 1343 | HANDLE h; |
| 1344 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1345 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1346 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1347 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1348 | /* Protocol violation: we explicitly clear errno, instead of |
| 1349 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1350 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1351 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1352 | if (h == INVALID_HANDLE_VALUE) { |
| 1353 | /* This is really a C library error (invalid file handle). |
| 1354 | We set the Win32 error to the closes one matching. */ |
| 1355 | SetLastError(ERROR_INVALID_HANDLE); |
| 1356 | return -1; |
| 1357 | } |
| 1358 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1359 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1360 | type = GetFileType(h); |
| 1361 | if (type == FILE_TYPE_UNKNOWN) { |
| 1362 | DWORD error = GetLastError(); |
| 1363 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1364 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1365 | } |
| 1366 | /* else: valid but unknown file */ |
| 1367 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1368 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1369 | if (type != FILE_TYPE_DISK) { |
| 1370 | if (type == FILE_TYPE_CHAR) |
| 1371 | result->st_mode = _S_IFCHR; |
| 1372 | else if (type == FILE_TYPE_PIPE) |
| 1373 | result->st_mode = _S_IFIFO; |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
| 1377 | if (!GetFileInformationByHandle(h, &info)) { |
| 1378 | return -1; |
| 1379 | } |
| 1380 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1381 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1382 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1383 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1384 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | #endif /* MS_WINDOWS */ |
| 1388 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1389 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1390 | "stat_result: Result from stat or lstat.\n\n\ |
| 1391 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1392 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1393 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1394 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1395 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1396 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1397 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1398 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1399 | |
| 1400 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1401 | {"st_mode", "protection bits"}, |
| 1402 | {"st_ino", "inode"}, |
| 1403 | {"st_dev", "device"}, |
| 1404 | {"st_nlink", "number of hard links"}, |
| 1405 | {"st_uid", "user ID of owner"}, |
| 1406 | {"st_gid", "group ID of owner"}, |
| 1407 | {"st_size", "total size, in bytes"}, |
| 1408 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1409 | {NULL, "integer time of last access"}, |
| 1410 | {NULL, "integer time of last modification"}, |
| 1411 | {NULL, "integer time of last change"}, |
| 1412 | {"st_atime", "time of last access"}, |
| 1413 | {"st_mtime", "time of last modification"}, |
| 1414 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1415 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1416 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1417 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1418 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1419 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1420 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1421 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1422 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1423 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1424 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1425 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1426 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1427 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1428 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1429 | #endif |
| 1430 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1431 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1432 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1433 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1434 | }; |
| 1435 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1436 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1437 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1438 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1439 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1440 | #endif |
| 1441 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1442 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1443 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1444 | #else |
| 1445 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1446 | #endif |
| 1447 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1448 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1449 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1450 | #else |
| 1451 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1452 | #endif |
| 1453 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1454 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1455 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1456 | #else |
| 1457 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1458 | #endif |
| 1459 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1460 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1461 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1462 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1463 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1464 | #endif |
| 1465 | |
| 1466 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1467 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1468 | #else |
| 1469 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1470 | #endif |
| 1471 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1472 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1473 | "stat_result", /* name */ |
| 1474 | stat_result__doc__, /* doc */ |
| 1475 | stat_result_fields, |
| 1476 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1477 | }; |
| 1478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1479 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1480 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1481 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1482 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1483 | 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] | 1484 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1485 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1486 | |
| 1487 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1488 | {"f_bsize", }, |
| 1489 | {"f_frsize", }, |
| 1490 | {"f_blocks", }, |
| 1491 | {"f_bfree", }, |
| 1492 | {"f_bavail", }, |
| 1493 | {"f_files", }, |
| 1494 | {"f_ffree", }, |
| 1495 | {"f_favail", }, |
| 1496 | {"f_flag", }, |
| 1497 | {"f_namemax",}, |
| 1498 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1499 | }; |
| 1500 | |
| 1501 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1502 | "statvfs_result", /* name */ |
| 1503 | statvfs_result__doc__, /* doc */ |
| 1504 | statvfs_result_fields, |
| 1505 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1506 | }; |
| 1507 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1508 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1509 | PyDoc_STRVAR(waitid_result__doc__, |
| 1510 | "waitid_result: Result from waitid.\n\n\ |
| 1511 | This object may be accessed either as a tuple of\n\ |
| 1512 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1513 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1514 | \n\ |
| 1515 | See os.waitid for more information."); |
| 1516 | |
| 1517 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1518 | {"si_pid", }, |
| 1519 | {"si_uid", }, |
| 1520 | {"si_signo", }, |
| 1521 | {"si_status", }, |
| 1522 | {"si_code", }, |
| 1523 | {0} |
| 1524 | }; |
| 1525 | |
| 1526 | static PyStructSequence_Desc waitid_result_desc = { |
| 1527 | "waitid_result", /* name */ |
| 1528 | waitid_result__doc__, /* doc */ |
| 1529 | waitid_result_fields, |
| 1530 | 5 |
| 1531 | }; |
| 1532 | static PyTypeObject WaitidResultType; |
| 1533 | #endif |
| 1534 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1535 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1536 | static PyTypeObject StatResultType; |
| 1537 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1538 | static newfunc structseq_new; |
| 1539 | |
| 1540 | static PyObject * |
| 1541 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1542 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1543 | PyStructSequence *result; |
| 1544 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1545 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1546 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1547 | if (!result) |
| 1548 | return NULL; |
| 1549 | /* If we have been initialized from a tuple, |
| 1550 | st_?time might be set to None. Initialize it |
| 1551 | from the int slots. */ |
| 1552 | for (i = 7; i <= 9; i++) { |
| 1553 | if (result->ob_item[i+3] == Py_None) { |
| 1554 | Py_DECREF(Py_None); |
| 1555 | Py_INCREF(result->ob_item[i]); |
| 1556 | result->ob_item[i+3] = result->ob_item[i]; |
| 1557 | } |
| 1558 | } |
| 1559 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1565 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1566 | |
| 1567 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1568 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1569 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1570 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1571 | future calls return ints. \n\ |
| 1572 | If newval is omitted, return the current setting.\n"); |
| 1573 | |
| 1574 | static PyObject* |
| 1575 | stat_float_times(PyObject* self, PyObject *args) |
| 1576 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1577 | int newval = -1; |
| 1578 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1579 | return NULL; |
| 1580 | if (newval == -1) |
| 1581 | /* Return old value */ |
| 1582 | return PyBool_FromLong(_stat_float_times); |
| 1583 | _stat_float_times = newval; |
| 1584 | Py_INCREF(Py_None); |
| 1585 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1586 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1587 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1588 | static void |
| 1589 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1590 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1591 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1592 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1593 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1594 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1595 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1596 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1597 | if (!ival) |
| 1598 | return; |
| 1599 | if (_stat_float_times) { |
| 1600 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1601 | } else { |
| 1602 | fval = ival; |
| 1603 | Py_INCREF(fval); |
| 1604 | } |
| 1605 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1606 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1609 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1610 | (used by posix_stat() and posix_fstat()) */ |
| 1611 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1612 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1613 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1614 | unsigned long ansec, mnsec, cnsec; |
| 1615 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1616 | if (v == NULL) |
| 1617 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1618 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1619 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1620 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1621 | PyStructSequence_SET_ITEM(v, 1, |
| 1622 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1623 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1624 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1625 | #endif |
| 1626 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1627 | PyStructSequence_SET_ITEM(v, 2, |
| 1628 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1629 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1630 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1631 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1632 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1633 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1634 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1635 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1636 | PyStructSequence_SET_ITEM(v, 6, |
| 1637 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1638 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1639 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1640 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1641 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1642 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1643 | ansec = st->st_atim.tv_nsec; |
| 1644 | mnsec = st->st_mtim.tv_nsec; |
| 1645 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1646 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1647 | ansec = st->st_atimespec.tv_nsec; |
| 1648 | mnsec = st->st_mtimespec.tv_nsec; |
| 1649 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1650 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1651 | ansec = st->st_atime_nsec; |
| 1652 | mnsec = st->st_mtime_nsec; |
| 1653 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1654 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1655 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1656 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1657 | fill_time(v, 7, st->st_atime, ansec); |
| 1658 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1659 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1660 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1661 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1662 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1663 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1664 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1665 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1666 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1667 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1668 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1669 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1670 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1671 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1672 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1673 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1674 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1675 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1676 | #endif |
| 1677 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1678 | { |
| 1679 | PyObject *val; |
| 1680 | unsigned long bsec,bnsec; |
| 1681 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1682 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1683 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1684 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1685 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1686 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1687 | if (_stat_float_times) { |
| 1688 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1689 | } else { |
| 1690 | val = PyLong_FromLong((long)bsec); |
| 1691 | } |
| 1692 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1693 | val); |
| 1694 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1695 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1696 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1697 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1698 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1699 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1700 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1701 | if (PyErr_Occurred()) { |
| 1702 | Py_DECREF(v); |
| 1703 | return NULL; |
| 1704 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1705 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1706 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1709 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1710 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1711 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1712 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1713 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1714 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1715 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1716 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1717 | char *wformat, |
| 1718 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1719 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1720 | STRUCT_STAT st; |
| 1721 | PyObject *opath; |
| 1722 | char *path; |
| 1723 | int res; |
| 1724 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1725 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1726 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1727 | PyUnicodeObject *po; |
| 1728 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1729 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1730 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1731 | Py_BEGIN_ALLOW_THREADS |
| 1732 | /* PyUnicode_AS_UNICODE result OK without |
| 1733 | thread lock as it is a simple dereference. */ |
| 1734 | res = wstatfunc(wpath, &st); |
| 1735 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1736 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1737 | if (res != 0) |
| 1738 | return win32_error_unicode("stat", wpath); |
| 1739 | return _pystat_fromstructstat(&st); |
| 1740 | } |
| 1741 | /* Drop the argument parsing error as narrow strings |
| 1742 | are also valid. */ |
| 1743 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1744 | #endif |
| 1745 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1746 | if (!PyArg_ParseTuple(args, format, |
| 1747 | PyUnicode_FSConverter, &opath)) |
| 1748 | return NULL; |
| 1749 | path = PyBytes_AsString(opath); |
| 1750 | Py_BEGIN_ALLOW_THREADS |
| 1751 | res = (*statfunc)(path, &st); |
| 1752 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1753 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1754 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1755 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1756 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1757 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1759 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1760 | } |
| 1761 | else |
| 1762 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1763 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1764 | Py_DECREF(opath); |
| 1765 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1768 | /* POSIX methods */ |
| 1769 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1770 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1771 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1772 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1773 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1774 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1775 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1776 | 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] | 1777 | |
| 1778 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1779 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1780 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1781 | PyObject *opath; |
| 1782 | char *path; |
| 1783 | int mode; |
| 1784 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1785 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1786 | DWORD attr; |
| 1787 | PyUnicodeObject *po; |
| 1788 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1789 | Py_BEGIN_ALLOW_THREADS |
| 1790 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1791 | it is a simple dereference. */ |
| 1792 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1793 | Py_END_ALLOW_THREADS |
| 1794 | goto finish; |
| 1795 | } |
| 1796 | /* Drop the argument parsing error as narrow strings |
| 1797 | are also valid. */ |
| 1798 | PyErr_Clear(); |
| 1799 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1800 | PyUnicode_FSConverter, &opath, &mode)) |
| 1801 | return NULL; |
| 1802 | path = PyBytes_AsString(opath); |
| 1803 | Py_BEGIN_ALLOW_THREADS |
| 1804 | attr = GetFileAttributesA(path); |
| 1805 | Py_END_ALLOW_THREADS |
| 1806 | Py_DECREF(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1807 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1808 | if (attr == 0xFFFFFFFF) |
| 1809 | /* File does not exist, or cannot read attributes */ |
| 1810 | return PyBool_FromLong(0); |
| 1811 | /* Access is possible if either write access wasn't requested, or |
| 1812 | the file isn't read-only, or if it's a directory, as there are |
| 1813 | no read-only directories on Windows. */ |
| 1814 | return PyBool_FromLong(!(mode & 2) |
| 1815 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1816 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1817 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1818 | int res; |
| 1819 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1820 | PyUnicode_FSConverter, &opath, &mode)) |
| 1821 | return NULL; |
| 1822 | path = PyBytes_AsString(opath); |
| 1823 | Py_BEGIN_ALLOW_THREADS |
| 1824 | res = access(path, mode); |
| 1825 | Py_END_ALLOW_THREADS |
| 1826 | Py_DECREF(opath); |
| 1827 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1828 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1831 | #ifndef F_OK |
| 1832 | #define F_OK 0 |
| 1833 | #endif |
| 1834 | #ifndef R_OK |
| 1835 | #define R_OK 4 |
| 1836 | #endif |
| 1837 | #ifndef W_OK |
| 1838 | #define W_OK 2 |
| 1839 | #endif |
| 1840 | #ifndef X_OK |
| 1841 | #define X_OK 1 |
| 1842 | #endif |
| 1843 | |
| 1844 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1845 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1846 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1847 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1848 | |
| 1849 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1850 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1851 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1852 | int id; |
| 1853 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1854 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1855 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1856 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1857 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1858 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1859 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1860 | if (id == 0) { |
| 1861 | ret = ttyname(); |
| 1862 | } |
| 1863 | else { |
| 1864 | ret = NULL; |
| 1865 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1866 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1867 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1868 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1869 | if (ret == NULL) |
| 1870 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1871 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1872 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1873 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1874 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1875 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1876 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1877 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1878 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1879 | |
| 1880 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1881 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1882 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1883 | char *ret; |
| 1884 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1885 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1886 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1887 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1888 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1889 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1890 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1891 | if (ret == NULL) |
| 1892 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1893 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1894 | } |
| 1895 | #endif |
| 1896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1897 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1898 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1899 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1900 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1901 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1902 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1903 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1904 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1905 | 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] | 1906 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1907 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1908 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1909 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1910 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1911 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1912 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1915 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1916 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1917 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1918 | 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] | 1919 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1920 | |
| 1921 | static PyObject * |
| 1922 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1923 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1924 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1925 | } |
| 1926 | #endif /* HAVE_FCHDIR */ |
| 1927 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1929 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1930 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1931 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1934 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1935 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1936 | PyObject *opath = NULL; |
| 1937 | char *path = NULL; |
| 1938 | int i; |
| 1939 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1940 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1941 | DWORD attr; |
| 1942 | PyUnicodeObject *po; |
| 1943 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1944 | Py_BEGIN_ALLOW_THREADS |
| 1945 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1946 | if (attr != 0xFFFFFFFF) { |
| 1947 | if (i & _S_IWRITE) |
| 1948 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1949 | else |
| 1950 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1951 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1952 | } |
| 1953 | else |
| 1954 | res = 0; |
| 1955 | Py_END_ALLOW_THREADS |
| 1956 | if (!res) |
| 1957 | return win32_error_unicode("chmod", |
| 1958 | PyUnicode_AS_UNICODE(po)); |
| 1959 | Py_INCREF(Py_None); |
| 1960 | return Py_None; |
| 1961 | } |
| 1962 | /* Drop the argument parsing error as narrow strings |
| 1963 | are also valid. */ |
| 1964 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1965 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1966 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1967 | &opath, &i)) |
| 1968 | return NULL; |
| 1969 | path = PyBytes_AsString(opath); |
| 1970 | Py_BEGIN_ALLOW_THREADS |
| 1971 | attr = GetFileAttributesA(path); |
| 1972 | if (attr != 0xFFFFFFFF) { |
| 1973 | if (i & _S_IWRITE) |
| 1974 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1975 | else |
| 1976 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1977 | res = SetFileAttributesA(path, attr); |
| 1978 | } |
| 1979 | else |
| 1980 | res = 0; |
| 1981 | Py_END_ALLOW_THREADS |
| 1982 | if (!res) { |
| 1983 | win32_error("chmod", path); |
| 1984 | Py_DECREF(opath); |
| 1985 | return NULL; |
| 1986 | } |
| 1987 | Py_DECREF(opath); |
| 1988 | Py_INCREF(Py_None); |
| 1989 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1990 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1991 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1992 | &opath, &i)) |
| 1993 | return NULL; |
| 1994 | path = PyBytes_AsString(opath); |
| 1995 | Py_BEGIN_ALLOW_THREADS |
| 1996 | res = chmod(path, i); |
| 1997 | Py_END_ALLOW_THREADS |
| 1998 | if (res < 0) |
| 1999 | return posix_error_with_allocated_filename(opath); |
| 2000 | Py_DECREF(opath); |
| 2001 | Py_INCREF(Py_None); |
| 2002 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2003 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2006 | #ifdef HAVE_FCHMOD |
| 2007 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2008 | "fchmod(fd, mode)\n\n\ |
| 2009 | Change the access permissions of the file given by file\n\ |
| 2010 | descriptor fd."); |
| 2011 | |
| 2012 | static PyObject * |
| 2013 | posix_fchmod(PyObject *self, PyObject *args) |
| 2014 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2015 | int fd, mode, res; |
| 2016 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2017 | return NULL; |
| 2018 | Py_BEGIN_ALLOW_THREADS |
| 2019 | res = fchmod(fd, mode); |
| 2020 | Py_END_ALLOW_THREADS |
| 2021 | if (res < 0) |
| 2022 | return posix_error(); |
| 2023 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2024 | } |
| 2025 | #endif /* HAVE_FCHMOD */ |
| 2026 | |
| 2027 | #ifdef HAVE_LCHMOD |
| 2028 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2029 | "lchmod(path, mode)\n\n\ |
| 2030 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2031 | affects the link itself rather than the target."); |
| 2032 | |
| 2033 | static PyObject * |
| 2034 | posix_lchmod(PyObject *self, PyObject *args) |
| 2035 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2036 | PyObject *opath; |
| 2037 | char *path; |
| 2038 | int i; |
| 2039 | int res; |
| 2040 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2041 | &opath, &i)) |
| 2042 | return NULL; |
| 2043 | path = PyBytes_AsString(opath); |
| 2044 | Py_BEGIN_ALLOW_THREADS |
| 2045 | res = lchmod(path, i); |
| 2046 | Py_END_ALLOW_THREADS |
| 2047 | if (res < 0) |
| 2048 | return posix_error_with_allocated_filename(opath); |
| 2049 | Py_DECREF(opath); |
| 2050 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2051 | } |
| 2052 | #endif /* HAVE_LCHMOD */ |
| 2053 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2054 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2055 | #ifdef HAVE_CHFLAGS |
| 2056 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2057 | "chflags(path, flags)\n\n\ |
| 2058 | Set file flags."); |
| 2059 | |
| 2060 | static PyObject * |
| 2061 | posix_chflags(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:chflags", |
| 2068 | PyUnicode_FSConverter, &opath, &flags)) |
| 2069 | return NULL; |
| 2070 | path = PyBytes_AsString(opath); |
| 2071 | Py_BEGIN_ALLOW_THREADS |
| 2072 | res = chflags(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_CHFLAGS */ |
| 2081 | |
| 2082 | #ifdef HAVE_LCHFLAGS |
| 2083 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2084 | "lchflags(path, flags)\n\n\ |
| 2085 | Set file flags.\n\ |
| 2086 | This function will not follow symbolic links."); |
| 2087 | |
| 2088 | static PyObject * |
| 2089 | posix_lchflags(PyObject *self, PyObject *args) |
| 2090 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2091 | PyObject *opath; |
| 2092 | char *path; |
| 2093 | unsigned long flags; |
| 2094 | int res; |
| 2095 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2096 | PyUnicode_FSConverter, &opath, &flags)) |
| 2097 | return NULL; |
| 2098 | path = PyBytes_AsString(opath); |
| 2099 | Py_BEGIN_ALLOW_THREADS |
| 2100 | res = lchflags(path, flags); |
| 2101 | Py_END_ALLOW_THREADS |
| 2102 | if (res < 0) |
| 2103 | return posix_error_with_allocated_filename(opath); |
| 2104 | Py_DECREF(opath); |
| 2105 | Py_INCREF(Py_None); |
| 2106 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2107 | } |
| 2108 | #endif /* HAVE_LCHFLAGS */ |
| 2109 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2110 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2111 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2112 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2113 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2114 | |
| 2115 | static PyObject * |
| 2116 | posix_chroot(PyObject *self, PyObject *args) |
| 2117 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2118 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2119 | } |
| 2120 | #endif |
| 2121 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2122 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2123 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2124 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2125 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2126 | |
| 2127 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2128 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2129 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2130 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2131 | } |
| 2132 | #endif /* HAVE_FSYNC */ |
| 2133 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2134 | #ifdef HAVE_SYNC |
| 2135 | PyDoc_STRVAR(posix_sync__doc__, |
| 2136 | "sync()\n\n\ |
| 2137 | Force write of everything to disk."); |
| 2138 | |
| 2139 | static PyObject * |
| 2140 | posix_sync(PyObject *self, PyObject *noargs) |
| 2141 | { |
| 2142 | Py_BEGIN_ALLOW_THREADS |
| 2143 | sync(); |
| 2144 | Py_END_ALLOW_THREADS |
| 2145 | Py_RETURN_NONE; |
| 2146 | } |
| 2147 | #endif |
| 2148 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2149 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2150 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2151 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2152 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2153 | #endif |
| 2154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2155 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2156 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2157 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2158 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2159 | |
| 2160 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2161 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2162 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2163 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2164 | } |
| 2165 | #endif /* HAVE_FDATASYNC */ |
| 2166 | |
| 2167 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2168 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2169 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2170 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2171 | 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] | 2172 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2173 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2174 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2175 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2176 | PyObject *opath; |
| 2177 | char *path; |
| 2178 | long uid, gid; |
| 2179 | int res; |
| 2180 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2181 | PyUnicode_FSConverter, &opath, |
| 2182 | &uid, &gid)) |
| 2183 | return NULL; |
| 2184 | path = PyBytes_AsString(opath); |
| 2185 | Py_BEGIN_ALLOW_THREADS |
| 2186 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2187 | Py_END_ALLOW_THREADS |
| 2188 | if (res < 0) |
| 2189 | return posix_error_with_allocated_filename(opath); |
| 2190 | Py_DECREF(opath); |
| 2191 | Py_INCREF(Py_None); |
| 2192 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2193 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2194 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2195 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2196 | #ifdef HAVE_FCHOWN |
| 2197 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2198 | "fchown(fd, uid, gid)\n\n\ |
| 2199 | Change the owner and group id of the file given by file descriptor\n\ |
| 2200 | fd to the numeric uid and gid."); |
| 2201 | |
| 2202 | static PyObject * |
| 2203 | posix_fchown(PyObject *self, PyObject *args) |
| 2204 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2205 | int fd; |
| 2206 | long uid, gid; |
| 2207 | int res; |
| 2208 | if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) |
| 2209 | return NULL; |
| 2210 | Py_BEGIN_ALLOW_THREADS |
| 2211 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2212 | Py_END_ALLOW_THREADS |
| 2213 | if (res < 0) |
| 2214 | return posix_error(); |
| 2215 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2216 | } |
| 2217 | #endif /* HAVE_FCHOWN */ |
| 2218 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2219 | #ifdef HAVE_LCHOWN |
| 2220 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2221 | "lchown(path, uid, gid)\n\n\ |
| 2222 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2223 | This function will not follow symbolic links."); |
| 2224 | |
| 2225 | static PyObject * |
| 2226 | posix_lchown(PyObject *self, PyObject *args) |
| 2227 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2228 | PyObject *opath; |
| 2229 | char *path; |
| 2230 | long uid, gid; |
| 2231 | int res; |
| 2232 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2233 | PyUnicode_FSConverter, &opath, |
| 2234 | &uid, &gid)) |
| 2235 | return NULL; |
| 2236 | path = PyBytes_AsString(opath); |
| 2237 | Py_BEGIN_ALLOW_THREADS |
| 2238 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2239 | Py_END_ALLOW_THREADS |
| 2240 | if (res < 0) |
| 2241 | return posix_error_with_allocated_filename(opath); |
| 2242 | Py_DECREF(opath); |
| 2243 | Py_INCREF(Py_None); |
| 2244 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2245 | } |
| 2246 | #endif /* HAVE_LCHOWN */ |
| 2247 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2248 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2249 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2250 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2251 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2252 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2253 | char buf[1026]; |
| 2254 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2255 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2256 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2257 | if (!use_bytes) { |
| 2258 | wchar_t wbuf[1026]; |
| 2259 | wchar_t *wbuf2 = wbuf; |
| 2260 | PyObject *resobj; |
| 2261 | DWORD len; |
| 2262 | Py_BEGIN_ALLOW_THREADS |
| 2263 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2264 | /* If the buffer is large enough, len does not include the |
| 2265 | terminating \0. If the buffer is too small, len includes |
| 2266 | the space needed for the terminator. */ |
| 2267 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2268 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2269 | if (wbuf2) |
| 2270 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2271 | } |
| 2272 | Py_END_ALLOW_THREADS |
| 2273 | if (!wbuf2) { |
| 2274 | PyErr_NoMemory(); |
| 2275 | return NULL; |
| 2276 | } |
| 2277 | if (!len) { |
| 2278 | if (wbuf2 != wbuf) free(wbuf2); |
| 2279 | return win32_error("getcwdu", NULL); |
| 2280 | } |
| 2281 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2282 | if (wbuf2 != wbuf) free(wbuf2); |
| 2283 | return resobj; |
| 2284 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2285 | #endif |
| 2286 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2287 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2288 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2289 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2290 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2291 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2292 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2293 | Py_END_ALLOW_THREADS |
| 2294 | if (res == NULL) |
| 2295 | return posix_error(); |
| 2296 | if (use_bytes) |
| 2297 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2298 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2299 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2300 | |
| 2301 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2302 | "getcwd() -> path\n\n\ |
| 2303 | Return a unicode string representing the current working directory."); |
| 2304 | |
| 2305 | static PyObject * |
| 2306 | posix_getcwd_unicode(PyObject *self) |
| 2307 | { |
| 2308 | return posix_getcwd(0); |
| 2309 | } |
| 2310 | |
| 2311 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2312 | "getcwdb() -> path\n\n\ |
| 2313 | Return a bytes string representing the current working directory."); |
| 2314 | |
| 2315 | static PyObject * |
| 2316 | posix_getcwd_bytes(PyObject *self) |
| 2317 | { |
| 2318 | return posix_getcwd(1); |
| 2319 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2320 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2321 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2322 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2323 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2324 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2325 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2326 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2327 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2328 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2329 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2330 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2331 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2332 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2333 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2334 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2335 | #ifdef MS_WINDOWS |
| 2336 | PyDoc_STRVAR(win32_link__doc__, |
| 2337 | "link(src, dst)\n\n\ |
| 2338 | Create a hard link to a file."); |
| 2339 | |
| 2340 | static PyObject * |
| 2341 | win32_link(PyObject *self, PyObject *args) |
| 2342 | { |
| 2343 | PyObject *osrc, *odst; |
| 2344 | char *src, *dst; |
| 2345 | BOOL rslt; |
| 2346 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2347 | PyUnicodeObject *usrc, *udst; |
| 2348 | if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) { |
| 2349 | Py_BEGIN_ALLOW_THREADS |
| 2350 | rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst), |
| 2351 | PyUnicode_AS_UNICODE(usrc), NULL); |
| 2352 | Py_END_ALLOW_THREADS |
| 2353 | |
| 2354 | if (rslt == 0) |
| 2355 | return win32_error("link", NULL); |
| 2356 | |
| 2357 | Py_RETURN_NONE; |
| 2358 | } |
| 2359 | |
| 2360 | /* Narrow strings also valid. */ |
| 2361 | PyErr_Clear(); |
| 2362 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2363 | if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, |
| 2364 | PyUnicode_FSConverter, &odst)) |
| 2365 | return NULL; |
| 2366 | |
| 2367 | src = PyBytes_AsString(osrc); |
| 2368 | dst = PyBytes_AsString(odst); |
| 2369 | |
| 2370 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2371 | rslt = CreateHardLinkA(dst, src, NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2372 | Py_END_ALLOW_THREADS |
| 2373 | |
Stefan Krah | 30b341f | 2010-11-27 11:44:18 +0000 | [diff] [blame] | 2374 | Py_DECREF(osrc); |
| 2375 | Py_DECREF(odst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2376 | if (rslt == 0) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2377 | return win32_error("link", NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2378 | |
| 2379 | Py_RETURN_NONE; |
| 2380 | } |
| 2381 | #endif /* MS_WINDOWS */ |
| 2382 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2384 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2385 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2386 | Return a list containing the names of the entries in the directory.\n\ |
| 2387 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2388 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2389 | \n\ |
| 2390 | 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] | 2391 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2392 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2393 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2394 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2395 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2396 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2397 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2398 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2399 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2400 | PyObject *d, *v; |
| 2401 | HANDLE hFindFile; |
| 2402 | BOOL result; |
| 2403 | WIN32_FIND_DATA FileData; |
| 2404 | PyObject *opath; |
| 2405 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2406 | char *bufptr = namebuf; |
| 2407 | 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] | 2408 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2409 | PyObject *po = NULL; |
| 2410 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2411 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2412 | Py_UNICODE *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2413 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2414 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2415 | po_wchars = L"."; |
| 2416 | len = 1; |
| 2417 | } else { |
| 2418 | po_wchars = PyUnicode_AS_UNICODE(po); |
| 2419 | len = PyUnicode_GET_SIZE(po); |
| 2420 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2421 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2422 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2423 | if (!wnamebuf) { |
| 2424 | PyErr_NoMemory(); |
| 2425 | return NULL; |
| 2426 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2427 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2428 | if (len > 0) { |
| 2429 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2430 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2431 | wnamebuf[len++] = L'\\'; |
| 2432 | wcscpy(wnamebuf + len, L"*.*"); |
| 2433 | } |
| 2434 | if ((d = PyList_New(0)) == NULL) { |
| 2435 | free(wnamebuf); |
| 2436 | return NULL; |
| 2437 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2438 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2439 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2440 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2441 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2442 | int error = GetLastError(); |
| 2443 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2444 | free(wnamebuf); |
| 2445 | return d; |
| 2446 | } |
| 2447 | Py_DECREF(d); |
| 2448 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2449 | free(wnamebuf); |
| 2450 | return NULL; |
| 2451 | } |
| 2452 | do { |
| 2453 | /* Skip over . and .. */ |
| 2454 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2455 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2456 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2457 | if (v == NULL) { |
| 2458 | Py_DECREF(d); |
| 2459 | d = NULL; |
| 2460 | break; |
| 2461 | } |
| 2462 | if (PyList_Append(d, v) != 0) { |
| 2463 | Py_DECREF(v); |
| 2464 | Py_DECREF(d); |
| 2465 | d = NULL; |
| 2466 | break; |
| 2467 | } |
| 2468 | Py_DECREF(v); |
| 2469 | } |
| 2470 | Py_BEGIN_ALLOW_THREADS |
| 2471 | result = FindNextFileW(hFindFile, &wFileData); |
| 2472 | Py_END_ALLOW_THREADS |
| 2473 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2474 | it got to the end of the directory. */ |
| 2475 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2476 | Py_DECREF(d); |
| 2477 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2478 | FindClose(hFindFile); |
| 2479 | free(wnamebuf); |
| 2480 | return NULL; |
| 2481 | } |
| 2482 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2483 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2484 | if (FindClose(hFindFile) == FALSE) { |
| 2485 | Py_DECREF(d); |
| 2486 | win32_error_unicode("FindClose", wnamebuf); |
| 2487 | free(wnamebuf); |
| 2488 | return NULL; |
| 2489 | } |
| 2490 | free(wnamebuf); |
| 2491 | return d; |
| 2492 | } |
| 2493 | /* Drop the argument parsing error as narrow strings |
| 2494 | are also valid. */ |
| 2495 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2496 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2497 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2498 | PyUnicode_FSConverter, &opath)) |
| 2499 | return NULL; |
| 2500 | if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { |
| 2501 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2502 | Py_DECREF(opath); |
| 2503 | return NULL; |
| 2504 | } |
| 2505 | strcpy(namebuf, PyBytes_AsString(opath)); |
| 2506 | len = PyObject_Size(opath); |
Stefan Krah | 2a7feee | 2010-11-27 22:06:49 +0000 | [diff] [blame] | 2507 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2508 | if (len > 0) { |
| 2509 | char ch = namebuf[len-1]; |
| 2510 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2511 | namebuf[len++] = '/'; |
| 2512 | strcpy(namebuf + len, "*.*"); |
| 2513 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2514 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2515 | if ((d = PyList_New(0)) == NULL) |
| 2516 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2517 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2518 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2519 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2520 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2521 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2522 | int error = GetLastError(); |
| 2523 | if (error == ERROR_FILE_NOT_FOUND) |
| 2524 | return d; |
| 2525 | Py_DECREF(d); |
| 2526 | return win32_error("FindFirstFile", namebuf); |
| 2527 | } |
| 2528 | do { |
| 2529 | /* Skip over . and .. */ |
| 2530 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2531 | strcmp(FileData.cFileName, "..") != 0) { |
| 2532 | v = PyBytes_FromString(FileData.cFileName); |
| 2533 | if (v == NULL) { |
| 2534 | Py_DECREF(d); |
| 2535 | d = NULL; |
| 2536 | break; |
| 2537 | } |
| 2538 | if (PyList_Append(d, v) != 0) { |
| 2539 | Py_DECREF(v); |
| 2540 | Py_DECREF(d); |
| 2541 | d = NULL; |
| 2542 | break; |
| 2543 | } |
| 2544 | Py_DECREF(v); |
| 2545 | } |
| 2546 | Py_BEGIN_ALLOW_THREADS |
| 2547 | result = FindNextFile(hFindFile, &FileData); |
| 2548 | Py_END_ALLOW_THREADS |
| 2549 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2550 | it got to the end of the directory. */ |
| 2551 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2552 | Py_DECREF(d); |
| 2553 | win32_error("FindNextFile", namebuf); |
| 2554 | FindClose(hFindFile); |
| 2555 | return NULL; |
| 2556 | } |
| 2557 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2558 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2559 | if (FindClose(hFindFile) == FALSE) { |
| 2560 | Py_DECREF(d); |
| 2561 | return win32_error("FindClose", namebuf); |
| 2562 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2563 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2564 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2565 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2566 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2567 | |
| 2568 | #ifndef MAX_PATH |
| 2569 | #define MAX_PATH CCHMAXPATH |
| 2570 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2571 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2572 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2573 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2574 | PyObject *d, *v; |
| 2575 | char namebuf[MAX_PATH+5]; |
| 2576 | HDIR hdir = 1; |
| 2577 | ULONG srchcnt = 1; |
| 2578 | FILEFINDBUF3 ep; |
| 2579 | APIRET rc; |
| 2580 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2581 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2582 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2583 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2584 | name = PyBytes_AsString(oname); |
| 2585 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2586 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2587 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2588 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2589 | return NULL; |
| 2590 | } |
| 2591 | strcpy(namebuf, name); |
| 2592 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2593 | if (*pt == ALTSEP) |
| 2594 | *pt = SEP; |
| 2595 | if (namebuf[len-1] != SEP) |
| 2596 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2597 | strcpy(namebuf + len, "*.*"); |
| 2598 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2599 | if ((d = PyList_New(0)) == NULL) { |
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 NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2602 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2603 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2604 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2605 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2606 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2607 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2608 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2609 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2610 | |
| 2611 | if (rc != NO_ERROR) { |
| 2612 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2613 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2616 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2617 | do { |
| 2618 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2619 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2620 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2621 | |
| 2622 | strcpy(namebuf, ep.achName); |
| 2623 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2624 | /* Leave Case of Name Alone -- In Native Form */ |
| 2625 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2626 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2627 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2628 | if (v == NULL) { |
| 2629 | Py_DECREF(d); |
| 2630 | d = NULL; |
| 2631 | break; |
| 2632 | } |
| 2633 | if (PyList_Append(d, v) != 0) { |
| 2634 | Py_DECREF(v); |
| 2635 | Py_DECREF(d); |
| 2636 | d = NULL; |
| 2637 | break; |
| 2638 | } |
| 2639 | Py_DECREF(v); |
| 2640 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2641 | } |
| 2642 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2643 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2644 | return d; |
| 2645 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2646 | PyObject *oname; |
| 2647 | char *name; |
| 2648 | PyObject *d, *v; |
| 2649 | DIR *dirp; |
| 2650 | struct dirent *ep; |
| 2651 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2652 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2653 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2654 | /* v is never read, so it does not need to be initialized yet. */ |
| 2655 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2656 | arg_is_unicode = 0; |
| 2657 | PyErr_Clear(); |
| 2658 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2659 | oname = NULL; |
| 2660 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2661 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2662 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2663 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2664 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2665 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2666 | Py_BEGIN_ALLOW_THREADS |
| 2667 | dirp = opendir(name); |
| 2668 | Py_END_ALLOW_THREADS |
| 2669 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2670 | return posix_error_with_allocated_filename(oname); |
| 2671 | } |
| 2672 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2673 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2674 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2675 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2676 | Py_DECREF(oname); |
| 2677 | return NULL; |
| 2678 | } |
| 2679 | for (;;) { |
| 2680 | errno = 0; |
| 2681 | Py_BEGIN_ALLOW_THREADS |
| 2682 | ep = readdir(dirp); |
| 2683 | Py_END_ALLOW_THREADS |
| 2684 | if (ep == NULL) { |
| 2685 | if (errno == 0) { |
| 2686 | break; |
| 2687 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2688 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2689 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2690 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2691 | Py_DECREF(d); |
| 2692 | return posix_error_with_allocated_filename(oname); |
| 2693 | } |
| 2694 | } |
| 2695 | if (ep->d_name[0] == '.' && |
| 2696 | (NAMLEN(ep) == 1 || |
| 2697 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2698 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2699 | if (arg_is_unicode) |
| 2700 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2701 | else |
| 2702 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2703 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2704 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2705 | break; |
| 2706 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2707 | if (PyList_Append(d, v) != 0) { |
| 2708 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2709 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2710 | break; |
| 2711 | } |
| 2712 | Py_DECREF(v); |
| 2713 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2714 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2715 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2716 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2717 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2718 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2719 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2720 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2721 | #endif /* which OS */ |
| 2722 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2724 | #ifdef HAVE_FDOPENDIR |
| 2725 | PyDoc_STRVAR(posix_fdlistdir__doc__, |
| 2726 | "fdlistdir(fd) -> list_of_strings\n\n\ |
| 2727 | Like listdir(), but uses a file descriptor instead.\n\ |
| 2728 | After succesful execution of this function, fd will be closed."); |
| 2729 | |
| 2730 | static PyObject * |
| 2731 | posix_fdlistdir(PyObject *self, PyObject *args) |
| 2732 | { |
| 2733 | PyObject *d, *v; |
| 2734 | DIR *dirp; |
| 2735 | struct dirent *ep; |
| 2736 | int fd; |
| 2737 | |
| 2738 | errno = 0; |
| 2739 | if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd)) |
| 2740 | return NULL; |
| 2741 | Py_BEGIN_ALLOW_THREADS |
| 2742 | dirp = fdopendir(fd); |
| 2743 | Py_END_ALLOW_THREADS |
| 2744 | if (dirp == NULL) { |
| 2745 | close(fd); |
| 2746 | return posix_error(); |
| 2747 | } |
| 2748 | if ((d = PyList_New(0)) == NULL) { |
| 2749 | Py_BEGIN_ALLOW_THREADS |
| 2750 | closedir(dirp); |
| 2751 | Py_END_ALLOW_THREADS |
| 2752 | return NULL; |
| 2753 | } |
| 2754 | for (;;) { |
| 2755 | errno = 0; |
| 2756 | Py_BEGIN_ALLOW_THREADS |
| 2757 | ep = readdir(dirp); |
| 2758 | Py_END_ALLOW_THREADS |
| 2759 | if (ep == NULL) { |
| 2760 | if (errno == 0) { |
| 2761 | break; |
| 2762 | } else { |
| 2763 | Py_BEGIN_ALLOW_THREADS |
| 2764 | closedir(dirp); |
| 2765 | Py_END_ALLOW_THREADS |
| 2766 | Py_DECREF(d); |
| 2767 | return posix_error(); |
| 2768 | } |
| 2769 | } |
| 2770 | if (ep->d_name[0] == '.' && |
| 2771 | (NAMLEN(ep) == 1 || |
| 2772 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2773 | continue; |
| 2774 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2775 | if (v == NULL) { |
| 2776 | Py_CLEAR(d); |
| 2777 | break; |
| 2778 | } |
| 2779 | if (PyList_Append(d, v) != 0) { |
| 2780 | Py_DECREF(v); |
| 2781 | Py_CLEAR(d); |
| 2782 | break; |
| 2783 | } |
| 2784 | Py_DECREF(v); |
| 2785 | } |
| 2786 | Py_BEGIN_ALLOW_THREADS |
| 2787 | closedir(dirp); |
| 2788 | Py_END_ALLOW_THREADS |
| 2789 | |
| 2790 | return d; |
| 2791 | } |
| 2792 | #endif |
| 2793 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2794 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2795 | /* A helper function for abspath on win32 */ |
| 2796 | static PyObject * |
| 2797 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2798 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2799 | PyObject *opath; |
| 2800 | char *path; |
| 2801 | char outbuf[MAX_PATH*2]; |
| 2802 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2803 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2804 | PyUnicodeObject *po; |
| 2805 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2806 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2807 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2808 | Py_UNICODE *wtemp; |
| 2809 | DWORD result; |
| 2810 | PyObject *v; |
| 2811 | result = GetFullPathNameW(wpath, |
| 2812 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2813 | woutbuf, &wtemp); |
| 2814 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2815 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2816 | if (!woutbufp) |
| 2817 | return PyErr_NoMemory(); |
| 2818 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2819 | } |
| 2820 | if (result) |
| 2821 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2822 | else |
| 2823 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2824 | if (woutbufp != woutbuf) |
| 2825 | free(woutbufp); |
| 2826 | return v; |
| 2827 | } |
| 2828 | /* Drop the argument parsing error as narrow strings |
| 2829 | are also valid. */ |
| 2830 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2831 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2832 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2833 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2834 | PyUnicode_FSConverter, &opath)) |
| 2835 | return NULL; |
| 2836 | path = PyBytes_AsString(opath); |
| 2837 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2838 | outbuf, &temp)) { |
| 2839 | win32_error("GetFullPathName", path); |
| 2840 | Py_DECREF(opath); |
| 2841 | return NULL; |
| 2842 | } |
| 2843 | Py_DECREF(opath); |
| 2844 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2845 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2846 | Py_FileSystemDefaultEncoding, NULL); |
| 2847 | } |
| 2848 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2849 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2850 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2851 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 2852 | static int has_GetFinalPathNameByHandle = 0; |
| 2853 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 2854 | DWORD); |
| 2855 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 2856 | DWORD); |
| 2857 | static int |
| 2858 | check_GetFinalPathNameByHandle() |
| 2859 | { |
| 2860 | HINSTANCE hKernel32; |
| 2861 | /* only recheck */ |
| 2862 | if (!has_GetFinalPathNameByHandle) |
| 2863 | { |
| 2864 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 2865 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 2866 | "GetFinalPathNameByHandleA"); |
| 2867 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 2868 | "GetFinalPathNameByHandleW"); |
| 2869 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 2870 | Py_GetFinalPathNameByHandleW; |
| 2871 | } |
| 2872 | return has_GetFinalPathNameByHandle; |
| 2873 | } |
| 2874 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2875 | /* A helper function for samepath on windows */ |
| 2876 | static PyObject * |
| 2877 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 2878 | { |
| 2879 | HANDLE hFile; |
| 2880 | int buf_size; |
| 2881 | wchar_t *target_path; |
| 2882 | int result_length; |
| 2883 | PyObject *result; |
| 2884 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2885 | |
Brian Curtin | 94622b0 | 2010-09-24 00:03:39 +0000 | [diff] [blame] | 2886 | if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) { |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2887 | return NULL; |
| 2888 | } |
| 2889 | |
| 2890 | if(!check_GetFinalPathNameByHandle()) { |
| 2891 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 2892 | NotImplementedError. */ |
| 2893 | return PyErr_Format(PyExc_NotImplementedError, |
| 2894 | "GetFinalPathNameByHandle not available on this platform"); |
| 2895 | } |
| 2896 | |
| 2897 | hFile = CreateFileW( |
| 2898 | path, |
| 2899 | 0, /* desired access */ |
| 2900 | 0, /* share mode */ |
| 2901 | NULL, /* security attributes */ |
| 2902 | OPEN_EXISTING, |
| 2903 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 2904 | FILE_FLAG_BACKUP_SEMANTICS, |
| 2905 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2906 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2907 | if(hFile == INVALID_HANDLE_VALUE) { |
| 2908 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 2909 | return PyErr_Format(PyExc_RuntimeError, |
| 2910 | "Could not get a handle to file."); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | /* We have a good handle to the target, use it to determine the |
| 2914 | target path name. */ |
| 2915 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 2916 | |
| 2917 | if(!buf_size) |
| 2918 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2919 | |
| 2920 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 2921 | if(!target_path) |
| 2922 | return PyErr_NoMemory(); |
| 2923 | |
| 2924 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 2925 | buf_size, VOLUME_NAME_DOS); |
| 2926 | if(!result_length) |
| 2927 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
| 2928 | |
| 2929 | if(!CloseHandle(hFile)) |
| 2930 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2931 | |
| 2932 | target_path[result_length] = 0; |
| 2933 | result = PyUnicode_FromUnicode(target_path, result_length); |
| 2934 | free(target_path); |
| 2935 | return result; |
| 2936 | |
| 2937 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2938 | |
| 2939 | static PyObject * |
| 2940 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 2941 | { |
| 2942 | HANDLE hFile; |
| 2943 | BY_HANDLE_FILE_INFORMATION info; |
| 2944 | int fd; |
| 2945 | |
| 2946 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 2947 | return NULL; |
| 2948 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 2949 | if (!_PyVerify_fd(fd)) |
| 2950 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2951 | |
| 2952 | hFile = (HANDLE)_get_osfhandle(fd); |
| 2953 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 2954 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2955 | |
| 2956 | if (!GetFileInformationByHandle(hFile, &info)) |
| 2957 | return win32_error("_getfileinformation", NULL); |
| 2958 | |
| 2959 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 2960 | info.nFileIndexHigh, |
| 2961 | info.nFileIndexLow); |
| 2962 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 2963 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 2964 | PyDoc_STRVAR(posix__isdir__doc__, |
| 2965 | "Return true if the pathname refers to an existing directory."); |
| 2966 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 2967 | static PyObject * |
| 2968 | posix__isdir(PyObject *self, PyObject *args) |
| 2969 | { |
| 2970 | PyObject *opath; |
| 2971 | char *path; |
| 2972 | PyUnicodeObject *po; |
| 2973 | DWORD attributes; |
| 2974 | |
| 2975 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
| 2976 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2977 | |
| 2978 | attributes = GetFileAttributesW(wpath); |
| 2979 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 2980 | Py_RETURN_FALSE; |
| 2981 | goto check; |
| 2982 | } |
| 2983 | /* Drop the argument parsing error as narrow strings |
| 2984 | are also valid. */ |
| 2985 | PyErr_Clear(); |
| 2986 | |
| 2987 | if (!PyArg_ParseTuple(args, "O&:_isdir", |
| 2988 | PyUnicode_FSConverter, &opath)) |
| 2989 | return NULL; |
| 2990 | |
| 2991 | path = PyBytes_AsString(opath); |
| 2992 | attributes = GetFileAttributesA(path); |
| 2993 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 2994 | Py_RETURN_FALSE; |
| 2995 | |
| 2996 | check: |
| 2997 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 2998 | Py_RETURN_TRUE; |
| 2999 | else |
| 3000 | Py_RETURN_FALSE; |
| 3001 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3002 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3004 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3005 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3006 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3007 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3008 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3009 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3010 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3011 | int res; |
| 3012 | PyObject *opath; |
| 3013 | char *path; |
| 3014 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3015 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3016 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3017 | PyUnicodeObject *po; |
| 3018 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 3019 | Py_BEGIN_ALLOW_THREADS |
| 3020 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 3021 | it is a simple dereference. */ |
| 3022 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 3023 | Py_END_ALLOW_THREADS |
| 3024 | if (!res) |
| 3025 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 3026 | Py_INCREF(Py_None); |
| 3027 | return Py_None; |
| 3028 | } |
| 3029 | /* Drop the argument parsing error as narrow strings |
| 3030 | are also valid. */ |
| 3031 | PyErr_Clear(); |
| 3032 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3033 | PyUnicode_FSConverter, &opath, &mode)) |
| 3034 | return NULL; |
| 3035 | path = PyBytes_AsString(opath); |
| 3036 | Py_BEGIN_ALLOW_THREADS |
| 3037 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 3038 | it is a simple dereference. */ |
| 3039 | res = CreateDirectoryA(path, NULL); |
| 3040 | Py_END_ALLOW_THREADS |
| 3041 | if (!res) { |
| 3042 | win32_error("mkdir", path); |
| 3043 | Py_DECREF(opath); |
| 3044 | return NULL; |
| 3045 | } |
| 3046 | Py_DECREF(opath); |
| 3047 | Py_INCREF(Py_None); |
| 3048 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3049 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3051 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3052 | PyUnicode_FSConverter, &opath, &mode)) |
| 3053 | return NULL; |
| 3054 | path = PyBytes_AsString(opath); |
| 3055 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3056 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3057 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3058 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3059 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3060 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3061 | Py_END_ALLOW_THREADS |
| 3062 | if (res < 0) |
| 3063 | return posix_error_with_allocated_filename(opath); |
| 3064 | Py_DECREF(opath); |
| 3065 | Py_INCREF(Py_None); |
| 3066 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3067 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3070 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3071 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3072 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3073 | #include <sys/resource.h> |
| 3074 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3075 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3076 | |
| 3077 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3078 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3079 | "nice(inc) -> new_priority\n\n\ |
| 3080 | 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] | 3081 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3082 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3083 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3084 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3085 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3086 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3087 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3088 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3089 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3090 | /* There are two flavours of 'nice': one that returns the new |
| 3091 | priority (as required by almost all standards out there) and the |
| 3092 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3093 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3094 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3095 | If we are of the nice family that returns the new priority, we |
| 3096 | need to clear errno before the call, and check if errno is filled |
| 3097 | before calling posix_error() on a returnvalue of -1, because the |
| 3098 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3099 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3100 | errno = 0; |
| 3101 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3102 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3103 | if (value == 0) |
| 3104 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3105 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3106 | if (value == -1 && errno != 0) |
| 3107 | /* either nice() or getpriority() returned an error */ |
| 3108 | return posix_error(); |
| 3109 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3110 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3111 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3112 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3113 | |
| 3114 | #ifdef HAVE_GETPRIORITY |
| 3115 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3116 | "getpriority(which, who) -> current_priority\n\n\ |
| 3117 | Get program scheduling priority."); |
| 3118 | |
| 3119 | static PyObject * |
| 3120 | posix_getpriority(PyObject *self, PyObject *args) |
| 3121 | { |
| 3122 | int which, who, retval; |
| 3123 | |
| 3124 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3125 | return NULL; |
| 3126 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3127 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3128 | if (errno != 0) |
| 3129 | return posix_error(); |
| 3130 | return PyLong_FromLong((long)retval); |
| 3131 | } |
| 3132 | #endif /* HAVE_GETPRIORITY */ |
| 3133 | |
| 3134 | |
| 3135 | #ifdef HAVE_SETPRIORITY |
| 3136 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3137 | "setpriority(which, who, prio) -> None\n\n\ |
| 3138 | Set program scheduling priority."); |
| 3139 | |
| 3140 | static PyObject * |
| 3141 | posix_setpriority(PyObject *self, PyObject *args) |
| 3142 | { |
| 3143 | int which, who, prio, retval; |
| 3144 | |
| 3145 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3146 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3147 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3148 | if (retval == -1) |
| 3149 | return posix_error(); |
| 3150 | Py_RETURN_NONE; |
| 3151 | } |
| 3152 | #endif /* HAVE_SETPRIORITY */ |
| 3153 | |
| 3154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3155 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3156 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3157 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3159 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3160 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3161 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3162 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3163 | PyObject *o1, *o2; |
| 3164 | char *p1, *p2; |
| 3165 | BOOL result; |
| 3166 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 3167 | goto error; |
| 3168 | if (!convert_to_unicode(&o1)) |
| 3169 | goto error; |
| 3170 | if (!convert_to_unicode(&o2)) { |
| 3171 | Py_DECREF(o1); |
| 3172 | goto error; |
| 3173 | } |
| 3174 | Py_BEGIN_ALLOW_THREADS |
| 3175 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 3176 | PyUnicode_AsUnicode(o2)); |
| 3177 | Py_END_ALLOW_THREADS |
| 3178 | Py_DECREF(o1); |
| 3179 | Py_DECREF(o2); |
| 3180 | if (!result) |
| 3181 | return win32_error("rename", NULL); |
| 3182 | Py_INCREF(Py_None); |
| 3183 | return Py_None; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3184 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3185 | PyErr_Clear(); |
| 3186 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 3187 | return NULL; |
| 3188 | Py_BEGIN_ALLOW_THREADS |
| 3189 | result = MoveFileA(p1, p2); |
| 3190 | Py_END_ALLOW_THREADS |
| 3191 | if (!result) |
| 3192 | return win32_error("rename", NULL); |
| 3193 | Py_INCREF(Py_None); |
| 3194 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3195 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3196 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3197 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3201 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3202 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3203 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3204 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3205 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3206 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3207 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3208 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3209 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3210 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3211 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3212 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3216 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3217 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3218 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3219 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3220 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3221 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3222 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3223 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3224 | 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] | 3225 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3226 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3227 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3230 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3231 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3232 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3233 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3234 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3235 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3236 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3237 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3238 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3239 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3240 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3241 | wchar_t *command; |
| 3242 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3243 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3244 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3245 | Py_BEGIN_ALLOW_THREADS |
| 3246 | sts = _wsystem(command); |
| 3247 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3248 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3249 | PyObject *command_obj; |
| 3250 | char *command; |
| 3251 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3252 | PyUnicode_FSConverter, &command_obj)) |
| 3253 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3254 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3255 | command = PyBytes_AsString(command_obj); |
| 3256 | Py_BEGIN_ALLOW_THREADS |
| 3257 | sts = system(command); |
| 3258 | Py_END_ALLOW_THREADS |
| 3259 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3260 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3261 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3262 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3263 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3264 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3266 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3267 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3268 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3269 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3270 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3271 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3272 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3273 | int i; |
| 3274 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3275 | return NULL; |
| 3276 | i = (int)umask(i); |
| 3277 | if (i < 0) |
| 3278 | return posix_error(); |
| 3279 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3282 | #ifdef MS_WINDOWS |
| 3283 | |
| 3284 | /* override the default DeleteFileW behavior so that directory |
| 3285 | symlinks can be removed with this function, the same as with |
| 3286 | Unix symlinks */ |
| 3287 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3288 | { |
| 3289 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3290 | WIN32_FIND_DATAW find_data; |
| 3291 | HANDLE find_data_handle; |
| 3292 | int is_directory = 0; |
| 3293 | int is_link = 0; |
| 3294 | |
| 3295 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3296 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3297 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3298 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3299 | it is a symlink */ |
| 3300 | if(is_directory && |
| 3301 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3302 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3303 | |
| 3304 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3305 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3306 | FindClose(find_data_handle); |
| 3307 | } |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | if (is_directory && is_link) |
| 3312 | return RemoveDirectoryW(lpFileName); |
| 3313 | |
| 3314 | return DeleteFileW(lpFileName); |
| 3315 | } |
| 3316 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3318 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3319 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3320 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3322 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3323 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3324 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3325 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3326 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3327 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3328 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3329 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3330 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3331 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3332 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3333 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3334 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3337 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3338 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3339 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3340 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3341 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3342 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3343 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3344 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3345 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3346 | struct utsname u; |
| 3347 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3348 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3349 | Py_BEGIN_ALLOW_THREADS |
| 3350 | res = uname(&u); |
| 3351 | Py_END_ALLOW_THREADS |
| 3352 | if (res < 0) |
| 3353 | return posix_error(); |
| 3354 | return Py_BuildValue("(sssss)", |
| 3355 | u.sysname, |
| 3356 | u.nodename, |
| 3357 | u.release, |
| 3358 | u.version, |
| 3359 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3360 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3361 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3362 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3363 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3364 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3365 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3366 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3367 | if (PyFloat_Check(t)) { |
| 3368 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3369 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3370 | if (!intobj) |
| 3371 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3372 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3373 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3374 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3375 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3376 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3377 | Py_DECREF(intobj); |
| 3378 | if (intval == -1 && PyErr_Occurred()) |
| 3379 | return -1; |
| 3380 | *sec = intval; |
| 3381 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
| 3382 | if (*usec < 0) |
| 3383 | /* If rounding gave us a negative number, |
| 3384 | truncate. */ |
| 3385 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3386 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3387 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3388 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3389 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3390 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3391 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3392 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3393 | if (intval == -1 && PyErr_Occurred()) |
| 3394 | return -1; |
| 3395 | *sec = intval; |
| 3396 | *usec = 0; |
| 3397 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3398 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3399 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3400 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3401 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3402 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3403 | 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] | 3404 | 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] | 3405 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3406 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3407 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3408 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3409 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3410 | PyObject *arg; |
| 3411 | PyUnicodeObject *obwpath; |
| 3412 | wchar_t *wpath = NULL; |
| 3413 | PyObject *oapath; |
| 3414 | char *apath; |
| 3415 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3416 | time_t atimesec, mtimesec; |
| 3417 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3418 | FILETIME atime, mtime; |
| 3419 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3420 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3421 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 3422 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 3423 | Py_BEGIN_ALLOW_THREADS |
| 3424 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3425 | NULL, OPEN_EXISTING, |
| 3426 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3427 | Py_END_ALLOW_THREADS |
| 3428 | if (hFile == INVALID_HANDLE_VALUE) |
| 3429 | return win32_error_unicode("utime", wpath); |
| 3430 | } else |
| 3431 | /* Drop the argument parsing error as narrow strings |
| 3432 | are also valid. */ |
| 3433 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3434 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3435 | if (!wpath) { |
| 3436 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3437 | PyUnicode_FSConverter, &oapath, &arg)) |
| 3438 | return NULL; |
| 3439 | apath = PyBytes_AsString(oapath); |
| 3440 | Py_BEGIN_ALLOW_THREADS |
| 3441 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3442 | NULL, OPEN_EXISTING, |
| 3443 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3444 | Py_END_ALLOW_THREADS |
| 3445 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3446 | win32_error("utime", apath); |
| 3447 | Py_DECREF(oapath); |
| 3448 | return NULL; |
| 3449 | } |
| 3450 | Py_DECREF(oapath); |
| 3451 | } |
| 3452 | |
| 3453 | if (arg == Py_None) { |
| 3454 | SYSTEMTIME now; |
| 3455 | GetSystemTime(&now); |
| 3456 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3457 | !SystemTimeToFileTime(&now, &atime)) { |
| 3458 | win32_error("utime", NULL); |
| 3459 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3460 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3461 | } |
| 3462 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3463 | PyErr_SetString(PyExc_TypeError, |
| 3464 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3465 | goto done; |
| 3466 | } |
| 3467 | else { |
| 3468 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3469 | &atimesec, &ausec) == -1) |
| 3470 | goto done; |
| 3471 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3472 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3473 | &mtimesec, &musec) == -1) |
| 3474 | goto done; |
| 3475 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3476 | } |
| 3477 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3478 | /* Avoid putting the file name into the error here, |
| 3479 | as that may confuse the user into believing that |
| 3480 | something is wrong with the file, when it also |
| 3481 | could be the time stamp that gives a problem. */ |
| 3482 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3483 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3484 | } |
| 3485 | Py_INCREF(Py_None); |
| 3486 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3487 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3488 | CloseHandle(hFile); |
| 3489 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3490 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3492 | PyObject *opath; |
| 3493 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3494 | time_t atime, mtime; |
| 3495 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3496 | int res; |
| 3497 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3498 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3499 | #if defined(HAVE_UTIMES) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3500 | struct timeval buf[2]; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3501 | #define ATIME buf[0].tv_sec |
| 3502 | #define MTIME buf[1].tv_sec |
| 3503 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3504 | /* XXX should define struct utimbuf instead, above */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3505 | struct utimbuf buf; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3506 | #define ATIME buf.actime |
| 3507 | #define MTIME buf.modtime |
| 3508 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3509 | #else /* HAVE_UTIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3510 | time_t buf[2]; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3511 | #define ATIME buf[0] |
| 3512 | #define MTIME buf[1] |
| 3513 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3514 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3515 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3517 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3518 | PyUnicode_FSConverter, &opath, &arg)) |
| 3519 | return NULL; |
| 3520 | path = PyBytes_AsString(opath); |
| 3521 | if (arg == Py_None) { |
| 3522 | /* optional time values not given */ |
| 3523 | Py_BEGIN_ALLOW_THREADS |
| 3524 | res = utime(path, NULL); |
| 3525 | Py_END_ALLOW_THREADS |
| 3526 | } |
| 3527 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3528 | PyErr_SetString(PyExc_TypeError, |
| 3529 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3530 | Py_DECREF(opath); |
| 3531 | return NULL; |
| 3532 | } |
| 3533 | else { |
| 3534 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3535 | &atime, &ausec) == -1) { |
| 3536 | Py_DECREF(opath); |
| 3537 | return NULL; |
| 3538 | } |
| 3539 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3540 | &mtime, &musec) == -1) { |
| 3541 | Py_DECREF(opath); |
| 3542 | return NULL; |
| 3543 | } |
| 3544 | ATIME = atime; |
| 3545 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3546 | #ifdef HAVE_UTIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3547 | buf[0].tv_usec = ausec; |
| 3548 | buf[1].tv_usec = musec; |
| 3549 | Py_BEGIN_ALLOW_THREADS |
| 3550 | res = utimes(path, buf); |
| 3551 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3552 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3553 | Py_BEGIN_ALLOW_THREADS |
| 3554 | res = utime(path, UTIME_ARG); |
| 3555 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3556 | #endif /* HAVE_UTIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3557 | } |
| 3558 | if (res < 0) { |
| 3559 | return posix_error_with_allocated_filename(opath); |
| 3560 | } |
| 3561 | Py_DECREF(opath); |
| 3562 | Py_INCREF(Py_None); |
| 3563 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3564 | #undef UTIME_ARG |
| 3565 | #undef ATIME |
| 3566 | #undef MTIME |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3567 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3570 | #ifdef HAVE_FUTIMES |
| 3571 | PyDoc_STRVAR(posix_futimes__doc__, |
| 3572 | "futimes(fd, (atime, mtime))\n\ |
| 3573 | futimes(fd, None)\n\n\ |
| 3574 | Set the access and modified time of the file specified by the file\n\ |
| 3575 | descriptor fd to the given values. If the second form is used, set the\n\ |
| 3576 | access and modified times to the current time."); |
| 3577 | |
| 3578 | static PyObject * |
| 3579 | posix_futimes(PyObject *self, PyObject *args) |
| 3580 | { |
| 3581 | int res, fd; |
| 3582 | PyObject* arg; |
| 3583 | struct timeval buf[2]; |
| 3584 | long ausec, musec; |
| 3585 | |
| 3586 | if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg)) |
| 3587 | return NULL; |
| 3588 | |
| 3589 | if (arg == Py_None) { |
| 3590 | /* optional time values not given */ |
| 3591 | Py_BEGIN_ALLOW_THREADS |
| 3592 | res = futimes(fd, NULL); |
| 3593 | Py_END_ALLOW_THREADS |
| 3594 | } |
| 3595 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3596 | PyErr_SetString(PyExc_TypeError, |
| 3597 | "futimes() arg 2 must be a tuple (atime, mtime)"); |
| 3598 | return NULL; |
| 3599 | } |
| 3600 | else { |
| 3601 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3602 | &(buf[0].tv_sec), &ausec) == -1) { |
| 3603 | return NULL; |
| 3604 | } |
| 3605 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3606 | &(buf[1].tv_sec), &musec) == -1) { |
| 3607 | return NULL; |
| 3608 | } |
| 3609 | buf[0].tv_usec = ausec; |
| 3610 | buf[1].tv_usec = musec; |
| 3611 | Py_BEGIN_ALLOW_THREADS |
| 3612 | res = futimes(fd, buf); |
| 3613 | Py_END_ALLOW_THREADS |
| 3614 | } |
| 3615 | if (res < 0) |
| 3616 | return posix_error(); |
| 3617 | Py_RETURN_NONE; |
| 3618 | } |
| 3619 | #endif |
| 3620 | |
| 3621 | #ifdef HAVE_LUTIMES |
| 3622 | PyDoc_STRVAR(posix_lutimes__doc__, |
| 3623 | "lutimes(path, (atime, mtime))\n\ |
| 3624 | lutimes(path, None)\n\n\ |
| 3625 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3626 | |
| 3627 | static PyObject * |
| 3628 | posix_lutimes(PyObject *self, PyObject *args) |
| 3629 | { |
| 3630 | PyObject *opath, *arg; |
| 3631 | const char *path; |
| 3632 | int res; |
| 3633 | struct timeval buf[2]; |
| 3634 | long ausec, musec; |
| 3635 | |
| 3636 | if (!PyArg_ParseTuple(args, "O&O:lutimes", |
| 3637 | PyUnicode_FSConverter, &opath, &arg)) |
| 3638 | return NULL; |
| 3639 | path = PyBytes_AsString(opath); |
| 3640 | if (arg == Py_None) { |
| 3641 | /* optional time values not given */ |
| 3642 | Py_BEGIN_ALLOW_THREADS |
| 3643 | res = lutimes(path, NULL); |
| 3644 | Py_END_ALLOW_THREADS |
| 3645 | } |
| 3646 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3647 | PyErr_SetString(PyExc_TypeError, |
| 3648 | "lutimes() arg 2 must be a tuple (atime, mtime)"); |
| 3649 | Py_DECREF(opath); |
| 3650 | return NULL; |
| 3651 | } |
| 3652 | else { |
| 3653 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3654 | &(buf[0].tv_sec), &ausec) == -1) { |
| 3655 | Py_DECREF(opath); |
| 3656 | return NULL; |
| 3657 | } |
| 3658 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3659 | &(buf[1].tv_sec), &musec) == -1) { |
| 3660 | Py_DECREF(opath); |
| 3661 | return NULL; |
| 3662 | } |
| 3663 | buf[0].tv_usec = ausec; |
| 3664 | buf[1].tv_usec = musec; |
| 3665 | Py_BEGIN_ALLOW_THREADS |
| 3666 | res = lutimes(path, buf); |
| 3667 | Py_END_ALLOW_THREADS |
| 3668 | } |
| 3669 | Py_DECREF(opath); |
| 3670 | if (res < 0) |
| 3671 | return posix_error(); |
| 3672 | Py_RETURN_NONE; |
| 3673 | } |
| 3674 | #endif |
| 3675 | |
| 3676 | #ifdef HAVE_FUTIMENS |
| 3677 | PyDoc_STRVAR(posix_futimens__doc__, |
| 3678 | "futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\ |
| 3679 | futimens(fd, None, None)\n\n\ |
| 3680 | Updates the timestamps of a file specified by the file descriptor fd, with\n\ |
| 3681 | nanosecond precision.\n\ |
| 3682 | The second form sets atime and mtime to the current time.\n\ |
| 3683 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 3684 | current time.\n\ |
| 3685 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 3686 | |
| 3687 | static PyObject * |
| 3688 | posix_futimens(PyObject *self, PyObject *args) |
| 3689 | { |
| 3690 | int res, fd; |
| 3691 | PyObject *atime, *mtime; |
| 3692 | struct timespec buf[2]; |
| 3693 | |
| 3694 | if (!PyArg_ParseTuple(args, "iOO:futimens", |
| 3695 | &fd, &atime, &mtime)) |
| 3696 | return NULL; |
| 3697 | if (atime == Py_None && mtime == Py_None) { |
| 3698 | /* optional time values not given */ |
| 3699 | Py_BEGIN_ALLOW_THREADS |
| 3700 | res = futimens(fd, NULL); |
| 3701 | Py_END_ALLOW_THREADS |
| 3702 | } |
| 3703 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 3704 | PyErr_SetString(PyExc_TypeError, |
| 3705 | "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)"); |
| 3706 | return NULL; |
| 3707 | } |
| 3708 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 3709 | PyErr_SetString(PyExc_TypeError, |
| 3710 | "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)"); |
| 3711 | return NULL; |
| 3712 | } |
| 3713 | else { |
| 3714 | if (!PyArg_ParseTuple(atime, "ll:futimens", |
| 3715 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 3716 | return NULL; |
| 3717 | } |
| 3718 | if (!PyArg_ParseTuple(mtime, "ll:futimens", |
| 3719 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 3720 | return NULL; |
| 3721 | } |
| 3722 | Py_BEGIN_ALLOW_THREADS |
| 3723 | res = futimens(fd, buf); |
| 3724 | Py_END_ALLOW_THREADS |
| 3725 | } |
| 3726 | if (res < 0) |
| 3727 | return posix_error(); |
| 3728 | Py_RETURN_NONE; |
| 3729 | } |
| 3730 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3731 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3732 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3734 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3735 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3736 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3738 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3739 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3740 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3741 | int sts; |
| 3742 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3743 | return NULL; |
| 3744 | _exit(sts); |
| 3745 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3748 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3749 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3750 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3751 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3752 | Py_ssize_t i; |
| 3753 | for (i = 0; i < count; i++) |
| 3754 | PyMem_Free(array[i]); |
| 3755 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3756 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3757 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3758 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3759 | int fsconvert_strdup(PyObject *o, char**out) |
| 3760 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3761 | PyObject *bytes; |
| 3762 | Py_ssize_t size; |
| 3763 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3764 | return 0; |
| 3765 | size = PyBytes_GET_SIZE(bytes); |
| 3766 | *out = PyMem_Malloc(size+1); |
| 3767 | if (!*out) |
| 3768 | return 0; |
| 3769 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3770 | Py_DECREF(bytes); |
| 3771 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3772 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3773 | #endif |
| 3774 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3775 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3776 | static char** |
| 3777 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3778 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3779 | char **envlist; |
| 3780 | Py_ssize_t i, pos, envc; |
| 3781 | PyObject *keys=NULL, *vals=NULL; |
| 3782 | PyObject *key, *val, *key2, *val2; |
| 3783 | char *p, *k, *v; |
| 3784 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3785 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3786 | i = PyMapping_Size(env); |
| 3787 | if (i < 0) |
| 3788 | return NULL; |
| 3789 | envlist = PyMem_NEW(char *, i + 1); |
| 3790 | if (envlist == NULL) { |
| 3791 | PyErr_NoMemory(); |
| 3792 | return NULL; |
| 3793 | } |
| 3794 | envc = 0; |
| 3795 | keys = PyMapping_Keys(env); |
| 3796 | vals = PyMapping_Values(env); |
| 3797 | if (!keys || !vals) |
| 3798 | goto error; |
| 3799 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3800 | PyErr_Format(PyExc_TypeError, |
| 3801 | "env.keys() or env.values() is not a list"); |
| 3802 | goto error; |
| 3803 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3804 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3805 | for (pos = 0; pos < i; pos++) { |
| 3806 | key = PyList_GetItem(keys, pos); |
| 3807 | val = PyList_GetItem(vals, pos); |
| 3808 | if (!key || !val) |
| 3809 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3810 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3811 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3812 | goto error; |
| 3813 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3814 | Py_DECREF(key2); |
| 3815 | goto error; |
| 3816 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3817 | |
| 3818 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3819 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3820 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3821 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3822 | k = PyBytes_AsString(key2); |
| 3823 | v = PyBytes_AsString(val2); |
| 3824 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3825 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3826 | p = PyMem_NEW(char, len); |
| 3827 | if (p == NULL) { |
| 3828 | PyErr_NoMemory(); |
| 3829 | Py_DECREF(key2); |
| 3830 | Py_DECREF(val2); |
| 3831 | goto error; |
| 3832 | } |
| 3833 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3834 | envlist[envc++] = p; |
| 3835 | Py_DECREF(key2); |
| 3836 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3837 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3838 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3839 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3840 | } |
| 3841 | Py_DECREF(vals); |
| 3842 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3843 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3844 | envlist[envc] = 0; |
| 3845 | *envc_ptr = envc; |
| 3846 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3847 | |
| 3848 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3849 | Py_XDECREF(keys); |
| 3850 | Py_XDECREF(vals); |
| 3851 | while (--envc >= 0) |
| 3852 | PyMem_DEL(envlist[envc]); |
| 3853 | PyMem_DEL(envlist); |
| 3854 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3855 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3856 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3857 | static char** |
| 3858 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 3859 | { |
| 3860 | int i; |
| 3861 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 3862 | if (argvlist == NULL) { |
| 3863 | PyErr_NoMemory(); |
| 3864 | return NULL; |
| 3865 | } |
| 3866 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3867 | PyObject* item = PySequence_ITEM(argv, i); |
| 3868 | if (item == NULL) |
| 3869 | goto fail; |
| 3870 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 3871 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3872 | goto fail; |
| 3873 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3874 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3875 | } |
| 3876 | argvlist[*argc] = NULL; |
| 3877 | return argvlist; |
| 3878 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3879 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3880 | free_string_array(argvlist, *argc); |
| 3881 | return NULL; |
| 3882 | } |
| 3883 | #endif |
| 3884 | |
| 3885 | #ifdef HAVE_EXECV |
| 3886 | PyDoc_STRVAR(posix_execv__doc__, |
| 3887 | "execv(path, args)\n\n\ |
| 3888 | Execute an executable path with arguments, replacing current process.\n\ |
| 3889 | \n\ |
| 3890 | path: path of executable file\n\ |
| 3891 | args: tuple or list of strings"); |
| 3892 | |
| 3893 | static PyObject * |
| 3894 | posix_execv(PyObject *self, PyObject *args) |
| 3895 | { |
| 3896 | PyObject *opath; |
| 3897 | char *path; |
| 3898 | PyObject *argv; |
| 3899 | char **argvlist; |
| 3900 | Py_ssize_t argc; |
| 3901 | |
| 3902 | /* execv has two arguments: (path, argv), where |
| 3903 | argv is a list or tuple of strings. */ |
| 3904 | |
| 3905 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3906 | PyUnicode_FSConverter, |
| 3907 | &opath, &argv)) |
| 3908 | return NULL; |
| 3909 | path = PyBytes_AsString(opath); |
| 3910 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 3911 | PyErr_SetString(PyExc_TypeError, |
| 3912 | "execv() arg 2 must be a tuple or list"); |
| 3913 | Py_DECREF(opath); |
| 3914 | return NULL; |
| 3915 | } |
| 3916 | argc = PySequence_Size(argv); |
| 3917 | if (argc < 1) { |
| 3918 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 3919 | Py_DECREF(opath); |
| 3920 | return NULL; |
| 3921 | } |
| 3922 | |
| 3923 | argvlist = parse_arglist(argv, &argc); |
| 3924 | if (argvlist == NULL) { |
| 3925 | Py_DECREF(opath); |
| 3926 | return NULL; |
| 3927 | } |
| 3928 | |
| 3929 | execv(path, argvlist); |
| 3930 | |
| 3931 | /* If we get here it's definitely an error */ |
| 3932 | |
| 3933 | free_string_array(argvlist, argc); |
| 3934 | Py_DECREF(opath); |
| 3935 | return posix_error(); |
| 3936 | } |
| 3937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3938 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3939 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3940 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3941 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3942 | path: path of executable file\n\ |
| 3943 | args: tuple or list of arguments\n\ |
| 3944 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3945 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3946 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3947 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3948 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3949 | PyObject *opath; |
| 3950 | char *path; |
| 3951 | PyObject *argv, *env; |
| 3952 | char **argvlist; |
| 3953 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3954 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3955 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3956 | /* execve has three arguments: (path, argv, env), where |
| 3957 | argv is a list or tuple of strings and env is a dictionary |
| 3958 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3959 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3960 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3961 | PyUnicode_FSConverter, |
| 3962 | &opath, &argv, &env)) |
| 3963 | return NULL; |
| 3964 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3965 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3966 | PyErr_SetString(PyExc_TypeError, |
| 3967 | "execve() arg 2 must be a tuple or list"); |
| 3968 | goto fail_0; |
| 3969 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3970 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3971 | if (!PyMapping_Check(env)) { |
| 3972 | PyErr_SetString(PyExc_TypeError, |
| 3973 | "execve() arg 3 must be a mapping object"); |
| 3974 | goto fail_0; |
| 3975 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3976 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3977 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3978 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3979 | goto fail_0; |
| 3980 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3981 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3982 | envlist = parse_envlist(env, &envc); |
| 3983 | if (envlist == NULL) |
| 3984 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3986 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3987 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3988 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3989 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3990 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3991 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3992 | while (--envc >= 0) |
| 3993 | PyMem_DEL(envlist[envc]); |
| 3994 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3995 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3996 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3997 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3998 | Py_DECREF(opath); |
| 3999 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4000 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4001 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4002 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4003 | #ifdef HAVE_FEXECVE |
| 4004 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4005 | "fexecve(fd, args, env)\n\n\ |
| 4006 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4007 | environment, replacing the current process.\n\ |
| 4008 | \n\ |
| 4009 | fd: file descriptor of executable\n\ |
| 4010 | args: tuple or list of arguments\n\ |
| 4011 | env: dictionary of strings mapping to strings"); |
| 4012 | |
| 4013 | static PyObject * |
| 4014 | posix_fexecve(PyObject *self, PyObject *args) |
| 4015 | { |
| 4016 | int fd; |
| 4017 | PyObject *argv, *env; |
| 4018 | char **argvlist; |
| 4019 | char **envlist; |
| 4020 | Py_ssize_t argc, envc; |
| 4021 | |
| 4022 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4023 | &fd, &argv, &env)) |
| 4024 | return NULL; |
| 4025 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4026 | PyErr_SetString(PyExc_TypeError, |
| 4027 | "fexecve() arg 2 must be a tuple or list"); |
| 4028 | return NULL; |
| 4029 | } |
| 4030 | argc = PySequence_Size(argv); |
| 4031 | if (!PyMapping_Check(env)) { |
| 4032 | PyErr_SetString(PyExc_TypeError, |
| 4033 | "fexecve() arg 3 must be a mapping object"); |
| 4034 | return NULL; |
| 4035 | } |
| 4036 | |
| 4037 | argvlist = parse_arglist(argv, &argc); |
| 4038 | if (argvlist == NULL) |
| 4039 | return NULL; |
| 4040 | |
| 4041 | envlist = parse_envlist(env, &envc); |
| 4042 | if (envlist == NULL) |
| 4043 | goto fail; |
| 4044 | |
| 4045 | fexecve(fd, argvlist, envlist); |
| 4046 | |
| 4047 | /* If we get here it's definitely an error */ |
| 4048 | |
| 4049 | (void) posix_error(); |
| 4050 | |
| 4051 | while (--envc >= 0) |
| 4052 | PyMem_DEL(envlist[envc]); |
| 4053 | PyMem_DEL(envlist); |
| 4054 | fail: |
| 4055 | free_string_array(argvlist, argc); |
| 4056 | return NULL; |
| 4057 | } |
| 4058 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4059 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4060 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4061 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4062 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4063 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4064 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4065 | mode: mode of process creation\n\ |
| 4066 | path: path of executable file\n\ |
| 4067 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4068 | |
| 4069 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4070 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4071 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4072 | PyObject *opath; |
| 4073 | char *path; |
| 4074 | PyObject *argv; |
| 4075 | char **argvlist; |
| 4076 | int mode, i; |
| 4077 | Py_ssize_t argc; |
| 4078 | Py_intptr_t spawnval; |
| 4079 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4080 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4081 | /* spawnv has three arguments: (mode, path, argv), where |
| 4082 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4083 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4084 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4085 | PyUnicode_FSConverter, |
| 4086 | &opath, &argv)) |
| 4087 | return NULL; |
| 4088 | path = PyBytes_AsString(opath); |
| 4089 | if (PyList_Check(argv)) { |
| 4090 | argc = PyList_Size(argv); |
| 4091 | getitem = PyList_GetItem; |
| 4092 | } |
| 4093 | else if (PyTuple_Check(argv)) { |
| 4094 | argc = PyTuple_Size(argv); |
| 4095 | getitem = PyTuple_GetItem; |
| 4096 | } |
| 4097 | else { |
| 4098 | PyErr_SetString(PyExc_TypeError, |
| 4099 | "spawnv() arg 2 must be a tuple or list"); |
| 4100 | Py_DECREF(opath); |
| 4101 | return NULL; |
| 4102 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4103 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4104 | argvlist = PyMem_NEW(char *, argc+1); |
| 4105 | if (argvlist == NULL) { |
| 4106 | Py_DECREF(opath); |
| 4107 | return PyErr_NoMemory(); |
| 4108 | } |
| 4109 | for (i = 0; i < argc; i++) { |
| 4110 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4111 | &argvlist[i])) { |
| 4112 | free_string_array(argvlist, i); |
| 4113 | PyErr_SetString( |
| 4114 | PyExc_TypeError, |
| 4115 | "spawnv() arg 2 must contain only strings"); |
| 4116 | Py_DECREF(opath); |
| 4117 | return NULL; |
| 4118 | } |
| 4119 | } |
| 4120 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4121 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4122 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4123 | Py_BEGIN_ALLOW_THREADS |
| 4124 | spawnval = spawnv(mode, path, argvlist); |
| 4125 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4126 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4127 | if (mode == _OLD_P_OVERLAY) |
| 4128 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4129 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4130 | Py_BEGIN_ALLOW_THREADS |
| 4131 | spawnval = _spawnv(mode, path, argvlist); |
| 4132 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4133 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4134 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4135 | free_string_array(argvlist, argc); |
| 4136 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4137 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4138 | if (spawnval == -1) |
| 4139 | return posix_error(); |
| 4140 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4141 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4142 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4143 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4144 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4145 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4149 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4150 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4151 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4152 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4153 | mode: mode of process creation\n\ |
| 4154 | path: path of executable file\n\ |
| 4155 | args: tuple or list of arguments\n\ |
| 4156 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4157 | |
| 4158 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4159 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4160 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4161 | PyObject *opath; |
| 4162 | char *path; |
| 4163 | PyObject *argv, *env; |
| 4164 | char **argvlist; |
| 4165 | char **envlist; |
| 4166 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4167 | int mode; |
| 4168 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4169 | Py_intptr_t spawnval; |
| 4170 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4171 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4172 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4173 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4174 | argv is a list or tuple of strings and env is a dictionary |
| 4175 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4176 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4177 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4178 | PyUnicode_FSConverter, |
| 4179 | &opath, &argv, &env)) |
| 4180 | return NULL; |
| 4181 | path = PyBytes_AsString(opath); |
| 4182 | if (PyList_Check(argv)) { |
| 4183 | argc = PyList_Size(argv); |
| 4184 | getitem = PyList_GetItem; |
| 4185 | } |
| 4186 | else if (PyTuple_Check(argv)) { |
| 4187 | argc = PyTuple_Size(argv); |
| 4188 | getitem = PyTuple_GetItem; |
| 4189 | } |
| 4190 | else { |
| 4191 | PyErr_SetString(PyExc_TypeError, |
| 4192 | "spawnve() arg 2 must be a tuple or list"); |
| 4193 | goto fail_0; |
| 4194 | } |
| 4195 | if (!PyMapping_Check(env)) { |
| 4196 | PyErr_SetString(PyExc_TypeError, |
| 4197 | "spawnve() arg 3 must be a mapping object"); |
| 4198 | goto fail_0; |
| 4199 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4200 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4201 | argvlist = PyMem_NEW(char *, argc+1); |
| 4202 | if (argvlist == NULL) { |
| 4203 | PyErr_NoMemory(); |
| 4204 | goto fail_0; |
| 4205 | } |
| 4206 | for (i = 0; i < argc; i++) { |
| 4207 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4208 | &argvlist[i])) |
| 4209 | { |
| 4210 | lastarg = i; |
| 4211 | goto fail_1; |
| 4212 | } |
| 4213 | } |
| 4214 | lastarg = argc; |
| 4215 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4216 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4217 | envlist = parse_envlist(env, &envc); |
| 4218 | if (envlist == NULL) |
| 4219 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4220 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4221 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4222 | Py_BEGIN_ALLOW_THREADS |
| 4223 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4224 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4225 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4226 | if (mode == _OLD_P_OVERLAY) |
| 4227 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4228 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4229 | Py_BEGIN_ALLOW_THREADS |
| 4230 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4231 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4232 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4233 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4234 | if (spawnval == -1) |
| 4235 | (void) posix_error(); |
| 4236 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4237 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4238 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4239 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4240 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4241 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4242 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4243 | while (--envc >= 0) |
| 4244 | PyMem_DEL(envlist[envc]); |
| 4245 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4246 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4247 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4248 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4249 | Py_DECREF(opath); |
| 4250 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4251 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4252 | |
| 4253 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4254 | #if defined(PYOS_OS2) |
| 4255 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4256 | "spawnvp(mode, file, args)\n\n\ |
| 4257 | Execute the program 'file' in a new process, using the environment\n\ |
| 4258 | search path to find the file.\n\ |
| 4259 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4260 | mode: mode of process creation\n\ |
| 4261 | file: executable file name\n\ |
| 4262 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4263 | |
| 4264 | static PyObject * |
| 4265 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4266 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4267 | PyObject *opath; |
| 4268 | char *path; |
| 4269 | PyObject *argv; |
| 4270 | char **argvlist; |
| 4271 | int mode, i, argc; |
| 4272 | Py_intptr_t spawnval; |
| 4273 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4274 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4275 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4276 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4277 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4278 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4279 | PyUnicode_FSConverter, |
| 4280 | &opath, &argv)) |
| 4281 | return NULL; |
| 4282 | path = PyBytes_AsString(opath); |
| 4283 | if (PyList_Check(argv)) { |
| 4284 | argc = PyList_Size(argv); |
| 4285 | getitem = PyList_GetItem; |
| 4286 | } |
| 4287 | else if (PyTuple_Check(argv)) { |
| 4288 | argc = PyTuple_Size(argv); |
| 4289 | getitem = PyTuple_GetItem; |
| 4290 | } |
| 4291 | else { |
| 4292 | PyErr_SetString(PyExc_TypeError, |
| 4293 | "spawnvp() arg 2 must be a tuple or list"); |
| 4294 | Py_DECREF(opath); |
| 4295 | return NULL; |
| 4296 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4297 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4298 | argvlist = PyMem_NEW(char *, argc+1); |
| 4299 | if (argvlist == NULL) { |
| 4300 | Py_DECREF(opath); |
| 4301 | return PyErr_NoMemory(); |
| 4302 | } |
| 4303 | for (i = 0; i < argc; i++) { |
| 4304 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4305 | &argvlist[i])) { |
| 4306 | free_string_array(argvlist, i); |
| 4307 | PyErr_SetString( |
| 4308 | PyExc_TypeError, |
| 4309 | "spawnvp() arg 2 must contain only strings"); |
| 4310 | Py_DECREF(opath); |
| 4311 | return NULL; |
| 4312 | } |
| 4313 | } |
| 4314 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4316 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4317 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4318 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4319 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4320 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4321 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4322 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4323 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4324 | free_string_array(argvlist, argc); |
| 4325 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4326 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4327 | if (spawnval == -1) |
| 4328 | return posix_error(); |
| 4329 | else |
| 4330 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | |
| 4334 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4335 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4336 | Execute the program 'file' in a new process, using the environment\n\ |
| 4337 | search path to find the file.\n\ |
| 4338 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4339 | mode: mode of process creation\n\ |
| 4340 | file: executable file name\n\ |
| 4341 | args: tuple or list of arguments\n\ |
| 4342 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4343 | |
| 4344 | static PyObject * |
| 4345 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4346 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4347 | PyObject *opath |
| 4348 | char *path; |
| 4349 | PyObject *argv, *env; |
| 4350 | char **argvlist; |
| 4351 | char **envlist; |
| 4352 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4353 | int mode; |
| 4354 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4355 | Py_intptr_t spawnval; |
| 4356 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4357 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4359 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4360 | argv is a list or tuple of strings and env is a dictionary |
| 4361 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4362 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4363 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4364 | PyUnicode_FSConverter, |
| 4365 | &opath, &argv, &env)) |
| 4366 | return NULL; |
| 4367 | path = PyBytes_AsString(opath); |
| 4368 | if (PyList_Check(argv)) { |
| 4369 | argc = PyList_Size(argv); |
| 4370 | getitem = PyList_GetItem; |
| 4371 | } |
| 4372 | else if (PyTuple_Check(argv)) { |
| 4373 | argc = PyTuple_Size(argv); |
| 4374 | getitem = PyTuple_GetItem; |
| 4375 | } |
| 4376 | else { |
| 4377 | PyErr_SetString(PyExc_TypeError, |
| 4378 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4379 | goto fail_0; |
| 4380 | } |
| 4381 | if (!PyMapping_Check(env)) { |
| 4382 | PyErr_SetString(PyExc_TypeError, |
| 4383 | "spawnvpe() arg 3 must be a mapping object"); |
| 4384 | goto fail_0; |
| 4385 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4387 | argvlist = PyMem_NEW(char *, argc+1); |
| 4388 | if (argvlist == NULL) { |
| 4389 | PyErr_NoMemory(); |
| 4390 | goto fail_0; |
| 4391 | } |
| 4392 | for (i = 0; i < argc; i++) { |
| 4393 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4394 | &argvlist[i])) |
| 4395 | { |
| 4396 | lastarg = i; |
| 4397 | goto fail_1; |
| 4398 | } |
| 4399 | } |
| 4400 | lastarg = argc; |
| 4401 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4402 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4403 | envlist = parse_envlist(env, &envc); |
| 4404 | if (envlist == NULL) |
| 4405 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4406 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4407 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4408 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4409 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4410 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4411 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4412 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4413 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4415 | if (spawnval == -1) |
| 4416 | (void) posix_error(); |
| 4417 | else |
| 4418 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4419 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4420 | while (--envc >= 0) |
| 4421 | PyMem_DEL(envlist[envc]); |
| 4422 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4423 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4424 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4425 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4426 | Py_DECREF(opath); |
| 4427 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4428 | } |
| 4429 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4430 | #endif /* HAVE_SPAWNV */ |
| 4431 | |
| 4432 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4433 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4434 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4435 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4436 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4437 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4438 | 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] | 4439 | |
| 4440 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4441 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4442 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4443 | pid_t pid; |
| 4444 | int result = 0; |
| 4445 | _PyImport_AcquireLock(); |
| 4446 | pid = fork1(); |
| 4447 | if (pid == 0) { |
| 4448 | /* child: this clobbers and resets the import lock. */ |
| 4449 | PyOS_AfterFork(); |
| 4450 | } else { |
| 4451 | /* parent: release the import lock. */ |
| 4452 | result = _PyImport_ReleaseLock(); |
| 4453 | } |
| 4454 | if (pid == -1) |
| 4455 | return posix_error(); |
| 4456 | if (result < 0) { |
| 4457 | /* Don't clobber the OSError if the fork failed. */ |
| 4458 | PyErr_SetString(PyExc_RuntimeError, |
| 4459 | "not holding the import lock"); |
| 4460 | return NULL; |
| 4461 | } |
| 4462 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4463 | } |
| 4464 | #endif |
| 4465 | |
| 4466 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4467 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4468 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4469 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4470 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4471 | 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] | 4472 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4473 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4474 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4475 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4476 | pid_t pid; |
| 4477 | int result = 0; |
| 4478 | _PyImport_AcquireLock(); |
| 4479 | pid = fork(); |
| 4480 | if (pid == 0) { |
| 4481 | /* child: this clobbers and resets the import lock. */ |
| 4482 | PyOS_AfterFork(); |
| 4483 | } else { |
| 4484 | /* parent: release the import lock. */ |
| 4485 | result = _PyImport_ReleaseLock(); |
| 4486 | } |
| 4487 | if (pid == -1) |
| 4488 | return posix_error(); |
| 4489 | if (result < 0) { |
| 4490 | /* Don't clobber the OSError if the fork failed. */ |
| 4491 | PyErr_SetString(PyExc_RuntimeError, |
| 4492 | "not holding the import lock"); |
| 4493 | return NULL; |
| 4494 | } |
| 4495 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4496 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4497 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4498 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4499 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 4500 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 4501 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4502 | #define DEV_PTY_FILE "/dev/ptc" |
| 4503 | #define HAVE_DEV_PTMX |
| 4504 | #else |
| 4505 | #define DEV_PTY_FILE "/dev/ptmx" |
| 4506 | #endif |
| 4507 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4508 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4509 | #ifdef HAVE_PTY_H |
| 4510 | #include <pty.h> |
| 4511 | #else |
| 4512 | #ifdef HAVE_LIBUTIL_H |
| 4513 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 4514 | #else |
| 4515 | #ifdef HAVE_UTIL_H |
| 4516 | #include <util.h> |
| 4517 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4518 | #endif /* HAVE_LIBUTIL_H */ |
| 4519 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 4520 | #ifdef HAVE_STROPTS_H |
| 4521 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4522 | #endif |
| 4523 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4524 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4525 | #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] | 4526 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4527 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4528 | 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] | 4529 | |
| 4530 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4531 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4532 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4533 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4534 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4535 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4536 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4537 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4538 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4539 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4540 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4541 | #endif |
| 4542 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4543 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4544 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4545 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 4546 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4547 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4548 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 4549 | if (slave_name == NULL) |
| 4550 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4551 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4552 | slave_fd = open(slave_name, O_RDWR); |
| 4553 | if (slave_fd < 0) |
| 4554 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4555 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4556 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 4557 | if (master_fd < 0) |
| 4558 | return posix_error(); |
| 4559 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 4560 | /* change permission of slave */ |
| 4561 | if (grantpt(master_fd) < 0) { |
| 4562 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4563 | return posix_error(); |
| 4564 | } |
| 4565 | /* unlock slave */ |
| 4566 | if (unlockpt(master_fd) < 0) { |
| 4567 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4568 | return posix_error(); |
| 4569 | } |
| 4570 | PyOS_setsig(SIGCHLD, sig_saved); |
| 4571 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 4572 | if (slave_name == NULL) |
| 4573 | return posix_error(); |
| 4574 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 4575 | if (slave_fd < 0) |
| 4576 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 4577 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4578 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 4579 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 4580 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4581 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 4582 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4583 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4584 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4585 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4586 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 4587 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4588 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 4589 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4590 | |
| 4591 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4592 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4593 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4594 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 4595 | 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] | 4596 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4597 | |
| 4598 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4599 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4600 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4601 | int master_fd = -1, result = 0; |
| 4602 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4603 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4604 | _PyImport_AcquireLock(); |
| 4605 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 4606 | if (pid == 0) { |
| 4607 | /* child: this clobbers and resets the import lock. */ |
| 4608 | PyOS_AfterFork(); |
| 4609 | } else { |
| 4610 | /* parent: release the import lock. */ |
| 4611 | result = _PyImport_ReleaseLock(); |
| 4612 | } |
| 4613 | if (pid == -1) |
| 4614 | return posix_error(); |
| 4615 | if (result < 0) { |
| 4616 | /* Don't clobber the OSError if the fork failed. */ |
| 4617 | PyErr_SetString(PyExc_RuntimeError, |
| 4618 | "not holding the import lock"); |
| 4619 | return NULL; |
| 4620 | } |
| 4621 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 4622 | } |
| 4623 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4624 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4625 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4626 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4627 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4628 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4629 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4630 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4631 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4632 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4633 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4634 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4635 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4636 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4637 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4638 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4639 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4640 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4641 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4642 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4643 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4644 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4645 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4646 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4647 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4648 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4649 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4650 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4651 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4652 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4653 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4654 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4655 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4656 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4657 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4658 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4659 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4660 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4661 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4662 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4663 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4664 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4665 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4666 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4667 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4668 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4669 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4670 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4671 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4672 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4675 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4676 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4677 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4678 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4679 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4680 | |
| 4681 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4682 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4683 | { |
| 4684 | PyObject *result = NULL; |
| 4685 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4686 | #ifdef NGROUPS_MAX |
| 4687 | #define MAX_GROUPS NGROUPS_MAX |
| 4688 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4689 | /* 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] | 4690 | #define MAX_GROUPS 64 |
| 4691 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4692 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4693 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4694 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4695 | * This is a helper variable to store the intermediate result when |
| 4696 | * that happens. |
| 4697 | * |
| 4698 | * To keep the code readable the OSX behaviour is unconditional, |
| 4699 | * according to the POSIX spec this should be safe on all unix-y |
| 4700 | * systems. |
| 4701 | */ |
| 4702 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4703 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4704 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4705 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4706 | if (n < 0) { |
| 4707 | if (errno == EINVAL) { |
| 4708 | n = getgroups(0, NULL); |
| 4709 | if (n == -1) { |
| 4710 | return posix_error(); |
| 4711 | } |
| 4712 | if (n == 0) { |
| 4713 | /* Avoid malloc(0) */ |
| 4714 | alt_grouplist = grouplist; |
| 4715 | } else { |
| 4716 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 4717 | if (alt_grouplist == NULL) { |
| 4718 | errno = EINVAL; |
| 4719 | return posix_error(); |
| 4720 | } |
| 4721 | n = getgroups(n, alt_grouplist); |
| 4722 | if (n == -1) { |
| 4723 | PyMem_Free(alt_grouplist); |
| 4724 | return posix_error(); |
| 4725 | } |
| 4726 | } |
| 4727 | } else { |
| 4728 | return posix_error(); |
| 4729 | } |
| 4730 | } |
| 4731 | result = PyList_New(n); |
| 4732 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4733 | int i; |
| 4734 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4735 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4736 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4737 | Py_DECREF(result); |
| 4738 | result = NULL; |
| 4739 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4740 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4741 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4742 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | if (alt_grouplist != grouplist) { |
| 4746 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4747 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4748 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4749 | return result; |
| 4750 | } |
| 4751 | #endif |
| 4752 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4753 | #ifdef HAVE_INITGROUPS |
| 4754 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 4755 | "initgroups(username, gid) -> None\n\n\ |
| 4756 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 4757 | the groups of which the specified username is a member, plus the specified\n\ |
| 4758 | group id."); |
| 4759 | |
| 4760 | static PyObject * |
| 4761 | posix_initgroups(PyObject *self, PyObject *args) |
| 4762 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4763 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4764 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4765 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4766 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4767 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4768 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 4769 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4770 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4771 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4772 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 4773 | res = initgroups(username, (gid_t) gid); |
| 4774 | Py_DECREF(oname); |
| 4775 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4776 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4777 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4778 | Py_INCREF(Py_None); |
| 4779 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 4780 | } |
| 4781 | #endif |
| 4782 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4783 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4784 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4785 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4786 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4787 | |
| 4788 | static PyObject * |
| 4789 | posix_getpgid(PyObject *self, PyObject *args) |
| 4790 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4791 | pid_t pid, pgid; |
| 4792 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 4793 | return NULL; |
| 4794 | pgid = getpgid(pid); |
| 4795 | if (pgid < 0) |
| 4796 | return posix_error(); |
| 4797 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4798 | } |
| 4799 | #endif /* HAVE_GETPGID */ |
| 4800 | |
| 4801 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4802 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4803 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4804 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4805 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4806 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4807 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4808 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4809 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4810 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4811 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4812 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4813 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4814 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4815 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4816 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4817 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4818 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4819 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4820 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4821 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 4822 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4823 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4824 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4825 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4826 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4827 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4828 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4829 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4830 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4831 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4832 | return posix_error(); |
| 4833 | Py_INCREF(Py_None); |
| 4834 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4837 | #endif /* HAVE_SETPGRP */ |
| 4838 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4839 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4840 | |
| 4841 | #ifdef MS_WINDOWS |
| 4842 | #include <tlhelp32.h> |
| 4843 | |
| 4844 | static PyObject* |
| 4845 | win32_getppid() |
| 4846 | { |
| 4847 | HANDLE snapshot; |
| 4848 | pid_t mypid; |
| 4849 | PyObject* result = NULL; |
| 4850 | BOOL have_record; |
| 4851 | PROCESSENTRY32 pe; |
| 4852 | |
| 4853 | mypid = getpid(); /* This function never fails */ |
| 4854 | |
| 4855 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 4856 | if (snapshot == INVALID_HANDLE_VALUE) |
| 4857 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 4858 | |
| 4859 | pe.dwSize = sizeof(pe); |
| 4860 | have_record = Process32First(snapshot, &pe); |
| 4861 | while (have_record) { |
| 4862 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 4863 | /* We could cache the ulong value in a static variable. */ |
| 4864 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 4865 | break; |
| 4866 | } |
| 4867 | |
| 4868 | have_record = Process32Next(snapshot, &pe); |
| 4869 | } |
| 4870 | |
| 4871 | /* If our loop exits and our pid was not found (result will be NULL) |
| 4872 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 4873 | * error anyway, so let's raise it. */ |
| 4874 | if (!result) |
| 4875 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 4876 | |
| 4877 | CloseHandle(snapshot); |
| 4878 | |
| 4879 | return result; |
| 4880 | } |
| 4881 | #endif /*MS_WINDOWS*/ |
| 4882 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4883 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4884 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4885 | Return the parent's process id. If the parent process has already exited,\n\ |
| 4886 | Windows machines will still return its id; others systems will return the id\n\ |
| 4887 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4888 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4889 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4890 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4891 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4892 | #ifdef MS_WINDOWS |
| 4893 | return win32_getppid(); |
| 4894 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4895 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4896 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 4897 | } |
| 4898 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4899 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4900 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4901 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4902 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4903 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4904 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4905 | |
| 4906 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4907 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4908 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4909 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4910 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4911 | wchar_t user_name[UNLEN + 1]; |
| 4912 | DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]); |
| 4913 | |
| 4914 | if (GetUserNameW(user_name, &num_chars)) { |
| 4915 | /* num_chars is the number of unicode chars plus null terminator */ |
| 4916 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4917 | } |
| 4918 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4919 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 4920 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4921 | char *name; |
| 4922 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4923 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4924 | errno = 0; |
| 4925 | name = getlogin(); |
| 4926 | if (name == NULL) { |
| 4927 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4928 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4929 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4930 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4931 | } |
| 4932 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 4933 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4934 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4935 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4936 | return result; |
| 4937 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 4938 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4939 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4940 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4941 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4942 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4943 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4944 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4945 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4946 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4947 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4948 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4949 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4950 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4951 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4952 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4953 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4954 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4955 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4956 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4957 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4958 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4959 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4960 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4961 | pid_t pid; |
| 4962 | int sig; |
| 4963 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 4964 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4965 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4966 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4967 | APIRET rc; |
| 4968 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4969 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4970 | |
| 4971 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4972 | APIRET rc; |
| 4973 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4974 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4975 | |
| 4976 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4977 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4978 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4979 | if (kill(pid, sig) == -1) |
| 4980 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4981 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4982 | Py_INCREF(Py_None); |
| 4983 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4984 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4985 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4986 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4987 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4988 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4989 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4990 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4991 | |
| 4992 | static PyObject * |
| 4993 | posix_killpg(PyObject *self, PyObject *args) |
| 4994 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4995 | int sig; |
| 4996 | pid_t pgid; |
| 4997 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4998 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4999 | take the same type. Moreover, pid_t is always at least as wide as |
| 5000 | int (else compilation of this module fails), which is safe. */ |
| 5001 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5002 | return NULL; |
| 5003 | if (killpg(pgid, sig) == -1) |
| 5004 | return posix_error(); |
| 5005 | Py_INCREF(Py_None); |
| 5006 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5007 | } |
| 5008 | #endif |
| 5009 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5010 | #ifdef MS_WINDOWS |
| 5011 | PyDoc_STRVAR(win32_kill__doc__, |
| 5012 | "kill(pid, sig)\n\n\ |
| 5013 | Kill a process with a signal."); |
| 5014 | |
| 5015 | static PyObject * |
| 5016 | win32_kill(PyObject *self, PyObject *args) |
| 5017 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5018 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5019 | DWORD pid, sig, err; |
| 5020 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5021 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5022 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5023 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5024 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5025 | /* Console processes which share a common console can be sent CTRL+C or |
| 5026 | CTRL+BREAK events, provided they handle said events. */ |
| 5027 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5028 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5029 | err = GetLastError(); |
| 5030 | PyErr_SetFromWindowsErr(err); |
| 5031 | } |
| 5032 | else |
| 5033 | Py_RETURN_NONE; |
| 5034 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5035 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5036 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5037 | attempt to open and terminate the process. */ |
| 5038 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5039 | if (handle == NULL) { |
| 5040 | err = GetLastError(); |
| 5041 | return PyErr_SetFromWindowsErr(err); |
| 5042 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5043 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5044 | if (TerminateProcess(handle, sig) == 0) { |
| 5045 | err = GetLastError(); |
| 5046 | result = PyErr_SetFromWindowsErr(err); |
| 5047 | } else { |
| 5048 | Py_INCREF(Py_None); |
| 5049 | result = Py_None; |
| 5050 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5051 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5052 | CloseHandle(handle); |
| 5053 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5054 | } |
| 5055 | #endif /* MS_WINDOWS */ |
| 5056 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5057 | #ifdef HAVE_PLOCK |
| 5058 | |
| 5059 | #ifdef HAVE_SYS_LOCK_H |
| 5060 | #include <sys/lock.h> |
| 5061 | #endif |
| 5062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5063 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5064 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5065 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5066 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5067 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5068 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5069 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5070 | int op; |
| 5071 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5072 | return NULL; |
| 5073 | if (plock(op) == -1) |
| 5074 | return posix_error(); |
| 5075 | Py_INCREF(Py_None); |
| 5076 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5077 | } |
| 5078 | #endif |
| 5079 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5080 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5081 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5082 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5083 | Set the current process's user id."); |
| 5084 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5085 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5086 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5087 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5088 | long uid_arg; |
| 5089 | uid_t uid; |
| 5090 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5091 | return NULL; |
| 5092 | uid = uid_arg; |
| 5093 | if (uid != uid_arg) { |
| 5094 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5095 | return NULL; |
| 5096 | } |
| 5097 | if (setuid(uid) < 0) |
| 5098 | return posix_error(); |
| 5099 | Py_INCREF(Py_None); |
| 5100 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5101 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5102 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5103 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5104 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5105 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5106 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5107 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5108 | Set the current process's effective user id."); |
| 5109 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5110 | static PyObject * |
| 5111 | posix_seteuid (PyObject *self, PyObject *args) |
| 5112 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5113 | long euid_arg; |
| 5114 | uid_t euid; |
| 5115 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5116 | return NULL; |
| 5117 | euid = euid_arg; |
| 5118 | if (euid != euid_arg) { |
| 5119 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5120 | return NULL; |
| 5121 | } |
| 5122 | if (seteuid(euid) < 0) { |
| 5123 | return posix_error(); |
| 5124 | } else { |
| 5125 | Py_INCREF(Py_None); |
| 5126 | return Py_None; |
| 5127 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5128 | } |
| 5129 | #endif /* HAVE_SETEUID */ |
| 5130 | |
| 5131 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5132 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5133 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5134 | Set the current process's effective group id."); |
| 5135 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5136 | static PyObject * |
| 5137 | posix_setegid (PyObject *self, PyObject *args) |
| 5138 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5139 | long egid_arg; |
| 5140 | gid_t egid; |
| 5141 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5142 | return NULL; |
| 5143 | egid = egid_arg; |
| 5144 | if (egid != egid_arg) { |
| 5145 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5146 | return NULL; |
| 5147 | } |
| 5148 | if (setegid(egid) < 0) { |
| 5149 | return posix_error(); |
| 5150 | } else { |
| 5151 | Py_INCREF(Py_None); |
| 5152 | return Py_None; |
| 5153 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5154 | } |
| 5155 | #endif /* HAVE_SETEGID */ |
| 5156 | |
| 5157 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5158 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5159 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5160 | Set the current process's real and effective user ids."); |
| 5161 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5162 | static PyObject * |
| 5163 | posix_setreuid (PyObject *self, PyObject *args) |
| 5164 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5165 | long ruid_arg, euid_arg; |
| 5166 | uid_t ruid, euid; |
| 5167 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5168 | return NULL; |
| 5169 | if (ruid_arg == -1) |
| 5170 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5171 | else |
| 5172 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5173 | if (euid_arg == -1) |
| 5174 | euid = (uid_t)-1; |
| 5175 | else |
| 5176 | euid = euid_arg; |
| 5177 | if ((euid_arg != -1 && euid != euid_arg) || |
| 5178 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 5179 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5180 | return NULL; |
| 5181 | } |
| 5182 | if (setreuid(ruid, euid) < 0) { |
| 5183 | return posix_error(); |
| 5184 | } else { |
| 5185 | Py_INCREF(Py_None); |
| 5186 | return Py_None; |
| 5187 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5188 | } |
| 5189 | #endif /* HAVE_SETREUID */ |
| 5190 | |
| 5191 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5192 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5193 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5194 | Set the current process's real and effective group ids."); |
| 5195 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5196 | static PyObject * |
| 5197 | posix_setregid (PyObject *self, PyObject *args) |
| 5198 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5199 | long rgid_arg, egid_arg; |
| 5200 | gid_t rgid, egid; |
| 5201 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 5202 | return NULL; |
| 5203 | if (rgid_arg == -1) |
| 5204 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 5205 | else |
| 5206 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 5207 | if (egid_arg == -1) |
| 5208 | egid = (gid_t)-1; |
| 5209 | else |
| 5210 | egid = egid_arg; |
| 5211 | if ((egid_arg != -1 && egid != egid_arg) || |
| 5212 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 5213 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5214 | return NULL; |
| 5215 | } |
| 5216 | if (setregid(rgid, egid) < 0) { |
| 5217 | return posix_error(); |
| 5218 | } else { |
| 5219 | Py_INCREF(Py_None); |
| 5220 | return Py_None; |
| 5221 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5222 | } |
| 5223 | #endif /* HAVE_SETREGID */ |
| 5224 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5225 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5226 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5227 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5228 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5229 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5230 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5231 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5232 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5233 | long gid_arg; |
| 5234 | gid_t gid; |
| 5235 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 5236 | return NULL; |
| 5237 | gid = gid_arg; |
| 5238 | if (gid != gid_arg) { |
| 5239 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5240 | return NULL; |
| 5241 | } |
| 5242 | if (setgid(gid) < 0) |
| 5243 | return posix_error(); |
| 5244 | Py_INCREF(Py_None); |
| 5245 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5246 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5247 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5248 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5249 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5250 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5251 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5252 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5253 | |
| 5254 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 5255 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5256 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5257 | int i, len; |
| 5258 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5259 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5260 | if (!PySequence_Check(groups)) { |
| 5261 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5262 | return NULL; |
| 5263 | } |
| 5264 | len = PySequence_Size(groups); |
| 5265 | if (len > MAX_GROUPS) { |
| 5266 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5267 | return NULL; |
| 5268 | } |
| 5269 | for(i = 0; i < len; i++) { |
| 5270 | PyObject *elem; |
| 5271 | elem = PySequence_GetItem(groups, i); |
| 5272 | if (!elem) |
| 5273 | return NULL; |
| 5274 | if (!PyLong_Check(elem)) { |
| 5275 | PyErr_SetString(PyExc_TypeError, |
| 5276 | "groups must be integers"); |
| 5277 | Py_DECREF(elem); |
| 5278 | return NULL; |
| 5279 | } else { |
| 5280 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5281 | if (PyErr_Occurred()) { |
| 5282 | PyErr_SetString(PyExc_TypeError, |
| 5283 | "group id too big"); |
| 5284 | Py_DECREF(elem); |
| 5285 | return NULL; |
| 5286 | } |
| 5287 | grouplist[i] = x; |
| 5288 | /* read back the value to see if it fitted in gid_t */ |
| 5289 | if (grouplist[i] != x) { |
| 5290 | PyErr_SetString(PyExc_TypeError, |
| 5291 | "group id too big"); |
| 5292 | Py_DECREF(elem); |
| 5293 | return NULL; |
| 5294 | } |
| 5295 | } |
| 5296 | Py_DECREF(elem); |
| 5297 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5298 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5299 | if (setgroups(len, grouplist) < 0) |
| 5300 | return posix_error(); |
| 5301 | Py_INCREF(Py_None); |
| 5302 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5303 | } |
| 5304 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5305 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5306 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 5307 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 5308 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5309 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5310 | PyObject *result; |
| 5311 | static PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5312 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5313 | if (pid == -1) |
| 5314 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5316 | if (struct_rusage == NULL) { |
| 5317 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 5318 | if (m == NULL) |
| 5319 | return NULL; |
| 5320 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5321 | Py_DECREF(m); |
| 5322 | if (struct_rusage == NULL) |
| 5323 | return NULL; |
| 5324 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5325 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5326 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5327 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5328 | if (!result) |
| 5329 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5330 | |
| 5331 | #ifndef doubletime |
| 5332 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5333 | #endif |
| 5334 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5335 | PyStructSequence_SET_ITEM(result, 0, |
| 5336 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5337 | PyStructSequence_SET_ITEM(result, 1, |
| 5338 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5339 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5340 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 5341 | SET_INT(result, 2, ru->ru_maxrss); |
| 5342 | SET_INT(result, 3, ru->ru_ixrss); |
| 5343 | SET_INT(result, 4, ru->ru_idrss); |
| 5344 | SET_INT(result, 5, ru->ru_isrss); |
| 5345 | SET_INT(result, 6, ru->ru_minflt); |
| 5346 | SET_INT(result, 7, ru->ru_majflt); |
| 5347 | SET_INT(result, 8, ru->ru_nswap); |
| 5348 | SET_INT(result, 9, ru->ru_inblock); |
| 5349 | SET_INT(result, 10, ru->ru_oublock); |
| 5350 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5351 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5352 | SET_INT(result, 13, ru->ru_nsignals); |
| 5353 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5354 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5355 | #undef SET_INT |
| 5356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5357 | if (PyErr_Occurred()) { |
| 5358 | Py_DECREF(result); |
| 5359 | return NULL; |
| 5360 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5361 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5362 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5363 | } |
| 5364 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 5365 | |
| 5366 | #ifdef HAVE_WAIT3 |
| 5367 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5368 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5369 | Wait for completion of a child process."); |
| 5370 | |
| 5371 | static PyObject * |
| 5372 | posix_wait3(PyObject *self, PyObject *args) |
| 5373 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5374 | pid_t pid; |
| 5375 | int options; |
| 5376 | struct rusage ru; |
| 5377 | WAIT_TYPE status; |
| 5378 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5379 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5380 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5381 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5382 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5383 | Py_BEGIN_ALLOW_THREADS |
| 5384 | pid = wait3(&status, options, &ru); |
| 5385 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5387 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5388 | } |
| 5389 | #endif /* HAVE_WAIT3 */ |
| 5390 | |
| 5391 | #ifdef HAVE_WAIT4 |
| 5392 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5393 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5394 | Wait for completion of a given child process."); |
| 5395 | |
| 5396 | static PyObject * |
| 5397 | posix_wait4(PyObject *self, PyObject *args) |
| 5398 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5399 | pid_t pid; |
| 5400 | int options; |
| 5401 | struct rusage ru; |
| 5402 | WAIT_TYPE status; |
| 5403 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5404 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5405 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 5406 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5407 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5408 | Py_BEGIN_ALLOW_THREADS |
| 5409 | pid = wait4(pid, &status, options, &ru); |
| 5410 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5411 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5412 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5413 | } |
| 5414 | #endif /* HAVE_WAIT4 */ |
| 5415 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5416 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 5417 | PyDoc_STRVAR(posix_waitid__doc__, |
| 5418 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 5419 | Wait for the completion of one or more child processes.\n\n\ |
| 5420 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 5421 | id specifies the pid to wait on.\n\ |
| 5422 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 5423 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 5424 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 5425 | no children in a waitable state."); |
| 5426 | |
| 5427 | static PyObject * |
| 5428 | posix_waitid(PyObject *self, PyObject *args) |
| 5429 | { |
| 5430 | PyObject *result; |
| 5431 | idtype_t idtype; |
| 5432 | id_t id; |
| 5433 | int options, res; |
| 5434 | siginfo_t si; |
| 5435 | si.si_pid = 0; |
| 5436 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 5437 | return NULL; |
| 5438 | Py_BEGIN_ALLOW_THREADS |
| 5439 | res = waitid(idtype, id, &si, options); |
| 5440 | Py_END_ALLOW_THREADS |
| 5441 | if (res == -1) |
| 5442 | return posix_error(); |
| 5443 | |
| 5444 | if (si.si_pid == 0) |
| 5445 | Py_RETURN_NONE; |
| 5446 | |
| 5447 | result = PyStructSequence_New(&WaitidResultType); |
| 5448 | if (!result) |
| 5449 | return NULL; |
| 5450 | |
| 5451 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 5452 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 5453 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 5454 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 5455 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 5456 | if (PyErr_Occurred()) { |
| 5457 | Py_DECREF(result); |
| 5458 | return NULL; |
| 5459 | } |
| 5460 | |
| 5461 | return result; |
| 5462 | } |
| 5463 | #endif |
| 5464 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5465 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5466 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5467 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5470 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5471 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5473 | pid_t pid; |
| 5474 | int options; |
| 5475 | WAIT_TYPE status; |
| 5476 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5477 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5478 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 5479 | return NULL; |
| 5480 | Py_BEGIN_ALLOW_THREADS |
| 5481 | pid = waitpid(pid, &status, options); |
| 5482 | Py_END_ALLOW_THREADS |
| 5483 | if (pid == -1) |
| 5484 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5485 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5486 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5487 | } |
| 5488 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5489 | #elif defined(HAVE_CWAIT) |
| 5490 | |
| 5491 | /* 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] | 5492 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5493 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5494 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5495 | |
| 5496 | static PyObject * |
| 5497 | posix_waitpid(PyObject *self, PyObject *args) |
| 5498 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5499 | Py_intptr_t pid; |
| 5500 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5501 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5502 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 5503 | return NULL; |
| 5504 | Py_BEGIN_ALLOW_THREADS |
| 5505 | pid = _cwait(&status, pid, options); |
| 5506 | Py_END_ALLOW_THREADS |
| 5507 | if (pid == -1) |
| 5508 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5509 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5510 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5511 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5512 | } |
| 5513 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5514 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5515 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5516 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5517 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5518 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5519 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5520 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5521 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5522 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5523 | pid_t pid; |
| 5524 | WAIT_TYPE status; |
| 5525 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5526 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5527 | Py_BEGIN_ALLOW_THREADS |
| 5528 | pid = wait(&status); |
| 5529 | Py_END_ALLOW_THREADS |
| 5530 | if (pid == -1) |
| 5531 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5532 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5533 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5534 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5535 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5536 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5537 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5538 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5539 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5540 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5542 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5543 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5544 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5545 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5546 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5547 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5548 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5549 | return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", |
| 5550 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5551 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5552 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5553 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5554 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5557 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5558 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5559 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5560 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5561 | 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] | 5562 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5563 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5564 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5565 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5566 | PyObject* v; |
| 5567 | char buf[MAXPATHLEN]; |
| 5568 | PyObject *opath; |
| 5569 | char *path; |
| 5570 | int n; |
| 5571 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5572 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5573 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 5574 | PyUnicode_FSConverter, &opath)) |
| 5575 | return NULL; |
| 5576 | path = PyBytes_AsString(opath); |
| 5577 | v = PySequence_GetItem(args, 0); |
| 5578 | if (v == NULL) { |
| 5579 | Py_DECREF(opath); |
| 5580 | return NULL; |
| 5581 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5582 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5583 | if (PyUnicode_Check(v)) { |
| 5584 | arg_is_unicode = 1; |
| 5585 | } |
| 5586 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5587 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5588 | Py_BEGIN_ALLOW_THREADS |
| 5589 | n = readlink(path, buf, (int) sizeof buf); |
| 5590 | Py_END_ALLOW_THREADS |
| 5591 | if (n < 0) |
| 5592 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5593 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5594 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 5595 | if (arg_is_unicode) |
| 5596 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 5597 | else |
| 5598 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5599 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5600 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5601 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5602 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5603 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5604 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5605 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5606 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5607 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5608 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5609 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5610 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5611 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5612 | } |
| 5613 | #endif /* HAVE_SYMLINK */ |
| 5614 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5615 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 5616 | |
| 5617 | PyDoc_STRVAR(win_readlink__doc__, |
| 5618 | "readlink(path) -> path\n\n\ |
| 5619 | Return a string representing the path to which the symbolic link points."); |
| 5620 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5621 | /* Windows readlink implementation */ |
| 5622 | static PyObject * |
| 5623 | win_readlink(PyObject *self, PyObject *args) |
| 5624 | { |
| 5625 | wchar_t *path; |
| 5626 | DWORD n_bytes_returned; |
| 5627 | DWORD io_result; |
| 5628 | PyObject *result; |
| 5629 | HANDLE reparse_point_handle; |
| 5630 | |
| 5631 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 5632 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 5633 | wchar_t *print_name; |
| 5634 | |
| 5635 | if (!PyArg_ParseTuple(args, |
| 5636 | "u:readlink", |
| 5637 | &path)) |
| 5638 | return NULL; |
| 5639 | |
| 5640 | /* First get a handle to the reparse point */ |
| 5641 | Py_BEGIN_ALLOW_THREADS |
| 5642 | reparse_point_handle = CreateFileW( |
| 5643 | path, |
| 5644 | 0, |
| 5645 | 0, |
| 5646 | 0, |
| 5647 | OPEN_EXISTING, |
| 5648 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 5649 | 0); |
| 5650 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5651 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5652 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 5653 | { |
| 5654 | return win32_error_unicode("readlink", path); |
| 5655 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5656 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5657 | Py_BEGIN_ALLOW_THREADS |
| 5658 | /* New call DeviceIoControl to read the reparse point */ |
| 5659 | io_result = DeviceIoControl( |
| 5660 | reparse_point_handle, |
| 5661 | FSCTL_GET_REPARSE_POINT, |
| 5662 | 0, 0, /* in buffer */ |
| 5663 | target_buffer, sizeof(target_buffer), |
| 5664 | &n_bytes_returned, |
| 5665 | 0 /* we're not using OVERLAPPED_IO */ |
| 5666 | ); |
| 5667 | CloseHandle(reparse_point_handle); |
| 5668 | Py_END_ALLOW_THREADS |
| 5669 | |
| 5670 | if (io_result==0) |
| 5671 | { |
| 5672 | return win32_error_unicode("readlink", path); |
| 5673 | } |
| 5674 | |
| 5675 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 5676 | { |
| 5677 | PyErr_SetString(PyExc_ValueError, |
| 5678 | "not a symbolic link"); |
| 5679 | return NULL; |
| 5680 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 5681 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 5682 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 5683 | |
| 5684 | result = PyUnicode_FromWideChar(print_name, |
| 5685 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5686 | return result; |
| 5687 | } |
| 5688 | |
| 5689 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 5690 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5691 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5692 | |
| 5693 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 5694 | static int has_CreateSymbolicLinkW = 0; |
| 5695 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 5696 | static int |
| 5697 | check_CreateSymbolicLinkW() |
| 5698 | { |
| 5699 | HINSTANCE hKernel32; |
| 5700 | /* only recheck */ |
| 5701 | if (has_CreateSymbolicLinkW) |
| 5702 | return has_CreateSymbolicLinkW; |
| 5703 | hKernel32 = GetModuleHandle("KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 5704 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 5705 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5706 | if (Py_CreateSymbolicLinkW) |
| 5707 | has_CreateSymbolicLinkW = 1; |
| 5708 | return has_CreateSymbolicLinkW; |
| 5709 | } |
| 5710 | |
| 5711 | PyDoc_STRVAR(win_symlink__doc__, |
| 5712 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 5713 | Create a symbolic link pointing to src named dst.\n\ |
| 5714 | target_is_directory is required if the target is to be interpreted as\n\ |
| 5715 | a directory.\n\ |
| 5716 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 5717 | NotImplementedError otherwise."); |
| 5718 | |
| 5719 | static PyObject * |
| 5720 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 5721 | { |
| 5722 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
| 5723 | PyObject *src, *dest; |
| 5724 | int target_is_directory = 0; |
| 5725 | DWORD res; |
| 5726 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5727 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5728 | if (!check_CreateSymbolicLinkW()) |
| 5729 | { |
| 5730 | /* raise NotImplementedError */ |
| 5731 | return PyErr_Format(PyExc_NotImplementedError, |
| 5732 | "CreateSymbolicLinkW not found"); |
| 5733 | } |
| 5734 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink", |
| 5735 | kwlist, &src, &dest, &target_is_directory)) |
| 5736 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 5737 | |
| 5738 | if (win32_can_symlink == 0) |
| 5739 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 5740 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5741 | if (!convert_to_unicode(&src)) { return NULL; } |
| 5742 | if (!convert_to_unicode(&dest)) { |
| 5743 | Py_DECREF(src); |
| 5744 | return NULL; |
| 5745 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5746 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5747 | /* if src is a directory, ensure target_is_directory==1 */ |
| 5748 | if( |
| 5749 | GetFileAttributesExW( |
| 5750 | PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info |
| 5751 | )) |
| 5752 | { |
| 5753 | target_is_directory = target_is_directory || |
| 5754 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 5755 | } |
| 5756 | |
| 5757 | Py_BEGIN_ALLOW_THREADS |
| 5758 | res = Py_CreateSymbolicLinkW( |
| 5759 | PyUnicode_AsUnicode(dest), |
| 5760 | PyUnicode_AsUnicode(src), |
| 5761 | target_is_directory); |
| 5762 | Py_END_ALLOW_THREADS |
| 5763 | Py_DECREF(src); |
| 5764 | Py_DECREF(dest); |
| 5765 | if (!res) |
| 5766 | { |
| 5767 | return win32_error_unicode("symlink", PyUnicode_AsUnicode(src)); |
| 5768 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5769 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5770 | Py_INCREF(Py_None); |
| 5771 | return Py_None; |
| 5772 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 5773 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5774 | |
| 5775 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5776 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5777 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5778 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5779 | { |
| 5780 | ULONG value = 0; |
| 5781 | |
| 5782 | Py_BEGIN_ALLOW_THREADS |
| 5783 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5784 | Py_END_ALLOW_THREADS |
| 5785 | |
| 5786 | return value; |
| 5787 | } |
| 5788 | |
| 5789 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5790 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5791 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5792 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5793 | return Py_BuildValue("ddddd", |
| 5794 | (double)0 /* t.tms_utime / HZ */, |
| 5795 | (double)0 /* t.tms_stime / HZ */, |
| 5796 | (double)0 /* t.tms_cutime / HZ */, |
| 5797 | (double)0 /* t.tms_cstime / HZ */, |
| 5798 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5799 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5800 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 5801 | #define NEED_TICKS_PER_SECOND |
| 5802 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5803 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5804 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5805 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5806 | struct tms t; |
| 5807 | clock_t c; |
| 5808 | errno = 0; |
| 5809 | c = times(&t); |
| 5810 | if (c == (clock_t) -1) |
| 5811 | return posix_error(); |
| 5812 | return Py_BuildValue("ddddd", |
| 5813 | (double)t.tms_utime / ticks_per_second, |
| 5814 | (double)t.tms_stime / ticks_per_second, |
| 5815 | (double)t.tms_cutime / ticks_per_second, |
| 5816 | (double)t.tms_cstime / ticks_per_second, |
| 5817 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5818 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5819 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5820 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5821 | |
| 5822 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5823 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5824 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5825 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5826 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5827 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5828 | FILETIME create, exit, kernel, user; |
| 5829 | HANDLE hProc; |
| 5830 | hProc = GetCurrentProcess(); |
| 5831 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5832 | /* The fields of a FILETIME structure are the hi and lo part |
| 5833 | of a 64-bit value expressed in 100 nanosecond units. |
| 5834 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5835 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5836 | */ |
| 5837 | return Py_BuildValue( |
| 5838 | "ddddd", |
| 5839 | (double)(user.dwHighDateTime*429.4967296 + |
| 5840 | user.dwLowDateTime*1e-7), |
| 5841 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5842 | kernel.dwLowDateTime*1e-7), |
| 5843 | (double)0, |
| 5844 | (double)0, |
| 5845 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5846 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5847 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5848 | |
| 5849 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5850 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5851 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5852 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5853 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5854 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5855 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5856 | #ifdef HAVE_GETSID |
| 5857 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5858 | "getsid(pid) -> sid\n\n\ |
| 5859 | Call the system call getsid()."); |
| 5860 | |
| 5861 | static PyObject * |
| 5862 | posix_getsid(PyObject *self, PyObject *args) |
| 5863 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5864 | pid_t pid; |
| 5865 | int sid; |
| 5866 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 5867 | return NULL; |
| 5868 | sid = getsid(pid); |
| 5869 | if (sid < 0) |
| 5870 | return posix_error(); |
| 5871 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5872 | } |
| 5873 | #endif /* HAVE_GETSID */ |
| 5874 | |
| 5875 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5876 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5877 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5878 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5879 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5880 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5881 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5882 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5883 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5884 | if (setsid() < 0) |
| 5885 | return posix_error(); |
| 5886 | Py_INCREF(Py_None); |
| 5887 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5888 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5889 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5890 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5891 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5892 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5893 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5894 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5895 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5896 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5897 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5898 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5899 | pid_t pid; |
| 5900 | int pgrp; |
| 5901 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 5902 | return NULL; |
| 5903 | if (setpgid(pid, pgrp) < 0) |
| 5904 | return posix_error(); |
| 5905 | Py_INCREF(Py_None); |
| 5906 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5907 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5908 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5909 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5910 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5911 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5912 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5913 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5914 | 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] | 5915 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5916 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5917 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5918 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5919 | int fd; |
| 5920 | pid_t pgid; |
| 5921 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 5922 | return NULL; |
| 5923 | pgid = tcgetpgrp(fd); |
| 5924 | if (pgid < 0) |
| 5925 | return posix_error(); |
| 5926 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5927 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5928 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5929 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5930 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5931 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5932 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5933 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5934 | 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] | 5935 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5936 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5937 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5938 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5939 | int fd; |
| 5940 | pid_t pgid; |
| 5941 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 5942 | return NULL; |
| 5943 | if (tcsetpgrp(fd, pgid) < 0) |
| 5944 | return posix_error(); |
| 5945 | Py_INCREF(Py_None); |
| 5946 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5947 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5948 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5949 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5950 | /* Functions acting on file descriptors */ |
| 5951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5952 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5953 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5954 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5955 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5956 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5957 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5958 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5959 | PyObject *ofile; |
| 5960 | char *file; |
| 5961 | int flag; |
| 5962 | int mode = 0777; |
| 5963 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5964 | |
| 5965 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5966 | PyUnicodeObject *po; |
| 5967 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5968 | Py_BEGIN_ALLOW_THREADS |
| 5969 | /* PyUnicode_AS_UNICODE OK without thread |
| 5970 | lock as it is a simple dereference. */ |
| 5971 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5972 | Py_END_ALLOW_THREADS |
| 5973 | if (fd < 0) |
| 5974 | return posix_error(); |
| 5975 | return PyLong_FromLong((long)fd); |
| 5976 | } |
| 5977 | /* Drop the argument parsing error as narrow strings |
| 5978 | are also valid. */ |
| 5979 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5980 | #endif |
| 5981 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5982 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 5983 | PyUnicode_FSConverter, &ofile, |
| 5984 | &flag, &mode)) |
| 5985 | return NULL; |
| 5986 | file = PyBytes_AsString(ofile); |
| 5987 | Py_BEGIN_ALLOW_THREADS |
| 5988 | fd = open(file, flag, mode); |
| 5989 | Py_END_ALLOW_THREADS |
| 5990 | if (fd < 0) |
| 5991 | return posix_error_with_allocated_filename(ofile); |
| 5992 | Py_DECREF(ofile); |
| 5993 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5994 | } |
| 5995 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5997 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5998 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5999 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6000 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6001 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6002 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6003 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6004 | int fd, res; |
| 6005 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6006 | return NULL; |
| 6007 | if (!_PyVerify_fd(fd)) |
| 6008 | return posix_error(); |
| 6009 | Py_BEGIN_ALLOW_THREADS |
| 6010 | res = close(fd); |
| 6011 | Py_END_ALLOW_THREADS |
| 6012 | if (res < 0) |
| 6013 | return posix_error(); |
| 6014 | Py_INCREF(Py_None); |
| 6015 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6018 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6019 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6020 | "closerange(fd_low, fd_high)\n\n\ |
| 6021 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6022 | |
| 6023 | static PyObject * |
| 6024 | posix_closerange(PyObject *self, PyObject *args) |
| 6025 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6026 | int fd_from, fd_to, i; |
| 6027 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6028 | return NULL; |
| 6029 | Py_BEGIN_ALLOW_THREADS |
| 6030 | for (i = fd_from; i < fd_to; i++) |
| 6031 | if (_PyVerify_fd(i)) |
| 6032 | close(i); |
| 6033 | Py_END_ALLOW_THREADS |
| 6034 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
| 6037 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6038 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6039 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6040 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6041 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6042 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6043 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6044 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6045 | int fd; |
| 6046 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6047 | return NULL; |
| 6048 | if (!_PyVerify_fd(fd)) |
| 6049 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6050 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6051 | if (fd < 0) |
| 6052 | return posix_error(); |
| 6053 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6056 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6057 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6058 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6059 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6060 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6061 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6062 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6064 | int fd, fd2, res; |
| 6065 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6066 | return NULL; |
| 6067 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6068 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6069 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6070 | if (res < 0) |
| 6071 | return posix_error(); |
| 6072 | Py_INCREF(Py_None); |
| 6073 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6074 | } |
| 6075 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6076 | #ifdef HAVE_LOCKF |
| 6077 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6078 | "lockf(fd, cmd, len)\n\n\ |
| 6079 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6080 | fd is an open file descriptor.\n\ |
| 6081 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6082 | F_TEST.\n\ |
| 6083 | len specifies the section of the file to lock."); |
| 6084 | |
| 6085 | static PyObject * |
| 6086 | posix_lockf(PyObject *self, PyObject *args) |
| 6087 | { |
| 6088 | int fd, cmd, res; |
| 6089 | off_t len; |
| 6090 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6091 | &fd, &cmd, _parse_off_t, &len)) |
| 6092 | return NULL; |
| 6093 | |
| 6094 | Py_BEGIN_ALLOW_THREADS |
| 6095 | res = lockf(fd, cmd, len); |
| 6096 | Py_END_ALLOW_THREADS |
| 6097 | |
| 6098 | if (res < 0) |
| 6099 | return posix_error(); |
| 6100 | |
| 6101 | Py_RETURN_NONE; |
| 6102 | } |
| 6103 | #endif |
| 6104 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6105 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6106 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6107 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6108 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6109 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6110 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6111 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6112 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6113 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6114 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6115 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6116 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6117 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6118 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6119 | PyObject *posobj; |
| 6120 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6121 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6122 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6123 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6124 | switch (how) { |
| 6125 | case 0: how = SEEK_SET; break; |
| 6126 | case 1: how = SEEK_CUR; break; |
| 6127 | case 2: how = SEEK_END; break; |
| 6128 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6129 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6130 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6131 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6132 | pos = PyLong_AsLong(posobj); |
| 6133 | #else |
| 6134 | pos = PyLong_AsLongLong(posobj); |
| 6135 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6136 | if (PyErr_Occurred()) |
| 6137 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6138 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6139 | if (!_PyVerify_fd(fd)) |
| 6140 | return posix_error(); |
| 6141 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6142 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6143 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6144 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6145 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6146 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6147 | Py_END_ALLOW_THREADS |
| 6148 | if (res < 0) |
| 6149 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6150 | |
| 6151 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6152 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6153 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6155 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6158 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6159 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6160 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6161 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6162 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6163 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6164 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6165 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6166 | int fd, size; |
| 6167 | Py_ssize_t n; |
| 6168 | PyObject *buffer; |
| 6169 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6170 | return NULL; |
| 6171 | if (size < 0) { |
| 6172 | errno = EINVAL; |
| 6173 | return posix_error(); |
| 6174 | } |
| 6175 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6176 | if (buffer == NULL) |
| 6177 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6178 | if (!_PyVerify_fd(fd)) { |
| 6179 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6180 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6181 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6182 | Py_BEGIN_ALLOW_THREADS |
| 6183 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 6184 | Py_END_ALLOW_THREADS |
| 6185 | if (n < 0) { |
| 6186 | Py_DECREF(buffer); |
| 6187 | return posix_error(); |
| 6188 | } |
| 6189 | if (n != size) |
| 6190 | _PyBytes_Resize(&buffer, n); |
| 6191 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6192 | } |
| 6193 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6194 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 6195 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6196 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6197 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 6198 | { |
| 6199 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6200 | Py_ssize_t blen, total = 0; |
| 6201 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6202 | *iov = PyMem_New(struct iovec, cnt); |
| 6203 | if (*iov == NULL) { |
| 6204 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6205 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6206 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6207 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6208 | *buf = PyMem_New(Py_buffer, cnt); |
| 6209 | if (*buf == NULL) { |
| 6210 | PyMem_Del(*iov); |
| 6211 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6212 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6213 | } |
| 6214 | |
| 6215 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6216 | PyObject *item = PySequence_GetItem(seq, i); |
| 6217 | if (item == NULL) |
| 6218 | goto fail; |
| 6219 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 6220 | Py_DECREF(item); |
| 6221 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6222 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6223 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6224 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6225 | blen = (*buf)[i].len; |
| 6226 | (*iov)[i].iov_len = blen; |
| 6227 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6228 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6229 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6230 | |
| 6231 | fail: |
| 6232 | PyMem_Del(*iov); |
| 6233 | for (j = 0; j < i; j++) { |
| 6234 | PyBuffer_Release(&(*buf)[j]); |
| 6235 | } |
| 6236 | PyMem_Del(*buf); |
| 6237 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
| 6240 | static void |
| 6241 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 6242 | { |
| 6243 | int i; |
| 6244 | PyMem_Del(iov); |
| 6245 | for (i = 0; i < cnt; i++) { |
| 6246 | PyBuffer_Release(&buf[i]); |
| 6247 | } |
| 6248 | PyMem_Del(buf); |
| 6249 | } |
| 6250 | #endif |
| 6251 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6252 | #ifdef HAVE_READV |
| 6253 | PyDoc_STRVAR(posix_readv__doc__, |
| 6254 | "readv(fd, buffers) -> bytesread\n\n\ |
| 6255 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 6256 | is an arbitrary sequence of writable buffers.\n\ |
| 6257 | Returns the total number of bytes read."); |
| 6258 | |
| 6259 | static PyObject * |
| 6260 | posix_readv(PyObject *self, PyObject *args) |
| 6261 | { |
| 6262 | int fd, cnt; |
| 6263 | Py_ssize_t n; |
| 6264 | PyObject *seq; |
| 6265 | struct iovec *iov; |
| 6266 | Py_buffer *buf; |
| 6267 | |
| 6268 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 6269 | return NULL; |
| 6270 | if (!PySequence_Check(seq)) { |
| 6271 | PyErr_SetString(PyExc_TypeError, |
| 6272 | "readv() arg 2 must be a sequence"); |
| 6273 | return NULL; |
| 6274 | } |
| 6275 | cnt = PySequence_Size(seq); |
| 6276 | |
| 6277 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 6278 | return NULL; |
| 6279 | |
| 6280 | Py_BEGIN_ALLOW_THREADS |
| 6281 | n = readv(fd, iov, cnt); |
| 6282 | Py_END_ALLOW_THREADS |
| 6283 | |
| 6284 | iov_cleanup(iov, buf, cnt); |
| 6285 | return PyLong_FromSsize_t(n); |
| 6286 | } |
| 6287 | #endif |
| 6288 | |
| 6289 | #ifdef HAVE_PREAD |
| 6290 | PyDoc_STRVAR(posix_pread__doc__, |
| 6291 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 6292 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 6293 | to buffersize number of bytes. The file offset remains unchanged."); |
| 6294 | |
| 6295 | static PyObject * |
| 6296 | posix_pread(PyObject *self, PyObject *args) |
| 6297 | { |
| 6298 | int fd, size; |
| 6299 | off_t offset; |
| 6300 | Py_ssize_t n; |
| 6301 | PyObject *buffer; |
| 6302 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 6303 | return NULL; |
| 6304 | |
| 6305 | if (size < 0) { |
| 6306 | errno = EINVAL; |
| 6307 | return posix_error(); |
| 6308 | } |
| 6309 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6310 | if (buffer == NULL) |
| 6311 | return NULL; |
| 6312 | if (!_PyVerify_fd(fd)) { |
| 6313 | Py_DECREF(buffer); |
| 6314 | return posix_error(); |
| 6315 | } |
| 6316 | Py_BEGIN_ALLOW_THREADS |
| 6317 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 6318 | Py_END_ALLOW_THREADS |
| 6319 | if (n < 0) { |
| 6320 | Py_DECREF(buffer); |
| 6321 | return posix_error(); |
| 6322 | } |
| 6323 | if (n != size) |
| 6324 | _PyBytes_Resize(&buffer, n); |
| 6325 | return buffer; |
| 6326 | } |
| 6327 | #endif |
| 6328 | |
| 6329 | PyDoc_STRVAR(posix_write__doc__, |
| 6330 | "write(fd, string) -> byteswritten\n\n\ |
| 6331 | Write a string to a file descriptor."); |
| 6332 | |
| 6333 | static PyObject * |
| 6334 | posix_write(PyObject *self, PyObject *args) |
| 6335 | { |
| 6336 | Py_buffer pbuf; |
| 6337 | int fd; |
| 6338 | Py_ssize_t size, len; |
| 6339 | |
| 6340 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 6341 | return NULL; |
| 6342 | if (!_PyVerify_fd(fd)) { |
| 6343 | PyBuffer_Release(&pbuf); |
| 6344 | return posix_error(); |
| 6345 | } |
| 6346 | len = pbuf.len; |
| 6347 | Py_BEGIN_ALLOW_THREADS |
| 6348 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 6349 | if (len > INT_MAX) |
| 6350 | len = INT_MAX; |
| 6351 | size = write(fd, pbuf.buf, (int)len); |
| 6352 | #else |
| 6353 | size = write(fd, pbuf.buf, len); |
| 6354 | #endif |
| 6355 | Py_END_ALLOW_THREADS |
| 6356 | PyBuffer_Release(&pbuf); |
| 6357 | if (size < 0) |
| 6358 | return posix_error(); |
| 6359 | return PyLong_FromSsize_t(size); |
| 6360 | } |
| 6361 | |
| 6362 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6363 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 6364 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 6365 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 6366 | -> byteswritten\n\ |
| 6367 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 6368 | |
| 6369 | static PyObject * |
| 6370 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 6371 | { |
| 6372 | int in, out; |
| 6373 | Py_ssize_t ret; |
| 6374 | off_t offset; |
| 6375 | |
| 6376 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 6377 | #ifndef __APPLE__ |
| 6378 | Py_ssize_t len; |
| 6379 | #endif |
| 6380 | PyObject *headers = NULL, *trailers = NULL; |
| 6381 | Py_buffer *hbuf, *tbuf; |
| 6382 | off_t sbytes; |
| 6383 | struct sf_hdtr sf; |
| 6384 | int flags = 0; |
| 6385 | sf.headers = NULL; |
| 6386 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 6387 | static char *keywords[] = {"out", "in", |
| 6388 | "offset", "count", |
| 6389 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6390 | |
| 6391 | #ifdef __APPLE__ |
| 6392 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 6393 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6394 | #else |
| 6395 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 6396 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6397 | #endif |
| 6398 | &headers, &trailers, &flags)) |
| 6399 | return NULL; |
| 6400 | if (headers != NULL) { |
| 6401 | if (!PySequence_Check(headers)) { |
| 6402 | PyErr_SetString(PyExc_TypeError, |
| 6403 | "sendfile() headers must be a sequence or None"); |
| 6404 | return NULL; |
| 6405 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6406 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6407 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6408 | if (sf.hdr_cnt > 0 && |
| 6409 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 6410 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6411 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6412 | #ifdef __APPLE__ |
| 6413 | sbytes += i; |
| 6414 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6415 | } |
| 6416 | } |
| 6417 | if (trailers != NULL) { |
| 6418 | if (!PySequence_Check(trailers)) { |
| 6419 | PyErr_SetString(PyExc_TypeError, |
| 6420 | "sendfile() trailers must be a sequence or None"); |
| 6421 | return NULL; |
| 6422 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6423 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6424 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6425 | if (sf.trl_cnt > 0 && |
| 6426 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 6427 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6428 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6429 | #ifdef __APPLE__ |
| 6430 | sbytes += i; |
| 6431 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6432 | } |
| 6433 | } |
| 6434 | |
| 6435 | Py_BEGIN_ALLOW_THREADS |
| 6436 | #ifdef __APPLE__ |
| 6437 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 6438 | #else |
| 6439 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 6440 | #endif |
| 6441 | Py_END_ALLOW_THREADS |
| 6442 | |
| 6443 | if (sf.headers != NULL) |
| 6444 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 6445 | if (sf.trailers != NULL) |
| 6446 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 6447 | |
| 6448 | if (ret < 0) { |
| 6449 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 6450 | if (sbytes != 0) { |
| 6451 | // some data has been sent |
| 6452 | goto done; |
| 6453 | } |
| 6454 | else { |
| 6455 | // no data has been sent; upper application is supposed |
| 6456 | // to retry on EAGAIN or EBUSY |
| 6457 | return posix_error(); |
| 6458 | } |
| 6459 | } |
| 6460 | return posix_error(); |
| 6461 | } |
| 6462 | goto done; |
| 6463 | |
| 6464 | done: |
| 6465 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6466 | return Py_BuildValue("l", sbytes); |
| 6467 | #else |
| 6468 | return Py_BuildValue("L", sbytes); |
| 6469 | #endif |
| 6470 | |
| 6471 | #else |
| 6472 | Py_ssize_t count; |
| 6473 | PyObject *offobj; |
| 6474 | static char *keywords[] = {"out", "in", |
| 6475 | "offset", "count", NULL}; |
| 6476 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 6477 | keywords, &out, &in, &offobj, &count)) |
| 6478 | return NULL; |
| 6479 | #ifdef linux |
| 6480 | if (offobj == Py_None) { |
| 6481 | Py_BEGIN_ALLOW_THREADS |
| 6482 | ret = sendfile(out, in, NULL, count); |
| 6483 | Py_END_ALLOW_THREADS |
| 6484 | if (ret < 0) |
| 6485 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 6486 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6487 | } |
| 6488 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 6489 | if (!_parse_off_t(offobj, &offset)) |
| 6490 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6491 | Py_BEGIN_ALLOW_THREADS |
| 6492 | ret = sendfile(out, in, &offset, count); |
| 6493 | Py_END_ALLOW_THREADS |
| 6494 | if (ret < 0) |
| 6495 | return posix_error(); |
| 6496 | return Py_BuildValue("n", ret); |
| 6497 | #endif |
| 6498 | } |
| 6499 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6500 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6501 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6502 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6503 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6504 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6505 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6506 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6507 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6508 | int fd; |
| 6509 | STRUCT_STAT st; |
| 6510 | int res; |
| 6511 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 6512 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6513 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6514 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 6515 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6516 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6517 | if (!_PyVerify_fd(fd)) |
| 6518 | return posix_error(); |
| 6519 | Py_BEGIN_ALLOW_THREADS |
| 6520 | res = FSTAT(fd, &st); |
| 6521 | Py_END_ALLOW_THREADS |
| 6522 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6523 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6524 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6525 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6526 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6527 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6528 | } |
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 _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6531 | } |
| 6532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6533 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6534 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6535 | 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] | 6536 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6537 | |
| 6538 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 6539 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6540 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6541 | int fd; |
| 6542 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 6543 | return NULL; |
| 6544 | if (!_PyVerify_fd(fd)) |
| 6545 | return PyBool_FromLong(0); |
| 6546 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6547 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6548 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6549 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6550 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6551 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6552 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6553 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6554 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6555 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6556 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6557 | #if defined(PYOS_OS2) |
| 6558 | HFILE read, write; |
| 6559 | APIRET rc; |
| 6560 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6561 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6562 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6563 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6564 | |
| 6565 | return Py_BuildValue("(ii)", read, write); |
| 6566 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6567 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6568 | int fds[2]; |
| 6569 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6570 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6571 | if (res != 0) |
| 6572 | return posix_error(); |
| 6573 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6574 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6575 | HANDLE read, write; |
| 6576 | int read_fd, write_fd; |
| 6577 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6578 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6579 | if (!ok) |
| 6580 | return win32_error("CreatePipe", NULL); |
| 6581 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 6582 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 6583 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6584 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6585 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6586 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6587 | #endif /* HAVE_PIPE */ |
| 6588 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 6589 | #ifdef HAVE_PIPE2 |
| 6590 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 6591 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 6592 | Create a pipe with flags set atomically.\n\ |
| 6593 | flags can be constructed by ORing together one or more of these values:\n\ |
| 6594 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 6595 | "); |
| 6596 | |
| 6597 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 6598 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 6599 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 6600 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 6601 | int fds[2]; |
| 6602 | int res; |
| 6603 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 6604 | flags = PyLong_AsLong(arg); |
| 6605 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 6606 | return NULL; |
| 6607 | |
| 6608 | res = pipe2(fds, flags); |
| 6609 | if (res != 0) |
| 6610 | return posix_error(); |
| 6611 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 6612 | } |
| 6613 | #endif /* HAVE_PIPE2 */ |
| 6614 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6615 | #ifdef HAVE_WRITEV |
| 6616 | PyDoc_STRVAR(posix_writev__doc__, |
| 6617 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 6618 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 6619 | arbitrary sequence of buffers.\n\ |
| 6620 | Returns the total bytes written."); |
| 6621 | |
| 6622 | static PyObject * |
| 6623 | posix_writev(PyObject *self, PyObject *args) |
| 6624 | { |
| 6625 | int fd, cnt; |
| 6626 | Py_ssize_t res; |
| 6627 | PyObject *seq; |
| 6628 | struct iovec *iov; |
| 6629 | Py_buffer *buf; |
| 6630 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 6631 | return NULL; |
| 6632 | if (!PySequence_Check(seq)) { |
| 6633 | PyErr_SetString(PyExc_TypeError, |
| 6634 | "writev() arg 2 must be a sequence"); |
| 6635 | return NULL; |
| 6636 | } |
| 6637 | cnt = PySequence_Size(seq); |
| 6638 | |
| 6639 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 6640 | return NULL; |
| 6641 | } |
| 6642 | |
| 6643 | Py_BEGIN_ALLOW_THREADS |
| 6644 | res = writev(fd, iov, cnt); |
| 6645 | Py_END_ALLOW_THREADS |
| 6646 | |
| 6647 | iov_cleanup(iov, buf, cnt); |
| 6648 | return PyLong_FromSsize_t(res); |
| 6649 | } |
| 6650 | #endif |
| 6651 | |
| 6652 | #ifdef HAVE_PWRITE |
| 6653 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 6654 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 6655 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 6656 | offset unchanged."); |
| 6657 | |
| 6658 | static PyObject * |
| 6659 | posix_pwrite(PyObject *self, PyObject *args) |
| 6660 | { |
| 6661 | Py_buffer pbuf; |
| 6662 | int fd; |
| 6663 | off_t offset; |
| 6664 | Py_ssize_t size; |
| 6665 | |
| 6666 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 6667 | return NULL; |
| 6668 | |
| 6669 | if (!_PyVerify_fd(fd)) { |
| 6670 | PyBuffer_Release(&pbuf); |
| 6671 | return posix_error(); |
| 6672 | } |
| 6673 | Py_BEGIN_ALLOW_THREADS |
| 6674 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 6675 | Py_END_ALLOW_THREADS |
| 6676 | PyBuffer_Release(&pbuf); |
| 6677 | if (size < 0) |
| 6678 | return posix_error(); |
| 6679 | return PyLong_FromSsize_t(size); |
| 6680 | } |
| 6681 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6682 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6683 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6684 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6685 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6686 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6687 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6688 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6689 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6690 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6691 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6692 | char *filename; |
| 6693 | int mode = 0666; |
| 6694 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6695 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 6696 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6697 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6698 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6699 | Py_BEGIN_ALLOW_THREADS |
| 6700 | res = mkfifo(filename, mode); |
| 6701 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6702 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6703 | if (res < 0) |
| 6704 | return posix_error(); |
| 6705 | Py_INCREF(Py_None); |
| 6706 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6707 | } |
| 6708 | #endif |
| 6709 | |
| 6710 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6711 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6712 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6713 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6714 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 6715 | named filename. mode specifies both the permissions to use and the\n\ |
| 6716 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 6717 | 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] | 6718 | device defines the newly created device special file (probably using\n\ |
| 6719 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6720 | |
| 6721 | |
| 6722 | static PyObject * |
| 6723 | posix_mknod(PyObject *self, PyObject *args) |
| 6724 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6725 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6726 | char *filename; |
| 6727 | int mode = 0600; |
| 6728 | int device = 0; |
| 6729 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6730 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 6731 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6732 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6733 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6734 | Py_BEGIN_ALLOW_THREADS |
| 6735 | res = mknod(filename, mode, device); |
| 6736 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 6737 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6738 | if (res < 0) |
| 6739 | return posix_error(); |
| 6740 | Py_INCREF(Py_None); |
| 6741 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6742 | } |
| 6743 | #endif |
| 6744 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6745 | #ifdef HAVE_DEVICE_MACROS |
| 6746 | PyDoc_STRVAR(posix_major__doc__, |
| 6747 | "major(device) -> major number\n\ |
| 6748 | Extracts a device major number from a raw device number."); |
| 6749 | |
| 6750 | static PyObject * |
| 6751 | posix_major(PyObject *self, PyObject *args) |
| 6752 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6753 | int device; |
| 6754 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6755 | return NULL; |
| 6756 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6757 | } |
| 6758 | |
| 6759 | PyDoc_STRVAR(posix_minor__doc__, |
| 6760 | "minor(device) -> minor number\n\ |
| 6761 | Extracts a device minor number from a raw device number."); |
| 6762 | |
| 6763 | static PyObject * |
| 6764 | posix_minor(PyObject *self, PyObject *args) |
| 6765 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6766 | int device; |
| 6767 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6768 | return NULL; |
| 6769 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6770 | } |
| 6771 | |
| 6772 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6773 | "makedev(major, minor) -> device number\n\ |
| 6774 | Composes a raw device number from the major and minor device numbers."); |
| 6775 | |
| 6776 | static PyObject * |
| 6777 | posix_makedev(PyObject *self, PyObject *args) |
| 6778 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6779 | int major, minor; |
| 6780 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 6781 | return NULL; |
| 6782 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6783 | } |
| 6784 | #endif /* device macros */ |
| 6785 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6786 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6787 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6788 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6789 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6790 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6791 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6792 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6793 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6794 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6795 | int fd; |
| 6796 | off_t length; |
| 6797 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6798 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6799 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6800 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6801 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6802 | Py_BEGIN_ALLOW_THREADS |
| 6803 | res = ftruncate(fd, length); |
| 6804 | Py_END_ALLOW_THREADS |
| 6805 | if (res < 0) |
| 6806 | return posix_error(); |
| 6807 | Py_INCREF(Py_None); |
| 6808 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6809 | } |
| 6810 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6811 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6812 | #ifdef HAVE_TRUNCATE |
| 6813 | PyDoc_STRVAR(posix_truncate__doc__, |
| 6814 | "truncate(path, length)\n\n\ |
| 6815 | Truncate the file given by path to length bytes."); |
| 6816 | |
| 6817 | static PyObject * |
| 6818 | posix_truncate(PyObject *self, PyObject *args) |
| 6819 | { |
| 6820 | PyObject *opath; |
| 6821 | const char *path; |
| 6822 | off_t length; |
| 6823 | int res; |
| 6824 | |
| 6825 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 6826 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 6827 | return NULL; |
| 6828 | path = PyBytes_AsString(opath); |
| 6829 | |
| 6830 | Py_BEGIN_ALLOW_THREADS |
| 6831 | res = truncate(path, length); |
| 6832 | Py_END_ALLOW_THREADS |
| 6833 | Py_DECREF(opath); |
| 6834 | if (res < 0) |
| 6835 | return posix_error(); |
| 6836 | Py_RETURN_NONE; |
| 6837 | } |
| 6838 | #endif |
| 6839 | |
| 6840 | #ifdef HAVE_POSIX_FALLOCATE |
| 6841 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 6842 | "posix_fallocate(fd, offset, len)\n\n\ |
| 6843 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 6844 | starting from offset and continuing for len bytes."); |
| 6845 | |
| 6846 | static PyObject * |
| 6847 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 6848 | { |
| 6849 | off_t len, offset; |
| 6850 | int res, fd; |
| 6851 | |
| 6852 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 6853 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 6854 | return NULL; |
| 6855 | |
| 6856 | Py_BEGIN_ALLOW_THREADS |
| 6857 | res = posix_fallocate(fd, offset, len); |
| 6858 | Py_END_ALLOW_THREADS |
| 6859 | if (res != 0) { |
| 6860 | errno = res; |
| 6861 | return posix_error(); |
| 6862 | } |
| 6863 | Py_RETURN_NONE; |
| 6864 | } |
| 6865 | #endif |
| 6866 | |
| 6867 | #ifdef HAVE_POSIX_FADVISE |
| 6868 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 6869 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 6870 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 6871 | the kernel to make optimizations.\n\ |
| 6872 | The advice applies to the region of the file specified by fd starting at\n\ |
| 6873 | offset and continuing for len bytes.\n\ |
| 6874 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 6875 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 6876 | POSIX_FADV_DONTNEED."); |
| 6877 | |
| 6878 | static PyObject * |
| 6879 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 6880 | { |
| 6881 | off_t len, offset; |
| 6882 | int res, fd, advice; |
| 6883 | |
| 6884 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 6885 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 6886 | return NULL; |
| 6887 | |
| 6888 | Py_BEGIN_ALLOW_THREADS |
| 6889 | res = posix_fadvise(fd, offset, len, advice); |
| 6890 | Py_END_ALLOW_THREADS |
| 6891 | if (res != 0) { |
| 6892 | errno = res; |
| 6893 | return posix_error(); |
| 6894 | } |
| 6895 | Py_RETURN_NONE; |
| 6896 | } |
| 6897 | #endif |
| 6898 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6899 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6900 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6901 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6902 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6903 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6904 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6905 | * get re-set with another call for the same key. */ |
| 6906 | static PyObject *posix_putenv_garbage; |
| 6907 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6908 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6909 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6910 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6911 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6912 | wchar_t *s1, *s2; |
| 6913 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6914 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6915 | PyObject *os1, *os2; |
| 6916 | char *s1, *s2; |
| 6917 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6918 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6919 | PyObject *newstr = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6920 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6921 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6922 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6923 | if (!PyArg_ParseTuple(args, |
| 6924 | "uu:putenv", |
| 6925 | &s1, &s2)) |
| 6926 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6927 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6928 | if (!PyArg_ParseTuple(args, |
| 6929 | "O&O&:putenv", |
| 6930 | PyUnicode_FSConverter, &os1, |
| 6931 | PyUnicode_FSConverter, &os2)) |
| 6932 | return NULL; |
| 6933 | s1 = PyBytes_AsString(os1); |
| 6934 | s2 = PyBytes_AsString(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6935 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6936 | |
| 6937 | #if defined(PYOS_OS2) |
| 6938 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6939 | APIRET rc; |
| 6940 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6941 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6942 | if (rc != NO_ERROR) { |
| 6943 | os2_error(rc); |
| 6944 | goto error; |
| 6945 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6946 | |
| 6947 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6948 | APIRET rc; |
| 6949 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6950 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6951 | if (rc != NO_ERROR) { |
| 6952 | os2_error(rc); |
| 6953 | goto error; |
| 6954 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6955 | } else { |
| 6956 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6957 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 6958 | /* len includes space for a trailing \0; the size arg to |
| 6959 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6960 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6961 | len = wcslen(s1) + wcslen(s2) + 2; |
| 6962 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6963 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6964 | len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6965 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6966 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6967 | if (newstr == NULL) { |
| 6968 | PyErr_NoMemory(); |
| 6969 | goto error; |
| 6970 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6971 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6972 | newenv = PyUnicode_AsUnicode(newstr); |
| 6973 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 6974 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6975 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6976 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6977 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6978 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6979 | newenv = PyBytes_AS_STRING(newstr); |
| 6980 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6981 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6982 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6983 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6984 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 6985 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6986 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6987 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6988 | * this will cause previous value to be collected. This has to |
| 6989 | * happen after the real putenv() call because the old value |
| 6990 | * was still accessible until then. */ |
| 6991 | if (PyDict_SetItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 6992 | #ifdef MS_WINDOWS |
| 6993 | PyTuple_GET_ITEM(args, 0), |
| 6994 | #else |
| 6995 | os1, |
| 6996 | #endif |
| 6997 | newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6998 | /* really not much we can do; just leak */ |
| 6999 | PyErr_Clear(); |
| 7000 | } |
| 7001 | else { |
| 7002 | Py_DECREF(newstr); |
| 7003 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7004 | |
| 7005 | #if defined(PYOS_OS2) |
| 7006 | } |
| 7007 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7008 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7009 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7010 | Py_DECREF(os1); |
| 7011 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7012 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7013 | Py_RETURN_NONE; |
| 7014 | |
| 7015 | error: |
| 7016 | #ifndef MS_WINDOWS |
| 7017 | Py_DECREF(os1); |
| 7018 | Py_DECREF(os2); |
| 7019 | #endif |
| 7020 | Py_XDECREF(newstr); |
| 7021 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7022 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7023 | #endif /* putenv */ |
| 7024 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7025 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7026 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7027 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7028 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7029 | |
| 7030 | static PyObject * |
| 7031 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7032 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7033 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7034 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7035 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7036 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 7037 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7038 | #else |
| 7039 | PyObject *os1; |
| 7040 | char *s1; |
| 7041 | |
| 7042 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
| 7043 | PyUnicode_FSConverter, &os1)) |
| 7044 | return NULL; |
| 7045 | s1 = PyBytes_AsString(os1); |
| 7046 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7047 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7048 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7049 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7050 | /* Remove the key from posix_putenv_garbage; |
| 7051 | * this will cause it to be collected. This has to |
| 7052 | * happen after the real unsetenv() call because the |
| 7053 | * old value was still accessible until then. |
| 7054 | */ |
| 7055 | if (PyDict_DelItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7056 | #ifdef MS_WINDOWS |
| 7057 | PyTuple_GET_ITEM(args, 0) |
| 7058 | #else |
| 7059 | os1 |
| 7060 | #endif |
| 7061 | )) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7062 | /* really not much we can do; just leak */ |
| 7063 | PyErr_Clear(); |
| 7064 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7065 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7066 | #ifndef MS_WINDOWS |
| 7067 | Py_DECREF(os1); |
| 7068 | #endif |
| 7069 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7070 | } |
| 7071 | #endif /* unsetenv */ |
| 7072 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7073 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7074 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7075 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7076 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7077 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7078 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7079 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7080 | int code; |
| 7081 | char *message; |
| 7082 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7083 | return NULL; |
| 7084 | message = strerror(code); |
| 7085 | if (message == NULL) { |
| 7086 | PyErr_SetString(PyExc_ValueError, |
| 7087 | "strerror() argument out of range"); |
| 7088 | return NULL; |
| 7089 | } |
| 7090 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7091 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7092 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7093 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7094 | #ifdef HAVE_SYS_WAIT_H |
| 7095 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7096 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7097 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7098 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7099 | 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] | 7100 | |
| 7101 | static PyObject * |
| 7102 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7103 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7104 | WAIT_TYPE status; |
| 7105 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7106 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7107 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7108 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7109 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7110 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7111 | } |
| 7112 | #endif /* WCOREDUMP */ |
| 7113 | |
| 7114 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7115 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7116 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7117 | 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] | 7118 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7119 | |
| 7120 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7121 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7122 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7123 | WAIT_TYPE status; |
| 7124 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7125 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7126 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7127 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7128 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7129 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7130 | } |
| 7131 | #endif /* WIFCONTINUED */ |
| 7132 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7133 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7134 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7135 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7136 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7137 | |
| 7138 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7139 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7140 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7141 | WAIT_TYPE status; |
| 7142 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7143 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7144 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7145 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7146 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7147 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7148 | } |
| 7149 | #endif /* WIFSTOPPED */ |
| 7150 | |
| 7151 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7152 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7153 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7154 | 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] | 7155 | |
| 7156 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7157 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7158 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7159 | WAIT_TYPE status; |
| 7160 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7161 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7162 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7163 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7164 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7165 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7166 | } |
| 7167 | #endif /* WIFSIGNALED */ |
| 7168 | |
| 7169 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7170 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7171 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7172 | 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] | 7173 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7174 | |
| 7175 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7176 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7177 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7178 | WAIT_TYPE status; |
| 7179 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7180 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7181 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7182 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7184 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7185 | } |
| 7186 | #endif /* WIFEXITED */ |
| 7187 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7188 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7189 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7190 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7191 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7192 | |
| 7193 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7194 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7195 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7196 | WAIT_TYPE status; |
| 7197 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7198 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7199 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 7200 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7201 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7202 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7203 | } |
| 7204 | #endif /* WEXITSTATUS */ |
| 7205 | |
| 7206 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7207 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7208 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7209 | 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] | 7210 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7211 | |
| 7212 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7213 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7214 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7215 | WAIT_TYPE status; |
| 7216 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7217 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7218 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 7219 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7221 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7222 | } |
| 7223 | #endif /* WTERMSIG */ |
| 7224 | |
| 7225 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7226 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7227 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7228 | Return the signal that stopped the process that provided\n\ |
| 7229 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7230 | |
| 7231 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7232 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7233 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7234 | WAIT_TYPE status; |
| 7235 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7236 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7237 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 7238 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7239 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7240 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7241 | } |
| 7242 | #endif /* WSTOPSIG */ |
| 7243 | |
| 7244 | #endif /* HAVE_SYS_WAIT_H */ |
| 7245 | |
| 7246 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7247 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 7248 | #ifdef _SCO_DS |
| 7249 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 7250 | needed definitions in sys/statvfs.h */ |
| 7251 | #define _SVID3 |
| 7252 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7253 | #include <sys/statvfs.h> |
| 7254 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7255 | static PyObject* |
| 7256 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7257 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 7258 | if (v == NULL) |
| 7259 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7260 | |
| 7261 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7262 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 7263 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 7264 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 7265 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 7266 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 7267 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 7268 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 7269 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 7270 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 7271 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7272 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7273 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 7274 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 7275 | PyStructSequence_SET_ITEM(v, 2, |
| 7276 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 7277 | PyStructSequence_SET_ITEM(v, 3, |
| 7278 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 7279 | PyStructSequence_SET_ITEM(v, 4, |
| 7280 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 7281 | PyStructSequence_SET_ITEM(v, 5, |
| 7282 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 7283 | PyStructSequence_SET_ITEM(v, 6, |
| 7284 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 7285 | PyStructSequence_SET_ITEM(v, 7, |
| 7286 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 7287 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 7288 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7289 | #endif |
| 7290 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7291 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7292 | } |
| 7293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7294 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7295 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7296 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7297 | |
| 7298 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7299 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7300 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7301 | int fd, res; |
| 7302 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7303 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7304 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 7305 | return NULL; |
| 7306 | Py_BEGIN_ALLOW_THREADS |
| 7307 | res = fstatvfs(fd, &st); |
| 7308 | Py_END_ALLOW_THREADS |
| 7309 | if (res != 0) |
| 7310 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7311 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7312 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7313 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7314 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7315 | |
| 7316 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7317 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7318 | #include <sys/statvfs.h> |
| 7319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7320 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7321 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7322 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7323 | |
| 7324 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7325 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7326 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7327 | char *path; |
| 7328 | int res; |
| 7329 | struct statvfs st; |
| 7330 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
| 7331 | return NULL; |
| 7332 | Py_BEGIN_ALLOW_THREADS |
| 7333 | res = statvfs(path, &st); |
| 7334 | Py_END_ALLOW_THREADS |
| 7335 | if (res != 0) |
| 7336 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7337 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7338 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7339 | } |
| 7340 | #endif /* HAVE_STATVFS */ |
| 7341 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7342 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 7343 | * It maps strings representing configuration variable names to |
| 7344 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7345 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7346 | * rarely-used constants. There are three separate tables that use |
| 7347 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7348 | * |
| 7349 | * This code is always included, even if none of the interfaces that |
| 7350 | * need it are included. The #if hackery needed to avoid it would be |
| 7351 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7352 | */ |
| 7353 | struct constdef { |
| 7354 | char *name; |
| 7355 | long value; |
| 7356 | }; |
| 7357 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7358 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7359 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 7360 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7361 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7362 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7363 | *valuep = PyLong_AS_LONG(arg); |
| 7364 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7365 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 7366 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7367 | /* look up the value in the table using a binary search */ |
| 7368 | size_t lo = 0; |
| 7369 | size_t mid; |
| 7370 | size_t hi = tablesize; |
| 7371 | int cmp; |
| 7372 | const char *confname; |
| 7373 | if (!PyUnicode_Check(arg)) { |
| 7374 | PyErr_SetString(PyExc_TypeError, |
| 7375 | "configuration names must be strings or integers"); |
| 7376 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7377 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7378 | confname = _PyUnicode_AsString(arg); |
| 7379 | if (confname == NULL) |
| 7380 | return 0; |
| 7381 | while (lo < hi) { |
| 7382 | mid = (lo + hi) / 2; |
| 7383 | cmp = strcmp(confname, table[mid].name); |
| 7384 | if (cmp < 0) |
| 7385 | hi = mid; |
| 7386 | else if (cmp > 0) |
| 7387 | lo = mid + 1; |
| 7388 | else { |
| 7389 | *valuep = table[mid].value; |
| 7390 | return 1; |
| 7391 | } |
| 7392 | } |
| 7393 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 7394 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7395 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7396 | } |
| 7397 | |
| 7398 | |
| 7399 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 7400 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7401 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7402 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7403 | #endif |
| 7404 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7405 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7406 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7407 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7408 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7409 | #endif |
| 7410 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7411 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7412 | #endif |
| 7413 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7414 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7415 | #endif |
| 7416 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7417 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7418 | #endif |
| 7419 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7420 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7421 | #endif |
| 7422 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7423 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7424 | #endif |
| 7425 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7426 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7427 | #endif |
| 7428 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7429 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7430 | #endif |
| 7431 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7432 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7433 | #endif |
| 7434 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7435 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7436 | #endif |
| 7437 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7438 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7439 | #endif |
| 7440 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7441 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7442 | #endif |
| 7443 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7444 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7445 | #endif |
| 7446 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7447 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7448 | #endif |
| 7449 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7450 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7451 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 7452 | #ifdef _PC_ACL_ENABLED |
| 7453 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 7454 | #endif |
| 7455 | #ifdef _PC_MIN_HOLE_SIZE |
| 7456 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 7457 | #endif |
| 7458 | #ifdef _PC_ALLOC_SIZE_MIN |
| 7459 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 7460 | #endif |
| 7461 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 7462 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 7463 | #endif |
| 7464 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 7465 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 7466 | #endif |
| 7467 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 7468 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 7469 | #endif |
| 7470 | #ifdef _PC_REC_XFER_ALIGN |
| 7471 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 7472 | #endif |
| 7473 | #ifdef _PC_SYMLINK_MAX |
| 7474 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 7475 | #endif |
| 7476 | #ifdef _PC_XATTR_ENABLED |
| 7477 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 7478 | #endif |
| 7479 | #ifdef _PC_XATTR_EXISTS |
| 7480 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 7481 | #endif |
| 7482 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 7483 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 7484 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7485 | }; |
| 7486 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7487 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7488 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7489 | { |
| 7490 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 7491 | sizeof(posix_constants_pathconf) |
| 7492 | / sizeof(struct constdef)); |
| 7493 | } |
| 7494 | #endif |
| 7495 | |
| 7496 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7497 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7498 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7499 | 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] | 7500 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7501 | |
| 7502 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7503 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7504 | { |
| 7505 | PyObject *result = NULL; |
| 7506 | int name, fd; |
| 7507 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7508 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 7509 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7510 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7511 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7512 | errno = 0; |
| 7513 | limit = fpathconf(fd, name); |
| 7514 | if (limit == -1 && errno != 0) |
| 7515 | posix_error(); |
| 7516 | else |
| 7517 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7518 | } |
| 7519 | return result; |
| 7520 | } |
| 7521 | #endif |
| 7522 | |
| 7523 | |
| 7524 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7525 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7526 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7527 | 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] | 7528 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7529 | |
| 7530 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7531 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7532 | { |
| 7533 | PyObject *result = NULL; |
| 7534 | int name; |
| 7535 | char *path; |
| 7536 | |
| 7537 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 7538 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7539 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7540 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7541 | errno = 0; |
| 7542 | limit = pathconf(path, name); |
| 7543 | if (limit == -1 && errno != 0) { |
| 7544 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7545 | /* could be a path or name problem */ |
| 7546 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7547 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7548 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7549 | } |
| 7550 | else |
| 7551 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7552 | } |
| 7553 | return result; |
| 7554 | } |
| 7555 | #endif |
| 7556 | |
| 7557 | #ifdef HAVE_CONFSTR |
| 7558 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7559 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7560 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7561 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 7562 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7563 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 7564 | #endif |
| 7565 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7566 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 7567 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7568 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7569 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7570 | #endif |
| 7571 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7572 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7573 | #endif |
| 7574 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7575 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7576 | #endif |
| 7577 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7578 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7579 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7580 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7581 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7582 | #endif |
| 7583 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7584 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7585 | #endif |
| 7586 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7587 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7588 | #endif |
| 7589 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7590 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7591 | #endif |
| 7592 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7593 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7594 | #endif |
| 7595 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7596 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7597 | #endif |
| 7598 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7599 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7600 | #endif |
| 7601 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7602 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7603 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7604 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7605 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7606 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7607 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7608 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7609 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7610 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7611 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7612 | #endif |
| 7613 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7614 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7615 | #endif |
| 7616 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7617 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7618 | #endif |
| 7619 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7620 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7621 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7622 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7623 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7624 | #endif |
| 7625 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7626 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7627 | #endif |
| 7628 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7629 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7630 | #endif |
| 7631 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7632 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7633 | #endif |
| 7634 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7635 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7636 | #endif |
| 7637 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7638 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7639 | #endif |
| 7640 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7641 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7642 | #endif |
| 7643 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7644 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7645 | #endif |
| 7646 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7647 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7648 | #endif |
| 7649 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7650 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7651 | #endif |
| 7652 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7653 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7654 | #endif |
| 7655 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7656 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7657 | #endif |
| 7658 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7659 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7660 | #endif |
| 7661 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7662 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7663 | #endif |
| 7664 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7665 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7666 | #endif |
| 7667 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7668 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7669 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7670 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7671 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7672 | #endif |
| 7673 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7674 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7675 | #endif |
| 7676 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7677 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7678 | #endif |
| 7679 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7680 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7681 | #endif |
| 7682 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7683 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7684 | #endif |
| 7685 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7686 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7687 | #endif |
| 7688 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7689 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7690 | #endif |
| 7691 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7692 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7693 | #endif |
| 7694 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7695 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7696 | #endif |
| 7697 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7698 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7699 | #endif |
| 7700 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7701 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7702 | #endif |
| 7703 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7704 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7705 | #endif |
| 7706 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7707 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7708 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7709 | }; |
| 7710 | |
| 7711 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7712 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7713 | { |
| 7714 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 7715 | sizeof(posix_constants_confstr) |
| 7716 | / sizeof(struct constdef)); |
| 7717 | } |
| 7718 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7719 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7720 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7721 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7722 | |
| 7723 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7724 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7725 | { |
| 7726 | PyObject *result = NULL; |
| 7727 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7728 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7729 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7730 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7731 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 7732 | return NULL; |
| 7733 | |
| 7734 | errno = 0; |
| 7735 | len = confstr(name, buffer, sizeof(buffer)); |
| 7736 | if (len == 0) { |
| 7737 | if (errno) { |
| 7738 | posix_error(); |
| 7739 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7740 | } |
| 7741 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7742 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7743 | } |
| 7744 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 7745 | |
| 7746 | if ((unsigned int)len >= sizeof(buffer)) { |
| 7747 | char *buf = PyMem_Malloc(len); |
| 7748 | if (buf == NULL) |
| 7749 | return PyErr_NoMemory(); |
| 7750 | confstr(name, buf, len); |
| 7751 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 7752 | PyMem_Free(buf); |
| 7753 | } |
| 7754 | else |
| 7755 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7756 | return result; |
| 7757 | } |
| 7758 | #endif |
| 7759 | |
| 7760 | |
| 7761 | #ifdef HAVE_SYSCONF |
| 7762 | static struct constdef posix_constants_sysconf[] = { |
| 7763 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7764 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7765 | #endif |
| 7766 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7767 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7768 | #endif |
| 7769 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7770 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7771 | #endif |
| 7772 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7773 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7774 | #endif |
| 7775 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7776 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7777 | #endif |
| 7778 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7779 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7780 | #endif |
| 7781 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7782 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7783 | #endif |
| 7784 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7785 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7786 | #endif |
| 7787 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7788 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7789 | #endif |
| 7790 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7791 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7792 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7793 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7794 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7795 | #endif |
| 7796 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7797 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7798 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7799 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7800 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7801 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7802 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7803 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7804 | #endif |
| 7805 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7806 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7807 | #endif |
| 7808 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7809 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7810 | #endif |
| 7811 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7812 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7813 | #endif |
| 7814 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7815 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7816 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7817 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7818 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7819 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7820 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7821 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7822 | #endif |
| 7823 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7824 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7825 | #endif |
| 7826 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7827 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7828 | #endif |
| 7829 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7830 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7831 | #endif |
| 7832 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7833 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7834 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7835 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7836 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7837 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7838 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7839 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7840 | #endif |
| 7841 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7842 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7843 | #endif |
| 7844 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7845 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7846 | #endif |
| 7847 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7848 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7849 | #endif |
| 7850 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7851 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7852 | #endif |
| 7853 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7854 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7855 | #endif |
| 7856 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7857 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7858 | #endif |
| 7859 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7860 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7861 | #endif |
| 7862 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7863 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7864 | #endif |
| 7865 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7866 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7867 | #endif |
| 7868 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7869 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7870 | #endif |
| 7871 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7872 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7873 | #endif |
| 7874 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7875 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7876 | #endif |
| 7877 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7878 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7879 | #endif |
| 7880 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7881 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7882 | #endif |
| 7883 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7884 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7885 | #endif |
| 7886 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7887 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7888 | #endif |
| 7889 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7890 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7891 | #endif |
| 7892 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7893 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7894 | #endif |
| 7895 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7896 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7897 | #endif |
| 7898 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7899 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7900 | #endif |
| 7901 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7902 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7903 | #endif |
| 7904 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7905 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7906 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7907 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7908 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7909 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7910 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7911 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7912 | #endif |
| 7913 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7914 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7915 | #endif |
| 7916 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7917 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7918 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7919 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7920 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7921 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7922 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7923 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7924 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7925 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7926 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7927 | #endif |
| 7928 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7929 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7930 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7931 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7932 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7933 | #endif |
| 7934 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7935 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7936 | #endif |
| 7937 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7938 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7939 | #endif |
| 7940 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7941 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7942 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7943 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7944 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7945 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7946 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7947 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7948 | #endif |
| 7949 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7950 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7951 | #endif |
| 7952 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7953 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7954 | #endif |
| 7955 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7956 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7957 | #endif |
| 7958 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7959 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7960 | #endif |
| 7961 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7962 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7963 | #endif |
| 7964 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7965 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7966 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7967 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7968 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7969 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7970 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7971 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7972 | #endif |
| 7973 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7974 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7975 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7976 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7977 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7978 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7979 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7980 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7981 | #endif |
| 7982 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7983 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7984 | #endif |
| 7985 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7986 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7987 | #endif |
| 7988 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7989 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7990 | #endif |
| 7991 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7992 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7993 | #endif |
| 7994 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7995 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7996 | #endif |
| 7997 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7998 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7999 | #endif |
| 8000 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8001 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8002 | #endif |
| 8003 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8004 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8005 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8006 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8007 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8008 | #endif |
| 8009 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8010 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8011 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8012 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8013 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8014 | #endif |
| 8015 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8016 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8017 | #endif |
| 8018 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8019 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8020 | #endif |
| 8021 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8022 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8023 | #endif |
| 8024 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8025 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8026 | #endif |
| 8027 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8028 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8029 | #endif |
| 8030 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8031 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8032 | #endif |
| 8033 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8034 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8035 | #endif |
| 8036 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8037 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8038 | #endif |
| 8039 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8040 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8041 | #endif |
| 8042 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8043 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8044 | #endif |
| 8045 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8046 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8047 | #endif |
| 8048 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8049 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8050 | #endif |
| 8051 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8052 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8053 | #endif |
| 8054 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8055 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8056 | #endif |
| 8057 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8058 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8059 | #endif |
| 8060 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8061 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8062 | #endif |
| 8063 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8064 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8065 | #endif |
| 8066 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8067 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8068 | #endif |
| 8069 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8070 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8071 | #endif |
| 8072 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8073 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8074 | #endif |
| 8075 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8076 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8077 | #endif |
| 8078 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8079 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8080 | #endif |
| 8081 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8082 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8083 | #endif |
| 8084 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8085 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8086 | #endif |
| 8087 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8088 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8089 | #endif |
| 8090 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8091 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8092 | #endif |
| 8093 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8094 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8095 | #endif |
| 8096 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8097 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8098 | #endif |
| 8099 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8100 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8101 | #endif |
| 8102 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8103 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8104 | #endif |
| 8105 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8106 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8107 | #endif |
| 8108 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8109 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8110 | #endif |
| 8111 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8112 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8113 | #endif |
| 8114 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8115 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8116 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8117 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8118 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8119 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8120 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8121 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8122 | #endif |
| 8123 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8124 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8125 | #endif |
| 8126 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8127 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8128 | #endif |
| 8129 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8130 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8131 | #endif |
| 8132 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8133 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8134 | #endif |
| 8135 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8136 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8137 | #endif |
| 8138 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8139 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8140 | #endif |
| 8141 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8142 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8143 | #endif |
| 8144 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8145 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8146 | #endif |
| 8147 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8148 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8149 | #endif |
| 8150 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8151 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8152 | #endif |
| 8153 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8154 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8155 | #endif |
| 8156 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8157 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8158 | #endif |
| 8159 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8160 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8161 | #endif |
| 8162 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8163 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8164 | #endif |
| 8165 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8166 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8167 | #endif |
| 8168 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8169 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8170 | #endif |
| 8171 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8172 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8173 | #endif |
| 8174 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8175 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8176 | #endif |
| 8177 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8178 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8179 | #endif |
| 8180 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8181 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8182 | #endif |
| 8183 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8184 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8185 | #endif |
| 8186 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8187 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8188 | #endif |
| 8189 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8190 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8191 | #endif |
| 8192 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8193 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8194 | #endif |
| 8195 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8196 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8197 | #endif |
| 8198 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8199 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8200 | #endif |
| 8201 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8202 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8203 | #endif |
| 8204 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8205 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8206 | #endif |
| 8207 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8208 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8209 | #endif |
| 8210 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8211 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8212 | #endif |
| 8213 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8214 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8215 | #endif |
| 8216 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8217 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8218 | #endif |
| 8219 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8220 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8221 | #endif |
| 8222 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8223 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8224 | #endif |
| 8225 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8226 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8227 | #endif |
| 8228 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8229 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8230 | #endif |
| 8231 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8232 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8233 | #endif |
| 8234 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8235 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8236 | #endif |
| 8237 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8238 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8239 | #endif |
| 8240 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8241 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8242 | #endif |
| 8243 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8244 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8245 | #endif |
| 8246 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8247 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8248 | #endif |
| 8249 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8250 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8251 | #endif |
| 8252 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8253 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8254 | #endif |
| 8255 | }; |
| 8256 | |
| 8257 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8258 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8259 | { |
| 8260 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 8261 | sizeof(posix_constants_sysconf) |
| 8262 | / sizeof(struct constdef)); |
| 8263 | } |
| 8264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8265 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8266 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8267 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8268 | |
| 8269 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8270 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8271 | { |
| 8272 | PyObject *result = NULL; |
| 8273 | int name; |
| 8274 | |
| 8275 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 8276 | int value; |
| 8277 | |
| 8278 | errno = 0; |
| 8279 | value = sysconf(name); |
| 8280 | if (value == -1 && errno != 0) |
| 8281 | posix_error(); |
| 8282 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8283 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8284 | } |
| 8285 | return result; |
| 8286 | } |
| 8287 | #endif |
| 8288 | |
| 8289 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8290 | /* This code is used to ensure that the tables of configuration value names |
| 8291 | * are in sorted order as required by conv_confname(), and also to build the |
| 8292 | * the exported dictionaries that are used to publish information about the |
| 8293 | * names available on the host platform. |
| 8294 | * |
| 8295 | * Sorting the table at runtime ensures that the table is properly ordered |
| 8296 | * when used, even for platforms we're not able to test on. It also makes |
| 8297 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8298 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8299 | |
| 8300 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8301 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8302 | { |
| 8303 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8304 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8305 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8306 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8307 | |
| 8308 | return strcmp(c1->name, c2->name); |
| 8309 | } |
| 8310 | |
| 8311 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8312 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8313 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8314 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8315 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8316 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8317 | |
| 8318 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 8319 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8320 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8321 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8322 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8323 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8324 | PyObject *o = PyLong_FromLong(table[i].value); |
| 8325 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 8326 | Py_XDECREF(o); |
| 8327 | Py_DECREF(d); |
| 8328 | return -1; |
| 8329 | } |
| 8330 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8331 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8332 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8333 | } |
| 8334 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8335 | /* Return -1 on failure, 0 on success. */ |
| 8336 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8337 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8338 | { |
| 8339 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8340 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8341 | sizeof(posix_constants_pathconf) |
| 8342 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8343 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8344 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8345 | #endif |
| 8346 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8347 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8348 | sizeof(posix_constants_confstr) |
| 8349 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8350 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8351 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8352 | #endif |
| 8353 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8354 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8355 | sizeof(posix_constants_sysconf) |
| 8356 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8357 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8358 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8359 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8360 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8361 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8362 | |
| 8363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8364 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8365 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8366 | 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] | 8367 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8368 | |
| 8369 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8370 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8371 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8372 | abort(); |
| 8373 | /*NOTREACHED*/ |
| 8374 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 8375 | return NULL; |
| 8376 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8377 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8378 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8379 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 8380 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 8381 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8382 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 8383 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 8384 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 8385 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 8386 | application (if any) its extension is associated.\n\ |
| 8387 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 8388 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8389 | \n\ |
| 8390 | startfile returns as soon as the associated application is launched.\n\ |
| 8391 | There is no option to wait for the application to close, and no way\n\ |
| 8392 | to retrieve the application's exit status.\n\ |
| 8393 | \n\ |
| 8394 | The filepath is relative to the current directory. If you want to use\n\ |
| 8395 | 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] | 8396 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8397 | |
| 8398 | static PyObject * |
| 8399 | win32_startfile(PyObject *self, PyObject *args) |
| 8400 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8401 | PyObject *ofilepath; |
| 8402 | char *filepath; |
| 8403 | char *operation = NULL; |
| 8404 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 8405 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8406 | PyObject *unipath, *woperation = NULL; |
| 8407 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 8408 | &unipath, &operation)) { |
| 8409 | PyErr_Clear(); |
| 8410 | goto normal; |
| 8411 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 8412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8413 | if (operation) { |
| 8414 | woperation = PyUnicode_DecodeASCII(operation, |
| 8415 | strlen(operation), NULL); |
| 8416 | if (!woperation) { |
| 8417 | PyErr_Clear(); |
| 8418 | operation = NULL; |
| 8419 | goto normal; |
| 8420 | } |
| 8421 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 8422 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8423 | Py_BEGIN_ALLOW_THREADS |
| 8424 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 8425 | PyUnicode_AS_UNICODE(unipath), |
| 8426 | NULL, NULL, SW_SHOWNORMAL); |
| 8427 | Py_END_ALLOW_THREADS |
| 8428 | |
| 8429 | Py_XDECREF(woperation); |
| 8430 | if (rc <= (HINSTANCE)32) { |
| 8431 | PyObject *errval = win32_error_unicode("startfile", |
| 8432 | PyUnicode_AS_UNICODE(unipath)); |
| 8433 | return errval; |
| 8434 | } |
| 8435 | Py_INCREF(Py_None); |
| 8436 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8437 | |
| 8438 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 8440 | PyUnicode_FSConverter, &ofilepath, |
| 8441 | &operation)) |
| 8442 | return NULL; |
| 8443 | filepath = PyBytes_AsString(ofilepath); |
| 8444 | Py_BEGIN_ALLOW_THREADS |
| 8445 | rc = ShellExecute((HWND)0, operation, filepath, |
| 8446 | NULL, NULL, SW_SHOWNORMAL); |
| 8447 | Py_END_ALLOW_THREADS |
| 8448 | if (rc <= (HINSTANCE)32) { |
| 8449 | PyObject *errval = win32_error("startfile", filepath); |
| 8450 | Py_DECREF(ofilepath); |
| 8451 | return errval; |
| 8452 | } |
| 8453 | Py_DECREF(ofilepath); |
| 8454 | Py_INCREF(Py_None); |
| 8455 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8456 | } |
| 8457 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8458 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8459 | #ifdef HAVE_GETLOADAVG |
| 8460 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 8461 | "getloadavg() -> (float, float, float)\n\n\ |
| 8462 | Return the number of processes in the system run queue averaged over\n\ |
| 8463 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 8464 | was unobtainable"); |
| 8465 | |
| 8466 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8467 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8468 | { |
| 8469 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8470 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8471 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 8472 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8473 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8474 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8475 | } |
| 8476 | #endif |
| 8477 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8478 | #ifdef MS_WINDOWS |
| 8479 | |
| 8480 | PyDoc_STRVAR(win32_urandom__doc__, |
| 8481 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 8482 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8483 | |
| 8484 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 8485 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 8486 | DWORD dwFlags ); |
| 8487 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 8488 | BYTE *pbBuffer ); |
| 8489 | |
| 8490 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 8491 | /* This handle is never explicitly released. Instead, the operating |
| 8492 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8493 | static HCRYPTPROV hCryptProv = 0; |
| 8494 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8495 | static PyObject* |
| 8496 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8497 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8498 | int howMany; |
| 8499 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8500 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8501 | /* Read arguments */ |
| 8502 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 8503 | return NULL; |
| 8504 | if (howMany < 0) |
| 8505 | return PyErr_Format(PyExc_ValueError, |
| 8506 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8508 | if (hCryptProv == 0) { |
| 8509 | HINSTANCE hAdvAPI32 = NULL; |
| 8510 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8511 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8512 | /* Obtain handle to the DLL containing CryptoAPI |
| 8513 | This should not fail */ |
| 8514 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 8515 | if(hAdvAPI32 == NULL) |
| 8516 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8517 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8518 | /* Obtain pointers to the CryptoAPI functions |
| 8519 | This will fail on some early versions of Win95 */ |
| 8520 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 8521 | hAdvAPI32, |
| 8522 | "CryptAcquireContextA"); |
| 8523 | if (pCryptAcquireContext == NULL) |
| 8524 | return PyErr_Format(PyExc_NotImplementedError, |
| 8525 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8526 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8527 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 8528 | hAdvAPI32, "CryptGenRandom"); |
| 8529 | if (pCryptGenRandom == NULL) |
| 8530 | return PyErr_Format(PyExc_NotImplementedError, |
| 8531 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8532 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8533 | /* Acquire context */ |
| 8534 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 8535 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 8536 | return win32_error("CryptAcquireContext", NULL); |
| 8537 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8538 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8539 | /* Allocate bytes */ |
| 8540 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 8541 | if (result != NULL) { |
| 8542 | /* Get random data */ |
| 8543 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 8544 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 8545 | PyBytes_AS_STRING(result))) { |
| 8546 | Py_DECREF(result); |
| 8547 | return win32_error("CryptGenRandom", NULL); |
| 8548 | } |
| 8549 | } |
| 8550 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8551 | } |
| 8552 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8553 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 8554 | PyDoc_STRVAR(device_encoding__doc__, |
| 8555 | "device_encoding(fd) -> str\n\n\ |
| 8556 | Return a string describing the encoding of the device\n\ |
| 8557 | if the output is a terminal; else return None."); |
| 8558 | |
| 8559 | static PyObject * |
| 8560 | device_encoding(PyObject *self, PyObject *args) |
| 8561 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8562 | int fd; |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 8563 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 8564 | UINT cp; |
| 8565 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8566 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 8567 | return NULL; |
| 8568 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 8569 | Py_INCREF(Py_None); |
| 8570 | return Py_None; |
| 8571 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 8572 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 8573 | if (fd == 0) |
| 8574 | cp = GetConsoleCP(); |
| 8575 | else if (fd == 1 || fd == 2) |
| 8576 | cp = GetConsoleOutputCP(); |
| 8577 | else |
| 8578 | cp = 0; |
| 8579 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 8580 | has no console */ |
| 8581 | if (cp != 0) |
| 8582 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 8583 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8584 | { |
| 8585 | char *codeset = nl_langinfo(CODESET); |
| 8586 | if (codeset != NULL && codeset[0] != 0) |
| 8587 | return PyUnicode_FromString(codeset); |
| 8588 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 8589 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8590 | Py_INCREF(Py_None); |
| 8591 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 8592 | } |
| 8593 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8594 | #ifdef __VMS |
| 8595 | /* Use openssl random routine */ |
| 8596 | #include <openssl/rand.h> |
| 8597 | PyDoc_STRVAR(vms_urandom__doc__, |
| 8598 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 8599 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8600 | |
| 8601 | static PyObject* |
| 8602 | vms_urandom(PyObject *self, PyObject *args) |
| 8603 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | int howMany; |
| 8605 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8606 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | /* Read arguments */ |
| 8608 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 8609 | return NULL; |
| 8610 | if (howMany < 0) |
| 8611 | return PyErr_Format(PyExc_ValueError, |
| 8612 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8613 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8614 | /* Allocate bytes */ |
| 8615 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 8616 | if (result != NULL) { |
| 8617 | /* Get random data */ |
| 8618 | if (RAND_pseudo_bytes((unsigned char*) |
| 8619 | PyBytes_AS_STRING(result), |
| 8620 | howMany) < 0) { |
| 8621 | Py_DECREF(result); |
| 8622 | return PyErr_Format(PyExc_ValueError, |
| 8623 | "RAND_pseudo_bytes"); |
| 8624 | } |
| 8625 | } |
| 8626 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8627 | } |
| 8628 | #endif |
| 8629 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8630 | #ifdef HAVE_SETRESUID |
| 8631 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 8632 | "setresuid(ruid, euid, suid)\n\n\ |
| 8633 | Set the current process's real, effective, and saved user ids."); |
| 8634 | |
| 8635 | static PyObject* |
| 8636 | posix_setresuid (PyObject *self, PyObject *args) |
| 8637 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8638 | /* We assume uid_t is no larger than a long. */ |
| 8639 | long ruid, euid, suid; |
| 8640 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 8641 | return NULL; |
| 8642 | if (setresuid(ruid, euid, suid) < 0) |
| 8643 | return posix_error(); |
| 8644 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8645 | } |
| 8646 | #endif |
| 8647 | |
| 8648 | #ifdef HAVE_SETRESGID |
| 8649 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 8650 | "setresgid(rgid, egid, sgid)\n\n\ |
| 8651 | Set the current process's real, effective, and saved group ids."); |
| 8652 | |
| 8653 | static PyObject* |
| 8654 | posix_setresgid (PyObject *self, PyObject *args) |
| 8655 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8656 | /* We assume uid_t is no larger than a long. */ |
| 8657 | long rgid, egid, sgid; |
| 8658 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 8659 | return NULL; |
| 8660 | if (setresgid(rgid, egid, sgid) < 0) |
| 8661 | return posix_error(); |
| 8662 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8663 | } |
| 8664 | #endif |
| 8665 | |
| 8666 | #ifdef HAVE_GETRESUID |
| 8667 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 8668 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 8669 | Get tuple of the current process's real, effective, and saved user ids."); |
| 8670 | |
| 8671 | static PyObject* |
| 8672 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 8673 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8674 | uid_t ruid, euid, suid; |
| 8675 | long l_ruid, l_euid, l_suid; |
| 8676 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 8677 | return posix_error(); |
| 8678 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 8679 | l_ruid = ruid; |
| 8680 | l_euid = euid; |
| 8681 | l_suid = suid; |
| 8682 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8683 | } |
| 8684 | #endif |
| 8685 | |
| 8686 | #ifdef HAVE_GETRESGID |
| 8687 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 8688 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 8689 | 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] | 8690 | |
| 8691 | static PyObject* |
| 8692 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 8693 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | uid_t rgid, egid, sgid; |
| 8695 | long l_rgid, l_egid, l_sgid; |
| 8696 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 8697 | return posix_error(); |
| 8698 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 8699 | l_rgid = rgid; |
| 8700 | l_egid = egid; |
| 8701 | l_sgid = sgid; |
| 8702 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 8703 | } |
| 8704 | #endif |
| 8705 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 8706 | /* Posix *at family of functions: |
| 8707 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 8708 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 8709 | unlinkat, utimensat, mkfifoat */ |
| 8710 | |
| 8711 | #ifdef HAVE_FACCESSAT |
| 8712 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 8713 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 8714 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8715 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 8716 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 8717 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8718 | is interpreted relative to the current working directory."); |
| 8719 | |
| 8720 | static PyObject * |
| 8721 | posix_faccessat(PyObject *self, PyObject *args) |
| 8722 | { |
| 8723 | PyObject *opath; |
| 8724 | char *path; |
| 8725 | int mode; |
| 8726 | int res; |
| 8727 | int dirfd, flags = 0; |
| 8728 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 8729 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 8730 | return NULL; |
| 8731 | path = PyBytes_AsString(opath); |
| 8732 | Py_BEGIN_ALLOW_THREADS |
| 8733 | res = faccessat(dirfd, path, mode, flags); |
| 8734 | Py_END_ALLOW_THREADS |
| 8735 | Py_DECREF(opath); |
| 8736 | return PyBool_FromLong(res == 0); |
| 8737 | } |
| 8738 | #endif |
| 8739 | |
| 8740 | #ifdef HAVE_FCHMODAT |
| 8741 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 8742 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 8743 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8744 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 8745 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8746 | is interpreted relative to the current working directory."); |
| 8747 | |
| 8748 | static PyObject * |
| 8749 | posix_fchmodat(PyObject *self, PyObject *args) |
| 8750 | { |
| 8751 | int dirfd, mode, res; |
| 8752 | int flags = 0; |
| 8753 | PyObject *opath; |
| 8754 | char *path; |
| 8755 | |
| 8756 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 8757 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 8758 | return NULL; |
| 8759 | |
| 8760 | path = PyBytes_AsString(opath); |
| 8761 | |
| 8762 | Py_BEGIN_ALLOW_THREADS |
| 8763 | res = fchmodat(dirfd, path, mode, flags); |
| 8764 | Py_END_ALLOW_THREADS |
| 8765 | Py_DECREF(opath); |
| 8766 | if (res < 0) |
| 8767 | return posix_error(); |
| 8768 | Py_RETURN_NONE; |
| 8769 | } |
| 8770 | #endif /* HAVE_FCHMODAT */ |
| 8771 | |
| 8772 | #ifdef HAVE_FCHOWNAT |
| 8773 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 8774 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 8775 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8776 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 8777 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8778 | is interpreted relative to the current working directory."); |
| 8779 | |
| 8780 | static PyObject * |
| 8781 | posix_fchownat(PyObject *self, PyObject *args) |
| 8782 | { |
| 8783 | PyObject *opath; |
| 8784 | int dirfd, res; |
| 8785 | long uid, gid; |
| 8786 | int flags = 0; |
| 8787 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8788 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 8789 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 8790 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 8791 | return NULL; |
| 8792 | |
| 8793 | path = PyBytes_AsString(opath); |
| 8794 | |
| 8795 | Py_BEGIN_ALLOW_THREADS |
| 8796 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 8797 | Py_END_ALLOW_THREADS |
| 8798 | Py_DECREF(opath); |
| 8799 | if (res < 0) |
| 8800 | return posix_error(); |
| 8801 | Py_RETURN_NONE; |
| 8802 | } |
| 8803 | #endif /* HAVE_FCHOWNAT */ |
| 8804 | |
| 8805 | #ifdef HAVE_FSTATAT |
| 8806 | PyDoc_STRVAR(posix_fstatat__doc__, |
| 8807 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
| 8808 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8809 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 8810 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8811 | is interpreted relative to the current working directory."); |
| 8812 | |
| 8813 | static PyObject * |
| 8814 | posix_fstatat(PyObject *self, PyObject *args) |
| 8815 | { |
| 8816 | PyObject *opath; |
| 8817 | char *path; |
| 8818 | STRUCT_STAT st; |
| 8819 | int dirfd, res, flags = 0; |
| 8820 | |
| 8821 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 8822 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 8823 | return NULL; |
| 8824 | path = PyBytes_AsString(opath); |
| 8825 | |
| 8826 | Py_BEGIN_ALLOW_THREADS |
| 8827 | res = fstatat(dirfd, path, &st, flags); |
| 8828 | Py_END_ALLOW_THREADS |
| 8829 | Py_DECREF(opath); |
| 8830 | if (res != 0) |
| 8831 | return posix_error(); |
| 8832 | |
| 8833 | return _pystat_fromstructstat(&st); |
| 8834 | } |
| 8835 | #endif |
| 8836 | |
| 8837 | #ifdef HAVE_FUTIMESAT |
| 8838 | PyDoc_STRVAR(posix_futimesat__doc__, |
| 8839 | "futimesat(dirfd, path, (atime, mtime))\n\ |
| 8840 | futimesat(dirfd, path, None)\n\n\ |
| 8841 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8842 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8843 | is interpreted relative to the current working directory."); |
| 8844 | |
| 8845 | static PyObject * |
| 8846 | posix_futimesat(PyObject *self, PyObject *args) |
| 8847 | { |
| 8848 | PyObject *opath; |
| 8849 | char *path; |
| 8850 | int res, dirfd; |
| 8851 | PyObject* arg; |
| 8852 | |
| 8853 | struct timeval buf[2]; |
| 8854 | |
| 8855 | if (!PyArg_ParseTuple(args, "iO&O:futimesat", |
| 8856 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 8857 | return NULL; |
| 8858 | path = PyBytes_AsString(opath); |
| 8859 | if (arg == Py_None) { |
| 8860 | /* optional time values not given */ |
| 8861 | Py_BEGIN_ALLOW_THREADS |
| 8862 | res = futimesat(dirfd, path, NULL); |
| 8863 | Py_END_ALLOW_THREADS |
| 8864 | } |
| 8865 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 8866 | PyErr_SetString(PyExc_TypeError, |
| 8867 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 8868 | Py_DECREF(opath); |
| 8869 | return NULL; |
| 8870 | } |
| 8871 | else { |
| 8872 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 8873 | &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) { |
| 8874 | Py_DECREF(opath); |
| 8875 | return NULL; |
| 8876 | } |
| 8877 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 8878 | &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) { |
| 8879 | Py_DECREF(opath); |
| 8880 | return NULL; |
| 8881 | } |
| 8882 | Py_BEGIN_ALLOW_THREADS |
| 8883 | res = futimesat(dirfd, path, buf); |
| 8884 | Py_END_ALLOW_THREADS |
| 8885 | } |
| 8886 | Py_DECREF(opath); |
| 8887 | if (res < 0) { |
| 8888 | return posix_error(); |
| 8889 | } |
| 8890 | Py_RETURN_NONE; |
| 8891 | } |
| 8892 | #endif |
| 8893 | |
| 8894 | #ifdef HAVE_LINKAT |
| 8895 | PyDoc_STRVAR(posix_linkat__doc__, |
| 8896 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 8897 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 8898 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 8899 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 8900 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 8901 | srcpath is interpreted relative to the current working directory. This\n\ |
| 8902 | also applies for dstpath."); |
| 8903 | |
| 8904 | static PyObject * |
| 8905 | posix_linkat(PyObject *self, PyObject *args) |
| 8906 | { |
| 8907 | PyObject *osrc, *odst; |
| 8908 | char *src, *dst; |
| 8909 | int res, srcfd, dstfd; |
| 8910 | int flags = 0; |
| 8911 | |
| 8912 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 8913 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 8914 | return NULL; |
| 8915 | src = PyBytes_AsString(osrc); |
| 8916 | dst = PyBytes_AsString(odst); |
| 8917 | Py_BEGIN_ALLOW_THREADS |
| 8918 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 8919 | Py_END_ALLOW_THREADS |
| 8920 | Py_DECREF(osrc); |
| 8921 | Py_DECREF(odst); |
| 8922 | if (res < 0) |
| 8923 | return posix_error(); |
| 8924 | Py_RETURN_NONE; |
| 8925 | } |
| 8926 | #endif /* HAVE_LINKAT */ |
| 8927 | |
| 8928 | #ifdef HAVE_MKDIRAT |
| 8929 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 8930 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 8931 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8932 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8933 | is interpreted relative to the current working directory."); |
| 8934 | |
| 8935 | static PyObject * |
| 8936 | posix_mkdirat(PyObject *self, PyObject *args) |
| 8937 | { |
| 8938 | int res, dirfd; |
| 8939 | PyObject *opath; |
| 8940 | char *path; |
| 8941 | int mode = 0777; |
| 8942 | |
| 8943 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 8944 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 8945 | return NULL; |
| 8946 | path = PyBytes_AsString(opath); |
| 8947 | Py_BEGIN_ALLOW_THREADS |
| 8948 | res = mkdirat(dirfd, path, mode); |
| 8949 | Py_END_ALLOW_THREADS |
| 8950 | Py_DECREF(opath); |
| 8951 | if (res < 0) |
| 8952 | return posix_error(); |
| 8953 | Py_RETURN_NONE; |
| 8954 | } |
| 8955 | #endif |
| 8956 | |
| 8957 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 8958 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 8959 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 8960 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8961 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8962 | is interpreted relative to the current working directory."); |
| 8963 | |
| 8964 | static PyObject * |
| 8965 | posix_mknodat(PyObject *self, PyObject *args) |
| 8966 | { |
| 8967 | PyObject *opath; |
| 8968 | char *filename; |
| 8969 | int mode = 0600; |
| 8970 | int device = 0; |
| 8971 | int res, dirfd; |
| 8972 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 8973 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 8974 | return NULL; |
| 8975 | filename = PyBytes_AS_STRING(opath); |
| 8976 | Py_BEGIN_ALLOW_THREADS |
| 8977 | res = mknodat(dirfd, filename, mode, device); |
| 8978 | Py_END_ALLOW_THREADS |
| 8979 | Py_DECREF(opath); |
| 8980 | if (res < 0) |
| 8981 | return posix_error(); |
| 8982 | Py_RETURN_NONE; |
| 8983 | } |
| 8984 | #endif |
| 8985 | |
| 8986 | #ifdef HAVE_OPENAT |
| 8987 | PyDoc_STRVAR(posix_openat__doc__, |
| 8988 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 8989 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 8990 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 8991 | is interpreted relative to the current working directory."); |
| 8992 | |
| 8993 | static PyObject * |
| 8994 | posix_openat(PyObject *self, PyObject *args) |
| 8995 | { |
| 8996 | PyObject *ofile; |
| 8997 | char *file; |
| 8998 | int flag, dirfd, fd; |
| 8999 | int mode = 0777; |
| 9000 | |
| 9001 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9002 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9003 | &flag, &mode)) |
| 9004 | return NULL; |
| 9005 | file = PyBytes_AsString(ofile); |
| 9006 | Py_BEGIN_ALLOW_THREADS |
| 9007 | fd = openat(dirfd, file, flag, mode); |
| 9008 | Py_END_ALLOW_THREADS |
| 9009 | Py_DECREF(ofile); |
| 9010 | if (fd < 0) |
| 9011 | return posix_error(); |
| 9012 | return PyLong_FromLong((long)fd); |
| 9013 | } |
| 9014 | #endif |
| 9015 | |
| 9016 | #ifdef HAVE_READLINKAT |
| 9017 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9018 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9019 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9020 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9021 | is interpreted relative to the current working directory."); |
| 9022 | |
| 9023 | static PyObject * |
| 9024 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9025 | { |
| 9026 | PyObject *v, *opath; |
| 9027 | char buf[MAXPATHLEN]; |
| 9028 | char *path; |
| 9029 | int n, dirfd; |
| 9030 | int arg_is_unicode = 0; |
| 9031 | |
| 9032 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9033 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9034 | return NULL; |
| 9035 | path = PyBytes_AsString(opath); |
| 9036 | v = PySequence_GetItem(args, 1); |
| 9037 | if (v == NULL) { |
| 9038 | Py_DECREF(opath); |
| 9039 | return NULL; |
| 9040 | } |
| 9041 | |
| 9042 | if (PyUnicode_Check(v)) { |
| 9043 | arg_is_unicode = 1; |
| 9044 | } |
| 9045 | Py_DECREF(v); |
| 9046 | |
| 9047 | Py_BEGIN_ALLOW_THREADS |
| 9048 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9049 | Py_END_ALLOW_THREADS |
| 9050 | Py_DECREF(opath); |
| 9051 | if (n < 0) |
| 9052 | return posix_error(); |
| 9053 | |
| 9054 | if (arg_is_unicode) |
| 9055 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9056 | else |
| 9057 | return PyBytes_FromStringAndSize(buf, n); |
| 9058 | } |
| 9059 | #endif /* HAVE_READLINKAT */ |
| 9060 | |
| 9061 | #ifdef HAVE_RENAMEAT |
| 9062 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9063 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9064 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9065 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9066 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9067 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9068 | also applies for newpath."); |
| 9069 | |
| 9070 | static PyObject * |
| 9071 | posix_renameat(PyObject *self, PyObject *args) |
| 9072 | { |
| 9073 | int res; |
| 9074 | PyObject *opathold, *opathnew; |
| 9075 | char *opath, *npath; |
| 9076 | int oldfd, newfd; |
| 9077 | |
| 9078 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9079 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9080 | return NULL; |
| 9081 | opath = PyBytes_AsString(opathold); |
| 9082 | npath = PyBytes_AsString(opathnew); |
| 9083 | Py_BEGIN_ALLOW_THREADS |
| 9084 | res = renameat(oldfd, opath, newfd, npath); |
| 9085 | Py_END_ALLOW_THREADS |
| 9086 | Py_DECREF(opathold); |
| 9087 | Py_DECREF(opathnew); |
| 9088 | if (res < 0) |
| 9089 | return posix_error(); |
| 9090 | Py_RETURN_NONE; |
| 9091 | } |
| 9092 | #endif |
| 9093 | |
| 9094 | #if HAVE_SYMLINKAT |
| 9095 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9096 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9097 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9098 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9099 | is interpreted relative to the current working directory."); |
| 9100 | |
| 9101 | static PyObject * |
| 9102 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9103 | { |
| 9104 | int res, dstfd; |
| 9105 | PyObject *osrc, *odst; |
| 9106 | char *src, *dst; |
| 9107 | |
| 9108 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9109 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9110 | return NULL; |
| 9111 | src = PyBytes_AsString(osrc); |
| 9112 | dst = PyBytes_AsString(odst); |
| 9113 | Py_BEGIN_ALLOW_THREADS |
| 9114 | res = symlinkat(src, dstfd, dst); |
| 9115 | Py_END_ALLOW_THREADS |
| 9116 | Py_DECREF(osrc); |
| 9117 | Py_DECREF(odst); |
| 9118 | if (res < 0) |
| 9119 | return posix_error(); |
| 9120 | Py_RETURN_NONE; |
| 9121 | } |
| 9122 | #endif /* HAVE_SYMLINKAT */ |
| 9123 | |
| 9124 | #ifdef HAVE_UNLINKAT |
| 9125 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9126 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9127 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9128 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9129 | specified, unlinkat() behaves like rmdir().\n\ |
| 9130 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9131 | is interpreted relative to the current working directory."); |
| 9132 | |
| 9133 | static PyObject * |
| 9134 | posix_unlinkat(PyObject *self, PyObject *args) |
| 9135 | { |
| 9136 | int dirfd, res, flags = 0; |
| 9137 | PyObject *opath; |
| 9138 | char *path; |
| 9139 | |
| 9140 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 9141 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9142 | return NULL; |
| 9143 | path = PyBytes_AsString(opath); |
| 9144 | Py_BEGIN_ALLOW_THREADS |
| 9145 | res = unlinkat(dirfd, path, flags); |
| 9146 | Py_END_ALLOW_THREADS |
| 9147 | Py_DECREF(opath); |
| 9148 | if (res < 0) |
| 9149 | return posix_error(); |
| 9150 | Py_RETURN_NONE; |
| 9151 | } |
| 9152 | #endif |
| 9153 | |
| 9154 | #ifdef HAVE_UTIMENSAT |
| 9155 | PyDoc_STRVAR(posix_utimensat__doc__, |
| 9156 | "utimensat(dirfd, path, (atime_sec, atime_nsec),\n\ |
| 9157 | (mtime_sec, mtime_nsec), flags)\n\ |
| 9158 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 9159 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 9160 | relative, it is taken as relative to dirfd.\n\ |
| 9161 | The second form sets atime and mtime to the current time.\n\ |
| 9162 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9163 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9164 | is interpreted relative to the current working directory.\n\ |
| 9165 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 9166 | current time.\n\ |
| 9167 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 9168 | |
| 9169 | static PyObject * |
| 9170 | posix_utimensat(PyObject *self, PyObject *args) |
| 9171 | { |
| 9172 | PyObject *opath; |
| 9173 | char *path; |
| 9174 | int res, dirfd, flags = 0; |
| 9175 | PyObject *atime, *mtime; |
| 9176 | |
| 9177 | struct timespec buf[2]; |
| 9178 | |
| 9179 | if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat", |
| 9180 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 9181 | return NULL; |
| 9182 | path = PyBytes_AsString(opath); |
| 9183 | if (atime == Py_None && mtime == Py_None) { |
| 9184 | /* optional time values not given */ |
| 9185 | Py_BEGIN_ALLOW_THREADS |
| 9186 | res = utimensat(dirfd, path, NULL, flags); |
| 9187 | Py_END_ALLOW_THREADS |
| 9188 | } |
| 9189 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 9190 | PyErr_SetString(PyExc_TypeError, |
| 9191 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 9192 | Py_DECREF(opath); |
| 9193 | return NULL; |
| 9194 | } |
| 9195 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 9196 | PyErr_SetString(PyExc_TypeError, |
| 9197 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 9198 | Py_DECREF(opath); |
| 9199 | return NULL; |
| 9200 | } |
| 9201 | else { |
| 9202 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 9203 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 9204 | Py_DECREF(opath); |
| 9205 | return NULL; |
| 9206 | } |
| 9207 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 9208 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 9209 | Py_DECREF(opath); |
| 9210 | return NULL; |
| 9211 | } |
| 9212 | Py_BEGIN_ALLOW_THREADS |
| 9213 | res = utimensat(dirfd, path, buf, flags); |
| 9214 | Py_END_ALLOW_THREADS |
| 9215 | } |
| 9216 | Py_DECREF(opath); |
| 9217 | if (res < 0) { |
| 9218 | return posix_error(); |
| 9219 | } |
| 9220 | Py_RETURN_NONE; |
| 9221 | } |
| 9222 | #endif |
| 9223 | |
| 9224 | #ifdef HAVE_MKFIFOAT |
| 9225 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 9226 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 9227 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9228 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9229 | is interpreted relative to the current working directory."); |
| 9230 | |
| 9231 | static PyObject * |
| 9232 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 9233 | { |
| 9234 | PyObject *opath; |
| 9235 | char *filename; |
| 9236 | int mode = 0666; |
| 9237 | int res, dirfd; |
| 9238 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 9239 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9240 | return NULL; |
| 9241 | filename = PyBytes_AS_STRING(opath); |
| 9242 | Py_BEGIN_ALLOW_THREADS |
| 9243 | res = mkfifoat(dirfd, filename, mode); |
| 9244 | Py_END_ALLOW_THREADS |
| 9245 | Py_DECREF(opath); |
| 9246 | if (res < 0) |
| 9247 | return posix_error(); |
| 9248 | Py_RETURN_NONE; |
| 9249 | } |
| 9250 | #endif |
| 9251 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9252 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9253 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9254 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9255 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9256 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9258 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9259 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9260 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9261 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9262 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9263 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9264 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9265 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9266 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9267 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9268 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9269 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9270 | #endif /* HAVE_LCHMOD */ |
| 9271 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9273 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9274 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9276 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 9277 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 9279 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 9280 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 9282 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9283 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9284 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9285 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 9286 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9287 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 9288 | METH_NOARGS, posix_getcwd__doc__}, |
| 9289 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 9290 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 9291 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9292 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9293 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9294 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9295 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 9296 | #ifdef HAVE_FDOPENDIR |
| 9297 | {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__}, |
| 9298 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9299 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 9300 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9301 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9302 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9303 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 9304 | #ifdef HAVE_GETPRIORITY |
| 9305 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 9306 | #endif /* HAVE_GETPRIORITY */ |
| 9307 | #ifdef HAVE_SETPRIORITY |
| 9308 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 9309 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9310 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9311 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9312 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 9313 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 9314 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 9315 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 9316 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 9317 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 9318 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9319 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9320 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9321 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9322 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9323 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9324 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9325 | win_symlink__doc__}, |
| 9326 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9327 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9328 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9329 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9330 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9331 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9332 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9333 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9334 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 9335 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 9336 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9337 | #ifdef HAVE_FUTIMES |
| 9338 | {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__}, |
| 9339 | #endif |
| 9340 | #ifdef HAVE_LUTIMES |
| 9341 | {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__}, |
| 9342 | #endif |
| 9343 | #ifdef HAVE_FUTIMENS |
| 9344 | {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__}, |
| 9345 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9346 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9347 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9348 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9349 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9350 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9351 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 9352 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9353 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9354 | #ifdef HAVE_FEXECVE |
| 9355 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 9356 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 9357 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9358 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 9359 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 9360 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9361 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 9362 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 9363 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 9364 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 9365 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9366 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 9367 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9368 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9369 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9370 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 9371 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9372 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 9373 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 9374 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9375 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 9376 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9377 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9378 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9379 | #endif /* HAVE_GETEGID */ |
| 9380 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9381 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9382 | #endif /* HAVE_GETEUID */ |
| 9383 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9384 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9385 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9386 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9387 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9388 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9389 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9390 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9391 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9392 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9393 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9394 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9395 | #endif /* HAVE_GETPPID */ |
| 9396 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9397 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9398 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9399 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9400 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9401 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9402 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9403 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9404 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 9405 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9406 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 9407 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 9408 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9409 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 9410 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 9411 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9412 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 9413 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 9414 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 9415 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9416 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9417 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9418 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9419 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9420 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9421 | #endif /* HAVE_SETEUID */ |
| 9422 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9423 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9424 | #endif /* HAVE_SETEGID */ |
| 9425 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9426 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9427 | #endif /* HAVE_SETREUID */ |
| 9428 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9429 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9430 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9431 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9432 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9433 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 9434 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9435 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 9436 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 9437 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9438 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 9439 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 9440 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9441 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 9442 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9443 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9444 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9445 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9446 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9447 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9448 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9449 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9450 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9451 | #endif /* HAVE_WAIT3 */ |
| 9452 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9453 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9454 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9455 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 9456 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 9457 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 9458 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9459 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9460 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 9461 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9462 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 9463 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9464 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9465 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9466 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9467 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9468 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9469 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9470 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9471 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9472 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9473 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9474 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9475 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9476 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 9477 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 9478 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 9479 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 9480 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 9481 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9482 | #ifdef HAVE_LOCKF |
| 9483 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 9484 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9485 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 9486 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9487 | #ifdef HAVE_READV |
| 9488 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 9489 | #endif |
| 9490 | #ifdef HAVE_PREAD |
| 9491 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 9492 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9493 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9494 | #ifdef HAVE_WRITEV |
| 9495 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 9496 | #endif |
| 9497 | #ifdef HAVE_PWRITE |
| 9498 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 9499 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9500 | #ifdef HAVE_SENDFILE |
| 9501 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 9502 | posix_sendfile__doc__}, |
| 9503 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9504 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 9505 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9506 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9507 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9508 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9509 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 9510 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9511 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9512 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9513 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9514 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 9515 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9516 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 9517 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 9518 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9519 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 9520 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 9521 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 9522 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9523 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9524 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9525 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9526 | #ifdef HAVE_TRUNCATE |
| 9527 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 9528 | #endif |
| 9529 | #ifdef HAVE_POSIX_FALLOCATE |
| 9530 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 9531 | #endif |
| 9532 | #ifdef HAVE_POSIX_FADVISE |
| 9533 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 9534 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9535 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9536 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9537 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9538 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9539 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9540 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9541 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9542 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9543 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9544 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 9545 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9546 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 9547 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9548 | #ifdef HAVE_SYNC |
| 9549 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 9550 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 9551 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9552 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 9553 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9554 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9555 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9556 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9557 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 9558 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9559 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 9560 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9561 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9562 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9563 | #endif /* WIFSTOPPED */ |
| 9564 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9565 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9566 | #endif /* WIFSIGNALED */ |
| 9567 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9568 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9569 | #endif /* WIFEXITED */ |
| 9570 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9571 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9572 | #endif /* WEXITSTATUS */ |
| 9573 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9574 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9575 | #endif /* WTERMSIG */ |
| 9576 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9577 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9578 | #endif /* WSTOPSIG */ |
| 9579 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9580 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9581 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9582 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9583 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9584 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9585 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9586 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9587 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9588 | #endif |
| 9589 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9590 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9591 | #endif |
| 9592 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9593 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9594 | #endif |
| 9595 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9596 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9597 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9598 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9599 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9600 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 9601 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 9602 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 9603 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 9604 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9605 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9606 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9607 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9608 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9609 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9610 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9611 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9612 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9613 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9614 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9615 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9616 | #endif |
| 9617 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9618 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9619 | #endif |
| 9620 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9621 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9622 | #endif |
| 9623 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9624 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9625 | #endif |
| 9626 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9627 | /* posix *at family of functions */ |
| 9628 | #ifdef HAVE_FACCESSAT |
| 9629 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 9630 | #endif |
| 9631 | #ifdef HAVE_FCHMODAT |
| 9632 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 9633 | #endif /* HAVE_FCHMODAT */ |
| 9634 | #ifdef HAVE_FCHOWNAT |
| 9635 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 9636 | #endif /* HAVE_FCHOWNAT */ |
| 9637 | #ifdef HAVE_FSTATAT |
| 9638 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
| 9639 | #endif |
| 9640 | #ifdef HAVE_FUTIMESAT |
| 9641 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 9642 | #endif |
| 9643 | #ifdef HAVE_LINKAT |
| 9644 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 9645 | #endif /* HAVE_LINKAT */ |
| 9646 | #ifdef HAVE_MKDIRAT |
| 9647 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 9648 | #endif |
| 9649 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9650 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 9651 | #endif |
| 9652 | #ifdef HAVE_OPENAT |
| 9653 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 9654 | #endif |
| 9655 | #ifdef HAVE_READLINKAT |
| 9656 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 9657 | #endif /* HAVE_READLINKAT */ |
| 9658 | #ifdef HAVE_RENAMEAT |
| 9659 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 9660 | #endif |
| 9661 | #if HAVE_SYMLINKAT |
| 9662 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 9663 | #endif /* HAVE_SYMLINKAT */ |
| 9664 | #ifdef HAVE_UNLINKAT |
| 9665 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 9666 | #endif |
| 9667 | #ifdef HAVE_UTIMENSAT |
| 9668 | {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__}, |
| 9669 | #endif |
| 9670 | #ifdef HAVE_MKFIFOAT |
| 9671 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 9672 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9673 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9674 | }; |
| 9675 | |
| 9676 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9677 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9678 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9679 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9680 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9681 | } |
| 9682 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9683 | #if defined(PYOS_OS2) |
| 9684 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9685 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9686 | { |
| 9687 | APIRET rc; |
| 9688 | ULONG values[QSV_MAX+1]; |
| 9689 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 9690 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9691 | |
| 9692 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 9693 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9694 | Py_END_ALLOW_THREADS |
| 9695 | |
| 9696 | if (rc != NO_ERROR) { |
| 9697 | os2_error(rc); |
| 9698 | return -1; |
| 9699 | } |
| 9700 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9701 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 9702 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 9703 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 9704 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 9705 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 9706 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 9707 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9708 | |
| 9709 | switch (values[QSV_VERSION_MINOR]) { |
| 9710 | case 0: ver = "2.00"; break; |
| 9711 | case 10: ver = "2.10"; break; |
| 9712 | case 11: ver = "2.11"; break; |
| 9713 | case 30: ver = "3.00"; break; |
| 9714 | case 40: ver = "4.00"; break; |
| 9715 | case 50: ver = "5.00"; break; |
| 9716 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 9717 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9718 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 9719 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9720 | ver = &tmp[0]; |
| 9721 | } |
| 9722 | |
| 9723 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9724 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9725 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9726 | |
| 9727 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 9728 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 9729 | tmp[1] = ':'; |
| 9730 | tmp[2] = '\0'; |
| 9731 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9732 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9733 | } |
| 9734 | #endif |
| 9735 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9736 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9737 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9738 | enable_symlink() |
| 9739 | { |
| 9740 | HANDLE tok; |
| 9741 | TOKEN_PRIVILEGES tok_priv; |
| 9742 | LUID luid; |
| 9743 | int meth_idx = 0; |
| 9744 | |
| 9745 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9746 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9747 | |
| 9748 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9749 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9750 | |
| 9751 | tok_priv.PrivilegeCount = 1; |
| 9752 | tok_priv.Privileges[0].Luid = luid; |
| 9753 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 9754 | |
| 9755 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 9756 | sizeof(TOKEN_PRIVILEGES), |
| 9757 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9758 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9759 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 9760 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 9761 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 9762 | } |
| 9763 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 9764 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9765 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 9766 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9767 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9768 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9769 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9770 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9771 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9772 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9773 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9774 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9775 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9776 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9777 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9778 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9779 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9780 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9781 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9782 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9783 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9784 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9785 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9786 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9787 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9788 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9789 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9790 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9791 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9792 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9793 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9794 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9795 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9796 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9797 | #endif |
| 9798 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9799 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9800 | #endif |
| 9801 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9802 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9803 | #endif |
| 9804 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9805 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9806 | #endif |
| 9807 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9808 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9809 | #endif |
| 9810 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9811 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9812 | #endif |
| 9813 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9814 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9815 | #endif |
| 9816 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9817 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9818 | #endif |
| 9819 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9820 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9821 | #endif |
| 9822 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9823 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9824 | #endif |
| 9825 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9826 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9827 | #endif |
| 9828 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9829 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9830 | #endif |
| 9831 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9832 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9833 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9834 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9835 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9836 | #endif |
| 9837 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9838 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9839 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9840 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9841 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9842 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9843 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9844 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9845 | #endif |
| 9846 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9847 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9848 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 9849 | #ifdef PRIO_PROCESS |
| 9850 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 9851 | #endif |
| 9852 | #ifdef PRIO_PGRP |
| 9853 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 9854 | #endif |
| 9855 | #ifdef PRIO_USER |
| 9856 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 9857 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 9858 | #ifdef O_CLOEXEC |
| 9859 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 9860 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9861 | /* posix - constants for *at functions */ |
| 9862 | #ifdef AT_SYMLINK_NOFOLLOW |
| 9863 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 9864 | #endif |
| 9865 | #ifdef AT_EACCESS |
| 9866 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 9867 | #endif |
| 9868 | #ifdef AT_FDCWD |
| 9869 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 9870 | #endif |
| 9871 | #ifdef AT_REMOVEDIR |
| 9872 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 9873 | #endif |
| 9874 | #ifdef AT_SYMLINK_FOLLOW |
| 9875 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 9876 | #endif |
| 9877 | #ifdef UTIME_NOW |
| 9878 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 9879 | #endif |
| 9880 | #ifdef UTIME_OMIT |
| 9881 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 9882 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 9883 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9884 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9885 | /* MS Windows */ |
| 9886 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9887 | /* Don't inherit in child processes. */ |
| 9888 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9889 | #endif |
| 9890 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9891 | /* Optimize for short life (keep in memory). */ |
| 9892 | /* MS forgot to define this one with a non-underscore form too. */ |
| 9893 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9894 | #endif |
| 9895 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9896 | /* Automatically delete when last handle is closed. */ |
| 9897 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9898 | #endif |
| 9899 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9900 | /* Optimize for random access. */ |
| 9901 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9902 | #endif |
| 9903 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9904 | /* Optimize for sequential access. */ |
| 9905 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9906 | #endif |
| 9907 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9908 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 9909 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9910 | /* Send a SIGIO signal whenever input or output |
| 9911 | becomes available on file descriptor */ |
| 9912 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 9913 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9914 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9915 | /* Direct disk access. */ |
| 9916 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9917 | #endif |
| 9918 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9919 | /* Must be a directory. */ |
| 9920 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9921 | #endif |
| 9922 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9923 | /* Do not follow links. */ |
| 9924 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9925 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 9926 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9927 | /* Do not update the access time. */ |
| 9928 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 9929 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9930 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9931 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9932 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9933 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9934 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9935 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9936 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9937 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9938 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9939 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9940 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9941 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9942 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9943 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9944 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9945 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9946 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9947 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9948 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9949 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9950 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9951 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9952 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9953 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9954 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9955 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9956 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9957 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9958 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9959 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9960 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9961 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9962 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9963 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9964 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9965 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9966 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9967 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9968 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9970 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9971 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9972 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9973 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9974 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9975 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9976 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9977 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9978 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9979 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9980 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9981 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9982 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9983 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 9984 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 9985 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 9986 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 9987 | #endif /* ST_RDONLY */ |
| 9988 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 9989 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 9990 | #endif /* ST_NOSUID */ |
| 9991 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9992 | /* FreeBSD sendfile() constants */ |
| 9993 | #ifdef SF_NODISKIO |
| 9994 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 9995 | #endif |
| 9996 | #ifdef SF_MNOWAIT |
| 9997 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 9998 | #endif |
| 9999 | #ifdef SF_SYNC |
| 10000 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 10001 | #endif |
| 10002 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10003 | /* constants for posix_fadvise */ |
| 10004 | #ifdef POSIX_FADV_NORMAL |
| 10005 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 10006 | #endif |
| 10007 | #ifdef POSIX_FADV_SEQUENTIAL |
| 10008 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 10009 | #endif |
| 10010 | #ifdef POSIX_FADV_RANDOM |
| 10011 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 10012 | #endif |
| 10013 | #ifdef POSIX_FADV_NOREUSE |
| 10014 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 10015 | #endif |
| 10016 | #ifdef POSIX_FADV_WILLNEED |
| 10017 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 10018 | #endif |
| 10019 | #ifdef POSIX_FADV_DONTNEED |
| 10020 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 10021 | #endif |
| 10022 | |
| 10023 | /* constants for waitid */ |
| 10024 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 10025 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 10026 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 10027 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 10028 | #endif |
| 10029 | #ifdef WEXITED |
| 10030 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 10031 | #endif |
| 10032 | #ifdef WNOWAIT |
| 10033 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 10034 | #endif |
| 10035 | #ifdef WSTOPPED |
| 10036 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 10037 | #endif |
| 10038 | #ifdef CLD_EXITED |
| 10039 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 10040 | #endif |
| 10041 | #ifdef CLD_DUMPED |
| 10042 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 10043 | #endif |
| 10044 | #ifdef CLD_TRAPPED |
| 10045 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 10046 | #endif |
| 10047 | #ifdef CLD_CONTINUED |
| 10048 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 10049 | #endif |
| 10050 | |
| 10051 | /* constants for lockf */ |
| 10052 | #ifdef F_LOCK |
| 10053 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 10054 | #endif |
| 10055 | #ifdef F_TLOCK |
| 10056 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 10057 | #endif |
| 10058 | #ifdef F_ULOCK |
| 10059 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 10060 | #endif |
| 10061 | #ifdef F_TEST |
| 10062 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 10063 | #endif |
| 10064 | |
| 10065 | /* constants for futimens */ |
| 10066 | #ifdef UTIME_NOW |
| 10067 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 10068 | #endif |
| 10069 | #ifdef UTIME_OMIT |
| 10070 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 10071 | #endif |
| 10072 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 10073 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 10074 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10075 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 10076 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 10077 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 10078 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 10079 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 10080 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 10081 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 10082 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 10083 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 10084 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 10085 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 10086 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 10087 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 10088 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 10089 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 10090 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 10091 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 10092 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 10093 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 10094 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 10095 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10096 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 10097 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 10098 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 10099 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 10100 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 10101 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 10102 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 10103 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10104 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10105 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10106 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10107 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10108 | } |
| 10109 | |
| 10110 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10111 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10112 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 10113 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 10114 | |
| 10115 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10116 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 10117 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 10118 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 10119 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10120 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 10121 | #define MODNAME "posix" |
| 10122 | #endif |
| 10123 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10124 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10125 | PyModuleDef_HEAD_INIT, |
| 10126 | MODNAME, |
| 10127 | posix__doc__, |
| 10128 | -1, |
| 10129 | posix_methods, |
| 10130 | NULL, |
| 10131 | NULL, |
| 10132 | NULL, |
| 10133 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10134 | }; |
| 10135 | |
| 10136 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 10137 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 10138 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10139 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10140 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10141 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10142 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10143 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10144 | #endif |
| 10145 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10146 | m = PyModule_Create(&posixmodule); |
| 10147 | if (m == NULL) |
| 10148 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10149 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10150 | /* Initialize environ dictionary */ |
| 10151 | v = convertenviron(); |
| 10152 | Py_XINCREF(v); |
| 10153 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 10154 | return NULL; |
| 10155 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10156 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10157 | if (all_ins(m)) |
| 10158 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10159 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10160 | if (setup_confname_tables(m)) |
| 10161 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10162 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10163 | Py_INCREF(PyExc_OSError); |
| 10164 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 10165 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 10166 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10167 | if (posix_putenv_garbage == NULL) |
| 10168 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 10169 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10170 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10171 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10172 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10173 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 10174 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 10175 | #endif |
| 10176 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10177 | stat_result_desc.name = MODNAME ".stat_result"; |
| 10178 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 10179 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 10180 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 10181 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 10182 | structseq_new = StatResultType.tp_new; |
| 10183 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10184 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10185 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 10186 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 10187 | #ifdef NEED_TICKS_PER_SECOND |
| 10188 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10189 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 10190 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10191 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 10192 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10193 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 10194 | # endif |
| 10195 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10196 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10197 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10198 | Py_INCREF((PyObject*) &WaitidResultType); |
| 10199 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 10200 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | Py_INCREF((PyObject*) &StatResultType); |
| 10202 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 10203 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 10204 | PyModule_AddObject(m, "statvfs_result", |
| 10205 | (PyObject*) &StatVFSResultType); |
| 10206 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10207 | |
| 10208 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10209 | /* |
| 10210 | * Step 2 of weak-linking support on Mac OS X. |
| 10211 | * |
| 10212 | * The code below removes functions that are not available on the |
| 10213 | * currently active platform. |
| 10214 | * |
| 10215 | * This block allow one to use a python binary that was build on |
| 10216 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 10217 | * OSX 10.4. |
| 10218 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10219 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10220 | if (fstatvfs == NULL) { |
| 10221 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 10222 | return NULL; |
| 10223 | } |
| 10224 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10225 | #endif /* HAVE_FSTATVFS */ |
| 10226 | |
| 10227 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10228 | if (statvfs == NULL) { |
| 10229 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 10230 | return NULL; |
| 10231 | } |
| 10232 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10233 | #endif /* HAVE_STATVFS */ |
| 10234 | |
| 10235 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10236 | if (lchown == NULL) { |
| 10237 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 10238 | return NULL; |
| 10239 | } |
| 10240 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10241 | #endif /* HAVE_LCHOWN */ |
| 10242 | |
| 10243 | |
| 10244 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10245 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10246 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10247 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10248 | |
| 10249 | #ifdef __cplusplus |
| 10250 | } |
| 10251 | #endif |