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 | 97b8988 | 2010-05-06 00:25:39 +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" |
| 31 | #include "structseq.h" |
| 32 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) |
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) |
| 49 | #define INCL_DOS |
| 50 | #define INCL_DOSERRORS |
| 51 | #define INCL_DOSPROCESS |
| 52 | #define INCL_NOPMAPI |
| 53 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 54 | #if defined(PYCC_GCC) |
| 55 | #include <ctype.h> |
| 56 | #include <io.h> |
| 57 | #include <stdio.h> |
| 58 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 59 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 60 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | #endif /* HAVE_SYS_TYPES_H */ |
| 66 | |
| 67 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 72 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 73 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 76 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | #ifdef HAVE_FCNTL_H |
| 80 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 81 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | #ifdef HAVE_GRP_H |
| 84 | #include <grp.h> |
| 85 | #endif |
| 86 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 87 | #ifdef HAVE_SYSEXITS_H |
| 88 | #include <sysexits.h> |
| 89 | #endif /* HAVE_SYSEXITS_H */ |
| 90 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYS_LOADAVG_H |
| 92 | #include <sys/loadavg.h> |
| 93 | #endif |
| 94 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 95 | #ifdef HAVE_LANGINFO_H |
| 96 | #include <langinfo.h> |
| 97 | #endif |
| 98 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> |
| 103 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
| 106 | #define HAVE_OPENDIR 1 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 107 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 108 | #if defined(__OS2__) |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> |
| 113 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR 1 |
| 118 | #define HAVE_PIPE 1 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 119 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 120 | #define HAVE_WAIT 1 |
| 121 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 122 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 123 | #define HAVE_GETCWD 1 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 124 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #define HAVE_EXECV 1 |
| 126 | #define HAVE_PIPE 1 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 127 | #define HAVE_SYSTEM 1 |
| 128 | #define HAVE_CWAIT 1 |
| 129 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 130 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 131 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 132 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 133 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 134 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 135 | /* Unix functions that the configure script doesn't check for */ |
| 136 | #define HAVE_EXECV 1 |
| 137 | #define HAVE_FORK 1 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 138 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 139 | #define HAVE_FORK1 1 |
| 140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #define HAVE_GETCWD 1 |
| 142 | #define HAVE_GETEGID 1 |
| 143 | #define HAVE_GETEUID 1 |
| 144 | #define HAVE_GETGID 1 |
| 145 | #define HAVE_GETPPID 1 |
| 146 | #define HAVE_GETUID 1 |
| 147 | #define HAVE_KILL 1 |
| 148 | #define HAVE_OPENDIR 1 |
| 149 | #define HAVE_PIPE 1 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 151 | #define HAVE_WAIT 1 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 152 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 153 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #endif /* _MSC_VER */ |
| 155 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 156 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 157 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 161 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 162 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 163 | (default) */ |
| 164 | extern char *ctermid_r(char *); |
| 165 | #endif |
| 166 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 167 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 170 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 171 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #endif |
| 177 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int chdir(char *); |
| 179 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 180 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int chdir(const char *); |
| 182 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 184 | #ifdef __BORLANDC__ |
| 185 | extern int chmod(const char *, int); |
| 186 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 188 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 189 | /*#ifdef HAVE_FCHMOD |
| 190 | extern int fchmod(int, mode_t); |
| 191 | #endif*/ |
| 192 | /*#ifdef HAVE_LCHMOD |
| 193 | extern int lchmod(const char *, mode_t); |
| 194 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int chown(const char *, uid_t, gid_t); |
| 196 | extern char *getcwd(char *, int); |
| 197 | extern char *strerror(int); |
| 198 | extern int link(const char *, const char *); |
| 199 | extern int rename(const char *, const char *); |
| 200 | extern int stat(const char *, struct stat *); |
| 201 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 206 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 207 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #ifdef HAVE_UTIME_H |
| 213 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 216 | #ifdef HAVE_SYS_UTIME_H |
| 217 | #include <sys/utime.h> |
| 218 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 219 | #endif /* HAVE_SYS_UTIME_H */ |
| 220 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #ifdef HAVE_SYS_TIMES_H |
| 222 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 223 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | |
| 225 | #ifdef HAVE_SYS_PARAM_H |
| 226 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 227 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | |
| 229 | #ifdef HAVE_SYS_UTSNAME_H |
| 230 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 231 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 236 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 237 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #include <direct.h> |
| 239 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 240 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 243 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 244 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 249 | #endif |
| 250 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 255 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 256 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 258 | #endif |
| 259 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 261 | #endif |
| 262 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 264 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 265 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 266 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <windows.h> |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 268 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 269 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 271 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 273 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 275 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 276 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 277 | #define MAXPATHLEN PATH_MAX |
| 278 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 279 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 280 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 281 | #endif /* MAXPATHLEN */ |
| 282 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 283 | #ifdef UNION_WAIT |
| 284 | /* Emulate some macros on systems that have a union instead of macros */ |
| 285 | |
| 286 | #ifndef WIFEXITED |
| 287 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 288 | #endif |
| 289 | |
| 290 | #ifndef WEXITSTATUS |
| 291 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 292 | #endif |
| 293 | |
| 294 | #ifndef WTERMSIG |
| 295 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 296 | #endif |
| 297 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 298 | #define WAIT_TYPE union wait |
| 299 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 300 | |
| 301 | #else /* !UNION_WAIT */ |
| 302 | #define WAIT_TYPE int |
| 303 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 304 | #endif /* UNION_WAIT */ |
| 305 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 306 | /* Issue #1983: pid_t can be longer than a C long on some systems */ |
Antoine Pitrou | 7852c42 | 2009-05-24 11:58:35 +0000 | [diff] [blame] | 307 | #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 308 | #define PARSE_PID "i" |
| 309 | #define PyLong_FromPid PyLong_FromLong |
| 310 | #define PyLong_AsPid PyLong_AsLong |
| 311 | #elif SIZEOF_PID_T == SIZEOF_LONG |
| 312 | #define PARSE_PID "l" |
| 313 | #define PyLong_FromPid PyLong_FromLong |
| 314 | #define PyLong_AsPid PyLong_AsLong |
| 315 | #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG |
| 316 | #define PARSE_PID "L" |
| 317 | #define PyLong_FromPid PyLong_FromLongLong |
| 318 | #define PyLong_AsPid PyLong_AsLongLong |
| 319 | #else |
| 320 | #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)" |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 321 | #endif /* SIZEOF_PID_T */ |
| 322 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 323 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 324 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 325 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 326 | #define USE_CTERMID_R |
| 327 | #endif |
| 328 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 329 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 330 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 331 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 332 | # define STAT win32_stat |
| 333 | # define FSTAT win32_fstat |
| 334 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 335 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 336 | # define STAT stat |
| 337 | # define FSTAT fstat |
| 338 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 339 | #endif |
| 340 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 341 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 342 | #include <sys/mkdev.h> |
| 343 | #else |
| 344 | #if defined(MAJOR_IN_SYSMACROS) |
| 345 | #include <sys/sysmacros.h> |
| 346 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 347 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 348 | #include <sys/mkdev.h> |
| 349 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 350 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 351 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 352 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 353 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 354 | * valid and throw an assertion if it isn't. |
| 355 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 356 | * an assertion can be useful, but it does contradict the POSIX standard |
| 357 | * which for write(2) states: |
| 358 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 359 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 360 | * writing." |
| 361 | * Furthermore, python allows the user to enter any old integer |
| 362 | * as a fd and should merely raise a python exception on error. |
| 363 | * The Microsoft CRT doesn't provide an official way to check for the |
| 364 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 365 | * 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] | 366 | * internal structures involved. |
| 367 | * The structures below must be updated for each version of visual studio |
| 368 | * according to the file internal.h in the CRT source, until MS comes |
| 369 | * up with a less hacky way to do this. |
| 370 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 371 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 372 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 373 | /* The actual size of the structure is determined at runtime. |
| 374 | * Only the first items must be present. |
| 375 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 376 | typedef struct { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 377 | intptr_t osfhnd; |
| 378 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 379 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 380 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 381 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 382 | #define IOINFO_L2E 5 |
| 383 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 384 | #define IOINFO_ARRAYS 64 |
| 385 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 386 | #define FOPEN 0x01 |
| 387 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 388 | |
| 389 | /* This function emulates what the windows CRT does to validate file handles */ |
| 390 | int |
| 391 | _PyVerify_fd(int fd) |
| 392 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 393 | const int i1 = fd >> IOINFO_L2E; |
| 394 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 395 | |
Antoine Pitrou | 835b445 | 2010-08-15 18:32:16 +0000 | [diff] [blame] | 396 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 397 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 398 | /* Determine the actual size of the ioinfo structure, |
| 399 | * as used by the CRT loaded in memory |
| 400 | */ |
| 401 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 402 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 403 | } |
| 404 | if (sizeof_ioinfo == 0) { |
| 405 | /* This should not happen... */ |
| 406 | goto fail; |
| 407 | } |
| 408 | |
| 409 | /* See that it isn't a special CLEAR fileno */ |
| 410 | if (fd != _NO_CONSOLE_FILENO) { |
| 411 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 412 | * we check pointer validity and other info |
| 413 | */ |
| 414 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 415 | /* finally, check that the file is open */ |
| 416 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 417 | if (info->osfile & FOPEN) { |
| 418 | return 1; |
| 419 | } |
| 420 | } |
| 421 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 422 | fail: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 423 | errno = EBADF; |
| 424 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 428 | static int |
| 429 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 430 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 431 | if (!_PyVerify_fd(fd1)) |
| 432 | return 0; |
| 433 | if (fd2 == _NO_CONSOLE_FILENO) |
| 434 | return 0; |
| 435 | if ((unsigned)fd2 < _NHANDLE_) |
| 436 | return 1; |
| 437 | else |
| 438 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 439 | } |
| 440 | #else |
| 441 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 442 | #define _PyVerify_fd_dup2(A, B) (1) |
| 443 | #endif |
| 444 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 445 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 446 | #ifdef WITH_NEXT_FRAMEWORK |
| 447 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 448 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 449 | */ |
| 450 | #include <crt_externs.h> |
| 451 | static char **environ; |
| 452 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 453 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 454 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 455 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 456 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 457 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 458 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 459 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 460 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 461 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 462 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 463 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 464 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 465 | #if defined(PYOS_OS2) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 466 | APIRET rc; |
| 467 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 468 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 469 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 470 | d = PyDict_New(); |
| 471 | if (d == NULL) |
| 472 | return NULL; |
| 473 | #ifdef WITH_NEXT_FRAMEWORK |
| 474 | if (environ == NULL) |
| 475 | environ = *_NSGetEnviron(); |
| 476 | #endif |
| 477 | #ifdef MS_WINDOWS |
| 478 | /* _wenviron must be initialized in this way if the program is started |
| 479 | through main() instead of wmain(). */ |
| 480 | _wgetenv(L""); |
| 481 | if (_wenviron == NULL) |
| 482 | return d; |
| 483 | /* This part ignores errors */ |
| 484 | for (e = _wenviron; *e != NULL; e++) { |
| 485 | PyObject *k; |
| 486 | PyObject *v; |
| 487 | wchar_t *p = wcschr(*e, L'='); |
| 488 | if (p == NULL) |
| 489 | continue; |
| 490 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 491 | if (k == NULL) { |
| 492 | PyErr_Clear(); |
| 493 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 494 | } |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 495 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 496 | if (v == NULL) { |
| 497 | PyErr_Clear(); |
| 498 | Py_DECREF(k); |
| 499 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 500 | } |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 501 | if (PyDict_GetItem(d, k) == NULL) { |
| 502 | if (PyDict_SetItem(d, k, v) != 0) |
| 503 | PyErr_Clear(); |
| 504 | } |
| 505 | Py_DECREF(k); |
| 506 | Py_DECREF(v); |
| 507 | } |
| 508 | #else |
| 509 | if (environ == NULL) |
| 510 | return d; |
| 511 | /* This part ignores errors */ |
| 512 | for (e = environ; *e != NULL; e++) { |
| 513 | PyObject *k; |
| 514 | PyObject *v; |
| 515 | char *p = strchr(*e, '='); |
| 516 | if (p == NULL) |
| 517 | continue; |
| 518 | k = PyUnicode_Decode(*e, (int)(p-*e), |
| 519 | Py_FileSystemDefaultEncoding, "surrogateescape"); |
| 520 | if (k == NULL) { |
| 521 | PyErr_Clear(); |
| 522 | continue; |
| 523 | } |
| 524 | v = PyUnicode_Decode(p+1, strlen(p+1), |
| 525 | Py_FileSystemDefaultEncoding, "surrogateescape"); |
| 526 | if (v == NULL) { |
| 527 | PyErr_Clear(); |
| 528 | Py_DECREF(k); |
| 529 | continue; |
| 530 | } |
| 531 | if (PyDict_GetItem(d, k) == NULL) { |
| 532 | if (PyDict_SetItem(d, k, v) != 0) |
| 533 | PyErr_Clear(); |
| 534 | } |
| 535 | Py_DECREF(k); |
| 536 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 537 | } |
| 538 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 539 | #if defined(PYOS_OS2) |
| 540 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 541 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 542 | PyObject *v = PyBytes_FromString(buffer); |
| 543 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 544 | Py_DECREF(v); |
| 545 | } |
| 546 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 547 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 548 | PyObject *v = PyBytes_FromString(buffer); |
| 549 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 550 | Py_DECREF(v); |
| 551 | } |
| 552 | #endif |
| 553 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 556 | /* Convert a bytes object to a char*. Optionally lock the buffer if it is a |
| 557 | bytes array. */ |
| 558 | |
| 559 | static char* |
| 560 | bytes2str(PyObject* o, int lock) |
| 561 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 562 | if(PyBytes_Check(o)) |
| 563 | return PyBytes_AsString(o); |
| 564 | else if(PyByteArray_Check(o)) { |
| 565 | if (lock && PyObject_GetBuffer(o, NULL, 0) < 0) |
| 566 | /* On a bytearray, this should not fail. */ |
| 567 | PyErr_BadInternalCall(); |
| 568 | return PyByteArray_AsString(o); |
| 569 | } else { |
| 570 | /* The FS converter should have verified that this |
| 571 | is either bytes or bytearray. */ |
| 572 | Py_FatalError("bad object passed to bytes2str"); |
| 573 | /* not reached. */ |
| 574 | return ""; |
| 575 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* Release the lock, decref the object. */ |
| 579 | static void |
| 580 | release_bytes(PyObject* o) |
| 581 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 582 | if (PyByteArray_Check(o)) |
| 583 | o->ob_type->tp_as_buffer->bf_releasebuffer(o, 0); |
| 584 | Py_DECREF(o); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 587 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 588 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 589 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 590 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 591 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 592 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 593 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 594 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 595 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 596 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 597 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 598 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 601 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 602 | static PyObject * |
| 603 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 604 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 605 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 606 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 607 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 608 | |
| 609 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 610 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 611 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 612 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 613 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
| 614 | bytes2str(name, 0)); |
| 615 | release_bytes(name); |
| 616 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 619 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 620 | static PyObject * |
| 621 | win32_error(char* function, char* filename) |
| 622 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 623 | /* XXX We should pass the function name along in the future. |
| 624 | (winreg.c also wants to pass the function name.) |
| 625 | This would however require an additional param to the |
| 626 | Windows error object, which is non-trivial. |
| 627 | */ |
| 628 | errno = GetLastError(); |
| 629 | if (filename) |
| 630 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 631 | else |
| 632 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 633 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 634 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 635 | static PyObject * |
| 636 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 637 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 638 | /* XXX - see win32_error for comments on 'function' */ |
| 639 | errno = GetLastError(); |
| 640 | if (filename) |
| 641 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 642 | else |
| 643 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 646 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 647 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 648 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 649 | if (PyUnicode_CheckExact(*param)) |
| 650 | Py_INCREF(*param); |
| 651 | else if (PyUnicode_Check(*param)) |
| 652 | /* For a Unicode subtype that's not a Unicode object, |
| 653 | return a true Unicode object with the same data. */ |
| 654 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 655 | PyUnicode_GET_SIZE(*param)); |
| 656 | else |
| 657 | *param = PyUnicode_FromEncodedObject(*param, |
| 658 | Py_FileSystemDefaultEncoding, |
| 659 | "strict"); |
| 660 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 663 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 664 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 665 | #if defined(PYOS_OS2) |
| 666 | /********************************************************************** |
| 667 | * Helper Function to Trim and Format OS/2 Messages |
| 668 | **********************************************************************/ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 669 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 670 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 671 | { |
| 672 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 673 | |
| 674 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 675 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 676 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 677 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 678 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 679 | } |
| 680 | |
| 681 | /* Add Optional Reason Text */ |
| 682 | if (reason) { |
| 683 | strcat(msgbuf, " : "); |
| 684 | strcat(msgbuf, reason); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /********************************************************************** |
| 689 | * Decode an OS/2 Operating System Error Code |
| 690 | * |
| 691 | * A convenience function to lookup an OS/2 error code and return a |
| 692 | * text message we can use to raise a Python exception. |
| 693 | * |
| 694 | * Notes: |
| 695 | * The messages for errors returned from the OS/2 kernel reside in |
| 696 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 697 | * |
| 698 | **********************************************************************/ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 699 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 700 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 701 | { |
| 702 | APIRET rc; |
| 703 | ULONG msglen; |
| 704 | |
| 705 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 706 | Py_BEGIN_ALLOW_THREADS |
| 707 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 708 | errorcode, "oso001.msg", &msglen); |
| 709 | Py_END_ALLOW_THREADS |
| 710 | |
| 711 | if (rc == NO_ERROR) |
| 712 | os2_formatmsg(msgbuf, msglen, reason); |
| 713 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 714 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 715 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 716 | |
| 717 | return msgbuf; |
| 718 | } |
| 719 | |
| 720 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 721 | errors are not in a global variable e.g. 'errno' nor are |
| 722 | they congruent with posix error numbers. */ |
| 723 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 724 | static PyObject * |
| 725 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 726 | { |
| 727 | char text[1024]; |
| 728 | PyObject *v; |
| 729 | |
| 730 | os2_strerror(text, sizeof(text), code, ""); |
| 731 | |
| 732 | v = Py_BuildValue("(is)", code, text); |
| 733 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 734 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 735 | Py_DECREF(v); |
| 736 | } |
| 737 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 738 | } |
| 739 | |
| 740 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 741 | |
| 742 | /* POSIX generic methods */ |
| 743 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 744 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 745 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 746 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 747 | int fd; |
| 748 | int res; |
| 749 | fd = PyObject_AsFileDescriptor(fdobj); |
| 750 | if (fd < 0) |
| 751 | return NULL; |
| 752 | if (!_PyVerify_fd(fd)) |
| 753 | return posix_error(); |
| 754 | Py_BEGIN_ALLOW_THREADS |
| 755 | res = (*func)(fd); |
| 756 | Py_END_ALLOW_THREADS |
| 757 | if (res < 0) |
| 758 | return posix_error(); |
| 759 | Py_INCREF(Py_None); |
| 760 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 761 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 762 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 763 | #ifdef MS_WINDOWS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 764 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 765 | unicode_file_names(void) |
| 766 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 767 | static int canusewide = -1; |
| 768 | if (canusewide == -1) { |
| 769 | /* As per doc for ::GetVersion(), this is the correct test for |
| 770 | the Windows NT family. */ |
| 771 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 772 | } |
| 773 | return canusewide; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 774 | } |
| 775 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 776 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 777 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 778 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 779 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 780 | PyObject *opath1 = NULL; |
| 781 | char *path1; |
| 782 | int res; |
| 783 | if (!PyArg_ParseTuple(args, format, |
| 784 | PyUnicode_FSConverter, &opath1)) |
| 785 | return NULL; |
| 786 | path1 = bytes2str(opath1, 1); |
| 787 | Py_BEGIN_ALLOW_THREADS |
| 788 | res = (*func)(path1); |
| 789 | Py_END_ALLOW_THREADS |
| 790 | if (res < 0) |
| 791 | return posix_error_with_allocated_filename(opath1); |
| 792 | release_bytes(opath1); |
| 793 | Py_INCREF(Py_None); |
| 794 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 797 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 798 | posix_2str(PyObject *args, |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 799 | char *format, |
| 800 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 801 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 802 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 803 | char *path1, *path2; |
| 804 | int res; |
| 805 | if (!PyArg_ParseTuple(args, format, |
| 806 | PyUnicode_FSConverter, &opath1, |
| 807 | PyUnicode_FSConverter, &opath2)) { |
| 808 | return NULL; |
| 809 | } |
| 810 | path1 = bytes2str(opath1, 1); |
| 811 | path2 = bytes2str(opath2, 1); |
| 812 | Py_BEGIN_ALLOW_THREADS |
| 813 | res = (*func)(path1, path2); |
| 814 | Py_END_ALLOW_THREADS |
| 815 | release_bytes(opath1); |
| 816 | release_bytes(opath2); |
| 817 | if (res != 0) |
| 818 | /* XXX how to report both path1 and path2??? */ |
| 819 | return posix_error(); |
| 820 | Py_INCREF(Py_None); |
| 821 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 824 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 825 | static PyObject* |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 826 | win32_1str(PyObject* args, char* func, |
| 827 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 828 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 829 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 830 | PyObject *uni; |
| 831 | char *ansi; |
| 832 | BOOL result; |
| 833 | if (unicode_file_names()) { |
| 834 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 835 | PyErr_Clear(); |
| 836 | else { |
| 837 | Py_BEGIN_ALLOW_THREADS |
| 838 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 839 | Py_END_ALLOW_THREADS |
| 840 | if (!result) |
| 841 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 842 | Py_INCREF(Py_None); |
| 843 | return Py_None; |
| 844 | } |
| 845 | } |
| 846 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 847 | return NULL; |
| 848 | Py_BEGIN_ALLOW_THREADS |
| 849 | result = funcA(ansi); |
| 850 | Py_END_ALLOW_THREADS |
| 851 | if (!result) |
| 852 | return win32_error(func, ansi); |
| 853 | Py_INCREF(Py_None); |
| 854 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 855 | |
| 856 | } |
| 857 | |
| 858 | /* This is a reimplementation of the C library's chdir function, |
| 859 | but one that produces Win32 errors instead of DOS error codes. |
| 860 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 861 | it also needs to set "magic" environment variables indicating |
| 862 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 863 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 864 | win32_chdir(LPCSTR path) |
| 865 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 866 | char new_path[MAX_PATH+1]; |
| 867 | int result; |
| 868 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 869 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 870 | if(!SetCurrentDirectoryA(path)) |
| 871 | return FALSE; |
| 872 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 873 | if (!result) |
| 874 | return FALSE; |
| 875 | /* In the ANSI API, there should not be any paths longer |
| 876 | than MAX_PATH. */ |
| 877 | assert(result <= MAX_PATH+1); |
| 878 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 879 | strncmp(new_path, "//", 2) == 0) |
| 880 | /* UNC path, nothing to do. */ |
| 881 | return TRUE; |
| 882 | env[1] = new_path[0]; |
| 883 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* The Unicode version differs from the ANSI version |
| 887 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 888 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 889 | win32_wchdir(LPCWSTR path) |
| 890 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 891 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 892 | int result; |
| 893 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 894 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 895 | if(!SetCurrentDirectoryW(path)) |
| 896 | return FALSE; |
| 897 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 898 | if (!result) |
| 899 | return FALSE; |
| 900 | if (result > MAX_PATH+1) { |
| 901 | new_path = malloc(result * sizeof(wchar_t)); |
| 902 | if (!new_path) { |
| 903 | SetLastError(ERROR_OUTOFMEMORY); |
| 904 | return FALSE; |
| 905 | } |
| 906 | result = GetCurrentDirectoryW(result, new_path); |
| 907 | if (!result) { |
| 908 | free(new_path); |
| 909 | return FALSE; |
| 910 | } |
| 911 | } |
| 912 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 913 | wcsncmp(new_path, L"//", 2) == 0) |
| 914 | /* UNC path, nothing to do. */ |
| 915 | return TRUE; |
| 916 | env[1] = new_path[0]; |
| 917 | result = SetEnvironmentVariableW(env, new_path); |
| 918 | if (new_path != _new_path) |
| 919 | free(new_path); |
| 920 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 921 | } |
| 922 | #endif |
| 923 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 924 | #ifdef MS_WINDOWS |
| 925 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 926 | - time stamps are restricted to second resolution |
| 927 | - file modification times suffer from forth-and-back conversions between |
| 928 | UTC and local time |
| 929 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 930 | */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 931 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 932 | |
| 933 | struct win32_stat{ |
| 934 | int st_dev; |
| 935 | __int64 st_ino; |
| 936 | unsigned short st_mode; |
| 937 | int st_nlink; |
| 938 | int st_uid; |
| 939 | int st_gid; |
| 940 | int st_rdev; |
| 941 | __int64 st_size; |
| 942 | int st_atime; |
| 943 | int st_atime_nsec; |
| 944 | int st_mtime; |
| 945 | int st_mtime_nsec; |
| 946 | int st_ctime; |
| 947 | int st_ctime_nsec; |
| 948 | }; |
| 949 | |
| 950 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 951 | |
| 952 | static void |
| 953 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 954 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 955 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 956 | /* Cannot simply cast and dereference in_ptr, |
| 957 | since it might not be aligned properly */ |
| 958 | __int64 in; |
| 959 | memcpy(&in, in_ptr, sizeof(in)); |
| 960 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 961 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 962 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 965 | static void |
| 966 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 967 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 968 | /* XXX endianness */ |
| 969 | __int64 out; |
| 970 | out = time_in + secs_between_epochs; |
| 971 | out = out * 10000000 + nsec_in / 100; |
| 972 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 975 | /* Below, we *know* that ugo+r is 0444 */ |
| 976 | #if _S_IREAD != 0400 |
| 977 | #error Unsupported C library |
| 978 | #endif |
| 979 | static int |
| 980 | attributes_to_mode(DWORD attr) |
| 981 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 982 | int m = 0; |
| 983 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 984 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 985 | else |
| 986 | m |= _S_IFREG; |
| 987 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 988 | m |= 0444; |
| 989 | else |
| 990 | m |= 0666; |
| 991 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static int |
| 995 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 996 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 997 | memset(result, 0, sizeof(*result)); |
| 998 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 999 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1000 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1001 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1002 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1003 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1004 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1007 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 1008 | static int checked = 0; |
| 1009 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 1010 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 1011 | static void |
| 1012 | check_gfax() |
| 1013 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1014 | HINSTANCE hKernel32; |
| 1015 | if (checked) |
| 1016 | return; |
| 1017 | checked = 1; |
| 1018 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1019 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 1020 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1023 | static BOOL |
| 1024 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1025 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1026 | HANDLE hFindFile; |
| 1027 | WIN32_FIND_DATAA FileData; |
| 1028 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1029 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1030 | return FALSE; |
| 1031 | FindClose(hFindFile); |
| 1032 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1033 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1034 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1035 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1036 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1037 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1038 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | static BOOL |
| 1042 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1043 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1044 | HANDLE hFindFile; |
| 1045 | WIN32_FIND_DATAW FileData; |
| 1046 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1047 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1048 | return FALSE; |
| 1049 | FindClose(hFindFile); |
| 1050 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1051 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1052 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1053 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1054 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1055 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1056 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1059 | static BOOL WINAPI |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1060 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 1061 | GET_FILEEX_INFO_LEVELS level, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1062 | LPVOID pv) |
| 1063 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1064 | BOOL result; |
| 1065 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1066 | /* First try to use the system's implementation, if that is |
| 1067 | available and either succeeds to gives an error other than |
| 1068 | that it isn't implemented. */ |
| 1069 | check_gfax(); |
| 1070 | if (gfaxa) { |
| 1071 | result = gfaxa(pszFile, level, pv); |
| 1072 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1073 | return result; |
| 1074 | } |
| 1075 | /* It's either not present, or not implemented. |
| 1076 | Emulate using FindFirstFile. */ |
| 1077 | if (level != GetFileExInfoStandard) { |
| 1078 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1079 | return FALSE; |
| 1080 | } |
| 1081 | /* Use GetFileAttributes to validate that the file name |
| 1082 | does not contain wildcards (which FindFirstFile would |
| 1083 | accept). */ |
| 1084 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 1085 | return FALSE; |
| 1086 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | static BOOL WINAPI |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1090 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 1091 | GET_FILEEX_INFO_LEVELS level, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1092 | LPVOID pv) |
| 1093 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1094 | BOOL result; |
| 1095 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1096 | /* First try to use the system's implementation, if that is |
| 1097 | available and either succeeds to gives an error other than |
| 1098 | that it isn't implemented. */ |
| 1099 | check_gfax(); |
| 1100 | if (gfaxa) { |
| 1101 | result = gfaxw(pszFile, level, pv); |
| 1102 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1103 | return result; |
| 1104 | } |
| 1105 | /* It's either not present, or not implemented. |
| 1106 | Emulate using FindFirstFile. */ |
| 1107 | if (level != GetFileExInfoStandard) { |
| 1108 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1109 | return FALSE; |
| 1110 | } |
| 1111 | /* Use GetFileAttributes to validate that the file name |
| 1112 | does not contain wildcards (which FindFirstFile would |
| 1113 | accept). */ |
| 1114 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 1115 | return FALSE; |
| 1116 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1119 | static int |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1120 | win32_stat(const char* path, struct win32_stat *result) |
| 1121 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1122 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1123 | int code; |
| 1124 | char *dot; |
| 1125 | /* XXX not supported on Win95 and NT 3.x */ |
| 1126 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 1127 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1128 | /* Protocol violation: we explicitly clear errno, instead of |
| 1129 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1130 | errno = 0; |
| 1131 | return -1; |
| 1132 | } else { |
| 1133 | /* Could not get attributes on open file. Fall back to |
| 1134 | reading the directory. */ |
| 1135 | if (!attributes_from_dir(path, &info)) { |
| 1136 | /* Very strange. This should not fail now */ |
| 1137 | errno = 0; |
| 1138 | return -1; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | code = attribute_data_to_stat(&info, result); |
| 1143 | if (code != 0) |
| 1144 | return code; |
| 1145 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1146 | dot = strrchr(path, '.'); |
| 1147 | if (dot) { |
| 1148 | if (stricmp(dot, ".bat") == 0 || |
| 1149 | stricmp(dot, ".cmd") == 0 || |
| 1150 | stricmp(dot, ".exe") == 0 || |
| 1151 | stricmp(dot, ".com") == 0) |
| 1152 | result->st_mode |= 0111; |
| 1153 | } |
| 1154 | return code; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1157 | static int |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1158 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1159 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1160 | int code; |
| 1161 | const wchar_t *dot; |
| 1162 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1163 | /* XXX not supported on Win95 and NT 3.x */ |
| 1164 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 1165 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1166 | /* Protocol violation: we explicitly clear errno, instead of |
| 1167 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1168 | errno = 0; |
| 1169 | return -1; |
| 1170 | } else { |
| 1171 | /* Could not get attributes on open file. Fall back to |
| 1172 | reading the directory. */ |
| 1173 | if (!attributes_from_dir_w(path, &info)) { |
| 1174 | /* Very strange. This should not fail now */ |
| 1175 | errno = 0; |
| 1176 | return -1; |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | code = attribute_data_to_stat(&info, result); |
| 1181 | if (code < 0) |
| 1182 | return code; |
| 1183 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1184 | dot = wcsrchr(path, '.'); |
| 1185 | if (dot) { |
| 1186 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1187 | _wcsicmp(dot, L".cmd") == 0 || |
| 1188 | _wcsicmp(dot, L".exe") == 0 || |
| 1189 | _wcsicmp(dot, L".com") == 0) |
| 1190 | result->st_mode |= 0111; |
| 1191 | } |
| 1192 | return code; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static int |
| 1196 | win32_fstat(int file_number, struct win32_stat *result) |
| 1197 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1198 | BY_HANDLE_FILE_INFORMATION info; |
| 1199 | HANDLE h; |
| 1200 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1201 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1202 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1203 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1204 | /* Protocol violation: we explicitly clear errno, instead of |
| 1205 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1206 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1207 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1208 | if (h == INVALID_HANDLE_VALUE) { |
| 1209 | /* This is really a C library error (invalid file handle). |
| 1210 | We set the Win32 error to the closes one matching. */ |
| 1211 | SetLastError(ERROR_INVALID_HANDLE); |
| 1212 | return -1; |
| 1213 | } |
| 1214 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1215 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1216 | type = GetFileType(h); |
| 1217 | if (type == FILE_TYPE_UNKNOWN) { |
| 1218 | DWORD error = GetLastError(); |
| 1219 | if (error != 0) { |
| 1220 | return -1; |
| 1221 | } |
| 1222 | /* else: valid but unknown file */ |
| 1223 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1224 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1225 | if (type != FILE_TYPE_DISK) { |
| 1226 | if (type == FILE_TYPE_CHAR) |
| 1227 | result->st_mode = _S_IFCHR; |
| 1228 | else if (type == FILE_TYPE_PIPE) |
| 1229 | result->st_mode = _S_IFIFO; |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | if (!GetFileInformationByHandle(h, &info)) { |
| 1234 | return -1; |
| 1235 | } |
| 1236 | |
| 1237 | /* similar to stat() */ |
| 1238 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1239 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1240 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1241 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1242 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1243 | /* specific to fstat() */ |
| 1244 | result->st_nlink = info.nNumberOfLinks; |
| 1245 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1246 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | #endif /* MS_WINDOWS */ |
| 1250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1251 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1252 | "stat_result: Result from stat or lstat.\n\n\ |
| 1253 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1254 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1255 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1256 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1257 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1258 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1259 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1260 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1261 | |
| 1262 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1263 | {"st_mode", "protection bits"}, |
| 1264 | {"st_ino", "inode"}, |
| 1265 | {"st_dev", "device"}, |
| 1266 | {"st_nlink", "number of hard links"}, |
| 1267 | {"st_uid", "user ID of owner"}, |
| 1268 | {"st_gid", "group ID of owner"}, |
| 1269 | {"st_size", "total size, in bytes"}, |
| 1270 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1271 | {NULL, "integer time of last access"}, |
| 1272 | {NULL, "integer time of last modification"}, |
| 1273 | {NULL, "integer time of last change"}, |
| 1274 | {"st_atime", "time of last access"}, |
| 1275 | {"st_mtime", "time of last modification"}, |
| 1276 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1277 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1278 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1279 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1280 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1281 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1282 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1283 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1284 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1285 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1286 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1287 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1288 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1289 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1290 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1291 | #endif |
| 1292 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1293 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1294 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1295 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1296 | }; |
| 1297 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1298 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1299 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1300 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1301 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1302 | #endif |
| 1303 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1304 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1305 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1306 | #else |
| 1307 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1308 | #endif |
| 1309 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1310 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1311 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1312 | #else |
| 1313 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1314 | #endif |
| 1315 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1316 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1317 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1318 | #else |
| 1319 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1320 | #endif |
| 1321 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1322 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1323 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1324 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1325 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | |
| 1328 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1329 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1330 | #else |
| 1331 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1332 | #endif |
| 1333 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1334 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1335 | "stat_result", /* name */ |
| 1336 | stat_result__doc__, /* doc */ |
| 1337 | stat_result_fields, |
| 1338 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1339 | }; |
| 1340 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1341 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1342 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1343 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1344 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1345 | 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] | 1346 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1347 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1348 | |
| 1349 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1350 | {"f_bsize", }, |
| 1351 | {"f_frsize", }, |
| 1352 | {"f_blocks", }, |
| 1353 | {"f_bfree", }, |
| 1354 | {"f_bavail", }, |
| 1355 | {"f_files", }, |
| 1356 | {"f_ffree", }, |
| 1357 | {"f_favail", }, |
| 1358 | {"f_flag", }, |
| 1359 | {"f_namemax",}, |
| 1360 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1361 | }; |
| 1362 | |
| 1363 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1364 | "statvfs_result", /* name */ |
| 1365 | statvfs_result__doc__, /* doc */ |
| 1366 | statvfs_result_fields, |
| 1367 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1368 | }; |
| 1369 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1370 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1371 | static PyTypeObject StatResultType; |
| 1372 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1373 | static newfunc structseq_new; |
| 1374 | |
| 1375 | static PyObject * |
| 1376 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1377 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1378 | PyStructSequence *result; |
| 1379 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1380 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1381 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1382 | if (!result) |
| 1383 | return NULL; |
| 1384 | /* If we have been initialized from a tuple, |
| 1385 | st_?time might be set to None. Initialize it |
| 1386 | from the int slots. */ |
| 1387 | for (i = 7; i <= 9; i++) { |
| 1388 | if (result->ob_item[i+3] == Py_None) { |
| 1389 | Py_DECREF(Py_None); |
| 1390 | Py_INCREF(result->ob_item[i]); |
| 1391 | result->ob_item[i+3] = result->ob_item[i]; |
| 1392 | } |
| 1393 | } |
| 1394 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1400 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1401 | |
| 1402 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1403 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1404 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1405 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1406 | future calls return ints. \n\ |
| 1407 | If newval is omitted, return the current setting.\n"); |
| 1408 | |
| 1409 | static PyObject* |
| 1410 | stat_float_times(PyObject* self, PyObject *args) |
| 1411 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1412 | int newval = -1; |
| 1413 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1414 | return NULL; |
| 1415 | if (newval == -1) |
| 1416 | /* Return old value */ |
| 1417 | return PyBool_FromLong(_stat_float_times); |
| 1418 | _stat_float_times = newval; |
| 1419 | Py_INCREF(Py_None); |
| 1420 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1421 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1422 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1423 | static void |
| 1424 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1425 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1426 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1427 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1428 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1429 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1430 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1431 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1432 | if (!ival) |
| 1433 | return; |
| 1434 | if (_stat_float_times) { |
| 1435 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1436 | } else { |
| 1437 | fval = ival; |
| 1438 | Py_INCREF(fval); |
| 1439 | } |
| 1440 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1441 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1444 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1445 | (used by posix_stat() and posix_fstat()) */ |
| 1446 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1447 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1448 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1449 | unsigned long ansec, mnsec, cnsec; |
| 1450 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1451 | if (v == NULL) |
| 1452 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1453 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1454 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1455 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1456 | PyStructSequence_SET_ITEM(v, 1, |
| 1457 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1458 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1459 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1460 | #endif |
| 1461 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1462 | PyStructSequence_SET_ITEM(v, 2, |
| 1463 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1464 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1465 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1466 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1467 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1468 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1469 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1470 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1471 | PyStructSequence_SET_ITEM(v, 6, |
| 1472 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1473 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1474 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1475 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1476 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1477 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1478 | ansec = st->st_atim.tv_nsec; |
| 1479 | mnsec = st->st_mtim.tv_nsec; |
| 1480 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1481 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1482 | ansec = st->st_atimespec.tv_nsec; |
| 1483 | mnsec = st->st_mtimespec.tv_nsec; |
| 1484 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1485 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1486 | ansec = st->st_atime_nsec; |
| 1487 | mnsec = st->st_mtime_nsec; |
| 1488 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1489 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1490 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1491 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1492 | fill_time(v, 7, st->st_atime, ansec); |
| 1493 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1494 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1495 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1496 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1497 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1498 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1499 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1500 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1501 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1502 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1503 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1504 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1505 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1506 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1507 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1508 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1509 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1510 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1511 | #endif |
| 1512 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1513 | { |
| 1514 | PyObject *val; |
| 1515 | unsigned long bsec,bnsec; |
| 1516 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1517 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1518 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1519 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1520 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1521 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1522 | if (_stat_float_times) { |
| 1523 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1524 | } else { |
| 1525 | val = PyLong_FromLong((long)bsec); |
| 1526 | } |
| 1527 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1528 | val); |
| 1529 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1530 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1531 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1532 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1533 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1534 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1535 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1536 | if (PyErr_Occurred()) { |
| 1537 | Py_DECREF(v); |
| 1538 | return NULL; |
| 1539 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1540 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1541 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1544 | #ifdef MS_WINDOWS |
| 1545 | |
| 1546 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1547 | where / can be used in place of \ and the trailing slash is optional. |
| 1548 | Both SERVER and SHARE must have at least one character. |
| 1549 | */ |
| 1550 | |
| 1551 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1552 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1553 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1554 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1555 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1556 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1557 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1558 | IsUNCRootA(char *path, int pathlen) |
| 1559 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1560 | #define ISSLASH ISSLASHA |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1561 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1562 | int i, share; |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1563 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1564 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1565 | /* minimum UNCRoot is \\x\y */ |
| 1566 | return FALSE; |
| 1567 | for (i = 2; i < pathlen ; i++) |
| 1568 | if (ISSLASH(path[i])) break; |
| 1569 | if (i == 2 || i == pathlen) |
| 1570 | /* do not allow \\\SHARE or \\SERVER */ |
| 1571 | return FALSE; |
| 1572 | share = i+1; |
| 1573 | for (i = share; i < pathlen; i++) |
| 1574 | if (ISSLASH(path[i])) break; |
| 1575 | return (i != share && (i == pathlen || i == pathlen-1)); |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1576 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1577 | #undef ISSLASH |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1580 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1581 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1582 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1583 | #define ISSLASH ISSLASHW |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1584 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1585 | int i, share; |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1586 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1587 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1588 | /* minimum UNCRoot is \\x\y */ |
| 1589 | return FALSE; |
| 1590 | for (i = 2; i < pathlen ; i++) |
| 1591 | if (ISSLASH(path[i])) break; |
| 1592 | if (i == 2 || i == pathlen) |
| 1593 | /* do not allow \\\SHARE or \\SERVER */ |
| 1594 | return FALSE; |
| 1595 | share = i+1; |
| 1596 | for (i = share; i < pathlen; i++) |
| 1597 | if (ISSLASH(path[i])) break; |
| 1598 | return (i != share && (i == pathlen || i == pathlen-1)); |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1599 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1600 | #undef ISSLASH |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1601 | } |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1602 | #endif /* MS_WINDOWS */ |
| 1603 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1604 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1605 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1606 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1607 | #ifdef __VMS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1608 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1609 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1610 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1611 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1612 | char *wformat, |
| 1613 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1614 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1615 | STRUCT_STAT st; |
| 1616 | PyObject *opath; |
| 1617 | char *path; |
| 1618 | int res; |
| 1619 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1620 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1621 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1622 | /* If on wide-character-capable OS see if argument |
| 1623 | is Unicode and if so use wide API. */ |
| 1624 | if (unicode_file_names()) { |
| 1625 | PyUnicodeObject *po; |
| 1626 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1627 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1628 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1629 | Py_BEGIN_ALLOW_THREADS |
| 1630 | /* PyUnicode_AS_UNICODE result OK without |
| 1631 | thread lock as it is a simple dereference. */ |
| 1632 | res = wstatfunc(wpath, &st); |
| 1633 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1634 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1635 | if (res != 0) |
| 1636 | return win32_error_unicode("stat", wpath); |
| 1637 | return _pystat_fromstructstat(&st); |
| 1638 | } |
| 1639 | /* Drop the argument parsing error as narrow strings |
| 1640 | are also valid. */ |
| 1641 | PyErr_Clear(); |
| 1642 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1643 | #endif |
| 1644 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1645 | if (!PyArg_ParseTuple(args, format, |
| 1646 | PyUnicode_FSConverter, &opath)) |
| 1647 | return NULL; |
| 1648 | path = bytes2str(opath, 1); |
| 1649 | Py_BEGIN_ALLOW_THREADS |
| 1650 | res = (*statfunc)(path, &st); |
| 1651 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1652 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1653 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1654 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1655 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1656 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1657 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1658 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1659 | } |
| 1660 | else |
| 1661 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1662 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1663 | release_bytes(opath); |
| 1664 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1667 | /* POSIX methods */ |
| 1668 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1669 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1670 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1671 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1672 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1673 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1674 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1675 | 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] | 1676 | |
| 1677 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1678 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1679 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1680 | PyObject *opath; |
| 1681 | char *path; |
| 1682 | int mode; |
| 1683 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1684 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1685 | DWORD attr; |
| 1686 | if (unicode_file_names()) { |
| 1687 | PyUnicodeObject *po; |
| 1688 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1689 | Py_BEGIN_ALLOW_THREADS |
| 1690 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1691 | it is a simple dereference. */ |
| 1692 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1693 | Py_END_ALLOW_THREADS |
| 1694 | goto finish; |
| 1695 | } |
| 1696 | /* Drop the argument parsing error as narrow strings |
| 1697 | are also valid. */ |
| 1698 | PyErr_Clear(); |
| 1699 | } |
| 1700 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1701 | PyUnicode_FSConverter, &opath, &mode)) |
| 1702 | return 0; |
| 1703 | path = bytes2str(opath, 1); |
| 1704 | Py_BEGIN_ALLOW_THREADS |
| 1705 | attr = GetFileAttributesA(path); |
| 1706 | Py_END_ALLOW_THREADS |
| 1707 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1708 | finish: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1709 | if (attr == 0xFFFFFFFF) |
| 1710 | /* File does not exist, or cannot read attributes */ |
| 1711 | return PyBool_FromLong(0); |
| 1712 | /* Access is possible if either write access wasn't requested, or |
| 1713 | the file isn't read-only, or if it's a directory, as there are |
| 1714 | no read-only directories on Windows. */ |
| 1715 | return PyBool_FromLong(!(mode & 2) |
| 1716 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1717 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1718 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1719 | int res; |
| 1720 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1721 | PyUnicode_FSConverter, &opath, &mode)) |
| 1722 | return NULL; |
| 1723 | path = bytes2str(opath, 1); |
| 1724 | Py_BEGIN_ALLOW_THREADS |
| 1725 | res = access(path, mode); |
| 1726 | Py_END_ALLOW_THREADS |
| 1727 | release_bytes(opath); |
| 1728 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1729 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1732 | #ifndef F_OK |
| 1733 | #define F_OK 0 |
| 1734 | #endif |
| 1735 | #ifndef R_OK |
| 1736 | #define R_OK 4 |
| 1737 | #endif |
| 1738 | #ifndef W_OK |
| 1739 | #define W_OK 2 |
| 1740 | #endif |
| 1741 | #ifndef X_OK |
| 1742 | #define X_OK 1 |
| 1743 | #endif |
| 1744 | |
| 1745 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1746 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1747 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1748 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1749 | |
| 1750 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1751 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1752 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1753 | int id; |
| 1754 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1755 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1756 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1757 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1758 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1759 | #if defined(__VMS) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1760 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1761 | if (id == 0) { |
| 1762 | ret = ttyname(); |
| 1763 | } |
| 1764 | else { |
| 1765 | ret = NULL; |
| 1766 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1767 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1768 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1769 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1770 | if (ret == NULL) |
| 1771 | return posix_error(); |
Victor Stinner | bae0e62 | 2010-08-15 09:15:02 +0000 | [diff] [blame] | 1772 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1773 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1774 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1775 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1776 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1777 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1778 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1779 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1780 | |
| 1781 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1782 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1783 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1784 | char *ret; |
| 1785 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1786 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1787 | #ifdef USE_CTERMID_R |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1788 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1789 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1790 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1791 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1792 | if (ret == NULL) |
| 1793 | return posix_error(); |
Victor Stinner | bae0e62 | 2010-08-15 09:15:02 +0000 | [diff] [blame] | 1794 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1795 | } |
| 1796 | #endif |
| 1797 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1798 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1799 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1800 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1801 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1802 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1803 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1804 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1805 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1806 | 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] | 1807 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1808 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1809 | #elif defined(__VMS) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1810 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1811 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1812 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1813 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1816 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1817 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1818 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1819 | 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] | 1820 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1821 | |
| 1822 | static PyObject * |
| 1823 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1824 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1825 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1826 | } |
| 1827 | #endif /* HAVE_FCHDIR */ |
| 1828 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1829 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1830 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1831 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1832 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1833 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1834 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1835 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1836 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1837 | PyObject *opath = NULL; |
| 1838 | char *path = NULL; |
| 1839 | int i; |
| 1840 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1841 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1842 | DWORD attr; |
| 1843 | if (unicode_file_names()) { |
| 1844 | PyUnicodeObject *po; |
| 1845 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1846 | Py_BEGIN_ALLOW_THREADS |
| 1847 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1848 | if (attr != 0xFFFFFFFF) { |
| 1849 | if (i & _S_IWRITE) |
| 1850 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1851 | else |
| 1852 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1853 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1854 | } |
| 1855 | else |
| 1856 | res = 0; |
| 1857 | Py_END_ALLOW_THREADS |
| 1858 | if (!res) |
| 1859 | return win32_error_unicode("chmod", |
| 1860 | PyUnicode_AS_UNICODE(po)); |
| 1861 | Py_INCREF(Py_None); |
| 1862 | return Py_None; |
| 1863 | } |
| 1864 | /* Drop the argument parsing error as narrow strings |
| 1865 | are also valid. */ |
| 1866 | PyErr_Clear(); |
| 1867 | } |
| 1868 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1869 | &opath, &i)) |
| 1870 | return NULL; |
| 1871 | path = bytes2str(opath, 1); |
| 1872 | Py_BEGIN_ALLOW_THREADS |
| 1873 | attr = GetFileAttributesA(path); |
| 1874 | if (attr != 0xFFFFFFFF) { |
| 1875 | if (i & _S_IWRITE) |
| 1876 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1877 | else |
| 1878 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1879 | res = SetFileAttributesA(path, attr); |
| 1880 | } |
| 1881 | else |
| 1882 | res = 0; |
| 1883 | Py_END_ALLOW_THREADS |
| 1884 | if (!res) { |
| 1885 | win32_error("chmod", path); |
| 1886 | release_bytes(opath); |
| 1887 | return NULL; |
| 1888 | } |
| 1889 | release_bytes(opath); |
| 1890 | Py_INCREF(Py_None); |
| 1891 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1892 | #else /* MS_WINDOWS */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1893 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1894 | &opath, &i)) |
| 1895 | return NULL; |
| 1896 | path = bytes2str(opath, 1); |
| 1897 | Py_BEGIN_ALLOW_THREADS |
| 1898 | res = chmod(path, i); |
| 1899 | Py_END_ALLOW_THREADS |
| 1900 | if (res < 0) |
| 1901 | return posix_error_with_allocated_filename(opath); |
| 1902 | release_bytes(opath); |
| 1903 | Py_INCREF(Py_None); |
| 1904 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1905 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1908 | #ifdef HAVE_FCHMOD |
| 1909 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1910 | "fchmod(fd, mode)\n\n\ |
| 1911 | Change the access permissions of the file given by file\n\ |
| 1912 | descriptor fd."); |
| 1913 | |
| 1914 | static PyObject * |
| 1915 | posix_fchmod(PyObject *self, PyObject *args) |
| 1916 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1917 | int fd, mode, res; |
| 1918 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1919 | return NULL; |
| 1920 | Py_BEGIN_ALLOW_THREADS |
| 1921 | res = fchmod(fd, mode); |
| 1922 | Py_END_ALLOW_THREADS |
| 1923 | if (res < 0) |
| 1924 | return posix_error(); |
| 1925 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1926 | } |
| 1927 | #endif /* HAVE_FCHMOD */ |
| 1928 | |
| 1929 | #ifdef HAVE_LCHMOD |
| 1930 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1931 | "lchmod(path, mode)\n\n\ |
| 1932 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1933 | affects the link itself rather than the target."); |
| 1934 | |
| 1935 | static PyObject * |
| 1936 | posix_lchmod(PyObject *self, PyObject *args) |
| 1937 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1938 | PyObject *opath; |
| 1939 | char *path; |
| 1940 | int i; |
| 1941 | int res; |
| 1942 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 1943 | &opath, &i)) |
| 1944 | return NULL; |
| 1945 | path = bytes2str(opath, 1); |
| 1946 | Py_BEGIN_ALLOW_THREADS |
| 1947 | res = lchmod(path, i); |
| 1948 | Py_END_ALLOW_THREADS |
| 1949 | if (res < 0) |
| 1950 | return posix_error_with_allocated_filename(opath); |
| 1951 | release_bytes(opath); |
| 1952 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1953 | } |
| 1954 | #endif /* HAVE_LCHMOD */ |
| 1955 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1956 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1957 | #ifdef HAVE_CHFLAGS |
| 1958 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1959 | "chflags(path, flags)\n\n\ |
| 1960 | Set file flags."); |
| 1961 | |
| 1962 | static PyObject * |
| 1963 | posix_chflags(PyObject *self, PyObject *args) |
| 1964 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1965 | PyObject *opath; |
| 1966 | char *path; |
| 1967 | unsigned long flags; |
| 1968 | int res; |
| 1969 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 1970 | PyUnicode_FSConverter, &opath, &flags)) |
| 1971 | return NULL; |
| 1972 | path = bytes2str(opath, 1); |
| 1973 | Py_BEGIN_ALLOW_THREADS |
| 1974 | res = chflags(path, flags); |
| 1975 | Py_END_ALLOW_THREADS |
| 1976 | if (res < 0) |
| 1977 | return posix_error_with_allocated_filename(opath); |
| 1978 | release_bytes(opath); |
| 1979 | Py_INCREF(Py_None); |
| 1980 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1981 | } |
| 1982 | #endif /* HAVE_CHFLAGS */ |
| 1983 | |
| 1984 | #ifdef HAVE_LCHFLAGS |
| 1985 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1986 | "lchflags(path, flags)\n\n\ |
| 1987 | Set file flags.\n\ |
| 1988 | This function will not follow symbolic links."); |
| 1989 | |
| 1990 | static PyObject * |
| 1991 | posix_lchflags(PyObject *self, PyObject *args) |
| 1992 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 1993 | PyObject *opath; |
| 1994 | char *path; |
| 1995 | unsigned long flags; |
| 1996 | int res; |
| 1997 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 1998 | PyUnicode_FSConverter, &opath, &flags)) |
| 1999 | return NULL; |
| 2000 | path = bytes2str(opath, 1); |
| 2001 | Py_BEGIN_ALLOW_THREADS |
| 2002 | res = lchflags(path, flags); |
| 2003 | Py_END_ALLOW_THREADS |
| 2004 | if (res < 0) |
| 2005 | return posix_error_with_allocated_filename(opath); |
| 2006 | release_bytes(opath); |
| 2007 | Py_INCREF(Py_None); |
| 2008 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2009 | } |
| 2010 | #endif /* HAVE_LCHFLAGS */ |
| 2011 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2012 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2013 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2014 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2015 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2016 | |
| 2017 | static PyObject * |
| 2018 | posix_chroot(PyObject *self, PyObject *args) |
| 2019 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2020 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2021 | } |
| 2022 | #endif |
| 2023 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2024 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2025 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2026 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2027 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2028 | |
| 2029 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2030 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2031 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2032 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2033 | } |
| 2034 | #endif /* HAVE_FSYNC */ |
| 2035 | |
| 2036 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2037 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2038 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2039 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2040 | #endif |
| 2041 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2042 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2043 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2044 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2045 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2046 | |
| 2047 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2048 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2049 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2050 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2051 | } |
| 2052 | #endif /* HAVE_FDATASYNC */ |
| 2053 | |
| 2054 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2055 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2056 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2057 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2058 | 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] | 2059 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2060 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2061 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2062 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2063 | PyObject *opath; |
| 2064 | char *path; |
| 2065 | long uid, gid; |
| 2066 | int res; |
| 2067 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2068 | PyUnicode_FSConverter, &opath, |
| 2069 | &uid, &gid)) |
| 2070 | return NULL; |
| 2071 | path = bytes2str(opath, 1); |
| 2072 | Py_BEGIN_ALLOW_THREADS |
| 2073 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2074 | Py_END_ALLOW_THREADS |
| 2075 | if (res < 0) |
| 2076 | return posix_error_with_allocated_filename(opath); |
| 2077 | release_bytes(opath); |
| 2078 | Py_INCREF(Py_None); |
| 2079 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2080 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2081 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2082 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2083 | #ifdef HAVE_FCHOWN |
| 2084 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2085 | "fchown(fd, uid, gid)\n\n\ |
| 2086 | Change the owner and group id of the file given by file descriptor\n\ |
| 2087 | fd to the numeric uid and gid."); |
| 2088 | |
| 2089 | static PyObject * |
| 2090 | posix_fchown(PyObject *self, PyObject *args) |
| 2091 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2092 | int fd; |
| 2093 | long uid, gid; |
| 2094 | int res; |
| 2095 | if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) |
| 2096 | return NULL; |
| 2097 | Py_BEGIN_ALLOW_THREADS |
| 2098 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2099 | Py_END_ALLOW_THREADS |
| 2100 | if (res < 0) |
| 2101 | return posix_error(); |
| 2102 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2103 | } |
| 2104 | #endif /* HAVE_FCHOWN */ |
| 2105 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2106 | #ifdef HAVE_LCHOWN |
| 2107 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2108 | "lchown(path, uid, gid)\n\n\ |
| 2109 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2110 | This function will not follow symbolic links."); |
| 2111 | |
| 2112 | static PyObject * |
| 2113 | posix_lchown(PyObject *self, PyObject *args) |
| 2114 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2115 | PyObject *opath; |
| 2116 | char *path; |
| 2117 | long uid, gid; |
| 2118 | int res; |
| 2119 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2120 | PyUnicode_FSConverter, &opath, |
| 2121 | &uid, &gid)) |
| 2122 | return NULL; |
| 2123 | path = bytes2str(opath, 1); |
| 2124 | Py_BEGIN_ALLOW_THREADS |
| 2125 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2126 | Py_END_ALLOW_THREADS |
| 2127 | if (res < 0) |
| 2128 | return posix_error_with_allocated_filename(opath); |
| 2129 | release_bytes(opath); |
| 2130 | Py_INCREF(Py_None); |
| 2131 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2132 | } |
| 2133 | #endif /* HAVE_LCHOWN */ |
| 2134 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2135 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2136 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2137 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2138 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2139 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2140 | char buf[1026]; |
| 2141 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2142 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2143 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2144 | if (!use_bytes && unicode_file_names()) { |
| 2145 | wchar_t wbuf[1026]; |
| 2146 | wchar_t *wbuf2 = wbuf; |
| 2147 | PyObject *resobj; |
| 2148 | DWORD len; |
| 2149 | Py_BEGIN_ALLOW_THREADS |
| 2150 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2151 | /* If the buffer is large enough, len does not include the |
| 2152 | terminating \0. If the buffer is too small, len includes |
| 2153 | the space needed for the terminator. */ |
| 2154 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2155 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2156 | if (wbuf2) |
| 2157 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2158 | } |
| 2159 | Py_END_ALLOW_THREADS |
| 2160 | if (!wbuf2) { |
| 2161 | PyErr_NoMemory(); |
| 2162 | return NULL; |
| 2163 | } |
| 2164 | if (!len) { |
| 2165 | if (wbuf2 != wbuf) free(wbuf2); |
| 2166 | return win32_error("getcwdu", NULL); |
| 2167 | } |
| 2168 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2169 | if (wbuf2 != wbuf) free(wbuf2); |
| 2170 | return resobj; |
| 2171 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2172 | #endif |
| 2173 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2174 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2175 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2176 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2177 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2178 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2179 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2180 | Py_END_ALLOW_THREADS |
| 2181 | if (res == NULL) |
| 2182 | return posix_error(); |
| 2183 | if (use_bytes) |
| 2184 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 2185 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape"); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2186 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2187 | |
| 2188 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2189 | "getcwd() -> path\n\n\ |
| 2190 | Return a unicode string representing the current working directory."); |
| 2191 | |
| 2192 | static PyObject * |
| 2193 | posix_getcwd_unicode(PyObject *self) |
| 2194 | { |
| 2195 | return posix_getcwd(0); |
| 2196 | } |
| 2197 | |
| 2198 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2199 | "getcwdb() -> path\n\n\ |
| 2200 | Return a bytes string representing the current working directory."); |
| 2201 | |
| 2202 | static PyObject * |
| 2203 | posix_getcwd_bytes(PyObject *self) |
| 2204 | { |
| 2205 | return posix_getcwd(1); |
| 2206 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2207 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2208 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2210 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2211 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2212 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2213 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2214 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2215 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2216 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2217 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2218 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2219 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2220 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2221 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2223 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2224 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2225 | Return a list containing the names of the entries in the directory.\n\ |
| 2226 | \n\ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2227 | path: path of directory to list\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2228 | \n\ |
| 2229 | 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] | 2230 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2231 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2232 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2233 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2234 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2235 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2236 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2237 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2238 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2239 | PyObject *d, *v; |
| 2240 | HANDLE hFindFile; |
| 2241 | BOOL result; |
| 2242 | WIN32_FIND_DATA FileData; |
| 2243 | PyObject *opath; |
| 2244 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2245 | char *bufptr = namebuf; |
| 2246 | 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] | 2247 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2248 | /* If on wide-character-capable OS see if argument |
| 2249 | is Unicode and if so use wide API. */ |
| 2250 | if (unicode_file_names()) { |
| 2251 | PyObject *po; |
| 2252 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2253 | WIN32_FIND_DATAW wFileData; |
| 2254 | Py_UNICODE *wnamebuf; |
| 2255 | /* Overallocate for \\*.*\0 */ |
| 2256 | len = PyUnicode_GET_SIZE(po); |
| 2257 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2258 | if (!wnamebuf) { |
| 2259 | PyErr_NoMemory(); |
| 2260 | return NULL; |
| 2261 | } |
| 2262 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2263 | if (len > 0) { |
| 2264 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2265 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2266 | wnamebuf[len++] = L'\\'; |
| 2267 | wcscpy(wnamebuf + len, L"*.*"); |
| 2268 | } |
| 2269 | if ((d = PyList_New(0)) == NULL) { |
| 2270 | free(wnamebuf); |
| 2271 | return NULL; |
| 2272 | } |
Antoine Pitrou | bd25d59 | 2010-08-09 23:47:57 +0000 | [diff] [blame] | 2273 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2274 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | bd25d59 | 2010-08-09 23:47:57 +0000 | [diff] [blame] | 2275 | Py_END_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2276 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2277 | int error = GetLastError(); |
| 2278 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2279 | free(wnamebuf); |
| 2280 | return d; |
| 2281 | } |
| 2282 | Py_DECREF(d); |
| 2283 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2284 | free(wnamebuf); |
| 2285 | return NULL; |
| 2286 | } |
| 2287 | do { |
| 2288 | /* Skip over . and .. */ |
| 2289 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2290 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2291 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2292 | if (v == NULL) { |
| 2293 | Py_DECREF(d); |
| 2294 | d = NULL; |
| 2295 | break; |
| 2296 | } |
| 2297 | if (PyList_Append(d, v) != 0) { |
| 2298 | Py_DECREF(v); |
| 2299 | Py_DECREF(d); |
| 2300 | d = NULL; |
| 2301 | break; |
| 2302 | } |
| 2303 | Py_DECREF(v); |
| 2304 | } |
| 2305 | Py_BEGIN_ALLOW_THREADS |
| 2306 | result = FindNextFileW(hFindFile, &wFileData); |
| 2307 | Py_END_ALLOW_THREADS |
| 2308 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2309 | it got to the end of the directory. */ |
| 2310 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2311 | Py_DECREF(d); |
| 2312 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2313 | FindClose(hFindFile); |
| 2314 | free(wnamebuf); |
| 2315 | return NULL; |
| 2316 | } |
| 2317 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2318 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2319 | if (FindClose(hFindFile) == FALSE) { |
| 2320 | Py_DECREF(d); |
| 2321 | win32_error_unicode("FindClose", wnamebuf); |
| 2322 | free(wnamebuf); |
| 2323 | return NULL; |
| 2324 | } |
| 2325 | free(wnamebuf); |
| 2326 | return d; |
| 2327 | } |
| 2328 | /* Drop the argument parsing error as narrow strings |
| 2329 | are also valid. */ |
| 2330 | PyErr_Clear(); |
| 2331 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2332 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2333 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2334 | PyUnicode_FSConverter, &opath)) |
| 2335 | return NULL; |
| 2336 | if (PyObject_Size(opath)+1 > MAX_PATH) { |
| 2337 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2338 | Py_DECREF(opath); |
| 2339 | return NULL; |
| 2340 | } |
| 2341 | strcpy(namebuf, bytes2str(opath, 0)); |
| 2342 | len = PyObject_Size(opath); |
| 2343 | if (len > 0) { |
| 2344 | char ch = namebuf[len-1]; |
| 2345 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2346 | namebuf[len++] = '/'; |
| 2347 | strcpy(namebuf + len, "*.*"); |
| 2348 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2349 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2350 | if ((d = PyList_New(0)) == NULL) |
| 2351 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2352 | |
Antoine Pitrou | bd25d59 | 2010-08-09 23:47:57 +0000 | [diff] [blame] | 2353 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2354 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | bd25d59 | 2010-08-09 23:47:57 +0000 | [diff] [blame] | 2355 | Py_END_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2356 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2357 | int error = GetLastError(); |
| 2358 | if (error == ERROR_FILE_NOT_FOUND) |
| 2359 | return d; |
| 2360 | Py_DECREF(d); |
| 2361 | return win32_error("FindFirstFile", namebuf); |
| 2362 | } |
| 2363 | do { |
| 2364 | /* Skip over . and .. */ |
| 2365 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2366 | strcmp(FileData.cFileName, "..") != 0) { |
| 2367 | v = PyBytes_FromString(FileData.cFileName); |
| 2368 | if (v == NULL) { |
| 2369 | Py_DECREF(d); |
| 2370 | d = NULL; |
| 2371 | break; |
| 2372 | } |
| 2373 | if (PyList_Append(d, v) != 0) { |
| 2374 | Py_DECREF(v); |
| 2375 | Py_DECREF(d); |
| 2376 | d = NULL; |
| 2377 | break; |
| 2378 | } |
| 2379 | Py_DECREF(v); |
| 2380 | } |
| 2381 | Py_BEGIN_ALLOW_THREADS |
| 2382 | result = FindNextFile(hFindFile, &FileData); |
| 2383 | Py_END_ALLOW_THREADS |
| 2384 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2385 | it got to the end of the directory. */ |
| 2386 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2387 | Py_DECREF(d); |
| 2388 | win32_error("FindNextFile", namebuf); |
| 2389 | FindClose(hFindFile); |
| 2390 | return NULL; |
| 2391 | } |
| 2392 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2393 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2394 | if (FindClose(hFindFile) == FALSE) { |
| 2395 | Py_DECREF(d); |
| 2396 | return win32_error("FindClose", namebuf); |
| 2397 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2398 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2399 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2400 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2401 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2402 | |
| 2403 | #ifndef MAX_PATH |
| 2404 | #define MAX_PATH CCHMAXPATH |
| 2405 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2406 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2407 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2408 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2409 | PyObject *d, *v; |
| 2410 | char namebuf[MAX_PATH+5]; |
| 2411 | HDIR hdir = 1; |
| 2412 | ULONG srchcnt = 1; |
| 2413 | FILEFINDBUF3 ep; |
| 2414 | APIRET rc; |
| 2415 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2416 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2417 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2418 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2419 | name = bytes2str(oname); |
| 2420 | len = PyObject_Size(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2421 | if (len >= MAX_PATH) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2422 | release_bytes(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2423 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2424 | return NULL; |
| 2425 | } |
| 2426 | strcpy(namebuf, name); |
| 2427 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2428 | if (*pt == ALTSEP) |
| 2429 | *pt = SEP; |
| 2430 | if (namebuf[len-1] != SEP) |
| 2431 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2432 | strcpy(namebuf + len, "*.*"); |
| 2433 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2434 | if ((d = PyList_New(0)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2435 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2436 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2437 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2438 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2439 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2440 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2441 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2442 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2443 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2444 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2445 | |
| 2446 | if (rc != NO_ERROR) { |
| 2447 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2448 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2451 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2452 | do { |
| 2453 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2454 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2455 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2456 | |
| 2457 | strcpy(namebuf, ep.achName); |
| 2458 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2459 | /* Leave Case of Name Alone -- In Native Form */ |
| 2460 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2461 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2462 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2463 | if (v == NULL) { |
| 2464 | Py_DECREF(d); |
| 2465 | d = NULL; |
| 2466 | break; |
| 2467 | } |
| 2468 | if (PyList_Append(d, v) != 0) { |
| 2469 | Py_DECREF(v); |
| 2470 | Py_DECREF(d); |
| 2471 | d = NULL; |
| 2472 | break; |
| 2473 | } |
| 2474 | Py_DECREF(v); |
| 2475 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2476 | } |
| 2477 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2478 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2479 | return d; |
| 2480 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2481 | PyObject *oname; |
| 2482 | char *name; |
| 2483 | PyObject *d, *v; |
| 2484 | DIR *dirp; |
| 2485 | struct dirent *ep; |
| 2486 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2487 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2488 | errno = 0; |
| 2489 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2490 | arg_is_unicode = 0; |
| 2491 | PyErr_Clear(); |
| 2492 | } |
| 2493 | if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname)) |
| 2494 | return NULL; |
| 2495 | name = bytes2str(oname, 1); |
Antoine Pitrou | 037077f | 2010-09-04 17:26:01 +0000 | [diff] [blame] | 2496 | Py_BEGIN_ALLOW_THREADS |
| 2497 | dirp = opendir(name); |
| 2498 | Py_END_ALLOW_THREADS |
| 2499 | if (dirp == NULL) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2500 | return posix_error_with_allocated_filename(oname); |
| 2501 | } |
| 2502 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | 037077f | 2010-09-04 17:26:01 +0000 | [diff] [blame] | 2503 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2504 | closedir(dirp); |
Antoine Pitrou | 037077f | 2010-09-04 17:26:01 +0000 | [diff] [blame] | 2505 | Py_END_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2506 | release_bytes(oname); |
| 2507 | return NULL; |
| 2508 | } |
| 2509 | for (;;) { |
| 2510 | errno = 0; |
| 2511 | Py_BEGIN_ALLOW_THREADS |
| 2512 | ep = readdir(dirp); |
| 2513 | Py_END_ALLOW_THREADS |
| 2514 | if (ep == NULL) { |
| 2515 | if (errno == 0) { |
| 2516 | break; |
| 2517 | } else { |
Antoine Pitrou | 037077f | 2010-09-04 17:26:01 +0000 | [diff] [blame] | 2518 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2519 | closedir(dirp); |
Antoine Pitrou | 037077f | 2010-09-04 17:26:01 +0000 | [diff] [blame] | 2520 | Py_END_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2521 | Py_DECREF(d); |
| 2522 | return posix_error_with_allocated_filename(oname); |
| 2523 | } |
| 2524 | } |
| 2525 | if (ep->d_name[0] == '.' && |
| 2526 | (NAMLEN(ep) == 1 || |
| 2527 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2528 | continue; |
Victor Stinner | 203406c | 2010-05-14 18:07:39 +0000 | [diff] [blame] | 2529 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2530 | if (v == NULL) { |
Victor Stinner | 203406c | 2010-05-14 18:07:39 +0000 | [diff] [blame] | 2531 | Py_DECREF(d); |
| 2532 | d = NULL; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2533 | break; |
| 2534 | } |
Victor Stinner | 203406c | 2010-05-14 18:07:39 +0000 | [diff] [blame] | 2535 | if (arg_is_unicode) { |
| 2536 | PyObject *w; |
| 2537 | |
| 2538 | w = PyUnicode_FromEncodedObject(v, |
| 2539 | Py_FileSystemDefaultEncoding, |
| 2540 | "surrogateescape"); |
| 2541 | Py_DECREF(v); |
| 2542 | if (w != NULL) |
| 2543 | v = w; |
| 2544 | else { |
| 2545 | /* Encoding failed to decode ASCII bytes. |
| 2546 | Raise exception. */ |
| 2547 | Py_DECREF(d); |
| 2548 | d = NULL; |
| 2549 | break; |
| 2550 | } |
| 2551 | } |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2552 | if (PyList_Append(d, v) != 0) { |
| 2553 | Py_DECREF(v); |
Victor Stinner | 203406c | 2010-05-14 18:07:39 +0000 | [diff] [blame] | 2554 | Py_DECREF(d); |
| 2555 | d = NULL; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2556 | break; |
| 2557 | } |
| 2558 | Py_DECREF(v); |
| 2559 | } |
Antoine Pitrou | 037077f | 2010-09-04 17:26:01 +0000 | [diff] [blame] | 2560 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2561 | closedir(dirp); |
Antoine Pitrou | 037077f | 2010-09-04 17:26:01 +0000 | [diff] [blame] | 2562 | Py_END_ALLOW_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2563 | release_bytes(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2564 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2565 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2566 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2567 | #endif /* which OS */ |
| 2568 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2569 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2570 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2571 | /* A helper function for abspath on win32 */ |
| 2572 | static PyObject * |
| 2573 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2574 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2575 | PyObject *opath; |
| 2576 | char *path; |
| 2577 | char outbuf[MAX_PATH*2]; |
| 2578 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2579 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2580 | if (unicode_file_names()) { |
| 2581 | PyUnicodeObject *po; |
| 2582 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2583 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2584 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2585 | Py_UNICODE *wtemp; |
| 2586 | DWORD result; |
| 2587 | PyObject *v; |
| 2588 | result = GetFullPathNameW(wpath, |
| 2589 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2590 | woutbuf, &wtemp); |
| 2591 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2592 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2593 | if (!woutbufp) |
| 2594 | return PyErr_NoMemory(); |
| 2595 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2596 | } |
| 2597 | if (result) |
| 2598 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2599 | else |
| 2600 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2601 | if (woutbufp != woutbuf) |
| 2602 | free(woutbufp); |
| 2603 | return v; |
| 2604 | } |
| 2605 | /* Drop the argument parsing error as narrow strings |
| 2606 | are also valid. */ |
| 2607 | PyErr_Clear(); |
| 2608 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2609 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2610 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2611 | PyUnicode_FSConverter, &opath)) |
| 2612 | return NULL; |
| 2613 | path = bytes2str(opath, 1); |
| 2614 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2615 | outbuf, &temp)) { |
| 2616 | win32_error("GetFullPathName", path); |
| 2617 | release_bytes(opath); |
| 2618 | return NULL; |
| 2619 | } |
| 2620 | release_bytes(opath); |
| 2621 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2622 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2623 | Py_FileSystemDefaultEncoding, NULL); |
| 2624 | } |
| 2625 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2626 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2627 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2629 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2630 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2631 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2632 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2633 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2634 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2635 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2636 | int res; |
| 2637 | PyObject *opath; |
| 2638 | char *path; |
| 2639 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2640 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2641 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2642 | if (unicode_file_names()) { |
| 2643 | PyUnicodeObject *po; |
| 2644 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 2645 | Py_BEGIN_ALLOW_THREADS |
| 2646 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2647 | it is a simple dereference. */ |
| 2648 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 2649 | Py_END_ALLOW_THREADS |
| 2650 | if (!res) |
| 2651 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 2652 | Py_INCREF(Py_None); |
| 2653 | return Py_None; |
| 2654 | } |
| 2655 | /* Drop the argument parsing error as narrow strings |
| 2656 | are also valid. */ |
| 2657 | PyErr_Clear(); |
| 2658 | } |
| 2659 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2660 | PyUnicode_FSConverter, &opath, &mode)) |
| 2661 | return NULL; |
| 2662 | path = bytes2str(opath, 1); |
| 2663 | Py_BEGIN_ALLOW_THREADS |
| 2664 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2665 | it is a simple dereference. */ |
| 2666 | res = CreateDirectoryA(path, NULL); |
| 2667 | Py_END_ALLOW_THREADS |
| 2668 | if (!res) { |
| 2669 | win32_error("mkdir", path); |
| 2670 | release_bytes(opath); |
| 2671 | return NULL; |
| 2672 | } |
| 2673 | release_bytes(opath); |
| 2674 | Py_INCREF(Py_None); |
| 2675 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2676 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2677 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2678 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2679 | PyUnicode_FSConverter, &opath, &mode)) |
| 2680 | return NULL; |
| 2681 | path = bytes2str(opath, 1); |
| 2682 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2683 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2684 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2685 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2686 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2687 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2688 | Py_END_ALLOW_THREADS |
| 2689 | if (res < 0) |
| 2690 | return posix_error_with_allocated_filename(opath); |
| 2691 | release_bytes(opath); |
| 2692 | Py_INCREF(Py_None); |
| 2693 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2694 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2697 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2698 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2699 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2700 | #include <sys/resource.h> |
| 2701 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2702 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2703 | |
| 2704 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2705 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2706 | "nice(inc) -> new_priority\n\n\ |
| 2707 | 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] | 2708 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2709 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2710 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2711 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2712 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2713 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2714 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 2715 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2716 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2717 | /* There are two flavours of 'nice': one that returns the new |
| 2718 | priority (as required by almost all standards out there) and the |
| 2719 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2720 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2721 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2722 | If we are of the nice family that returns the new priority, we |
| 2723 | need to clear errno before the call, and check if errno is filled |
| 2724 | before calling posix_error() on a returnvalue of -1, because the |
| 2725 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2726 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2727 | errno = 0; |
| 2728 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2729 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2730 | if (value == 0) |
| 2731 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2732 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2733 | if (value == -1 && errno != 0) |
| 2734 | /* either nice() or getpriority() returned an error */ |
| 2735 | return posix_error(); |
| 2736 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2737 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2738 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2739 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2740 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2741 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2742 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2743 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2744 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2745 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2746 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2747 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2748 | PyObject *o1, *o2; |
| 2749 | char *p1, *p2; |
| 2750 | BOOL result; |
| 2751 | if (unicode_file_names()) { |
| 2752 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 2753 | goto error; |
| 2754 | if (!convert_to_unicode(&o1)) |
| 2755 | goto error; |
| 2756 | if (!convert_to_unicode(&o2)) { |
| 2757 | Py_DECREF(o1); |
| 2758 | goto error; |
| 2759 | } |
| 2760 | Py_BEGIN_ALLOW_THREADS |
| 2761 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2762 | PyUnicode_AsUnicode(o2)); |
| 2763 | Py_END_ALLOW_THREADS |
| 2764 | Py_DECREF(o1); |
| 2765 | Py_DECREF(o2); |
| 2766 | if (!result) |
| 2767 | return win32_error("rename", NULL); |
| 2768 | Py_INCREF(Py_None); |
| 2769 | return Py_None; |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2770 | error: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2771 | PyErr_Clear(); |
| 2772 | } |
| 2773 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2774 | return NULL; |
| 2775 | Py_BEGIN_ALLOW_THREADS |
| 2776 | result = MoveFileA(p1, p2); |
| 2777 | Py_END_ALLOW_THREADS |
| 2778 | if (!result) |
| 2779 | return win32_error("rename", NULL); |
| 2780 | Py_INCREF(Py_None); |
| 2781 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2782 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2783 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2784 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2787 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2788 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2789 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2790 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2791 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2792 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2793 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2794 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2795 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2796 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2797 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2798 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2799 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2802 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2803 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2804 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2805 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2806 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2807 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2808 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2809 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2810 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2811 | return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2812 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2813 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2814 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2817 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2818 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2819 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2820 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2821 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2822 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2823 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2824 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2825 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2826 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2827 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2828 | wchar_t *command; |
| 2829 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2830 | return NULL; |
Victor Stinner | a5ab4fc | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 2831 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2832 | Py_BEGIN_ALLOW_THREADS |
| 2833 | sts = _wsystem(command); |
| 2834 | Py_END_ALLOW_THREADS |
Victor Stinner | a5ab4fc | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 2835 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2836 | PyObject *command_obj; |
| 2837 | char *command; |
| 2838 | if (!PyArg_ParseTuple(args, "O&:system", |
| 2839 | PyUnicode_FSConverter, &command_obj)) |
| 2840 | return NULL; |
Victor Stinner | a5ab4fc | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 2841 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2842 | command = bytes2str(command_obj, 1); |
| 2843 | Py_BEGIN_ALLOW_THREADS |
| 2844 | sts = system(command); |
| 2845 | Py_END_ALLOW_THREADS |
| 2846 | release_bytes(command_obj); |
Victor Stinner | a5ab4fc | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 2847 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2848 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2849 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2850 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2851 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2853 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2854 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2855 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2856 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2857 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2858 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2859 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2860 | int i; |
| 2861 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 2862 | return NULL; |
| 2863 | i = (int)umask(i); |
| 2864 | if (i < 0) |
| 2865 | return posix_error(); |
| 2866 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2869 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2870 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2871 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2872 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2874 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2875 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2876 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2877 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2878 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2879 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2880 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2881 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2882 | return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2883 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2884 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2885 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2888 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2889 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2890 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2891 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2892 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2893 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2894 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2895 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2896 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2897 | struct utsname u; |
| 2898 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2899 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2900 | Py_BEGIN_ALLOW_THREADS |
| 2901 | res = uname(&u); |
| 2902 | Py_END_ALLOW_THREADS |
| 2903 | if (res < 0) |
| 2904 | return posix_error(); |
| 2905 | return Py_BuildValue("(sssss)", |
| 2906 | u.sysname, |
| 2907 | u.nodename, |
| 2908 | u.release, |
| 2909 | u.version, |
| 2910 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2911 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2912 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2913 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2914 | static int |
| 2915 | extract_time(PyObject *t, long* sec, long* usec) |
| 2916 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2917 | long intval; |
| 2918 | if (PyFloat_Check(t)) { |
| 2919 | double tval = PyFloat_AsDouble(t); |
| 2920 | PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t); |
| 2921 | if (!intobj) |
| 2922 | return -1; |
| 2923 | intval = PyLong_AsLong(intobj); |
| 2924 | Py_DECREF(intobj); |
| 2925 | if (intval == -1 && PyErr_Occurred()) |
| 2926 | return -1; |
| 2927 | *sec = intval; |
| 2928 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
| 2929 | if (*usec < 0) |
| 2930 | /* If rounding gave us a negative number, |
| 2931 | truncate. */ |
| 2932 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2933 | return 0; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2934 | } |
| 2935 | intval = PyLong_AsLong(t); |
| 2936 | if (intval == -1 && PyErr_Occurred()) |
| 2937 | return -1; |
| 2938 | *sec = intval; |
| 2939 | *usec = 0; |
| 2940 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2941 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2943 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2944 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2945 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2946 | 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] | 2947 | 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] | 2948 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2949 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2950 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2951 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2952 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2953 | PyObject *arg; |
| 2954 | PyUnicodeObject *obwpath; |
| 2955 | wchar_t *wpath = NULL; |
| 2956 | PyObject *oapath; |
| 2957 | char *apath; |
| 2958 | HANDLE hFile; |
| 2959 | long atimesec, mtimesec, ausec, musec; |
| 2960 | FILETIME atime, mtime; |
| 2961 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2962 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 2963 | if (unicode_file_names()) { |
| 2964 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2965 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2966 | Py_BEGIN_ALLOW_THREADS |
| 2967 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 2968 | NULL, OPEN_EXISTING, |
| 2969 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2970 | Py_END_ALLOW_THREADS |
| 2971 | if (hFile == INVALID_HANDLE_VALUE) |
| 2972 | return win32_error_unicode("utime", wpath); |
| 2973 | } else |
| 2974 | /* Drop the argument parsing error as narrow strings |
| 2975 | are also valid. */ |
| 2976 | PyErr_Clear(); |
| 2977 | } |
| 2978 | if (!wpath) { |
| 2979 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 2980 | PyUnicode_FSConverter, &oapath, &arg)) |
| 2981 | return NULL; |
| 2982 | apath = bytes2str(oapath, 1); |
| 2983 | Py_BEGIN_ALLOW_THREADS |
| 2984 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 2985 | NULL, OPEN_EXISTING, |
| 2986 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2987 | Py_END_ALLOW_THREADS |
| 2988 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2989 | win32_error("utime", apath); |
| 2990 | release_bytes(oapath); |
| 2991 | return NULL; |
| 2992 | } |
| 2993 | release_bytes(oapath); |
| 2994 | } |
| 2995 | |
| 2996 | if (arg == Py_None) { |
| 2997 | SYSTEMTIME now; |
| 2998 | GetSystemTime(&now); |
| 2999 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3000 | !SystemTimeToFileTime(&now, &atime)) { |
| 3001 | win32_error("utime", NULL); |
| 3002 | goto done; |
| 3003 | } |
| 3004 | } |
| 3005 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3006 | PyErr_SetString(PyExc_TypeError, |
| 3007 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3008 | goto done; |
| 3009 | } |
| 3010 | else { |
| 3011 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3012 | &atimesec, &ausec) == -1) |
| 3013 | goto done; |
| 3014 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3015 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3016 | &mtimesec, &musec) == -1) |
| 3017 | goto done; |
| 3018 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3019 | } |
| 3020 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3021 | /* Avoid putting the file name into the error here, |
| 3022 | as that may confuse the user into believing that |
| 3023 | something is wrong with the file, when it also |
| 3024 | could be the time stamp that gives a problem. */ |
| 3025 | win32_error("utime", NULL); |
| 3026 | } |
| 3027 | Py_INCREF(Py_None); |
| 3028 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3029 | done: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3030 | CloseHandle(hFile); |
| 3031 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3032 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3033 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3034 | PyObject *opath; |
| 3035 | char *path; |
| 3036 | long atime, mtime, ausec, musec; |
| 3037 | int res; |
| 3038 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3039 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3040 | #if defined(HAVE_UTIMES) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3041 | struct timeval buf[2]; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3042 | #define ATIME buf[0].tv_sec |
| 3043 | #define MTIME buf[1].tv_sec |
| 3044 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3045 | /* XXX should define struct utimbuf instead, above */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3046 | struct utimbuf buf; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3047 | #define ATIME buf.actime |
| 3048 | #define MTIME buf.modtime |
| 3049 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3050 | #else /* HAVE_UTIMES */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3051 | time_t buf[2]; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3052 | #define ATIME buf[0] |
| 3053 | #define MTIME buf[1] |
| 3054 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3055 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3056 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3057 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3058 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3059 | PyUnicode_FSConverter, &opath, &arg)) |
| 3060 | return NULL; |
| 3061 | path = bytes2str(opath, 1); |
| 3062 | if (arg == Py_None) { |
| 3063 | /* optional time values not given */ |
| 3064 | Py_BEGIN_ALLOW_THREADS |
| 3065 | res = utime(path, NULL); |
| 3066 | Py_END_ALLOW_THREADS |
| 3067 | } |
| 3068 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3069 | PyErr_SetString(PyExc_TypeError, |
| 3070 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3071 | release_bytes(opath); |
| 3072 | return NULL; |
| 3073 | } |
| 3074 | else { |
| 3075 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3076 | &atime, &ausec) == -1) { |
| 3077 | release_bytes(opath); |
| 3078 | return NULL; |
| 3079 | } |
| 3080 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3081 | &mtime, &musec) == -1) { |
| 3082 | release_bytes(opath); |
| 3083 | return NULL; |
| 3084 | } |
| 3085 | ATIME = atime; |
| 3086 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3087 | #ifdef HAVE_UTIMES |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3088 | buf[0].tv_usec = ausec; |
| 3089 | buf[1].tv_usec = musec; |
| 3090 | Py_BEGIN_ALLOW_THREADS |
| 3091 | res = utimes(path, buf); |
| 3092 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3093 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3094 | Py_BEGIN_ALLOW_THREADS |
| 3095 | res = utime(path, UTIME_ARG); |
| 3096 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3097 | #endif /* HAVE_UTIMES */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3098 | } |
| 3099 | if (res < 0) { |
| 3100 | return posix_error_with_allocated_filename(opath); |
| 3101 | } |
| 3102 | release_bytes(opath); |
| 3103 | Py_INCREF(Py_None); |
| 3104 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3105 | #undef UTIME_ARG |
| 3106 | #undef ATIME |
| 3107 | #undef MTIME |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3108 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3111 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3112 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3113 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3114 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3115 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3116 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3117 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3118 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3119 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3120 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3121 | int sts; |
| 3122 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3123 | return NULL; |
| 3124 | _exit(sts); |
| 3125 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3128 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3129 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3130 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3131 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3132 | Py_ssize_t i; |
| 3133 | for (i = 0; i < count; i++) |
| 3134 | PyMem_Free(array[i]); |
| 3135 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3136 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3137 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3138 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3139 | int fsconvert_strdup(PyObject *o, char**out) |
| 3140 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3141 | PyObject *bytes; |
| 3142 | Py_ssize_t size; |
| 3143 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3144 | return 0; |
| 3145 | size = PyObject_Size(bytes); |
| 3146 | *out = PyMem_Malloc(size+1); |
| 3147 | if (!*out) |
| 3148 | return 0; |
| 3149 | /* Don't lock bytes, as we hold the GIL */ |
| 3150 | memcpy(*out, bytes2str(bytes, 0), size+1); |
| 3151 | Py_DECREF(bytes); |
| 3152 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3153 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3154 | #endif |
| 3155 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3156 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3157 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3158 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3159 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3160 | Execute an executable path with arguments, replacing current process.\n\ |
| 3161 | \n\ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3162 | path: path of executable file\n\ |
| 3163 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3164 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3165 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3166 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3167 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3168 | PyObject *opath; |
| 3169 | char *path; |
| 3170 | PyObject *argv; |
| 3171 | char **argvlist; |
| 3172 | Py_ssize_t i, argc; |
| 3173 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3174 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3175 | /* execv has two arguments: (path, argv), where |
| 3176 | argv is a list or tuple of strings. */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3177 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3178 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3179 | PyUnicode_FSConverter, |
| 3180 | &opath, &argv)) |
| 3181 | return NULL; |
| 3182 | path = bytes2str(opath, 1); |
| 3183 | if (PyList_Check(argv)) { |
| 3184 | argc = PyList_Size(argv); |
| 3185 | getitem = PyList_GetItem; |
| 3186 | } |
| 3187 | else if (PyTuple_Check(argv)) { |
| 3188 | argc = PyTuple_Size(argv); |
| 3189 | getitem = PyTuple_GetItem; |
| 3190 | } |
| 3191 | else { |
| 3192 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
| 3193 | release_bytes(opath); |
| 3194 | return NULL; |
| 3195 | } |
| 3196 | if (argc < 1) { |
| 3197 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 3198 | release_bytes(opath); |
| 3199 | return NULL; |
| 3200 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3201 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3202 | argvlist = PyMem_NEW(char *, argc+1); |
| 3203 | if (argvlist == NULL) { |
| 3204 | release_bytes(opath); |
| 3205 | return PyErr_NoMemory(); |
| 3206 | } |
| 3207 | for (i = 0; i < argc; i++) { |
| 3208 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3209 | &argvlist[i])) { |
| 3210 | free_string_array(argvlist, i); |
| 3211 | PyErr_SetString(PyExc_TypeError, |
| 3212 | "execv() arg 2 must contain only strings"); |
| 3213 | release_bytes(opath); |
| 3214 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3215 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | argvlist[argc] = NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3219 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3220 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3221 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3222 | /* If we get here it's definitely an error */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3223 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3224 | free_string_array(argvlist, argc); |
| 3225 | release_bytes(opath); |
| 3226 | return posix_error(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3229 | static char** |
| 3230 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3231 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3232 | char **envlist; |
| 3233 | Py_ssize_t i, pos, envc; |
| 3234 | PyObject *keys=NULL, *vals=NULL; |
| 3235 | PyObject *key, *val, *key2, *val2; |
| 3236 | char *p, *k, *v; |
| 3237 | size_t len; |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3238 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3239 | i = PyMapping_Size(env); |
| 3240 | if (i < 0) |
| 3241 | return NULL; |
| 3242 | envlist = PyMem_NEW(char *, i + 1); |
| 3243 | if (envlist == NULL) { |
| 3244 | PyErr_NoMemory(); |
| 3245 | return NULL; |
| 3246 | } |
| 3247 | envc = 0; |
| 3248 | keys = PyMapping_Keys(env); |
| 3249 | vals = PyMapping_Values(env); |
| 3250 | if (!keys || !vals) |
| 3251 | goto error; |
| 3252 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3253 | PyErr_Format(PyExc_TypeError, |
| 3254 | "env.keys() or env.values() is not a list"); |
| 3255 | goto error; |
| 3256 | } |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3257 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3258 | for (pos = 0; pos < i; pos++) { |
| 3259 | key = PyList_GetItem(keys, pos); |
| 3260 | val = PyList_GetItem(vals, pos); |
| 3261 | if (!key || !val) |
| 3262 | goto error; |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3263 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3264 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3265 | goto error; |
| 3266 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3267 | Py_DECREF(key2); |
| 3268 | goto error; |
| 3269 | } |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3270 | |
| 3271 | #if defined(PYOS_OS2) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3272 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3273 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3274 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3275 | k = PyBytes_AsString(key2); |
| 3276 | v = PyBytes_AsString(val2); |
| 3277 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3278 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3279 | p = PyMem_NEW(char, len); |
| 3280 | if (p == NULL) { |
| 3281 | PyErr_NoMemory(); |
| 3282 | Py_DECREF(key2); |
| 3283 | Py_DECREF(val2); |
| 3284 | goto error; |
| 3285 | } |
| 3286 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3287 | envlist[envc++] = p; |
| 3288 | Py_DECREF(key2); |
| 3289 | Py_DECREF(val2); |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3290 | #if defined(PYOS_OS2) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3291 | } |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3292 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3293 | } |
| 3294 | Py_DECREF(vals); |
| 3295 | Py_DECREF(keys); |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3296 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3297 | envlist[envc] = 0; |
| 3298 | *envc_ptr = envc; |
| 3299 | return envlist; |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3300 | |
| 3301 | error: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3302 | Py_XDECREF(keys); |
| 3303 | Py_XDECREF(vals); |
| 3304 | while (--envc >= 0) |
| 3305 | PyMem_DEL(envlist[envc]); |
| 3306 | PyMem_DEL(envlist); |
| 3307 | return NULL; |
Victor Stinner | a27dcb7 | 2010-04-25 22:39:07 +0000 | [diff] [blame] | 3308 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3310 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3311 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3312 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3313 | \n\ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3314 | path: path of executable file\n\ |
| 3315 | args: tuple or list of arguments\n\ |
| 3316 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3317 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3318 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3319 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3320 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3321 | PyObject *opath; |
| 3322 | char *path; |
| 3323 | PyObject *argv, *env; |
| 3324 | char **argvlist; |
| 3325 | char **envlist; |
| 3326 | Py_ssize_t i, argc, envc; |
| 3327 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3328 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3329 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3330 | /* execve has three arguments: (path, argv, env), where |
| 3331 | argv is a list or tuple of strings and env is a dictionary |
| 3332 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3333 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3334 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3335 | PyUnicode_FSConverter, |
| 3336 | &opath, &argv, &env)) |
| 3337 | return NULL; |
| 3338 | path = bytes2str(opath, 1); |
| 3339 | if (PyList_Check(argv)) { |
| 3340 | argc = PyList_Size(argv); |
| 3341 | getitem = PyList_GetItem; |
| 3342 | } |
| 3343 | else if (PyTuple_Check(argv)) { |
| 3344 | argc = PyTuple_Size(argv); |
| 3345 | getitem = PyTuple_GetItem; |
| 3346 | } |
| 3347 | else { |
| 3348 | PyErr_SetString(PyExc_TypeError, |
| 3349 | "execve() arg 2 must be a tuple or list"); |
| 3350 | goto fail_0; |
| 3351 | } |
| 3352 | if (!PyMapping_Check(env)) { |
| 3353 | PyErr_SetString(PyExc_TypeError, |
| 3354 | "execve() arg 3 must be a mapping object"); |
| 3355 | goto fail_0; |
| 3356 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3357 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3358 | argvlist = PyMem_NEW(char *, argc+1); |
| 3359 | if (argvlist == NULL) { |
| 3360 | PyErr_NoMemory(); |
| 3361 | goto fail_0; |
| 3362 | } |
| 3363 | for (i = 0; i < argc; i++) { |
| 3364 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3365 | &argvlist[i])) |
| 3366 | { |
| 3367 | lastarg = i; |
| 3368 | goto fail_1; |
| 3369 | } |
| 3370 | } |
| 3371 | lastarg = argc; |
| 3372 | argvlist[argc] = NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3373 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3374 | envlist = parse_envlist(env, &envc); |
| 3375 | if (envlist == NULL) |
| 3376 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3377 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3378 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3379 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3380 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3381 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3382 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3383 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3384 | while (--envc >= 0) |
| 3385 | PyMem_DEL(envlist[envc]); |
| 3386 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3387 | fail_1: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3388 | free_string_array(argvlist, lastarg); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3389 | fail_0: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3390 | release_bytes(opath); |
| 3391 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3392 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3393 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3394 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3395 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3396 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3397 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3398 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3399 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3400 | \n\ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3401 | mode: mode of process creation\n\ |
| 3402 | path: path of executable file\n\ |
| 3403 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3404 | |
| 3405 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3406 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3407 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3408 | PyObject *opath; |
| 3409 | char *path; |
| 3410 | PyObject *argv; |
| 3411 | char **argvlist; |
| 3412 | int mode, i; |
| 3413 | Py_ssize_t argc; |
| 3414 | Py_intptr_t spawnval; |
| 3415 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3416 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3417 | /* spawnv has three arguments: (mode, path, argv), where |
| 3418 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3419 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3420 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 3421 | PyUnicode_FSConverter, |
| 3422 | &opath, &argv)) |
| 3423 | return NULL; |
| 3424 | path = bytes2str(opath, 1); |
| 3425 | if (PyList_Check(argv)) { |
| 3426 | argc = PyList_Size(argv); |
| 3427 | getitem = PyList_GetItem; |
| 3428 | } |
| 3429 | else if (PyTuple_Check(argv)) { |
| 3430 | argc = PyTuple_Size(argv); |
| 3431 | getitem = PyTuple_GetItem; |
| 3432 | } |
| 3433 | else { |
| 3434 | PyErr_SetString(PyExc_TypeError, |
| 3435 | "spawnv() arg 2 must be a tuple or list"); |
| 3436 | release_bytes(opath); |
| 3437 | return NULL; |
| 3438 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3439 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3440 | argvlist = PyMem_NEW(char *, argc+1); |
| 3441 | if (argvlist == NULL) { |
| 3442 | release_bytes(opath); |
| 3443 | return PyErr_NoMemory(); |
| 3444 | } |
| 3445 | for (i = 0; i < argc; i++) { |
| 3446 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3447 | &argvlist[i])) { |
| 3448 | free_string_array(argvlist, i); |
| 3449 | PyErr_SetString( |
| 3450 | PyExc_TypeError, |
| 3451 | "spawnv() arg 2 must contain only strings"); |
| 3452 | release_bytes(opath); |
| 3453 | return NULL; |
| 3454 | } |
| 3455 | } |
| 3456 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3457 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3458 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3459 | Py_BEGIN_ALLOW_THREADS |
| 3460 | spawnval = spawnv(mode, path, argvlist); |
| 3461 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3462 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3463 | if (mode == _OLD_P_OVERLAY) |
| 3464 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3465 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3466 | Py_BEGIN_ALLOW_THREADS |
| 3467 | spawnval = _spawnv(mode, path, argvlist); |
| 3468 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3469 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3470 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3471 | free_string_array(argvlist, argc); |
| 3472 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3473 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3474 | if (spawnval == -1) |
| 3475 | return posix_error(); |
| 3476 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3477 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3478 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3479 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3480 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3481 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3485 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3486 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3487 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3488 | \n\ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3489 | mode: mode of process creation\n\ |
| 3490 | path: path of executable file\n\ |
| 3491 | args: tuple or list of arguments\n\ |
| 3492 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3493 | |
| 3494 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3495 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3496 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3497 | PyObject *opath; |
| 3498 | char *path; |
| 3499 | PyObject *argv, *env; |
| 3500 | char **argvlist; |
| 3501 | char **envlist; |
| 3502 | PyObject *res = NULL; |
Antoine Pitrou | 835b445 | 2010-08-15 18:32:16 +0000 | [diff] [blame] | 3503 | int mode; |
| 3504 | Py_ssize_t argc, i, envc; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3505 | Py_intptr_t spawnval; |
| 3506 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3507 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3508 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3509 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3510 | argv is a list or tuple of strings and env is a dictionary |
| 3511 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3512 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3513 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 3514 | PyUnicode_FSConverter, |
| 3515 | &opath, &argv, &env)) |
| 3516 | return NULL; |
| 3517 | path = bytes2str(opath, 1); |
| 3518 | if (PyList_Check(argv)) { |
| 3519 | argc = PyList_Size(argv); |
| 3520 | getitem = PyList_GetItem; |
| 3521 | } |
| 3522 | else if (PyTuple_Check(argv)) { |
| 3523 | argc = PyTuple_Size(argv); |
| 3524 | getitem = PyTuple_GetItem; |
| 3525 | } |
| 3526 | else { |
| 3527 | PyErr_SetString(PyExc_TypeError, |
| 3528 | "spawnve() arg 2 must be a tuple or list"); |
| 3529 | goto fail_0; |
| 3530 | } |
| 3531 | if (!PyMapping_Check(env)) { |
| 3532 | PyErr_SetString(PyExc_TypeError, |
| 3533 | "spawnve() arg 3 must be a mapping object"); |
| 3534 | goto fail_0; |
| 3535 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3536 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3537 | argvlist = PyMem_NEW(char *, argc+1); |
| 3538 | if (argvlist == NULL) { |
| 3539 | PyErr_NoMemory(); |
| 3540 | goto fail_0; |
| 3541 | } |
| 3542 | for (i = 0; i < argc; i++) { |
| 3543 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3544 | &argvlist[i])) |
| 3545 | { |
| 3546 | lastarg = i; |
| 3547 | goto fail_1; |
| 3548 | } |
| 3549 | } |
| 3550 | lastarg = argc; |
| 3551 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3552 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3553 | envlist = parse_envlist(env, &envc); |
| 3554 | if (envlist == NULL) |
| 3555 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3556 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3557 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3558 | Py_BEGIN_ALLOW_THREADS |
| 3559 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3560 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3561 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3562 | if (mode == _OLD_P_OVERLAY) |
| 3563 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3564 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3565 | Py_BEGIN_ALLOW_THREADS |
| 3566 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3567 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3568 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3569 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3570 | if (spawnval == -1) |
| 3571 | (void) posix_error(); |
| 3572 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3573 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3574 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3575 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3576 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3577 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3578 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3579 | while (--envc >= 0) |
| 3580 | PyMem_DEL(envlist[envc]); |
| 3581 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3582 | fail_1: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3583 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3584 | fail_0: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3585 | release_bytes(opath); |
| 3586 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3587 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3588 | |
| 3589 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3590 | #if defined(PYOS_OS2) |
| 3591 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3592 | "spawnvp(mode, file, args)\n\n\ |
| 3593 | Execute the program 'file' in a new process, using the environment\n\ |
| 3594 | search path to find the file.\n\ |
| 3595 | \n\ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3596 | mode: mode of process creation\n\ |
| 3597 | file: executable file name\n\ |
| 3598 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3599 | |
| 3600 | static PyObject * |
| 3601 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3602 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3603 | PyObject *opath; |
| 3604 | char *path; |
| 3605 | PyObject *argv; |
| 3606 | char **argvlist; |
| 3607 | int mode, i, argc; |
| 3608 | Py_intptr_t spawnval; |
| 3609 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3610 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3611 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3612 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3613 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3614 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 3615 | PyUnicode_FSConverter, |
| 3616 | &opath, &argv)) |
| 3617 | return NULL; |
| 3618 | path = bytes2str(opath); |
| 3619 | if (PyList_Check(argv)) { |
| 3620 | argc = PyList_Size(argv); |
| 3621 | getitem = PyList_GetItem; |
| 3622 | } |
| 3623 | else if (PyTuple_Check(argv)) { |
| 3624 | argc = PyTuple_Size(argv); |
| 3625 | getitem = PyTuple_GetItem; |
| 3626 | } |
| 3627 | else { |
| 3628 | PyErr_SetString(PyExc_TypeError, |
| 3629 | "spawnvp() arg 2 must be a tuple or list"); |
| 3630 | release_bytes(opath); |
| 3631 | return NULL; |
| 3632 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3633 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3634 | argvlist = PyMem_NEW(char *, argc+1); |
| 3635 | if (argvlist == NULL) { |
| 3636 | release_bytes(opath); |
| 3637 | return PyErr_NoMemory(); |
| 3638 | } |
| 3639 | for (i = 0; i < argc; i++) { |
| 3640 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3641 | &argvlist[i])) { |
| 3642 | free_string_array(argvlist, i); |
| 3643 | PyErr_SetString( |
| 3644 | PyExc_TypeError, |
| 3645 | "spawnvp() arg 2 must contain only strings"); |
| 3646 | release_bytes(opath); |
| 3647 | return NULL; |
| 3648 | } |
| 3649 | } |
| 3650 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3651 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3652 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3653 | #if defined(PYCC_GCC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3654 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3655 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3656 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3657 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3658 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3659 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3660 | free_string_array(argvlist, argc); |
| 3661 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3662 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3663 | if (spawnval == -1) |
| 3664 | return posix_error(); |
| 3665 | else |
| 3666 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | |
| 3670 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3671 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3672 | Execute the program 'file' in a new process, using the environment\n\ |
| 3673 | search path to find the file.\n\ |
| 3674 | \n\ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3675 | mode: mode of process creation\n\ |
| 3676 | file: executable file name\n\ |
| 3677 | args: tuple or list of arguments\n\ |
| 3678 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3679 | |
| 3680 | static PyObject * |
| 3681 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3682 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3683 | PyObject *opath |
| 3684 | char *path; |
| 3685 | PyObject *argv, *env; |
| 3686 | char **argvlist; |
| 3687 | char **envlist; |
| 3688 | PyObject *res=NULL; |
Antoine Pitrou | 835b445 | 2010-08-15 18:32:16 +0000 | [diff] [blame] | 3689 | int mode; |
| 3690 | Py_ssize_t argc, i, envc; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3691 | Py_intptr_t spawnval; |
| 3692 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3693 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3694 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3695 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3696 | argv is a list or tuple of strings and env is a dictionary |
| 3697 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3698 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3699 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3700 | PyUnicode_FSConverter, |
| 3701 | &opath, &argv, &env)) |
| 3702 | return NULL; |
| 3703 | path = bytes2str(opath); |
| 3704 | if (PyList_Check(argv)) { |
| 3705 | argc = PyList_Size(argv); |
| 3706 | getitem = PyList_GetItem; |
| 3707 | } |
| 3708 | else if (PyTuple_Check(argv)) { |
| 3709 | argc = PyTuple_Size(argv); |
| 3710 | getitem = PyTuple_GetItem; |
| 3711 | } |
| 3712 | else { |
| 3713 | PyErr_SetString(PyExc_TypeError, |
| 3714 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3715 | goto fail_0; |
| 3716 | } |
| 3717 | if (!PyMapping_Check(env)) { |
| 3718 | PyErr_SetString(PyExc_TypeError, |
| 3719 | "spawnvpe() arg 3 must be a mapping object"); |
| 3720 | goto fail_0; |
| 3721 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3722 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3723 | argvlist = PyMem_NEW(char *, argc+1); |
| 3724 | if (argvlist == NULL) { |
| 3725 | PyErr_NoMemory(); |
| 3726 | goto fail_0; |
| 3727 | } |
| 3728 | for (i = 0; i < argc; i++) { |
| 3729 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3730 | &argvlist[i])) |
| 3731 | { |
| 3732 | lastarg = i; |
| 3733 | goto fail_1; |
| 3734 | } |
| 3735 | } |
| 3736 | lastarg = argc; |
| 3737 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3738 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3739 | envlist = parse_envlist(env, &envc); |
| 3740 | if (envlist == NULL) |
| 3741 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3742 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3743 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3744 | #if defined(PYCC_GCC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3745 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3746 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3747 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3748 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3749 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3750 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3751 | if (spawnval == -1) |
| 3752 | (void) posix_error(); |
| 3753 | else |
| 3754 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3755 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3756 | while (--envc >= 0) |
| 3757 | PyMem_DEL(envlist[envc]); |
| 3758 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3759 | fail_1: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3760 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3761 | fail_0: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3762 | release_bytes(opath); |
| 3763 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3764 | } |
| 3765 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3766 | #endif /* HAVE_SPAWNV */ |
| 3767 | |
| 3768 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3769 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3770 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3771 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3772 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3773 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3774 | 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] | 3775 | |
| 3776 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3777 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3778 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3779 | pid_t pid; |
| 3780 | int result; |
| 3781 | _PyImport_AcquireLock(); |
| 3782 | pid = fork1(); |
| 3783 | result = _PyImport_ReleaseLock(); |
| 3784 | if (pid == -1) |
| 3785 | return posix_error(); |
| 3786 | if (pid == 0) |
| 3787 | PyOS_AfterFork(); |
| 3788 | if (result < 0) { |
| 3789 | /* Don't clobber the OSError if the fork failed. */ |
| 3790 | PyErr_SetString(PyExc_RuntimeError, |
| 3791 | "not holding the import lock"); |
| 3792 | return NULL; |
| 3793 | } |
| 3794 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3795 | } |
| 3796 | #endif |
| 3797 | |
| 3798 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3799 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3800 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3801 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3802 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3803 | 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] | 3804 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3805 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3806 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3807 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3808 | pid_t pid; |
| 3809 | int result; |
| 3810 | _PyImport_AcquireLock(); |
| 3811 | pid = fork(); |
| 3812 | result = _PyImport_ReleaseLock(); |
| 3813 | if (pid == -1) |
| 3814 | return posix_error(); |
| 3815 | if (pid == 0) |
| 3816 | PyOS_AfterFork(); |
| 3817 | if (result < 0) { |
| 3818 | /* Don't clobber the OSError if the fork failed. */ |
| 3819 | PyErr_SetString(PyExc_RuntimeError, |
| 3820 | "not holding the import lock"); |
| 3821 | return NULL; |
| 3822 | } |
| 3823 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3824 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3825 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3826 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3827 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3828 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3829 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3830 | #define DEV_PTY_FILE "/dev/ptc" |
| 3831 | #define HAVE_DEV_PTMX |
| 3832 | #else |
| 3833 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3834 | #endif |
| 3835 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3836 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3837 | #ifdef HAVE_PTY_H |
| 3838 | #include <pty.h> |
| 3839 | #else |
| 3840 | #ifdef HAVE_LIBUTIL_H |
| 3841 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3842 | #endif /* HAVE_LIBUTIL_H */ |
| 3843 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3844 | #ifdef HAVE_STROPTS_H |
| 3845 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3846 | #endif |
| 3847 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3848 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3849 | #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] | 3850 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3851 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3852 | 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] | 3853 | |
| 3854 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3855 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3856 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3857 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3858 | #ifndef HAVE_OPENPTY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3859 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3860 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3861 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3862 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3863 | #ifdef sun |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3864 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3865 | #endif |
| 3866 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3867 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3868 | #ifdef HAVE_OPENPTY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3869 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3870 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3871 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3872 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3873 | if (slave_name == NULL) |
| 3874 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3875 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3876 | slave_fd = open(slave_name, O_RDWR); |
| 3877 | if (slave_fd < 0) |
| 3878 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3879 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3880 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 3881 | if (master_fd < 0) |
| 3882 | return posix_error(); |
| 3883 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 3884 | /* change permission of slave */ |
| 3885 | if (grantpt(master_fd) < 0) { |
| 3886 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3887 | return posix_error(); |
| 3888 | } |
| 3889 | /* unlock slave */ |
| 3890 | if (unlockpt(master_fd) < 0) { |
| 3891 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3892 | return posix_error(); |
| 3893 | } |
| 3894 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3895 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3896 | if (slave_name == NULL) |
| 3897 | return posix_error(); |
| 3898 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3899 | if (slave_fd < 0) |
| 3900 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3901 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3902 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3903 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3904 | #ifndef __hpux |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3905 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3906 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3907 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3908 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3909 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3910 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3911 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3912 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3913 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3914 | |
| 3915 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3916 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3917 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3918 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3919 | 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] | 3920 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3921 | |
| 3922 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3923 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3924 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3925 | int master_fd = -1, result; |
| 3926 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3927 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3928 | _PyImport_AcquireLock(); |
| 3929 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3930 | result = _PyImport_ReleaseLock(); |
| 3931 | if (pid == -1) |
| 3932 | return posix_error(); |
| 3933 | if (pid == 0) |
| 3934 | PyOS_AfterFork(); |
| 3935 | if (result < 0) { |
| 3936 | /* Don't clobber the OSError if the fork failed. */ |
| 3937 | PyErr_SetString(PyExc_RuntimeError, |
| 3938 | "not holding the import lock"); |
| 3939 | return NULL; |
| 3940 | } |
| 3941 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3942 | } |
| 3943 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3944 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3945 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3946 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3947 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3948 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3949 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3950 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3951 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3952 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3953 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3954 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3955 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3956 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3957 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3958 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3959 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3960 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3961 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3962 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3963 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3964 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3965 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3966 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3967 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3968 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3969 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3970 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3971 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3972 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3973 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3974 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3975 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3976 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3977 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3978 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3979 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3980 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3981 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3982 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3983 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3984 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3985 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3986 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3987 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3988 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3989 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3990 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 3991 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3994 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3995 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3996 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3997 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3998 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3999 | |
| 4000 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4001 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4002 | { |
| 4003 | PyObject *result = NULL; |
| 4004 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4005 | #ifdef NGROUPS_MAX |
| 4006 | #define MAX_GROUPS NGROUPS_MAX |
| 4007 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4008 | /* 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] | 4009 | #define MAX_GROUPS 64 |
| 4010 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4011 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | 47076f7 | 2010-07-23 15:46:03 +0000 | [diff] [blame] | 4012 | |
| 4013 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
| 4014 | * This is a helper variable to store the intermediate result when |
| 4015 | * that happens. |
| 4016 | * |
| 4017 | * To keep the code readable the OSX behaviour is unconditional, |
| 4018 | * according to the POSIX spec this should be safe on all unix-y |
| 4019 | * systems. |
| 4020 | */ |
| 4021 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4022 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4023 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4024 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | 47076f7 | 2010-07-23 15:46:03 +0000 | [diff] [blame] | 4025 | if (n < 0) { |
| 4026 | if (errno == EINVAL) { |
| 4027 | n = getgroups(0, NULL); |
| 4028 | if (n == -1) { |
| 4029 | return posix_error(); |
| 4030 | } |
| 4031 | if (n == 0) { |
| 4032 | /* Avoid malloc(0) */ |
| 4033 | alt_grouplist = grouplist; |
| 4034 | } else { |
| 4035 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 4036 | if (alt_grouplist == NULL) { |
| 4037 | errno = EINVAL; |
| 4038 | return posix_error(); |
| 4039 | } |
| 4040 | n = getgroups(n, alt_grouplist); |
| 4041 | if (n == -1) { |
| 4042 | PyMem_Free(alt_grouplist); |
| 4043 | return posix_error(); |
| 4044 | } |
| 4045 | } |
| 4046 | } else { |
| 4047 | return posix_error(); |
| 4048 | } |
| 4049 | } |
| 4050 | result = PyList_New(n); |
| 4051 | if (result != NULL) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4052 | int i; |
| 4053 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | 47076f7 | 2010-07-23 15:46:03 +0000 | [diff] [blame] | 4054 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4055 | if (o == NULL) { |
| 4056 | Py_DECREF(result); |
| 4057 | result = NULL; |
| 4058 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4059 | } |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4060 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4061 | } |
Ronald Oussoren | 47076f7 | 2010-07-23 15:46:03 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | if (alt_grouplist != grouplist) { |
| 4065 | PyMem_Free(alt_grouplist); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4066 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4067 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4068 | return result; |
| 4069 | } |
| 4070 | #endif |
| 4071 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4072 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4073 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4074 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4075 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4076 | |
| 4077 | static PyObject * |
| 4078 | posix_getpgid(PyObject *self, PyObject *args) |
| 4079 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4080 | pid_t pid, pgid; |
| 4081 | if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid)) |
| 4082 | return NULL; |
| 4083 | pgid = getpgid(pid); |
| 4084 | if (pgid < 0) |
| 4085 | return posix_error(); |
| 4086 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4087 | } |
| 4088 | #endif /* HAVE_GETPGID */ |
| 4089 | |
| 4090 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4091 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4092 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4093 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4094 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4095 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4096 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4097 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4098 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4099 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4100 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4101 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4102 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4103 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4104 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4105 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4106 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4107 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4108 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4109 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4110 | "setpgrp()\n\n\ |
Senthil Kumaran | 28fdadb | 2010-06-17 16:51:08 +0000 | [diff] [blame] | 4111 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4112 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4113 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4114 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4115 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4116 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4117 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4118 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4119 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4120 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4121 | return posix_error(); |
| 4122 | Py_INCREF(Py_None); |
| 4123 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4126 | #endif /* HAVE_SETPGRP */ |
| 4127 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4128 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4129 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4130 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4131 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4132 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4133 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4134 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4135 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4136 | return PyLong_FromPid(getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4137 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4138 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4139 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4140 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4141 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4142 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4143 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4144 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4145 | |
| 4146 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4147 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4148 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4149 | PyObject *result = NULL; |
| 4150 | char *name; |
| 4151 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4152 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4153 | errno = 0; |
| 4154 | name = getlogin(); |
| 4155 | if (name == NULL) { |
| 4156 | if (errno) |
Victor Stinner | 8567599 | 2010-08-15 09:35:13 +0000 | [diff] [blame] | 4157 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4158 | else |
Victor Stinner | 8567599 | 2010-08-15 09:35:13 +0000 | [diff] [blame] | 4159 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4160 | } |
| 4161 | else |
Victor Stinner | 8567599 | 2010-08-15 09:35:13 +0000 | [diff] [blame] | 4162 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4163 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4164 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4165 | return result; |
| 4166 | } |
| 4167 | #endif |
| 4168 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4169 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4170 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4171 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4172 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4173 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4174 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4175 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4176 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4177 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4178 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4179 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4180 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4181 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4182 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4183 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4184 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4185 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4186 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4187 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4188 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4189 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4190 | pid_t pid; |
| 4191 | int sig; |
| 4192 | if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig)) |
| 4193 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4194 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4195 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4196 | APIRET rc; |
| 4197 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4198 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4199 | |
| 4200 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4201 | APIRET rc; |
| 4202 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4203 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4204 | |
| 4205 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4206 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4207 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4208 | if (kill(pid, sig) == -1) |
| 4209 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4210 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4211 | Py_INCREF(Py_None); |
| 4212 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4213 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4214 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4215 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4216 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4217 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4218 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4219 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4220 | |
| 4221 | static PyObject * |
| 4222 | posix_killpg(PyObject *self, PyObject *args) |
| 4223 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4224 | int sig; |
| 4225 | pid_t pgid; |
| 4226 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4227 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4228 | take the same type. Moreover, pid_t is always at least as wide as |
| 4229 | int (else compilation of this module fails), which is safe. */ |
| 4230 | if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig)) |
| 4231 | return NULL; |
| 4232 | if (killpg(pgid, sig) == -1) |
| 4233 | return posix_error(); |
| 4234 | Py_INCREF(Py_None); |
| 4235 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4236 | } |
| 4237 | #endif |
| 4238 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4239 | #ifdef HAVE_PLOCK |
| 4240 | |
| 4241 | #ifdef HAVE_SYS_LOCK_H |
| 4242 | #include <sys/lock.h> |
| 4243 | #endif |
| 4244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4245 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4246 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4247 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4248 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4249 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4250 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4251 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4252 | int op; |
| 4253 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 4254 | return NULL; |
| 4255 | if (plock(op) == -1) |
| 4256 | return posix_error(); |
| 4257 | Py_INCREF(Py_None); |
| 4258 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4259 | } |
| 4260 | #endif |
| 4261 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4262 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4263 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4264 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4265 | Set the current process's user id."); |
| 4266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4267 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4268 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4269 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4270 | long uid_arg; |
| 4271 | uid_t uid; |
| 4272 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 4273 | return NULL; |
| 4274 | uid = uid_arg; |
| 4275 | if (uid != uid_arg) { |
| 4276 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4277 | return NULL; |
| 4278 | } |
| 4279 | if (setuid(uid) < 0) |
| 4280 | return posix_error(); |
| 4281 | Py_INCREF(Py_None); |
| 4282 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4283 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4284 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4285 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4286 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4287 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4288 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4289 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4290 | Set the current process's effective user id."); |
| 4291 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4292 | static PyObject * |
| 4293 | posix_seteuid (PyObject *self, PyObject *args) |
| 4294 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4295 | long euid_arg; |
| 4296 | uid_t euid; |
| 4297 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 4298 | return NULL; |
| 4299 | euid = euid_arg; |
| 4300 | if (euid != euid_arg) { |
| 4301 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4302 | return NULL; |
| 4303 | } |
| 4304 | if (seteuid(euid) < 0) { |
| 4305 | return posix_error(); |
| 4306 | } else { |
| 4307 | Py_INCREF(Py_None); |
| 4308 | return Py_None; |
| 4309 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4310 | } |
| 4311 | #endif /* HAVE_SETEUID */ |
| 4312 | |
| 4313 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4314 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4315 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4316 | Set the current process's effective group id."); |
| 4317 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4318 | static PyObject * |
| 4319 | posix_setegid (PyObject *self, PyObject *args) |
| 4320 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4321 | long egid_arg; |
| 4322 | gid_t egid; |
| 4323 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 4324 | return NULL; |
| 4325 | egid = egid_arg; |
| 4326 | if (egid != egid_arg) { |
| 4327 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4328 | return NULL; |
| 4329 | } |
| 4330 | if (setegid(egid) < 0) { |
| 4331 | return posix_error(); |
| 4332 | } else { |
| 4333 | Py_INCREF(Py_None); |
| 4334 | return Py_None; |
| 4335 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4336 | } |
| 4337 | #endif /* HAVE_SETEGID */ |
| 4338 | |
| 4339 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4340 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4341 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4342 | Set the current process's real and effective user ids."); |
| 4343 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4344 | static PyObject * |
| 4345 | posix_setreuid (PyObject *self, PyObject *args) |
| 4346 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4347 | long ruid_arg, euid_arg; |
| 4348 | uid_t ruid, euid; |
| 4349 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 4350 | return NULL; |
| 4351 | if (ruid_arg == -1) |
| 4352 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 4353 | else |
| 4354 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 4355 | if (euid_arg == -1) |
| 4356 | euid = (uid_t)-1; |
| 4357 | else |
| 4358 | euid = euid_arg; |
| 4359 | if ((euid_arg != -1 && euid != euid_arg) || |
| 4360 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 4361 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4362 | return NULL; |
| 4363 | } |
| 4364 | if (setreuid(ruid, euid) < 0) { |
| 4365 | return posix_error(); |
| 4366 | } else { |
| 4367 | Py_INCREF(Py_None); |
| 4368 | return Py_None; |
| 4369 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4370 | } |
| 4371 | #endif /* HAVE_SETREUID */ |
| 4372 | |
| 4373 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4374 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4375 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4376 | Set the current process's real and effective group ids."); |
| 4377 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4378 | static PyObject * |
| 4379 | posix_setregid (PyObject *self, PyObject *args) |
| 4380 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4381 | long rgid_arg, egid_arg; |
| 4382 | gid_t rgid, egid; |
| 4383 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 4384 | return NULL; |
| 4385 | if (rgid_arg == -1) |
| 4386 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 4387 | else |
| 4388 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 4389 | if (egid_arg == -1) |
| 4390 | egid = (gid_t)-1; |
| 4391 | else |
| 4392 | egid = egid_arg; |
| 4393 | if ((egid_arg != -1 && egid != egid_arg) || |
| 4394 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 4395 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4396 | return NULL; |
| 4397 | } |
| 4398 | if (setregid(rgid, egid) < 0) { |
| 4399 | return posix_error(); |
| 4400 | } else { |
| 4401 | Py_INCREF(Py_None); |
| 4402 | return Py_None; |
| 4403 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4404 | } |
| 4405 | #endif /* HAVE_SETREGID */ |
| 4406 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4407 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4408 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4409 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4410 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4411 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4412 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4413 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4414 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4415 | long gid_arg; |
| 4416 | gid_t gid; |
| 4417 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 4418 | return NULL; |
| 4419 | gid = gid_arg; |
| 4420 | if (gid != gid_arg) { |
| 4421 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4422 | return NULL; |
| 4423 | } |
| 4424 | if (setgid(gid) < 0) |
| 4425 | return posix_error(); |
| 4426 | Py_INCREF(Py_None); |
| 4427 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4428 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4429 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4430 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4431 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4432 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4433 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4434 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4435 | |
| 4436 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4437 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4438 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4439 | int i, len; |
| 4440 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4441 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4442 | if (!PySequence_Check(groups)) { |
| 4443 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4444 | return NULL; |
| 4445 | } |
| 4446 | len = PySequence_Size(groups); |
| 4447 | if (len > MAX_GROUPS) { |
| 4448 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4449 | return NULL; |
| 4450 | } |
| 4451 | for(i = 0; i < len; i++) { |
| 4452 | PyObject *elem; |
| 4453 | elem = PySequence_GetItem(groups, i); |
| 4454 | if (!elem) |
| 4455 | return NULL; |
| 4456 | if (!PyLong_Check(elem)) { |
| 4457 | PyErr_SetString(PyExc_TypeError, |
| 4458 | "groups must be integers"); |
| 4459 | Py_DECREF(elem); |
| 4460 | return NULL; |
| 4461 | } else { |
| 4462 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4463 | if (PyErr_Occurred()) { |
| 4464 | PyErr_SetString(PyExc_TypeError, |
| 4465 | "group id too big"); |
| 4466 | Py_DECREF(elem); |
| 4467 | return NULL; |
| 4468 | } |
| 4469 | grouplist[i] = x; |
| 4470 | /* read back the value to see if it fitted in gid_t */ |
| 4471 | if (grouplist[i] != x) { |
| 4472 | PyErr_SetString(PyExc_TypeError, |
| 4473 | "group id too big"); |
| 4474 | Py_DECREF(elem); |
| 4475 | return NULL; |
| 4476 | } |
| 4477 | } |
| 4478 | Py_DECREF(elem); |
| 4479 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4480 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4481 | if (setgroups(len, grouplist) < 0) |
| 4482 | return posix_error(); |
| 4483 | Py_INCREF(Py_None); |
| 4484 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4485 | } |
| 4486 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4487 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4488 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4489 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4490 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4491 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4492 | PyObject *result; |
| 4493 | static PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4494 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4495 | if (pid == -1) |
| 4496 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4497 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4498 | if (struct_rusage == NULL) { |
| 4499 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 4500 | if (m == NULL) |
| 4501 | return NULL; |
| 4502 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4503 | Py_DECREF(m); |
| 4504 | if (struct_rusage == NULL) |
| 4505 | return NULL; |
| 4506 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4507 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4508 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4509 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4510 | if (!result) |
| 4511 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4512 | |
| 4513 | #ifndef doubletime |
| 4514 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4515 | #endif |
| 4516 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4517 | PyStructSequence_SET_ITEM(result, 0, |
| 4518 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4519 | PyStructSequence_SET_ITEM(result, 1, |
| 4520 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4521 | #define SET_INT(result, index, value)\ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4522 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 4523 | SET_INT(result, 2, ru->ru_maxrss); |
| 4524 | SET_INT(result, 3, ru->ru_ixrss); |
| 4525 | SET_INT(result, 4, ru->ru_idrss); |
| 4526 | SET_INT(result, 5, ru->ru_isrss); |
| 4527 | SET_INT(result, 6, ru->ru_minflt); |
| 4528 | SET_INT(result, 7, ru->ru_majflt); |
| 4529 | SET_INT(result, 8, ru->ru_nswap); |
| 4530 | SET_INT(result, 9, ru->ru_inblock); |
| 4531 | SET_INT(result, 10, ru->ru_oublock); |
| 4532 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4533 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4534 | SET_INT(result, 13, ru->ru_nsignals); |
| 4535 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4536 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4537 | #undef SET_INT |
| 4538 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4539 | if (PyErr_Occurred()) { |
| 4540 | Py_DECREF(result); |
| 4541 | return NULL; |
| 4542 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4543 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4544 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4545 | } |
| 4546 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4547 | |
| 4548 | #ifdef HAVE_WAIT3 |
| 4549 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4550 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4551 | Wait for completion of a child process."); |
| 4552 | |
| 4553 | static PyObject * |
| 4554 | posix_wait3(PyObject *self, PyObject *args) |
| 4555 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4556 | pid_t pid; |
| 4557 | int options; |
| 4558 | struct rusage ru; |
| 4559 | WAIT_TYPE status; |
| 4560 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4561 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4562 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4563 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4564 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4565 | Py_BEGIN_ALLOW_THREADS |
| 4566 | pid = wait3(&status, options, &ru); |
| 4567 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4568 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4569 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4570 | } |
| 4571 | #endif /* HAVE_WAIT3 */ |
| 4572 | |
| 4573 | #ifdef HAVE_WAIT4 |
| 4574 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4575 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4576 | Wait for completion of a given child process."); |
| 4577 | |
| 4578 | static PyObject * |
| 4579 | posix_wait4(PyObject *self, PyObject *args) |
| 4580 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4581 | pid_t pid; |
| 4582 | int options; |
| 4583 | struct rusage ru; |
| 4584 | WAIT_TYPE status; |
| 4585 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4586 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4587 | if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options)) |
| 4588 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4589 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4590 | Py_BEGIN_ALLOW_THREADS |
| 4591 | pid = wait4(pid, &status, options, &ru); |
| 4592 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4593 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4594 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4595 | } |
| 4596 | #endif /* HAVE_WAIT4 */ |
| 4597 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4598 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4599 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4600 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4601 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4602 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4603 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4604 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4605 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4606 | pid_t pid; |
| 4607 | int options; |
| 4608 | WAIT_TYPE status; |
| 4609 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4610 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4611 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
| 4612 | return NULL; |
| 4613 | Py_BEGIN_ALLOW_THREADS |
| 4614 | pid = waitpid(pid, &status, options); |
| 4615 | Py_END_ALLOW_THREADS |
| 4616 | if (pid == -1) |
| 4617 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4618 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4619 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4622 | #elif defined(HAVE_CWAIT) |
| 4623 | |
| 4624 | /* 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] | 4625 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4626 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4627 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4628 | |
| 4629 | static PyObject * |
| 4630 | posix_waitpid(PyObject *self, PyObject *args) |
| 4631 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4632 | Py_intptr_t pid; |
| 4633 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4634 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4635 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
| 4636 | return NULL; |
| 4637 | Py_BEGIN_ALLOW_THREADS |
| 4638 | pid = _cwait(&status, pid, options); |
| 4639 | Py_END_ALLOW_THREADS |
| 4640 | if (pid == -1) |
| 4641 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4642 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4643 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4644 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4645 | } |
| 4646 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4647 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4648 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4649 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4650 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4651 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4652 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4653 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4654 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4655 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4656 | pid_t pid; |
| 4657 | WAIT_TYPE status; |
| 4658 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4659 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4660 | Py_BEGIN_ALLOW_THREADS |
| 4661 | pid = wait(&status); |
| 4662 | Py_END_ALLOW_THREADS |
| 4663 | if (pid == -1) |
| 4664 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4665 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4666 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4667 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4668 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4669 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4671 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4672 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4673 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4674 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4675 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4676 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4677 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4678 | #ifdef HAVE_LSTAT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4679 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4680 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4681 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4682 | return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4683 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4684 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4685 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4686 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4689 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4690 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4691 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4692 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4693 | 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] | 4694 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4695 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4696 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4697 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4698 | PyObject* v; |
| 4699 | char buf[MAXPATHLEN]; |
| 4700 | PyObject *opath; |
| 4701 | char *path; |
| 4702 | int n; |
| 4703 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4704 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4705 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 4706 | PyUnicode_FSConverter, &opath)) |
| 4707 | return NULL; |
| 4708 | path = bytes2str(opath, 1); |
| 4709 | v = PySequence_GetItem(args, 0); |
| 4710 | if (v == NULL) { |
| 4711 | release_bytes(opath); |
| 4712 | return NULL; |
| 4713 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4714 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4715 | if (PyUnicode_Check(v)) { |
| 4716 | arg_is_unicode = 1; |
| 4717 | } |
| 4718 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4719 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4720 | Py_BEGIN_ALLOW_THREADS |
| 4721 | n = readlink(path, buf, (int) sizeof buf); |
| 4722 | Py_END_ALLOW_THREADS |
| 4723 | if (n < 0) |
| 4724 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4725 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4726 | release_bytes(opath); |
Victor Stinner | 203406c | 2010-05-14 18:07:39 +0000 | [diff] [blame] | 4727 | v = PyBytes_FromStringAndSize(buf, n); |
| 4728 | if (arg_is_unicode) { |
| 4729 | PyObject *w; |
| 4730 | |
| 4731 | w = PyUnicode_FromEncodedObject(v, |
| 4732 | Py_FileSystemDefaultEncoding, |
| 4733 | "surrogateescape"); |
| 4734 | if (w != NULL) { |
| 4735 | Py_DECREF(v); |
| 4736 | v = w; |
| 4737 | } |
| 4738 | else { |
| 4739 | v = NULL; |
| 4740 | } |
| 4741 | } |
| 4742 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4743 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4744 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4745 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4746 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4747 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4748 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4749 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4750 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4751 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4752 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4753 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4754 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4755 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4756 | } |
| 4757 | #endif /* HAVE_SYMLINK */ |
| 4758 | |
| 4759 | |
| 4760 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4761 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4762 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4763 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4764 | { |
| 4765 | ULONG value = 0; |
| 4766 | |
| 4767 | Py_BEGIN_ALLOW_THREADS |
| 4768 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4769 | Py_END_ALLOW_THREADS |
| 4770 | |
| 4771 | return value; |
| 4772 | } |
| 4773 | |
| 4774 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4775 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4776 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4777 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4778 | return Py_BuildValue("ddddd", |
| 4779 | (double)0 /* t.tms_utime / HZ */, |
| 4780 | (double)0 /* t.tms_stime / HZ */, |
| 4781 | (double)0 /* t.tms_cutime / HZ */, |
| 4782 | (double)0 /* t.tms_cstime / HZ */, |
| 4783 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4784 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4785 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4786 | #define NEED_TICKS_PER_SECOND |
| 4787 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4788 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4789 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4790 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4791 | struct tms t; |
| 4792 | clock_t c; |
| 4793 | errno = 0; |
| 4794 | c = times(&t); |
| 4795 | if (c == (clock_t) -1) |
| 4796 | return posix_error(); |
| 4797 | return Py_BuildValue("ddddd", |
| 4798 | (double)t.tms_utime / ticks_per_second, |
| 4799 | (double)t.tms_stime / ticks_per_second, |
| 4800 | (double)t.tms_cutime / ticks_per_second, |
| 4801 | (double)t.tms_cstime / ticks_per_second, |
| 4802 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4803 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4804 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4805 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4806 | |
| 4807 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4808 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4809 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4810 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4811 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4812 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4813 | FILETIME create, exit, kernel, user; |
| 4814 | HANDLE hProc; |
| 4815 | hProc = GetCurrentProcess(); |
| 4816 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4817 | /* The fields of a FILETIME structure are the hi and lo part |
| 4818 | of a 64-bit value expressed in 100 nanosecond units. |
| 4819 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4820 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4821 | */ |
| 4822 | return Py_BuildValue( |
| 4823 | "ddddd", |
| 4824 | (double)(user.dwHighDateTime*429.4967296 + |
| 4825 | user.dwLowDateTime*1e-7), |
| 4826 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4827 | kernel.dwLowDateTime*1e-7), |
| 4828 | (double)0, |
| 4829 | (double)0, |
| 4830 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4831 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4832 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4833 | |
| 4834 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4835 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4836 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4837 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4838 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4839 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4840 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4841 | #ifdef HAVE_GETSID |
| 4842 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4843 | "getsid(pid) -> sid\n\n\ |
| 4844 | Call the system call getsid()."); |
| 4845 | |
| 4846 | static PyObject * |
| 4847 | posix_getsid(PyObject *self, PyObject *args) |
| 4848 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4849 | pid_t pid; |
| 4850 | int sid; |
| 4851 | if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid)) |
| 4852 | return NULL; |
| 4853 | sid = getsid(pid); |
| 4854 | if (sid < 0) |
| 4855 | return posix_error(); |
| 4856 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4857 | } |
| 4858 | #endif /* HAVE_GETSID */ |
| 4859 | |
| 4860 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4861 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4862 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4863 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4864 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4865 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4866 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4867 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4868 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4869 | if (setsid() < 0) |
| 4870 | return posix_error(); |
| 4871 | Py_INCREF(Py_None); |
| 4872 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4873 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4874 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4875 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4876 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4877 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4878 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4879 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4880 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4881 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4882 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4883 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4884 | pid_t pid; |
| 4885 | int pgrp; |
| 4886 | if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 4887 | return NULL; |
| 4888 | if (setpgid(pid, pgrp) < 0) |
| 4889 | return posix_error(); |
| 4890 | Py_INCREF(Py_None); |
| 4891 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4892 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4893 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4894 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4895 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4896 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4897 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4898 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4899 | 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] | 4900 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4901 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4902 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4903 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4904 | int fd; |
| 4905 | pid_t pgid; |
| 4906 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 4907 | return NULL; |
| 4908 | pgid = tcgetpgrp(fd); |
| 4909 | if (pgid < 0) |
| 4910 | return posix_error(); |
| 4911 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4912 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4913 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4914 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4915 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4916 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4917 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4918 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4919 | 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] | 4920 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4921 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4922 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4923 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4924 | int fd; |
| 4925 | pid_t pgid; |
| 4926 | if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 4927 | return NULL; |
| 4928 | if (tcsetpgrp(fd, pgid) < 0) |
| 4929 | return posix_error(); |
| 4930 | Py_INCREF(Py_None); |
| 4931 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4932 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4933 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4934 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4935 | /* Functions acting on file descriptors */ |
| 4936 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4937 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4938 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4939 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4940 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4941 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4942 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4943 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4944 | PyObject *ofile; |
| 4945 | char *file; |
| 4946 | int flag; |
| 4947 | int mode = 0777; |
| 4948 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4949 | |
| 4950 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4951 | if (unicode_file_names()) { |
| 4952 | PyUnicodeObject *po; |
| 4953 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4954 | Py_BEGIN_ALLOW_THREADS |
| 4955 | /* PyUnicode_AS_UNICODE OK without thread |
| 4956 | lock as it is a simple dereference. */ |
| 4957 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4958 | Py_END_ALLOW_THREADS |
| 4959 | if (fd < 0) |
| 4960 | return posix_error(); |
| 4961 | return PyLong_FromLong((long)fd); |
| 4962 | } |
| 4963 | /* Drop the argument parsing error as narrow strings |
| 4964 | are also valid. */ |
| 4965 | PyErr_Clear(); |
| 4966 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4967 | #endif |
| 4968 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4969 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 4970 | PyUnicode_FSConverter, &ofile, |
| 4971 | &flag, &mode)) |
| 4972 | return NULL; |
| 4973 | file = bytes2str(ofile, 1); |
| 4974 | Py_BEGIN_ALLOW_THREADS |
| 4975 | fd = open(file, flag, mode); |
| 4976 | Py_END_ALLOW_THREADS |
| 4977 | if (fd < 0) |
| 4978 | return posix_error_with_allocated_filename(ofile); |
| 4979 | release_bytes(ofile); |
| 4980 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4983 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4984 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4985 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4986 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4987 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4988 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4989 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4990 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 4991 | int fd, res; |
| 4992 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 4993 | return NULL; |
| 4994 | if (!_PyVerify_fd(fd)) |
| 4995 | return posix_error(); |
| 4996 | Py_BEGIN_ALLOW_THREADS |
| 4997 | res = close(fd); |
| 4998 | Py_END_ALLOW_THREADS |
| 4999 | if (res < 0) |
| 5000 | return posix_error(); |
| 5001 | Py_INCREF(Py_None); |
| 5002 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5005 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5006 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5007 | "closerange(fd_low, fd_high)\n\n\ |
| 5008 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 5009 | |
| 5010 | static PyObject * |
| 5011 | posix_closerange(PyObject *self, PyObject *args) |
| 5012 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5013 | int fd_from, fd_to, i; |
| 5014 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 5015 | return NULL; |
| 5016 | Py_BEGIN_ALLOW_THREADS |
| 5017 | for (i = fd_from; i < fd_to; i++) |
| 5018 | if (_PyVerify_fd(i)) |
| 5019 | close(i); |
| 5020 | Py_END_ALLOW_THREADS |
| 5021 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5026 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5027 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5028 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5029 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5030 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5031 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5032 | int fd; |
| 5033 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 5034 | return NULL; |
| 5035 | if (!_PyVerify_fd(fd)) |
| 5036 | return posix_error(); |
| 5037 | Py_BEGIN_ALLOW_THREADS |
| 5038 | fd = dup(fd); |
| 5039 | Py_END_ALLOW_THREADS |
| 5040 | if (fd < 0) |
| 5041 | return posix_error(); |
| 5042 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5045 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5046 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5047 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5048 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5049 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5050 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5051 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5052 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5053 | int fd, fd2, res; |
| 5054 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 5055 | return NULL; |
| 5056 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 5057 | return posix_error(); |
| 5058 | Py_BEGIN_ALLOW_THREADS |
| 5059 | res = dup2(fd, fd2); |
| 5060 | Py_END_ALLOW_THREADS |
| 5061 | if (res < 0) |
| 5062 | return posix_error(); |
| 5063 | Py_INCREF(Py_None); |
| 5064 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5068 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5069 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5070 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5071 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5072 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5073 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5074 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5075 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5076 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5077 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5078 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5079 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5080 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5081 | PyObject *posobj; |
| 5082 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
| 5083 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5084 | #ifdef SEEK_SET |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5085 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5086 | switch (how) { |
| 5087 | case 0: how = SEEK_SET; break; |
| 5088 | case 1: how = SEEK_CUR; break; |
| 5089 | case 2: how = SEEK_END; break; |
| 5090 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5091 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5092 | |
| 5093 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5094 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5095 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5096 | pos = PyLong_Check(posobj) ? |
| 5097 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5098 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5099 | if (PyErr_Occurred()) |
| 5100 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5101 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5102 | if (!_PyVerify_fd(fd)) |
| 5103 | return posix_error(); |
| 5104 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5105 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5106 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5107 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5108 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5109 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5110 | Py_END_ALLOW_THREADS |
| 5111 | if (res < 0) |
| 5112 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5113 | |
| 5114 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5115 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5116 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5117 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5118 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5122 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5123 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5124 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5125 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5126 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5127 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5128 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5129 | int fd, size; |
| 5130 | Py_ssize_t n; |
| 5131 | PyObject *buffer; |
| 5132 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 5133 | return NULL; |
| 5134 | if (size < 0) { |
| 5135 | errno = EINVAL; |
| 5136 | return posix_error(); |
| 5137 | } |
| 5138 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 5139 | if (buffer == NULL) |
| 5140 | return NULL; |
| 5141 | if (!_PyVerify_fd(fd)) |
| 5142 | return posix_error(); |
| 5143 | Py_BEGIN_ALLOW_THREADS |
| 5144 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 5145 | Py_END_ALLOW_THREADS |
| 5146 | if (n < 0) { |
| 5147 | Py_DECREF(buffer); |
| 5148 | return posix_error(); |
| 5149 | } |
| 5150 | if (n != size) |
| 5151 | _PyBytes_Resize(&buffer, n); |
| 5152 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5156 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5157 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5158 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5159 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5160 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5161 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5162 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5163 | Py_buffer pbuf; |
| 5164 | int fd; |
| 5165 | Py_ssize_t size; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5166 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5167 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 5168 | return NULL; |
| 5169 | if (!_PyVerify_fd(fd)) |
| 5170 | return posix_error(); |
| 5171 | Py_BEGIN_ALLOW_THREADS |
| 5172 | size = write(fd, pbuf.buf, (size_t)pbuf.len); |
| 5173 | Py_END_ALLOW_THREADS |
| 5174 | PyBuffer_Release(&pbuf); |
| 5175 | if (size < 0) |
| 5176 | return posix_error(); |
| 5177 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5180 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5181 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5182 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5183 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5185 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5186 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5187 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5188 | int fd; |
| 5189 | STRUCT_STAT st; |
| 5190 | int res; |
| 5191 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 5192 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5193 | #ifdef __VMS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5194 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5195 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5196 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5197 | if (!_PyVerify_fd(fd)) |
| 5198 | return posix_error(); |
| 5199 | Py_BEGIN_ALLOW_THREADS |
| 5200 | res = FSTAT(fd, &st); |
| 5201 | Py_END_ALLOW_THREADS |
| 5202 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5203 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5204 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5205 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5206 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5207 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5208 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5209 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5210 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5213 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5214 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5215 | 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] | 5216 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5217 | |
| 5218 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5219 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5220 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5221 | int fd; |
| 5222 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5223 | return NULL; |
| 5224 | if (!_PyVerify_fd(fd)) |
| 5225 | return PyBool_FromLong(0); |
| 5226 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5227 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5228 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5229 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5230 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5231 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5232 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5233 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5234 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5235 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5236 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5237 | #if defined(PYOS_OS2) |
| 5238 | HFILE read, write; |
| 5239 | APIRET rc; |
| 5240 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5241 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5242 | rc = DosCreatePipe( &read, &write, 4096); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5243 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5244 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5245 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5246 | |
| 5247 | return Py_BuildValue("(ii)", read, write); |
| 5248 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5249 | #if !defined(MS_WINDOWS) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5250 | int fds[2]; |
| 5251 | int res; |
| 5252 | Py_BEGIN_ALLOW_THREADS |
| 5253 | res = pipe(fds); |
| 5254 | Py_END_ALLOW_THREADS |
| 5255 | if (res != 0) |
| 5256 | return posix_error(); |
| 5257 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5258 | #else /* MS_WINDOWS */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5259 | HANDLE read, write; |
| 5260 | int read_fd, write_fd; |
| 5261 | BOOL ok; |
| 5262 | Py_BEGIN_ALLOW_THREADS |
| 5263 | ok = CreatePipe(&read, &write, NULL, 0); |
| 5264 | Py_END_ALLOW_THREADS |
| 5265 | if (!ok) |
| 5266 | return win32_error("CreatePipe", NULL); |
| 5267 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5268 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 5269 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5270 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5271 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5272 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5273 | #endif /* HAVE_PIPE */ |
| 5274 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5275 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5276 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5277 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5278 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5279 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5280 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5281 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5282 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5283 | { |
Benjamin Peterson | 9ecbc07 | 2010-08-11 19:24:27 +0000 | [diff] [blame] | 5284 | PyObject *opath; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5285 | char *filename; |
| 5286 | int mode = 0666; |
| 5287 | int res; |
Benjamin Peterson | 9ecbc07 | 2010-08-11 19:24:27 +0000 | [diff] [blame] | 5288 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 5289 | &mode)) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5290 | return NULL; |
Benjamin Peterson | 9ecbc07 | 2010-08-11 19:24:27 +0000 | [diff] [blame] | 5291 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5292 | Py_BEGIN_ALLOW_THREADS |
| 5293 | res = mkfifo(filename, mode); |
| 5294 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 9ecbc07 | 2010-08-11 19:24:27 +0000 | [diff] [blame] | 5295 | Py_DECREF(opath); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5296 | if (res < 0) |
| 5297 | return posix_error(); |
| 5298 | Py_INCREF(Py_None); |
| 5299 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5300 | } |
| 5301 | #endif |
| 5302 | |
| 5303 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5304 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5305 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5306 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5307 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5308 | named filename. mode specifies both the permissions to use and the\n\ |
| 5309 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5310 | 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] | 5311 | device defines the newly created device special file (probably using\n\ |
| 5312 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5313 | |
| 5314 | |
| 5315 | static PyObject * |
| 5316 | posix_mknod(PyObject *self, PyObject *args) |
| 5317 | { |
Benjamin Peterson | 9ecbc07 | 2010-08-11 19:24:27 +0000 | [diff] [blame] | 5318 | PyObject *opath; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5319 | char *filename; |
| 5320 | int mode = 0600; |
| 5321 | int device = 0; |
| 5322 | int res; |
Benjamin Peterson | 9ecbc07 | 2010-08-11 19:24:27 +0000 | [diff] [blame] | 5323 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 5324 | &mode, &device)) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5325 | return NULL; |
Benjamin Peterson | 9ecbc07 | 2010-08-11 19:24:27 +0000 | [diff] [blame] | 5326 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5327 | Py_BEGIN_ALLOW_THREADS |
| 5328 | res = mknod(filename, mode, device); |
| 5329 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 9ecbc07 | 2010-08-11 19:24:27 +0000 | [diff] [blame] | 5330 | Py_DECREF(opath); |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5331 | if (res < 0) |
| 5332 | return posix_error(); |
| 5333 | Py_INCREF(Py_None); |
| 5334 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5335 | } |
| 5336 | #endif |
| 5337 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5338 | #ifdef HAVE_DEVICE_MACROS |
| 5339 | PyDoc_STRVAR(posix_major__doc__, |
| 5340 | "major(device) -> major number\n\ |
| 5341 | Extracts a device major number from a raw device number."); |
| 5342 | |
| 5343 | static PyObject * |
| 5344 | posix_major(PyObject *self, PyObject *args) |
| 5345 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5346 | int device; |
| 5347 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5348 | return NULL; |
| 5349 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | PyDoc_STRVAR(posix_minor__doc__, |
| 5353 | "minor(device) -> minor number\n\ |
| 5354 | Extracts a device minor number from a raw device number."); |
| 5355 | |
| 5356 | static PyObject * |
| 5357 | posix_minor(PyObject *self, PyObject *args) |
| 5358 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5359 | int device; |
| 5360 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5361 | return NULL; |
| 5362 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5366 | "makedev(major, minor) -> device number\n\ |
| 5367 | Composes a raw device number from the major and minor device numbers."); |
| 5368 | |
| 5369 | static PyObject * |
| 5370 | posix_makedev(PyObject *self, PyObject *args) |
| 5371 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5372 | int major, minor; |
| 5373 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5374 | return NULL; |
| 5375 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5376 | } |
| 5377 | #endif /* device macros */ |
| 5378 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5379 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5380 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5381 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5382 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5383 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5384 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5385 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5386 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5387 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5388 | int fd; |
| 5389 | off_t length; |
| 5390 | int res; |
| 5391 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5392 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5393 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
| 5394 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5395 | |
| 5396 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5397 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5398 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5399 | length = PyLong_Check(lenobj) ? |
| 5400 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5401 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5402 | if (PyErr_Occurred()) |
| 5403 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5404 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5405 | Py_BEGIN_ALLOW_THREADS |
| 5406 | res = ftruncate(fd, length); |
| 5407 | Py_END_ALLOW_THREADS |
| 5408 | if (res < 0) |
| 5409 | return posix_error(); |
| 5410 | Py_INCREF(Py_None); |
| 5411 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5412 | } |
| 5413 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5414 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5415 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5416 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5417 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5418 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5419 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5420 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5421 | * get re-set with another call for the same key. */ |
| 5422 | static PyObject *posix_putenv_garbage; |
| 5423 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5424 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5425 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5426 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5427 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5428 | wchar_t *s1, *s2; |
| 5429 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5430 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5431 | PyObject *os1, *os2; |
| 5432 | char *s1, *s2; |
| 5433 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5434 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5435 | PyObject *newstr; |
| 5436 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5437 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5438 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5439 | if (!PyArg_ParseTuple(args, |
| 5440 | "uu:putenv", |
| 5441 | &s1, &s2)) |
| 5442 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5443 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5444 | if (!PyArg_ParseTuple(args, |
| 5445 | "O&O&:putenv", |
| 5446 | PyUnicode_FSConverter, &os1, |
| 5447 | PyUnicode_FSConverter, &os2)) |
| 5448 | return NULL; |
| 5449 | s1 = bytes2str(os1, 1); |
| 5450 | s2 = bytes2str(os2, 1); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5451 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5452 | |
| 5453 | #if defined(PYOS_OS2) |
| 5454 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5455 | APIRET rc; |
| 5456 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5457 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5458 | if (rc != NO_ERROR) |
| 5459 | return os2_error(rc); |
| 5460 | |
| 5461 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5462 | APIRET rc; |
| 5463 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5464 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5465 | if (rc != NO_ERROR) |
| 5466 | return os2_error(rc); |
| 5467 | } else { |
| 5468 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5469 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 5470 | /* len includes space for a trailing \0; the size arg to |
| 5471 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5472 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5473 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5474 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5475 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5476 | len = strlen(s1) + strlen(s2) + 2; |
| 5477 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5478 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5479 | if (newstr == NULL) |
| 5480 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5481 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5482 | newenv = PyUnicode_AsUnicode(newstr); |
| 5483 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5484 | if (_wputenv(newenv)) { |
| 5485 | Py_DECREF(newstr); |
| 5486 | posix_error(); |
| 5487 | return NULL; |
| 5488 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5489 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5490 | newenv = PyBytes_AS_STRING(newstr); |
| 5491 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5492 | if (putenv(newenv)) { |
| 5493 | Py_DECREF(newstr); |
| 5494 | release_bytes(os1); |
| 5495 | release_bytes(os2); |
| 5496 | posix_error(); |
| 5497 | return NULL; |
| 5498 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5499 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5500 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5501 | * this will cause previous value to be collected. This has to |
| 5502 | * happen after the real putenv() call because the old value |
| 5503 | * was still accessible until then. */ |
| 5504 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5505 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5506 | /* really not much we can do; just leak */ |
| 5507 | PyErr_Clear(); |
| 5508 | } |
| 5509 | else { |
| 5510 | Py_DECREF(newstr); |
| 5511 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5512 | |
| 5513 | #if defined(PYOS_OS2) |
| 5514 | } |
| 5515 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5516 | #ifndef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5517 | release_bytes(os1); |
| 5518 | release_bytes(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5519 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5520 | Py_INCREF(Py_None); |
| 5521 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5522 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5523 | #endif /* putenv */ |
| 5524 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5525 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5526 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5527 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5528 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5529 | |
| 5530 | static PyObject * |
| 5531 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5532 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5533 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5534 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5535 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5536 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5537 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5538 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5539 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5540 | /* Remove the key from posix_putenv_garbage; |
| 5541 | * this will cause it to be collected. This has to |
| 5542 | * happen after the real unsetenv() call because the |
| 5543 | * old value was still accessible until then. |
| 5544 | */ |
| 5545 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5546 | PyTuple_GET_ITEM(args, 0))) { |
| 5547 | /* really not much we can do; just leak */ |
| 5548 | PyErr_Clear(); |
| 5549 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5550 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5551 | Py_INCREF(Py_None); |
| 5552 | return Py_None; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5553 | } |
| 5554 | #endif /* unsetenv */ |
| 5555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5556 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5557 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5558 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5559 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5560 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5561 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5562 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5563 | int code; |
| 5564 | char *message; |
| 5565 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 5566 | return NULL; |
| 5567 | message = strerror(code); |
| 5568 | if (message == NULL) { |
| 5569 | PyErr_SetString(PyExc_ValueError, |
| 5570 | "strerror() argument out of range"); |
| 5571 | return NULL; |
| 5572 | } |
| 5573 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5574 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5575 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5576 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5577 | #ifdef HAVE_SYS_WAIT_H |
| 5578 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5579 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5580 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5581 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5582 | 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] | 5583 | |
| 5584 | static PyObject * |
| 5585 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5586 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5587 | WAIT_TYPE status; |
| 5588 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5589 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5590 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 5591 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5592 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5593 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5594 | } |
| 5595 | #endif /* WCOREDUMP */ |
| 5596 | |
| 5597 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5598 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5599 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5600 | 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] | 5601 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5602 | |
| 5603 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5604 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5605 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5606 | WAIT_TYPE status; |
| 5607 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5608 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5609 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 5610 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5611 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5612 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5613 | } |
| 5614 | #endif /* WIFCONTINUED */ |
| 5615 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5616 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5617 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5618 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5619 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5620 | |
| 5621 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5622 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5623 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5624 | WAIT_TYPE status; |
| 5625 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5626 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5627 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 5628 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5629 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5630 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5631 | } |
| 5632 | #endif /* WIFSTOPPED */ |
| 5633 | |
| 5634 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5635 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5636 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5637 | 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] | 5638 | |
| 5639 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5640 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5641 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5642 | WAIT_TYPE status; |
| 5643 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5644 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5645 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 5646 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5647 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5648 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5649 | } |
| 5650 | #endif /* WIFSIGNALED */ |
| 5651 | |
| 5652 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5653 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5654 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5655 | 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] | 5656 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5657 | |
| 5658 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5659 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5660 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5661 | WAIT_TYPE status; |
| 5662 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5663 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5664 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 5665 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5666 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5667 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5668 | } |
| 5669 | #endif /* WIFEXITED */ |
| 5670 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5671 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5672 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5673 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5674 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5675 | |
| 5676 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5677 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5678 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5679 | WAIT_TYPE status; |
| 5680 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5681 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5682 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 5683 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5684 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5685 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5686 | } |
| 5687 | #endif /* WEXITSTATUS */ |
| 5688 | |
| 5689 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5690 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5691 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5692 | 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] | 5693 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5694 | |
| 5695 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5696 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5697 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5698 | WAIT_TYPE status; |
| 5699 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5700 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5701 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 5702 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5703 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5704 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5705 | } |
| 5706 | #endif /* WTERMSIG */ |
| 5707 | |
| 5708 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5709 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5710 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5711 | Return the signal that stopped the process that provided\n\ |
| 5712 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5713 | |
| 5714 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5715 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5716 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5717 | WAIT_TYPE status; |
| 5718 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5719 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5720 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 5721 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5722 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5723 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5724 | } |
| 5725 | #endif /* WSTOPSIG */ |
| 5726 | |
| 5727 | #endif /* HAVE_SYS_WAIT_H */ |
| 5728 | |
| 5729 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5730 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5731 | #ifdef _SCO_DS |
| 5732 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5733 | needed definitions in sys/statvfs.h */ |
| 5734 | #define _SVID3 |
| 5735 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5736 | #include <sys/statvfs.h> |
| 5737 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5738 | static PyObject* |
| 5739 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5740 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5741 | if (v == NULL) |
| 5742 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5743 | |
| 5744 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5745 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5746 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5747 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5748 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5749 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5750 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5751 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5752 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 5753 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5754 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5755 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5756 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5757 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5758 | PyStructSequence_SET_ITEM(v, 2, |
| 5759 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 5760 | PyStructSequence_SET_ITEM(v, 3, |
| 5761 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 5762 | PyStructSequence_SET_ITEM(v, 4, |
| 5763 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 5764 | PyStructSequence_SET_ITEM(v, 5, |
| 5765 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 5766 | PyStructSequence_SET_ITEM(v, 6, |
| 5767 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 5768 | PyStructSequence_SET_ITEM(v, 7, |
| 5769 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 5770 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5771 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5772 | #endif |
| 5773 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5774 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5775 | } |
| 5776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5777 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5778 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5779 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5780 | |
| 5781 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5782 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5783 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5784 | int fd, res; |
| 5785 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5786 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5787 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 5788 | return NULL; |
| 5789 | Py_BEGIN_ALLOW_THREADS |
| 5790 | res = fstatvfs(fd, &st); |
| 5791 | Py_END_ALLOW_THREADS |
| 5792 | if (res != 0) |
| 5793 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5794 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5795 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5796 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5797 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5798 | |
| 5799 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5800 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5801 | #include <sys/statvfs.h> |
| 5802 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5803 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5804 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5805 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5806 | |
| 5807 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5808 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5809 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5810 | char *path; |
| 5811 | int res; |
| 5812 | struct statvfs st; |
| 5813 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
| 5814 | return NULL; |
| 5815 | Py_BEGIN_ALLOW_THREADS |
| 5816 | res = statvfs(path, &st); |
| 5817 | Py_END_ALLOW_THREADS |
| 5818 | if (res != 0) |
| 5819 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5820 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5821 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5822 | } |
| 5823 | #endif /* HAVE_STATVFS */ |
| 5824 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5825 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5826 | * It maps strings representing configuration variable names to |
| 5827 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5828 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5829 | * rarely-used constants. There are three separate tables that use |
| 5830 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5831 | * |
| 5832 | * This code is always included, even if none of the interfaces that |
| 5833 | * need it are included. The #if hackery needed to avoid it would be |
| 5834 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5835 | */ |
| 5836 | struct constdef { |
| 5837 | char *name; |
| 5838 | long value; |
| 5839 | }; |
| 5840 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5841 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5842 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5843 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5844 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5845 | if (PyLong_Check(arg)) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5846 | *valuep = PyLong_AS_LONG(arg); |
| 5847 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5848 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5849 | else { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5850 | /* look up the value in the table using a binary search */ |
| 5851 | size_t lo = 0; |
| 5852 | size_t mid; |
| 5853 | size_t hi = tablesize; |
| 5854 | int cmp; |
| 5855 | const char *confname; |
| 5856 | if (!PyUnicode_Check(arg)) { |
| 5857 | PyErr_SetString(PyExc_TypeError, |
| 5858 | "configuration names must be strings or integers"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5859 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5860 | } |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5861 | confname = _PyUnicode_AsString(arg); |
| 5862 | if (confname == NULL) |
| 5863 | return 0; |
| 5864 | while (lo < hi) { |
| 5865 | mid = (lo + hi) / 2; |
| 5866 | cmp = strcmp(confname, table[mid].name); |
| 5867 | if (cmp < 0) |
| 5868 | hi = mid; |
| 5869 | else if (cmp > 0) |
| 5870 | lo = mid + 1; |
| 5871 | else { |
| 5872 | *valuep = table[mid].value; |
| 5873 | return 1; |
| 5874 | } |
| 5875 | } |
| 5876 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 5877 | return 0; |
| 5878 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5879 | } |
| 5880 | |
| 5881 | |
| 5882 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5883 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5884 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5885 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5886 | #endif |
| 5887 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5888 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5889 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5890 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5891 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5892 | #endif |
| 5893 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5894 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5895 | #endif |
| 5896 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5897 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5898 | #endif |
| 5899 | #ifdef _PC_LAST |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5900 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5901 | #endif |
| 5902 | #ifdef _PC_LINK_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5903 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5904 | #endif |
| 5905 | #ifdef _PC_MAX_CANON |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5906 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5907 | #endif |
| 5908 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5909 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5910 | #endif |
| 5911 | #ifdef _PC_NAME_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5912 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5913 | #endif |
| 5914 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5915 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5916 | #endif |
| 5917 | #ifdef _PC_PATH_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5918 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5919 | #endif |
| 5920 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5921 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5922 | #endif |
| 5923 | #ifdef _PC_PRIO_IO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5924 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5925 | #endif |
| 5926 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5927 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5928 | #endif |
| 5929 | #ifdef _PC_SYNC_IO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5930 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5931 | #endif |
| 5932 | #ifdef _PC_VDISABLE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5933 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5934 | #endif |
| 5935 | }; |
| 5936 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5937 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5938 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5939 | { |
| 5940 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5941 | sizeof(posix_constants_pathconf) |
| 5942 | / sizeof(struct constdef)); |
| 5943 | } |
| 5944 | #endif |
| 5945 | |
| 5946 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5947 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5948 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5949 | 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] | 5950 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5951 | |
| 5952 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5953 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5954 | { |
| 5955 | PyObject *result = NULL; |
| 5956 | int name, fd; |
| 5957 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5958 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5959 | conv_path_confname, &name)) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5960 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5961 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5962 | errno = 0; |
| 5963 | limit = fpathconf(fd, name); |
| 5964 | if (limit == -1 && errno != 0) |
| 5965 | posix_error(); |
| 5966 | else |
| 5967 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5968 | } |
| 5969 | return result; |
| 5970 | } |
| 5971 | #endif |
| 5972 | |
| 5973 | |
| 5974 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5975 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5976 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5977 | 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] | 5978 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5979 | |
| 5980 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5981 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5982 | { |
| 5983 | PyObject *result = NULL; |
| 5984 | int name; |
| 5985 | char *path; |
| 5986 | |
| 5987 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5988 | conv_path_confname, &name)) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5989 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5990 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5991 | errno = 0; |
| 5992 | limit = pathconf(path, name); |
| 5993 | if (limit == -1 && errno != 0) { |
| 5994 | if (errno == EINVAL) |
| 5995 | /* could be a path or name problem */ |
| 5996 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5997 | else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 5998 | posix_error_with_filename(path); |
| 5999 | } |
| 6000 | else |
| 6001 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6002 | } |
| 6003 | return result; |
| 6004 | } |
| 6005 | #endif |
| 6006 | |
| 6007 | #ifdef HAVE_CONFSTR |
| 6008 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6009 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6010 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6011 | #endif |
Mark Dickinson | 466e926 | 2010-04-16 16:32:49 +0000 | [diff] [blame] | 6012 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6013 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 466e926 | 2010-04-16 16:32:49 +0000 | [diff] [blame] | 6014 | #endif |
| 6015 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6016 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 466e926 | 2010-04-16 16:32:49 +0000 | [diff] [blame] | 6017 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6018 | #ifdef _CS_HOSTNAME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6019 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6020 | #endif |
| 6021 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6022 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6023 | #endif |
| 6024 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6025 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6026 | #endif |
| 6027 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6028 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6029 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6030 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6031 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6032 | #endif |
| 6033 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6034 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6035 | #endif |
| 6036 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6037 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6038 | #endif |
| 6039 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6040 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6041 | #endif |
| 6042 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6043 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6044 | #endif |
| 6045 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6046 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6047 | #endif |
| 6048 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6049 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6050 | #endif |
| 6051 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6052 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6053 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6054 | #ifdef _CS_MACHINE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6055 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6056 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6057 | #ifdef _CS_PATH |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6058 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6059 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6060 | #ifdef _CS_RELEASE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6061 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6062 | #endif |
| 6063 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6064 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6065 | #endif |
| 6066 | #ifdef _CS_SYSNAME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6067 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6068 | #endif |
| 6069 | #ifdef _CS_VERSION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6070 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6071 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6072 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6073 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6074 | #endif |
| 6075 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6076 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6077 | #endif |
| 6078 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6079 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6080 | #endif |
| 6081 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6082 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6083 | #endif |
| 6084 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6085 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6086 | #endif |
| 6087 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6088 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6089 | #endif |
| 6090 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6091 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6092 | #endif |
| 6093 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6094 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6095 | #endif |
| 6096 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6097 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6098 | #endif |
| 6099 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6100 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6101 | #endif |
| 6102 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6103 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6104 | #endif |
| 6105 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6106 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6107 | #endif |
| 6108 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6109 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6110 | #endif |
| 6111 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6112 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6113 | #endif |
| 6114 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6115 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6116 | #endif |
| 6117 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6118 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6119 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6120 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6121 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6122 | #endif |
| 6123 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6124 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6125 | #endif |
| 6126 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6127 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6128 | #endif |
| 6129 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6130 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6131 | #endif |
| 6132 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6133 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6134 | #endif |
| 6135 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6136 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6137 | #endif |
| 6138 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6139 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6140 | #endif |
| 6141 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6142 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6143 | #endif |
| 6144 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6145 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6146 | #endif |
| 6147 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6148 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6149 | #endif |
| 6150 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6151 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6152 | #endif |
| 6153 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6154 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6155 | #endif |
| 6156 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6157 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6158 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6159 | }; |
| 6160 | |
| 6161 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6162 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6163 | { |
| 6164 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6165 | sizeof(posix_constants_confstr) |
| 6166 | / sizeof(struct constdef)); |
| 6167 | } |
| 6168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6169 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6170 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6171 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6172 | |
| 6173 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6174 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6175 | { |
| 6176 | PyObject *result = NULL; |
| 6177 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6178 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6179 | |
| 6180 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6181 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6182 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6183 | errno = 0; |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6184 | len = confstr(name, buffer, sizeof(buffer)); |
| 6185 | if (len == 0) { |
| 6186 | if (errno) { |
| 6187 | posix_error(); |
| 6188 | } |
| 6189 | else { |
| 6190 | result = Py_None; |
| 6191 | Py_INCREF(Py_None); |
| 6192 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6193 | } |
| 6194 | else { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6195 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6196 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6197 | if (result != NULL) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 6198 | confstr(name, _PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6199 | } |
| 6200 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6201 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6202 | } |
| 6203 | } |
| 6204 | return result; |
| 6205 | } |
| 6206 | #endif |
| 6207 | |
| 6208 | |
| 6209 | #ifdef HAVE_SYSCONF |
| 6210 | static struct constdef posix_constants_sysconf[] = { |
| 6211 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6212 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6213 | #endif |
| 6214 | #ifdef _SC_2_C_BIND |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6215 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6216 | #endif |
| 6217 | #ifdef _SC_2_C_DEV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6218 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6219 | #endif |
| 6220 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6221 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6222 | #endif |
| 6223 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6224 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6225 | #endif |
| 6226 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6227 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6228 | #endif |
| 6229 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6230 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6231 | #endif |
| 6232 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6233 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6234 | #endif |
| 6235 | #ifdef _SC_2_UPE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6236 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6237 | #endif |
| 6238 | #ifdef _SC_2_VERSION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6239 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6240 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6241 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6242 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6243 | #endif |
| 6244 | #ifdef _SC_ACL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6245 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6246 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6247 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6248 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6249 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6250 | #ifdef _SC_AIO_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6251 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6252 | #endif |
| 6253 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6254 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6255 | #endif |
| 6256 | #ifdef _SC_ARG_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6257 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6258 | #endif |
| 6259 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6260 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6261 | #endif |
| 6262 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6263 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6264 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6265 | #ifdef _SC_AUDIT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6266 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6267 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6268 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6269 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6270 | #endif |
| 6271 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6272 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6273 | #endif |
| 6274 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6275 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6276 | #endif |
| 6277 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6278 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6279 | #endif |
| 6280 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6281 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6282 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6283 | #ifdef _SC_CAP |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6284 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6285 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6286 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6287 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6288 | #endif |
| 6289 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6290 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6291 | #endif |
| 6292 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6293 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6294 | #endif |
| 6295 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6296 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6297 | #endif |
| 6298 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6299 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6300 | #endif |
| 6301 | #ifdef _SC_CLK_TCK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6302 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6303 | #endif |
| 6304 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6305 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6306 | #endif |
| 6307 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6308 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6309 | #endif |
| 6310 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6311 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6312 | #endif |
| 6313 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6314 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6315 | #endif |
| 6316 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6317 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6318 | #endif |
| 6319 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6320 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6321 | #endif |
| 6322 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6323 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6324 | #endif |
| 6325 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6326 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6327 | #endif |
| 6328 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6329 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6330 | #endif |
| 6331 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6332 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6333 | #endif |
| 6334 | #ifdef _SC_FSYNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6335 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6336 | #endif |
| 6337 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6338 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6339 | #endif |
| 6340 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6341 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6342 | #endif |
| 6343 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6344 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6345 | #endif |
| 6346 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6347 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6348 | #endif |
| 6349 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6350 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6351 | #endif |
| 6352 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6353 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6354 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6355 | #ifdef _SC_INF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6356 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6357 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6358 | #ifdef _SC_INT_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6359 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6360 | #endif |
| 6361 | #ifdef _SC_INT_MIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6362 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6363 | #endif |
| 6364 | #ifdef _SC_IOV_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6365 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6366 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6367 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6368 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6369 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6370 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6371 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6372 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6373 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6374 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6375 | #endif |
| 6376 | #ifdef _SC_KERN_SIM |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6377 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6378 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6379 | #ifdef _SC_LINE_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6380 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6381 | #endif |
| 6382 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6383 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6384 | #endif |
| 6385 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6386 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6387 | #endif |
| 6388 | #ifdef _SC_LONG_BIT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6389 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6390 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6391 | #ifdef _SC_MAC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6392 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6393 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6394 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6395 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6396 | #endif |
| 6397 | #ifdef _SC_MAXPID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6398 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6399 | #endif |
| 6400 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6401 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6402 | #endif |
| 6403 | #ifdef _SC_MEMLOCK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6404 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6405 | #endif |
| 6406 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6407 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6408 | #endif |
| 6409 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6410 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6411 | #endif |
| 6412 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6413 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6414 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6415 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6416 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6417 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6418 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6419 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6420 | #endif |
| 6421 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6422 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6423 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6424 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6425 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6426 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6427 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6428 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6429 | #endif |
| 6430 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6431 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6432 | #endif |
| 6433 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6434 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6435 | #endif |
| 6436 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6437 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6438 | #endif |
| 6439 | #ifdef _SC_NL_NMAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6440 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6441 | #endif |
| 6442 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6443 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6444 | #endif |
| 6445 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6446 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6447 | #endif |
| 6448 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6449 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6450 | #endif |
| 6451 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6452 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6453 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6454 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6455 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6456 | #endif |
| 6457 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6458 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6459 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6460 | #ifdef _SC_NZERO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6461 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6462 | #endif |
| 6463 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6464 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6465 | #endif |
| 6466 | #ifdef _SC_PAGESIZE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6467 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6468 | #endif |
| 6469 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6470 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6471 | #endif |
| 6472 | #ifdef _SC_PASS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6473 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6474 | #endif |
| 6475 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6476 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6477 | #endif |
| 6478 | #ifdef _SC_PII |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6479 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6480 | #endif |
| 6481 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6482 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6483 | #endif |
| 6484 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6485 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6486 | #endif |
| 6487 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6488 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6489 | #endif |
| 6490 | #ifdef _SC_PII_OSI |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6491 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6492 | #endif |
| 6493 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6494 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6495 | #endif |
| 6496 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6497 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6498 | #endif |
| 6499 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6500 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6501 | #endif |
| 6502 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6503 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6504 | #endif |
| 6505 | #ifdef _SC_PII_XTI |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6506 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6507 | #endif |
| 6508 | #ifdef _SC_POLL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6509 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6510 | #endif |
| 6511 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6512 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6513 | #endif |
| 6514 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6515 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6516 | #endif |
| 6517 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6518 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6519 | #endif |
| 6520 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6521 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6522 | #endif |
| 6523 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6524 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6525 | #endif |
| 6526 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6527 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6528 | #endif |
| 6529 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6530 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6531 | #endif |
| 6532 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6533 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6534 | #endif |
| 6535 | #ifdef _SC_SELECT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6536 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6537 | #endif |
| 6538 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6539 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6540 | #endif |
| 6541 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6542 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6543 | #endif |
| 6544 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6545 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6546 | #endif |
| 6547 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6548 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6549 | #endif |
| 6550 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6551 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6552 | #endif |
| 6553 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6554 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6555 | #endif |
| 6556 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6557 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6558 | #endif |
| 6559 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6560 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6561 | #endif |
| 6562 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6563 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6564 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6565 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6566 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6567 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6568 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6569 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6570 | #endif |
| 6571 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6572 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6573 | #endif |
| 6574 | #ifdef _SC_STACK_PROT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6575 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6576 | #endif |
| 6577 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6578 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6579 | #endif |
| 6580 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6581 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6582 | #endif |
| 6583 | #ifdef _SC_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6584 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6585 | #endif |
| 6586 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6587 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6588 | #endif |
| 6589 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6590 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6591 | #endif |
| 6592 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6593 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6594 | #endif |
| 6595 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6596 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6597 | #endif |
| 6598 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6599 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6600 | #endif |
| 6601 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6602 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6603 | #endif |
| 6604 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6605 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6606 | #endif |
| 6607 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6608 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6609 | #endif |
| 6610 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6611 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6612 | #endif |
| 6613 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6614 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6615 | #endif |
| 6616 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6617 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6618 | #endif |
| 6619 | #ifdef _SC_TIMERS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6620 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6621 | #endif |
| 6622 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6623 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6624 | #endif |
| 6625 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6626 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6627 | #endif |
| 6628 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6629 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6630 | #endif |
| 6631 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6632 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6633 | #endif |
| 6634 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6635 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6636 | #endif |
| 6637 | #ifdef _SC_UINT_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6638 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6639 | #endif |
| 6640 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6641 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6642 | #endif |
| 6643 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6644 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6645 | #endif |
| 6646 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6647 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6648 | #endif |
| 6649 | #ifdef _SC_VERSION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6650 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6651 | #endif |
| 6652 | #ifdef _SC_WORD_BIT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6653 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6654 | #endif |
| 6655 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6656 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6657 | #endif |
| 6658 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6659 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6660 | #endif |
| 6661 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6662 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6663 | #endif |
| 6664 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6665 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6666 | #endif |
| 6667 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6668 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6669 | #endif |
| 6670 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6671 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6672 | #endif |
| 6673 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6674 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6675 | #endif |
| 6676 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6677 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6678 | #endif |
| 6679 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6680 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6681 | #endif |
| 6682 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6683 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6684 | #endif |
| 6685 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6686 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6687 | #endif |
| 6688 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6689 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6690 | #endif |
| 6691 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6692 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6693 | #endif |
| 6694 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6695 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6696 | #endif |
| 6697 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6698 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6699 | #endif |
| 6700 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6701 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6702 | #endif |
| 6703 | }; |
| 6704 | |
| 6705 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6706 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6707 | { |
| 6708 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6709 | sizeof(posix_constants_sysconf) |
| 6710 | / sizeof(struct constdef)); |
| 6711 | } |
| 6712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6713 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6714 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6715 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6716 | |
| 6717 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6718 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6719 | { |
| 6720 | PyObject *result = NULL; |
| 6721 | int name; |
| 6722 | |
| 6723 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6724 | int value; |
| 6725 | |
| 6726 | errno = 0; |
| 6727 | value = sysconf(name); |
| 6728 | if (value == -1 && errno != 0) |
| 6729 | posix_error(); |
| 6730 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6731 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6732 | } |
| 6733 | return result; |
| 6734 | } |
| 6735 | #endif |
| 6736 | |
| 6737 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6738 | /* This code is used to ensure that the tables of configuration value names |
| 6739 | * are in sorted order as required by conv_confname(), and also to build the |
| 6740 | * the exported dictionaries that are used to publish information about the |
| 6741 | * names available on the host platform. |
| 6742 | * |
| 6743 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6744 | * when used, even for platforms we're not able to test on. It also makes |
| 6745 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6746 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6747 | |
| 6748 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6749 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6750 | { |
| 6751 | const struct constdef *c1 = |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6752 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6753 | const struct constdef *c2 = |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6754 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6755 | |
| 6756 | return strcmp(c1->name, c2->name); |
| 6757 | } |
| 6758 | |
| 6759 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6760 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6761 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6762 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6763 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6764 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6765 | |
| 6766 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6767 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6768 | if (d == NULL) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6769 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6770 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6771 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6772 | PyObject *o = PyLong_FromLong(table[i].value); |
| 6773 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6774 | Py_XDECREF(o); |
| 6775 | Py_DECREF(d); |
| 6776 | return -1; |
| 6777 | } |
| 6778 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6779 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6780 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6781 | } |
| 6782 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6783 | /* Return -1 on failure, 0 on success. */ |
| 6784 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6785 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6786 | { |
| 6787 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6788 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6789 | sizeof(posix_constants_pathconf) |
| 6790 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6791 | "pathconf_names", module)) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6792 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6793 | #endif |
| 6794 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6795 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6796 | sizeof(posix_constants_confstr) |
| 6797 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6798 | "confstr_names", module)) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6799 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6800 | #endif |
| 6801 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6802 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6803 | sizeof(posix_constants_sysconf) |
| 6804 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6805 | "sysconf_names", module)) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6806 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6807 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6808 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6809 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6810 | |
| 6811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6812 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6813 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6814 | 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] | 6815 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6816 | |
| 6817 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6818 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6819 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6820 | abort(); |
| 6821 | /*NOTREACHED*/ |
| 6822 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6823 | return NULL; |
| 6824 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6825 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6826 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6827 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6828 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6829 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6830 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6831 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6832 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6833 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6834 | application (if any) its extension is associated.\n\ |
| 6835 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6836 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6837 | \n\ |
| 6838 | startfile returns as soon as the associated application is launched.\n\ |
| 6839 | There is no option to wait for the application to close, and no way\n\ |
| 6840 | to retrieve the application's exit status.\n\ |
| 6841 | \n\ |
| 6842 | The filepath is relative to the current directory. If you want to use\n\ |
| 6843 | 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] | 6844 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6845 | |
| 6846 | static PyObject * |
| 6847 | win32_startfile(PyObject *self, PyObject *args) |
| 6848 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6849 | PyObject *ofilepath; |
| 6850 | char *filepath; |
| 6851 | char *operation = NULL; |
| 6852 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 6853 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6854 | if (unicode_file_names()) { |
| 6855 | PyObject *unipath, *woperation = NULL; |
| 6856 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6857 | &unipath, &operation)) { |
| 6858 | PyErr_Clear(); |
| 6859 | goto normal; |
| 6860 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6861 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6862 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6863 | if (operation) { |
| 6864 | woperation = PyUnicode_DecodeASCII(operation, |
| 6865 | strlen(operation), NULL); |
| 6866 | if (!woperation) { |
| 6867 | PyErr_Clear(); |
| 6868 | operation = NULL; |
| 6869 | goto normal; |
| 6870 | } |
| 6871 | } |
| 6872 | |
| 6873 | Py_BEGIN_ALLOW_THREADS |
| 6874 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6875 | PyUnicode_AS_UNICODE(unipath), |
| 6876 | NULL, NULL, SW_SHOWNORMAL); |
| 6877 | Py_END_ALLOW_THREADS |
| 6878 | |
| 6879 | Py_XDECREF(woperation); |
| 6880 | if (rc <= (HINSTANCE)32) { |
| 6881 | PyObject *errval = win32_error_unicode("startfile", |
| 6882 | PyUnicode_AS_UNICODE(unipath)); |
| 6883 | return errval; |
| 6884 | } |
| 6885 | Py_INCREF(Py_None); |
| 6886 | return Py_None; |
| 6887 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6888 | |
| 6889 | normal: |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6890 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 6891 | PyUnicode_FSConverter, &ofilepath, |
| 6892 | &operation)) |
| 6893 | return NULL; |
| 6894 | filepath = bytes2str(ofilepath, 1); |
| 6895 | Py_BEGIN_ALLOW_THREADS |
| 6896 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6897 | NULL, NULL, SW_SHOWNORMAL); |
| 6898 | Py_END_ALLOW_THREADS |
| 6899 | if (rc <= (HINSTANCE)32) { |
| 6900 | PyObject *errval = win32_error("startfile", filepath); |
| 6901 | release_bytes(ofilepath); |
| 6902 | return errval; |
| 6903 | } |
| 6904 | release_bytes(ofilepath); |
| 6905 | Py_INCREF(Py_None); |
| 6906 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6907 | } |
| 6908 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6909 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6910 | #ifdef HAVE_GETLOADAVG |
| 6911 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6912 | "getloadavg() -> (float, float, float)\n\n\ |
| 6913 | Return the number of processes in the system run queue averaged over\n\ |
| 6914 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6915 | was unobtainable"); |
| 6916 | |
| 6917 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6918 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6919 | { |
| 6920 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6921 | if (getloadavg(loadavg, 3)!=3) { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6922 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6923 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6924 | } else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6925 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6926 | } |
| 6927 | #endif |
| 6928 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6929 | #ifdef MS_WINDOWS |
| 6930 | |
| 6931 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6932 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6933 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6934 | |
| 6935 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6936 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6937 | DWORD dwFlags ); |
| 6938 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6939 | BYTE *pbBuffer ); |
| 6940 | |
| 6941 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6942 | /* This handle is never explicitly released. Instead, the operating |
| 6943 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6944 | static HCRYPTPROV hCryptProv = 0; |
| 6945 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6946 | static PyObject* |
| 6947 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6948 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6949 | int howMany; |
| 6950 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6951 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6952 | /* Read arguments */ |
| 6953 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6954 | return NULL; |
| 6955 | if (howMany < 0) |
| 6956 | return PyErr_Format(PyExc_ValueError, |
| 6957 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6958 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6959 | if (hCryptProv == 0) { |
| 6960 | HINSTANCE hAdvAPI32 = NULL; |
| 6961 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6962 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6963 | /* Obtain handle to the DLL containing CryptoAPI |
| 6964 | This should not fail */ |
| 6965 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6966 | if(hAdvAPI32 == NULL) |
| 6967 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6968 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6969 | /* Obtain pointers to the CryptoAPI functions |
| 6970 | This will fail on some early versions of Win95 */ |
| 6971 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6972 | hAdvAPI32, |
| 6973 | "CryptAcquireContextA"); |
| 6974 | if (pCryptAcquireContext == NULL) |
| 6975 | return PyErr_Format(PyExc_NotImplementedError, |
| 6976 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6977 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6978 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6979 | hAdvAPI32, "CryptGenRandom"); |
| 6980 | if (pCryptGenRandom == NULL) |
| 6981 | return PyErr_Format(PyExc_NotImplementedError, |
| 6982 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6983 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6984 | /* Acquire context */ |
| 6985 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6986 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6987 | return win32_error("CryptAcquireContext", NULL); |
| 6988 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6989 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 6990 | /* Allocate bytes */ |
| 6991 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 6992 | if (result != NULL) { |
| 6993 | /* Get random data */ |
| 6994 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 6995 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 6996 | PyBytes_AS_STRING(result))) { |
| 6997 | Py_DECREF(result); |
| 6998 | return win32_error("CryptGenRandom", NULL); |
| 6999 | } |
| 7000 | } |
| 7001 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7002 | } |
| 7003 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7004 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7005 | PyDoc_STRVAR(device_encoding__doc__, |
| 7006 | "device_encoding(fd) -> str\n\n\ |
| 7007 | Return a string describing the encoding of the device\n\ |
| 7008 | if the output is a terminal; else return None."); |
| 7009 | |
| 7010 | static PyObject * |
| 7011 | device_encoding(PyObject *self, PyObject *args) |
| 7012 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7013 | int fd; |
| 7014 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 7015 | return NULL; |
| 7016 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 7017 | Py_INCREF(Py_None); |
| 7018 | return Py_None; |
| 7019 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7020 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7021 | if (fd == 0) { |
| 7022 | char buf[100]; |
| 7023 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 7024 | return PyUnicode_FromString(buf); |
| 7025 | } |
| 7026 | if (fd == 1 || fd == 2) { |
| 7027 | char buf[100]; |
| 7028 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 7029 | return PyUnicode_FromString(buf); |
| 7030 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7031 | #elif defined(CODESET) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7032 | { |
| 7033 | char *codeset = nl_langinfo(CODESET); |
| 7034 | if (codeset != NULL && codeset[0] != 0) |
| 7035 | return PyUnicode_FromString(codeset); |
| 7036 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7037 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7038 | Py_INCREF(Py_None); |
| 7039 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7042 | #ifdef __VMS |
| 7043 | /* Use openssl random routine */ |
| 7044 | #include <openssl/rand.h> |
| 7045 | PyDoc_STRVAR(vms_urandom__doc__, |
| 7046 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 7047 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7048 | |
| 7049 | static PyObject* |
| 7050 | vms_urandom(PyObject *self, PyObject *args) |
| 7051 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7052 | int howMany; |
| 7053 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7054 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7055 | /* Read arguments */ |
| 7056 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7057 | return NULL; |
| 7058 | if (howMany < 0) |
| 7059 | return PyErr_Format(PyExc_ValueError, |
| 7060 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7061 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7062 | /* Allocate bytes */ |
| 7063 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 7064 | if (result != NULL) { |
| 7065 | /* Get random data */ |
| 7066 | if (RAND_pseudo_bytes((unsigned char*) |
| 7067 | PyBytes_AS_STRING(result), |
| 7068 | howMany) < 0) { |
| 7069 | Py_DECREF(result); |
| 7070 | return PyErr_Format(PyExc_ValueError, |
| 7071 | "RAND_pseudo_bytes"); |
| 7072 | } |
| 7073 | } |
| 7074 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7075 | } |
| 7076 | #endif |
| 7077 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7078 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7079 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7080 | #ifdef HAVE_TTYNAME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7081 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7082 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7083 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7084 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7085 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7086 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7087 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7088 | #ifdef HAVE_FCHMOD |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7089 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7090 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7091 | #ifdef HAVE_CHOWN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7092 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7093 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7094 | #ifdef HAVE_LCHMOD |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7095 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7096 | #endif /* HAVE_LCHMOD */ |
| 7097 | #ifdef HAVE_FCHOWN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7098 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7099 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7100 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7101 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7102 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7103 | #ifdef HAVE_LCHOWN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7104 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7105 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7106 | #ifdef HAVE_CHROOT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7107 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7108 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7109 | #ifdef HAVE_CTERMID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7110 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7111 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7112 | #ifdef HAVE_GETCWD |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7113 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 7114 | METH_NOARGS, posix_getcwd__doc__}, |
| 7115 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 7116 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7117 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7118 | #ifdef HAVE_LINK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7119 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7120 | #endif /* HAVE_LINK */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7121 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7122 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7123 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7124 | #ifdef HAVE_NICE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7125 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7126 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7127 | #ifdef HAVE_READLINK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7128 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7129 | #endif /* HAVE_READLINK */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7130 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7131 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7132 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
| 7133 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7134 | #ifdef HAVE_SYMLINK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7135 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7136 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7137 | #ifdef HAVE_SYSTEM |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7138 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7139 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7140 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7141 | #ifdef HAVE_UNAME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7142 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7143 | #endif /* HAVE_UNAME */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7144 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7145 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7146 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7147 | #ifdef HAVE_TIMES |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7148 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7149 | #endif /* HAVE_TIMES */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7150 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7151 | #ifdef HAVE_EXECV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7152 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7153 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7154 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7155 | #ifdef HAVE_SPAWNV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7156 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7157 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7158 | #if defined(PYOS_OS2) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7159 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7160 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7161 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7162 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7163 | #ifdef HAVE_FORK1 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7164 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7165 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7166 | #ifdef HAVE_FORK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7167 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7168 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7169 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7170 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7171 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7172 | #ifdef HAVE_FORKPTY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7173 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7174 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7175 | #ifdef HAVE_GETEGID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7176 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7177 | #endif /* HAVE_GETEGID */ |
| 7178 | #ifdef HAVE_GETEUID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7179 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7180 | #endif /* HAVE_GETEUID */ |
| 7181 | #ifdef HAVE_GETGID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7182 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7183 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7184 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7185 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7186 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7187 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7188 | #ifdef HAVE_GETPGRP |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7189 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7190 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7191 | #ifdef HAVE_GETPPID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7192 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7193 | #endif /* HAVE_GETPPID */ |
| 7194 | #ifdef HAVE_GETUID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7195 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7196 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7197 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7198 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7199 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7200 | #ifdef HAVE_KILL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7201 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7202 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7203 | #ifdef HAVE_KILLPG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7204 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7205 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7206 | #ifdef HAVE_PLOCK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7207 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7208 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7209 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7210 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7211 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7212 | #ifdef HAVE_SETUID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7213 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7214 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7215 | #ifdef HAVE_SETEUID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7216 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7217 | #endif /* HAVE_SETEUID */ |
| 7218 | #ifdef HAVE_SETEGID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7219 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7220 | #endif /* HAVE_SETEGID */ |
| 7221 | #ifdef HAVE_SETREUID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7222 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7223 | #endif /* HAVE_SETREUID */ |
| 7224 | #ifdef HAVE_SETREGID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7225 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7226 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7227 | #ifdef HAVE_SETGID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7228 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7229 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7230 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7231 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7232 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7233 | #ifdef HAVE_GETPGID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7234 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7235 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7236 | #ifdef HAVE_SETPGRP |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7237 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7238 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7239 | #ifdef HAVE_WAIT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7240 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7241 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7242 | #ifdef HAVE_WAIT3 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7243 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7244 | #endif /* HAVE_WAIT3 */ |
| 7245 | #ifdef HAVE_WAIT4 |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7246 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7247 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7248 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7249 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7250 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7251 | #ifdef HAVE_GETSID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7252 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7253 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7254 | #ifdef HAVE_SETSID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7255 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7256 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7257 | #ifdef HAVE_SETPGID |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7258 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7259 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7260 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7261 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7262 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7263 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7264 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7265 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7266 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7267 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7268 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 7269 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 7270 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7271 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7272 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7273 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7274 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7275 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7276 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7277 | #ifdef HAVE_PIPE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7278 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7279 | #endif |
| 7280 | #ifdef HAVE_MKFIFO |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7281 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7282 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7283 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7284 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7285 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7286 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7287 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7288 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7289 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7290 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7291 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7292 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7293 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7294 | #ifdef HAVE_PUTENV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7295 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7296 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7297 | #ifdef HAVE_UNSETENV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7298 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7299 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7300 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7301 | #ifdef HAVE_FCHDIR |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7302 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7303 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7304 | #ifdef HAVE_FSYNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7305 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7306 | #endif |
| 7307 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7308 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7309 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7310 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7311 | #ifdef WCOREDUMP |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7312 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7313 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7314 | #ifdef WIFCONTINUED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7315 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7316 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7317 | #ifdef WIFSTOPPED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7318 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7319 | #endif /* WIFSTOPPED */ |
| 7320 | #ifdef WIFSIGNALED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7321 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7322 | #endif /* WIFSIGNALED */ |
| 7323 | #ifdef WIFEXITED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7324 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7325 | #endif /* WIFEXITED */ |
| 7326 | #ifdef WEXITSTATUS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7327 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7328 | #endif /* WEXITSTATUS */ |
| 7329 | #ifdef WTERMSIG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7330 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7331 | #endif /* WTERMSIG */ |
| 7332 | #ifdef WSTOPSIG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7333 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7334 | #endif /* WSTOPSIG */ |
| 7335 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7336 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7337 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7338 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7339 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7340 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7341 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7342 | #ifdef HAVE_CONFSTR |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7343 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7344 | #endif |
| 7345 | #ifdef HAVE_SYSCONF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7346 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7347 | #endif |
| 7348 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7349 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7350 | #endif |
| 7351 | #ifdef HAVE_PATHCONF |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7352 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7353 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7354 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7355 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7356 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7357 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7358 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7359 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7360 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7361 | #ifdef MS_WINDOWS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7362 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7363 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7364 | #ifdef __VMS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7365 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7366 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7367 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7368 | }; |
| 7369 | |
| 7370 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7371 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7372 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7373 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7374 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7377 | #if defined(PYOS_OS2) |
| 7378 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7379 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7380 | { |
| 7381 | APIRET rc; |
| 7382 | ULONG values[QSV_MAX+1]; |
| 7383 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7384 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7385 | |
| 7386 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7387 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7388 | Py_END_ALLOW_THREADS |
| 7389 | |
| 7390 | if (rc != NO_ERROR) { |
| 7391 | os2_error(rc); |
| 7392 | return -1; |
| 7393 | } |
| 7394 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7395 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7396 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7397 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7398 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7399 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7400 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7401 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7402 | |
| 7403 | switch (values[QSV_VERSION_MINOR]) { |
| 7404 | case 0: ver = "2.00"; break; |
| 7405 | case 10: ver = "2.10"; break; |
| 7406 | case 11: ver = "2.11"; break; |
| 7407 | case 30: ver = "3.00"; break; |
| 7408 | case 40: ver = "4.00"; break; |
| 7409 | case 50: ver = "5.00"; break; |
| 7410 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7411 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7412 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7413 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7414 | ver = &tmp[0]; |
| 7415 | } |
| 7416 | |
| 7417 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7418 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7419 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7420 | |
| 7421 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7422 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7423 | tmp[1] = ':'; |
| 7424 | tmp[2] = '\0'; |
| 7425 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7426 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7427 | } |
| 7428 | #endif |
| 7429 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7430 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7431 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7432 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7433 | #ifdef F_OK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7434 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7435 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7436 | #ifdef R_OK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7437 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7438 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7439 | #ifdef W_OK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7440 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7441 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7442 | #ifdef X_OK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7443 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7444 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7445 | #ifdef NGROUPS_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7446 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7447 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7448 | #ifdef TMP_MAX |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7449 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7450 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7451 | #ifdef WCONTINUED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7452 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7453 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7454 | #ifdef WNOHANG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7455 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7456 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7457 | #ifdef WUNTRACED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7458 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7459 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7460 | #ifdef O_RDONLY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7461 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7462 | #endif |
| 7463 | #ifdef O_WRONLY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7464 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7465 | #endif |
| 7466 | #ifdef O_RDWR |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7467 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7468 | #endif |
| 7469 | #ifdef O_NDELAY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7470 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7471 | #endif |
| 7472 | #ifdef O_NONBLOCK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7473 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7474 | #endif |
| 7475 | #ifdef O_APPEND |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7476 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7477 | #endif |
| 7478 | #ifdef O_DSYNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7479 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7480 | #endif |
| 7481 | #ifdef O_RSYNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7482 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7483 | #endif |
| 7484 | #ifdef O_SYNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7485 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7486 | #endif |
| 7487 | #ifdef O_NOCTTY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7488 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7489 | #endif |
| 7490 | #ifdef O_CREAT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7491 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7492 | #endif |
| 7493 | #ifdef O_EXCL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7494 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7495 | #endif |
| 7496 | #ifdef O_TRUNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7497 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7498 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7499 | #ifdef O_BINARY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7500 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7501 | #endif |
| 7502 | #ifdef O_TEXT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7503 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7504 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7505 | #ifdef O_LARGEFILE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7506 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7507 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7508 | #ifdef O_SHLOCK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7509 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7510 | #endif |
| 7511 | #ifdef O_EXLOCK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7512 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7513 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7514 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7515 | /* MS Windows */ |
| 7516 | #ifdef O_NOINHERIT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7517 | /* Don't inherit in child processes. */ |
| 7518 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7519 | #endif |
| 7520 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7521 | /* Optimize for short life (keep in memory). */ |
| 7522 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7523 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7524 | #endif |
| 7525 | #ifdef O_TEMPORARY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7526 | /* Automatically delete when last handle is closed. */ |
| 7527 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7528 | #endif |
| 7529 | #ifdef O_RANDOM |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7530 | /* Optimize for random access. */ |
| 7531 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7532 | #endif |
| 7533 | #ifdef O_SEQUENTIAL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7534 | /* Optimize for sequential access. */ |
| 7535 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7536 | #endif |
| 7537 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7538 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7539 | #ifdef O_ASYNC |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7540 | /* Send a SIGIO signal whenever input or output |
| 7541 | becomes available on file descriptor */ |
| 7542 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7543 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7544 | #ifdef O_DIRECT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7545 | /* Direct disk access. */ |
| 7546 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7547 | #endif |
| 7548 | #ifdef O_DIRECTORY |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7549 | /* Must be a directory. */ |
| 7550 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7551 | #endif |
| 7552 | #ifdef O_NOFOLLOW |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7553 | /* Do not follow links. */ |
| 7554 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7555 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7556 | #ifdef O_NOATIME |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7557 | /* Do not update the access time. */ |
| 7558 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7559 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7560 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7561 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7562 | #ifdef EX_OK |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7563 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7564 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7565 | #ifdef EX_USAGE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7566 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7567 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7568 | #ifdef EX_DATAERR |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7569 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7570 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7571 | #ifdef EX_NOINPUT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7572 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7573 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7574 | #ifdef EX_NOUSER |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7575 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7576 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7577 | #ifdef EX_NOHOST |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7578 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7579 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7580 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7581 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7582 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7583 | #ifdef EX_SOFTWARE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7584 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7585 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7586 | #ifdef EX_OSERR |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7587 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7588 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7589 | #ifdef EX_OSFILE |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7590 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7591 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7592 | #ifdef EX_CANTCREAT |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7593 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7594 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7595 | #ifdef EX_IOERR |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7596 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7597 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7598 | #ifdef EX_TEMPFAIL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7599 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7600 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7601 | #ifdef EX_PROTOCOL |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7602 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7603 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7604 | #ifdef EX_NOPERM |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7605 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7606 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7607 | #ifdef EX_CONFIG |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7608 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7609 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7610 | #ifdef EX_NOTFOUND |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7611 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7612 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7613 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7614 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7615 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7616 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7617 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7618 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7619 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7620 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7621 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7622 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7623 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7624 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7625 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7626 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7627 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7628 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7629 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7630 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7631 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7632 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7633 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7634 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7635 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7636 | #else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7637 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7638 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7639 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7640 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7641 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7642 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7643 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7644 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7645 | #if defined(PYOS_OS2) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7646 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7647 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7648 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7649 | } |
| 7650 | |
| 7651 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7652 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7653 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7654 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7655 | |
| 7656 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7657 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7658 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7659 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7660 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7661 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7662 | #define MODNAME "posix" |
| 7663 | #endif |
| 7664 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7665 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7666 | PyModuleDef_HEAD_INIT, |
| 7667 | MODNAME, |
| 7668 | posix__doc__, |
| 7669 | -1, |
| 7670 | posix_methods, |
| 7671 | NULL, |
| 7672 | NULL, |
| 7673 | NULL, |
| 7674 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7675 | }; |
| 7676 | |
| 7677 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7678 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7679 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7680 | { |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7681 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7682 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7683 | m = PyModule_Create(&posixmodule); |
| 7684 | if (m == NULL) |
| 7685 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7686 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7687 | /* Initialize environ dictionary */ |
| 7688 | v = convertenviron(); |
| 7689 | Py_XINCREF(v); |
| 7690 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 7691 | return NULL; |
| 7692 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7693 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7694 | if (all_ins(m)) |
| 7695 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7696 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7697 | if (setup_confname_tables(m)) |
| 7698 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7699 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7700 | Py_INCREF(PyExc_OSError); |
| 7701 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7702 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7703 | #ifdef HAVE_PUTENV |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7704 | if (posix_putenv_garbage == NULL) |
| 7705 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7706 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7707 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7708 | if (!initialized) { |
| 7709 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7710 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7711 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7712 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7713 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7714 | structseq_new = StatResultType.tp_new; |
| 7715 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7716 | |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7717 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7718 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7719 | #ifdef NEED_TICKS_PER_SECOND |
| 7720 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7721 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7722 | # elif defined(HZ) |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7723 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7724 | # else |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7725 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7726 | # endif |
| 7727 | #endif |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7728 | } |
| 7729 | Py_INCREF((PyObject*) &StatResultType); |
| 7730 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 7731 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7732 | PyModule_AddObject(m, "statvfs_result", |
| 7733 | (PyObject*) &StatVFSResultType); |
| 7734 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7735 | |
| 7736 | #ifdef __APPLE__ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7737 | /* |
| 7738 | * Step 2 of weak-linking support on Mac OS X. |
| 7739 | * |
| 7740 | * The code below removes functions that are not available on the |
| 7741 | * currently active platform. |
| 7742 | * |
| 7743 | * This block allow one to use a python binary that was build on |
| 7744 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7745 | * OSX 10.4. |
| 7746 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7747 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7748 | if (fstatvfs == NULL) { |
| 7749 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 7750 | return NULL; |
| 7751 | } |
| 7752 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7753 | #endif /* HAVE_FSTATVFS */ |
| 7754 | |
| 7755 | #ifdef HAVE_STATVFS |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7756 | if (statvfs == NULL) { |
| 7757 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 7758 | return NULL; |
| 7759 | } |
| 7760 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7761 | #endif /* HAVE_STATVFS */ |
| 7762 | |
| 7763 | # ifdef HAVE_LCHOWN |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7764 | if (lchown == NULL) { |
| 7765 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 7766 | return NULL; |
| 7767 | } |
| 7768 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7769 | #endif /* HAVE_LCHOWN */ |
| 7770 | |
| 7771 | |
| 7772 | #endif /* __APPLE__ */ |
Victor Stinner | 97b8988 | 2010-05-06 00:25:39 +0000 | [diff] [blame] | 7773 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7774 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7775 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7776 | |
| 7777 | #ifdef __cplusplus |
| 7778 | } |
| 7779 | #endif |