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 | /* |
| 18 | * Step 1 of support for weak-linking a number of symbols existing on |
| 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 |
| 72 | #include <sys/wait.h> /* For WNOHANG */ |
| 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 |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +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 |
| 107 | #define HAVE_SYSTEM 1 |
| 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 |
| 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 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 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 119 | #define HAVE_SYSTEM 1 |
| 120 | #define HAVE_WAIT 1 |
| 121 | #else |
| 122 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 123 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +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 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 128 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 129 | #define HAVE_FSYNC 1 |
| 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 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #else /* all other compilers */ |
| 135 | /* Unix functions that the configure script doesn't check for */ |
| 136 | #define HAVE_EXECV 1 |
| 137 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 138 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 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 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
| 151 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +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> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +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) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +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 |
| 336 | # define STAT stat |
| 337 | # define FSTAT fstat |
| 338 | # define STRUCT_STAT struct stat |
| 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 |
| 365 | * by using the exported __pinfo data member and knowledge of the |
| 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 { |
| 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 | { |
| 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 | |
| 396 | static int sizeof_ioinfo = 0; |
| 397 | |
| 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 | } |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 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 */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 416 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 417 | if (info->osfile & FOPEN) { |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 418 | return 1; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 419 | } |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 422 | fail: |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 423 | errno = EBADF; |
| 424 | return 0; |
| 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 | { |
| 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; |
| 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 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 459 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 460 | #ifdef MS_WINDOWS |
| 461 | wchar_t **e; |
| 462 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 463 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 464 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 465 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 466 | if (d == NULL) |
| 467 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 468 | #ifdef WITH_NEXT_FRAMEWORK |
| 469 | if (environ == NULL) |
| 470 | environ = *_NSGetEnviron(); |
| 471 | #endif |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 472 | #ifdef MS_WINDOWS |
| 473 | /* _wenviron must be initialized in this way if the program is started |
| 474 | through main() instead of wmain(). */ |
| 475 | _wgetenv(L""); |
| 476 | if (_wenviron == NULL) |
| 477 | return d; |
| 478 | /* This part ignores errors */ |
| 479 | for (e = _wenviron; *e != NULL; e++) { |
| 480 | PyObject *k; |
| 481 | PyObject *v; |
| 482 | wchar_t *p = wcschr(*e, L'='); |
| 483 | if (p == NULL) |
| 484 | continue; |
| 485 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 486 | if (k == NULL) { |
| 487 | PyErr_Clear(); |
| 488 | continue; |
| 489 | } |
| 490 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 491 | if (v == NULL) { |
| 492 | PyErr_Clear(); |
| 493 | Py_DECREF(k); |
| 494 | continue; |
| 495 | } |
| 496 | if (PyDict_GetItem(d, k) == NULL) { |
| 497 | if (PyDict_SetItem(d, k, v) != 0) |
| 498 | PyErr_Clear(); |
| 499 | } |
| 500 | Py_DECREF(k); |
| 501 | Py_DECREF(v); |
| 502 | } |
| 503 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 504 | if (environ == NULL) |
| 505 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 506 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 508 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 509 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 510 | char *p = strchr(*e, '='); |
| 511 | if (p == NULL) |
| 512 | continue; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 513 | k = PyUnicode_Decode(*e, (int)(p-*e), |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 514 | Py_FileSystemDefaultEncoding, "surrogateescape"); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 515 | if (k == NULL) { |
| 516 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 517 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 518 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 519 | v = PyUnicode_Decode(p+1, strlen(p+1), |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 520 | Py_FileSystemDefaultEncoding, "surrogateescape"); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 521 | if (v == NULL) { |
| 522 | PyErr_Clear(); |
| 523 | Py_DECREF(k); |
| 524 | continue; |
| 525 | } |
| 526 | if (PyDict_GetItem(d, k) == NULL) { |
| 527 | if (PyDict_SetItem(d, k, v) != 0) |
| 528 | PyErr_Clear(); |
| 529 | } |
| 530 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 531 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 533 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 534 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 535 | { |
| 536 | APIRET rc; |
| 537 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 538 | |
| 539 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 540 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 541 | PyObject *v = PyBytes_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 542 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 543 | Py_DECREF(v); |
| 544 | } |
| 545 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 546 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 547 | PyObject *v = PyBytes_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 548 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 549 | Py_DECREF(v); |
| 550 | } |
| 551 | } |
| 552 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 553 | return d; |
| 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 | { |
| 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 | } |
| 576 | } |
| 577 | |
| 578 | /* Release the lock, decref the object. */ |
| 579 | static void |
| 580 | release_bytes(PyObject* o) |
| 581 | { |
| 582 | if (PyByteArray_Check(o)) |
| 583 | o->ob_type->tp_as_buffer->bf_releasebuffer(NULL, 0); |
| 584 | Py_DECREF(o); |
| 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 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +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 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +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 | { |
| 605 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 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 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 613 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
| 614 | bytes2str(name, 0)); |
| 615 | release_bytes(name); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 616 | return rc; |
| 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 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 623 | /* XXX We should pass the function name along in the future. |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 624 | (winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 625 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 626 | Windows error object, which is non-trivial. |
| 627 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 628 | errno = GetLastError(); |
| 629 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 630 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 631 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 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 | { |
| 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); |
| 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 | { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 649 | if (PyUnicode_CheckExact(*param)) |
| 650 | Py_INCREF(*param); |
| 651 | else if (PyUnicode_Check(*param)) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 652 | /* For a Unicode subtype that's not a Unicode object, |
| 653 | return a true Unicode object with the same data. */ |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 654 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 655 | PyUnicode_GET_SIZE(*param)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 656 | else |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 657 | *param = PyUnicode_FromEncodedObject(*param, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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 | **********************************************************************/ |
| 669 | static void |
| 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 | **********************************************************************/ |
| 699 | static char * |
| 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, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +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 | |
| 724 | static PyObject * os2_error(int code) |
| 725 | { |
| 726 | char text[1024]; |
| 727 | PyObject *v; |
| 728 | |
| 729 | os2_strerror(text, sizeof(text), code, ""); |
| 730 | |
| 731 | v = Py_BuildValue("(is)", code, text); |
| 732 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 733 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 734 | Py_DECREF(v); |
| 735 | } |
| 736 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 737 | } |
| 738 | |
| 739 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 740 | |
| 741 | /* POSIX generic methods */ |
| 742 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 743 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 744 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 745 | { |
| 746 | int fd; |
| 747 | int res; |
| 748 | fd = PyObject_AsFileDescriptor(fdobj); |
| 749 | if (fd < 0) |
| 750 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 751 | if (!_PyVerify_fd(fd)) |
| 752 | return posix_error(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 753 | Py_BEGIN_ALLOW_THREADS |
| 754 | res = (*func)(fd); |
| 755 | Py_END_ALLOW_THREADS |
| 756 | if (res < 0) |
| 757 | return posix_error(); |
| 758 | Py_INCREF(Py_None); |
| 759 | return Py_None; |
| 760 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 761 | |
| 762 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 763 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 764 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 765 | PyObject *opath1 = NULL; |
| 766 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 767 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 768 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 769 | PyUnicode_FSConverter, &opath1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 770 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 771 | path1 = bytes2str(opath1, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 772 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 773 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 774 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 775 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 776 | return posix_error_with_allocated_filename(opath1); |
| 777 | release_bytes(opath1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 778 | Py_INCREF(Py_None); |
| 779 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 782 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 783 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 784 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 785 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 786 | { |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 787 | PyObject *opath1 = NULL, *opath2 = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 788 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 789 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 790 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 791 | PyUnicode_FSConverter, &opath1, |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 792 | PyUnicode_FSConverter, &opath2)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 793 | return NULL; |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 794 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 795 | path1 = bytes2str(opath1, 1); |
| 796 | path2 = bytes2str(opath2, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 797 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 798 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 799 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 800 | release_bytes(opath1); |
| 801 | release_bytes(opath2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 802 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 803 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 804 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 805 | Py_INCREF(Py_None); |
| 806 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 809 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 810 | static PyObject* |
| 811 | win32_1str(PyObject* args, char* func, |
| 812 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 813 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 814 | { |
| 815 | PyObject *uni; |
| 816 | char *ansi; |
| 817 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 818 | |
| 819 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 820 | PyErr_Clear(); |
| 821 | else { |
| 822 | Py_BEGIN_ALLOW_THREADS |
| 823 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 824 | Py_END_ALLOW_THREADS |
| 825 | if (!result) |
| 826 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 827 | Py_INCREF(Py_None); |
| 828 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 829 | } |
| 830 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 831 | return NULL; |
| 832 | Py_BEGIN_ALLOW_THREADS |
| 833 | result = funcA(ansi); |
| 834 | Py_END_ALLOW_THREADS |
| 835 | if (!result) |
| 836 | return win32_error(func, ansi); |
| 837 | Py_INCREF(Py_None); |
| 838 | return Py_None; |
| 839 | |
| 840 | } |
| 841 | |
| 842 | /* This is a reimplementation of the C library's chdir function, |
| 843 | but one that produces Win32 errors instead of DOS error codes. |
| 844 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 845 | it also needs to set "magic" environment variables indicating |
| 846 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 847 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 848 | win32_chdir(LPCSTR path) |
| 849 | { |
| 850 | char new_path[MAX_PATH+1]; |
| 851 | int result; |
| 852 | char env[4] = "=x:"; |
| 853 | |
| 854 | if(!SetCurrentDirectoryA(path)) |
| 855 | return FALSE; |
| 856 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 857 | if (!result) |
| 858 | return FALSE; |
| 859 | /* In the ANSI API, there should not be any paths longer |
| 860 | than MAX_PATH. */ |
| 861 | assert(result <= MAX_PATH+1); |
| 862 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 863 | strncmp(new_path, "//", 2) == 0) |
| 864 | /* UNC path, nothing to do. */ |
| 865 | return TRUE; |
| 866 | env[1] = new_path[0]; |
| 867 | return SetEnvironmentVariableA(env, new_path); |
| 868 | } |
| 869 | |
| 870 | /* The Unicode version differs from the ANSI version |
| 871 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 872 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 873 | win32_wchdir(LPCWSTR path) |
| 874 | { |
| 875 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 876 | int result; |
| 877 | wchar_t env[4] = L"=x:"; |
| 878 | |
| 879 | if(!SetCurrentDirectoryW(path)) |
| 880 | return FALSE; |
| 881 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 882 | if (!result) |
| 883 | return FALSE; |
| 884 | if (result > MAX_PATH+1) { |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 885 | new_path = malloc(result * sizeof(wchar_t)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 886 | if (!new_path) { |
| 887 | SetLastError(ERROR_OUTOFMEMORY); |
| 888 | return FALSE; |
| 889 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 890 | result = GetCurrentDirectoryW(result, new_path); |
| 891 | if (!result) { |
| 892 | free(new_path); |
| 893 | return FALSE; |
| 894 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 895 | } |
| 896 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 897 | wcsncmp(new_path, L"//", 2) == 0) |
| 898 | /* UNC path, nothing to do. */ |
| 899 | return TRUE; |
| 900 | env[1] = new_path[0]; |
| 901 | result = SetEnvironmentVariableW(env, new_path); |
| 902 | if (new_path != _new_path) |
| 903 | free(new_path); |
| 904 | return result; |
| 905 | } |
| 906 | #endif |
| 907 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 908 | #ifdef MS_WINDOWS |
| 909 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 910 | - time stamps are restricted to second resolution |
| 911 | - file modification times suffer from forth-and-back conversions between |
| 912 | UTC and local time |
| 913 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 914 | */ |
| 915 | #define HAVE_STAT_NSEC 1 |
| 916 | |
| 917 | struct win32_stat{ |
| 918 | int st_dev; |
| 919 | __int64 st_ino; |
| 920 | unsigned short st_mode; |
| 921 | int st_nlink; |
| 922 | int st_uid; |
| 923 | int st_gid; |
| 924 | int st_rdev; |
| 925 | __int64 st_size; |
| 926 | int st_atime; |
| 927 | int st_atime_nsec; |
| 928 | int st_mtime; |
| 929 | int st_mtime_nsec; |
| 930 | int st_ctime; |
| 931 | int st_ctime_nsec; |
| 932 | }; |
| 933 | |
| 934 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 935 | |
| 936 | static void |
| 937 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 938 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 939 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 940 | /* Cannot simply cast and dereference in_ptr, |
| 941 | since it might not be aligned properly */ |
| 942 | __int64 in; |
| 943 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 944 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 945 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 946 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 947 | } |
| 948 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 949 | static void |
| 950 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 951 | { |
| 952 | /* XXX endianness */ |
| 953 | __int64 out; |
| 954 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 955 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 956 | memcpy(out_ptr, &out, sizeof(out)); |
| 957 | } |
| 958 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 959 | /* Below, we *know* that ugo+r is 0444 */ |
| 960 | #if _S_IREAD != 0400 |
| 961 | #error Unsupported C library |
| 962 | #endif |
| 963 | static int |
| 964 | attributes_to_mode(DWORD attr) |
| 965 | { |
| 966 | int m = 0; |
| 967 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 968 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 969 | else |
| 970 | m |= _S_IFREG; |
| 971 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 972 | m |= 0444; |
| 973 | else |
| 974 | m |= 0666; |
| 975 | return m; |
| 976 | } |
| 977 | |
| 978 | static int |
| 979 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 980 | { |
| 981 | memset(result, 0, sizeof(*result)); |
| 982 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 983 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 984 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 985 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 986 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 991 | static BOOL |
| 992 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 993 | { |
| 994 | HANDLE hFindFile; |
| 995 | WIN32_FIND_DATAA FileData; |
| 996 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 997 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 998 | return FALSE; |
| 999 | FindClose(hFindFile); |
| 1000 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1001 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1002 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1003 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1004 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1005 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1006 | return TRUE; |
| 1007 | } |
| 1008 | |
| 1009 | static BOOL |
| 1010 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1011 | { |
| 1012 | HANDLE hFindFile; |
| 1013 | WIN32_FIND_DATAW FileData; |
| 1014 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1015 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1016 | return FALSE; |
| 1017 | FindClose(hFindFile); |
| 1018 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1019 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1020 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1021 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1022 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1023 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1024 | return TRUE; |
| 1025 | } |
| 1026 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1027 | static int |
| 1028 | win32_stat(const char* path, struct win32_stat *result) |
| 1029 | { |
| 1030 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1031 | int code; |
| 1032 | char *dot; |
Hirokazu Yamamoto | 6fbdfda | 2009-06-29 11:37:19 +0000 | [diff] [blame] | 1033 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1034 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1035 | /* Protocol violation: we explicitly clear errno, instead of |
| 1036 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1037 | errno = 0; |
| 1038 | return -1; |
| 1039 | } else { |
| 1040 | /* Could not get attributes on open file. Fall back to |
| 1041 | reading the directory. */ |
| 1042 | if (!attributes_from_dir(path, &info)) { |
| 1043 | /* Very strange. This should not fail now */ |
| 1044 | errno = 0; |
| 1045 | return -1; |
| 1046 | } |
| 1047 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1048 | } |
| 1049 | code = attribute_data_to_stat(&info, result); |
| 1050 | if (code != 0) |
| 1051 | return code; |
| 1052 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1053 | dot = strrchr(path, '.'); |
| 1054 | if (dot) { |
| 1055 | if (stricmp(dot, ".bat") == 0 || |
| 1056 | stricmp(dot, ".cmd") == 0 || |
| 1057 | stricmp(dot, ".exe") == 0 || |
| 1058 | stricmp(dot, ".com") == 0) |
| 1059 | result->st_mode |= 0111; |
| 1060 | } |
| 1061 | return code; |
| 1062 | } |
| 1063 | |
| 1064 | static int |
| 1065 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1066 | { |
| 1067 | int code; |
| 1068 | const wchar_t *dot; |
| 1069 | WIN32_FILE_ATTRIBUTE_DATA info; |
Hirokazu Yamamoto | 6fbdfda | 2009-06-29 11:37:19 +0000 | [diff] [blame] | 1070 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1071 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1072 | /* Protocol violation: we explicitly clear errno, instead of |
| 1073 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1074 | errno = 0; |
| 1075 | return -1; |
| 1076 | } else { |
| 1077 | /* Could not get attributes on open file. Fall back to |
| 1078 | reading the directory. */ |
| 1079 | if (!attributes_from_dir_w(path, &info)) { |
| 1080 | /* Very strange. This should not fail now */ |
| 1081 | errno = 0; |
| 1082 | return -1; |
| 1083 | } |
| 1084 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1085 | } |
| 1086 | code = attribute_data_to_stat(&info, result); |
| 1087 | if (code < 0) |
| 1088 | return code; |
| 1089 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1090 | dot = wcsrchr(path, '.'); |
| 1091 | if (dot) { |
| 1092 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1093 | _wcsicmp(dot, L".cmd") == 0 || |
| 1094 | _wcsicmp(dot, L".exe") == 0 || |
| 1095 | _wcsicmp(dot, L".com") == 0) |
| 1096 | result->st_mode |= 0111; |
| 1097 | } |
| 1098 | return code; |
| 1099 | } |
| 1100 | |
| 1101 | static int |
| 1102 | win32_fstat(int file_number, struct win32_stat *result) |
| 1103 | { |
| 1104 | BY_HANDLE_FILE_INFORMATION info; |
| 1105 | HANDLE h; |
| 1106 | int type; |
| 1107 | |
| 1108 | h = (HANDLE)_get_osfhandle(file_number); |
| 1109 | |
| 1110 | /* Protocol violation: we explicitly clear errno, instead of |
| 1111 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1112 | errno = 0; |
| 1113 | |
| 1114 | if (h == INVALID_HANDLE_VALUE) { |
| 1115 | /* This is really a C library error (invalid file handle). |
| 1116 | We set the Win32 error to the closes one matching. */ |
| 1117 | SetLastError(ERROR_INVALID_HANDLE); |
| 1118 | return -1; |
| 1119 | } |
| 1120 | memset(result, 0, sizeof(*result)); |
| 1121 | |
| 1122 | type = GetFileType(h); |
| 1123 | if (type == FILE_TYPE_UNKNOWN) { |
| 1124 | DWORD error = GetLastError(); |
| 1125 | if (error != 0) { |
| 1126 | return -1; |
| 1127 | } |
| 1128 | /* else: valid but unknown file */ |
| 1129 | } |
| 1130 | |
| 1131 | if (type != FILE_TYPE_DISK) { |
| 1132 | if (type == FILE_TYPE_CHAR) |
| 1133 | result->st_mode = _S_IFCHR; |
| 1134 | else if (type == FILE_TYPE_PIPE) |
| 1135 | result->st_mode = _S_IFIFO; |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | if (!GetFileInformationByHandle(h, &info)) { |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
| 1143 | /* similar to stat() */ |
| 1144 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1145 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1146 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1147 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1148 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1149 | /* specific to fstat() */ |
| 1150 | result->st_nlink = info.nNumberOfLinks; |
| 1151 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | #endif /* MS_WINDOWS */ |
| 1156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1157 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1158 | "stat_result: Result from stat or lstat.\n\n\ |
| 1159 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1160 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1161 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1162 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1163 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1164 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1165 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1166 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1167 | |
| 1168 | static PyStructSequence_Field stat_result_fields[] = { |
| 1169 | {"st_mode", "protection bits"}, |
| 1170 | {"st_ino", "inode"}, |
| 1171 | {"st_dev", "device"}, |
| 1172 | {"st_nlink", "number of hard links"}, |
| 1173 | {"st_uid", "user ID of owner"}, |
| 1174 | {"st_gid", "group ID of owner"}, |
| 1175 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1176 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1177 | {NULL, "integer time of last access"}, |
| 1178 | {NULL, "integer time of last modification"}, |
| 1179 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1180 | {"st_atime", "time of last access"}, |
| 1181 | {"st_mtime", "time of last modification"}, |
| 1182 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1183 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1184 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1185 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1186 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1187 | {"st_blocks", "number of blocks allocated"}, |
| 1188 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1189 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1190 | {"st_rdev", "device type (if inode device)"}, |
| 1191 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1192 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1193 | {"st_flags", "user defined flags for file"}, |
| 1194 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1195 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1196 | {"st_gen", "generation number"}, |
| 1197 | #endif |
| 1198 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1199 | {"st_birthtime", "time of creation"}, |
| 1200 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1201 | {0} |
| 1202 | }; |
| 1203 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1204 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1205 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1206 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1207 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1208 | #endif |
| 1209 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1210 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1211 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1212 | #else |
| 1213 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1214 | #endif |
| 1215 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1216 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1217 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1218 | #else |
| 1219 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1220 | #endif |
| 1221 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1222 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1223 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1224 | #else |
| 1225 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1226 | #endif |
| 1227 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1228 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1229 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1230 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1231 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1232 | #endif |
| 1233 | |
| 1234 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1235 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1236 | #else |
| 1237 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1238 | #endif |
| 1239 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1240 | static PyStructSequence_Desc stat_result_desc = { |
| 1241 | "stat_result", /* name */ |
| 1242 | stat_result__doc__, /* doc */ |
| 1243 | stat_result_fields, |
| 1244 | 10 |
| 1245 | }; |
| 1246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1247 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1248 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1249 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1250 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1251 | 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] | 1252 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1253 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1254 | |
| 1255 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1256 | {"f_bsize", }, |
| 1257 | {"f_frsize", }, |
| 1258 | {"f_blocks", }, |
| 1259 | {"f_bfree", }, |
| 1260 | {"f_bavail", }, |
| 1261 | {"f_files", }, |
| 1262 | {"f_ffree", }, |
| 1263 | {"f_favail", }, |
| 1264 | {"f_flag", }, |
| 1265 | {"f_namemax",}, |
| 1266 | {0} |
| 1267 | }; |
| 1268 | |
| 1269 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1270 | "statvfs_result", /* name */ |
| 1271 | statvfs_result__doc__, /* doc */ |
| 1272 | statvfs_result_fields, |
| 1273 | 10 |
| 1274 | }; |
| 1275 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1276 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1277 | static PyTypeObject StatResultType; |
| 1278 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1279 | static newfunc structseq_new; |
| 1280 | |
| 1281 | static PyObject * |
| 1282 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1283 | { |
| 1284 | PyStructSequence *result; |
| 1285 | int i; |
| 1286 | |
| 1287 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1288 | if (!result) |
| 1289 | return NULL; |
| 1290 | /* If we have been initialized from a tuple, |
| 1291 | st_?time might be set to None. Initialize it |
| 1292 | from the int slots. */ |
| 1293 | for (i = 7; i <= 9; i++) { |
| 1294 | if (result->ob_item[i+3] == Py_None) { |
| 1295 | Py_DECREF(Py_None); |
| 1296 | Py_INCREF(result->ob_item[i]); |
| 1297 | result->ob_item[i+3] = result->ob_item[i]; |
| 1298 | } |
| 1299 | } |
| 1300 | return (PyObject*)result; |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1306 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1307 | |
| 1308 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1309 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1310 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1311 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1312 | future calls return ints. \n\ |
| 1313 | If newval is omitted, return the current setting.\n"); |
| 1314 | |
| 1315 | static PyObject* |
| 1316 | stat_float_times(PyObject* self, PyObject *args) |
| 1317 | { |
| 1318 | int newval = -1; |
| 1319 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1320 | return NULL; |
| 1321 | if (newval == -1) |
| 1322 | /* Return old value */ |
| 1323 | return PyBool_FromLong(_stat_float_times); |
| 1324 | _stat_float_times = newval; |
| 1325 | Py_INCREF(Py_None); |
| 1326 | return Py_None; |
| 1327 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1328 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1329 | static void |
| 1330 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1331 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1332 | PyObject *fval,*ival; |
| 1333 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1334 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1335 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1336 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1337 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1338 | if (!ival) |
| 1339 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1340 | if (_stat_float_times) { |
| 1341 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1342 | } else { |
| 1343 | fval = ival; |
| 1344 | Py_INCREF(fval); |
| 1345 | } |
| 1346 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1347 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1350 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1351 | (used by posix_stat() and posix_fstat()) */ |
| 1352 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1353 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1354 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1355 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1356 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1357 | if (v == NULL) |
| 1358 | return NULL; |
| 1359 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1360 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1361 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1362 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1363 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1364 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1365 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1366 | #endif |
| 1367 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1368 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1369 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1370 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1371 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1372 | #endif |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1373 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1374 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1375 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1376 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1377 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1378 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1379 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1380 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1381 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1382 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1383 | #if defined(HAVE_STAT_TV_NSEC) |
| 1384 | ansec = st->st_atim.tv_nsec; |
| 1385 | mnsec = st->st_mtim.tv_nsec; |
| 1386 | cnsec = st->st_ctim.tv_nsec; |
| 1387 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1388 | ansec = st->st_atimespec.tv_nsec; |
| 1389 | mnsec = st->st_mtimespec.tv_nsec; |
| 1390 | cnsec = st->st_ctimespec.tv_nsec; |
| 1391 | #elif defined(HAVE_STAT_NSEC) |
| 1392 | ansec = st->st_atime_nsec; |
| 1393 | mnsec = st->st_mtime_nsec; |
| 1394 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1395 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1396 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1397 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1398 | fill_time(v, 7, st->st_atime, ansec); |
| 1399 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1400 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1401 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1402 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1403 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1404 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1405 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1406 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1407 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1408 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1409 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1410 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1411 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1412 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1413 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1414 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1415 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1416 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1417 | #endif |
| 1418 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1419 | { |
| 1420 | PyObject *val; |
| 1421 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1422 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1423 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1424 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1425 | #else |
| 1426 | bnsec = 0; |
| 1427 | #endif |
| 1428 | if (_stat_float_times) { |
| 1429 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1430 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1431 | val = PyLong_FromLong((long)bsec); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1432 | } |
| 1433 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1434 | val); |
| 1435 | } |
| 1436 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1437 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1438 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1439 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1440 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1441 | |
| 1442 | if (PyErr_Occurred()) { |
| 1443 | Py_DECREF(v); |
| 1444 | return NULL; |
| 1445 | } |
| 1446 | |
| 1447 | return v; |
| 1448 | } |
| 1449 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1450 | #ifdef MS_WINDOWS |
| 1451 | |
| 1452 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1453 | where / can be used in place of \ and the trailing slash is optional. |
| 1454 | Both SERVER and SHARE must have at least one character. |
| 1455 | */ |
| 1456 | |
| 1457 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1458 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1459 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1460 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1461 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1462 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1463 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1464 | IsUNCRootA(char *path, int pathlen) |
| 1465 | { |
| 1466 | #define ISSLASH ISSLASHA |
| 1467 | |
| 1468 | int i, share; |
| 1469 | |
| 1470 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1471 | /* minimum UNCRoot is \\x\y */ |
| 1472 | return FALSE; |
| 1473 | for (i = 2; i < pathlen ; i++) |
| 1474 | if (ISSLASH(path[i])) break; |
| 1475 | if (i == 2 || i == pathlen) |
| 1476 | /* do not allow \\\SHARE or \\SERVER */ |
| 1477 | return FALSE; |
| 1478 | share = i+1; |
| 1479 | for (i = share; i < pathlen; i++) |
| 1480 | if (ISSLASH(path[i])) break; |
| 1481 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1482 | |
| 1483 | #undef ISSLASH |
| 1484 | } |
| 1485 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1486 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1487 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1488 | { |
| 1489 | #define ISSLASH ISSLASHW |
| 1490 | |
| 1491 | int i, share; |
| 1492 | |
| 1493 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1494 | /* minimum UNCRoot is \\x\y */ |
| 1495 | return FALSE; |
| 1496 | for (i = 2; i < pathlen ; i++) |
| 1497 | if (ISSLASH(path[i])) break; |
| 1498 | if (i == 2 || i == pathlen) |
| 1499 | /* do not allow \\\SHARE or \\SERVER */ |
| 1500 | return FALSE; |
| 1501 | share = i+1; |
| 1502 | for (i = share; i < pathlen; i++) |
| 1503 | if (ISSLASH(path[i])) break; |
| 1504 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1505 | |
| 1506 | #undef ISSLASH |
| 1507 | } |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1508 | #endif /* MS_WINDOWS */ |
| 1509 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1510 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1511 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1512 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1513 | #ifdef __VMS |
| 1514 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1515 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1516 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1517 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1518 | char *wformat, |
| 1519 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1520 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1521 | STRUCT_STAT st; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1522 | PyObject *opath; |
| 1523 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1524 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1525 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1526 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1527 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1528 | PyUnicodeObject *po; |
| 1529 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1530 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1531 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1532 | Py_BEGIN_ALLOW_THREADS |
| 1533 | /* PyUnicode_AS_UNICODE result OK without |
| 1534 | thread lock as it is a simple dereference. */ |
| 1535 | res = wstatfunc(wpath, &st); |
| 1536 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1537 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1538 | if (res != 0) |
| 1539 | return win32_error_unicode("stat", wpath); |
| 1540 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1541 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1542 | /* Drop the argument parsing error as narrow strings |
| 1543 | are also valid. */ |
| 1544 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1545 | #endif |
| 1546 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1547 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1548 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1549 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1550 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1551 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1552 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1553 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1554 | |
| 1555 | if (res != 0) { |
| 1556 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1557 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1558 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1559 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1560 | #endif |
| 1561 | } |
| 1562 | else |
| 1563 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1564 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1565 | release_bytes(opath); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1566 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1569 | /* POSIX methods */ |
| 1570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1571 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1572 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1573 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1574 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1575 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1576 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1577 | 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] | 1578 | |
| 1579 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1580 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1581 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1582 | PyObject *opath; |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1583 | char *path; |
| 1584 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1585 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1586 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1587 | DWORD attr; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1588 | PyUnicodeObject *po; |
| 1589 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1590 | Py_BEGIN_ALLOW_THREADS |
| 1591 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1592 | it is a simple dereference. */ |
| 1593 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1594 | Py_END_ALLOW_THREADS |
| 1595 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1596 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1597 | /* Drop the argument parsing error as narrow strings |
| 1598 | are also valid. */ |
| 1599 | PyErr_Clear(); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1600 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1601 | PyUnicode_FSConverter, &opath, &mode)) |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1602 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1603 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1604 | Py_BEGIN_ALLOW_THREADS |
| 1605 | attr = GetFileAttributesA(path); |
| 1606 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1607 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1608 | finish: |
| 1609 | if (attr == 0xFFFFFFFF) |
| 1610 | /* File does not exist, or cannot read attributes */ |
| 1611 | return PyBool_FromLong(0); |
| 1612 | /* Access is possible if either write access wasn't requested, or |
Guido van Rossum | b00324f | 2007-12-04 01:13:14 +0000 | [diff] [blame] | 1613 | the file isn't read-only, or if it's a directory, as there are |
| 1614 | no read-only directories on Windows. */ |
| 1615 | return PyBool_FromLong(!(mode & 2) |
| 1616 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1617 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1618 | #else |
| 1619 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1620 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1621 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1622 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1623 | path = bytes2str(opath, 1); |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1624 | Py_BEGIN_ALLOW_THREADS |
| 1625 | res = access(path, mode); |
| 1626 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1627 | release_bytes(opath); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1628 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1629 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1632 | #ifndef F_OK |
| 1633 | #define F_OK 0 |
| 1634 | #endif |
| 1635 | #ifndef R_OK |
| 1636 | #define R_OK 4 |
| 1637 | #endif |
| 1638 | #ifndef W_OK |
| 1639 | #define W_OK 2 |
| 1640 | #endif |
| 1641 | #ifndef X_OK |
| 1642 | #define X_OK 1 |
| 1643 | #endif |
| 1644 | |
| 1645 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1646 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1647 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1648 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1649 | |
| 1650 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1651 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1652 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1653 | int id; |
| 1654 | char *ret; |
| 1655 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1656 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1657 | return NULL; |
| 1658 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1659 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1660 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1661 | if (id == 0) { |
| 1662 | ret = ttyname(); |
| 1663 | } |
| 1664 | else { |
| 1665 | ret = NULL; |
| 1666 | } |
| 1667 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1668 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1669 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1670 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1671 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1672 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1673 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1674 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1675 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1676 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1677 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1678 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1679 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1680 | |
| 1681 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1682 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1683 | { |
| 1684 | char *ret; |
| 1685 | char buffer[L_ctermid]; |
| 1686 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1687 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1688 | ret = ctermid_r(buffer); |
| 1689 | #else |
| 1690 | ret = ctermid(buffer); |
| 1691 | #endif |
| 1692 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1693 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1694 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1695 | } |
| 1696 | #endif |
| 1697 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1698 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1699 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1700 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1701 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1702 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1703 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1704 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1705 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 1706 | 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] | 1707 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1708 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1709 | #elif defined(__VMS) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1710 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1711 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1712 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1713 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1716 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1717 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1718 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1719 | 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] | 1720 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1721 | |
| 1722 | static PyObject * |
| 1723 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1724 | { |
| 1725 | return posix_fildes(fdobj, fchdir); |
| 1726 | } |
| 1727 | #endif /* HAVE_FCHDIR */ |
| 1728 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1729 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1730 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1731 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1732 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1733 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1734 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1735 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1736 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1737 | PyObject *opath = NULL; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1738 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1739 | int i; |
| 1740 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1741 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1742 | DWORD attr; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1743 | PyUnicodeObject *po; |
| 1744 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1745 | Py_BEGIN_ALLOW_THREADS |
| 1746 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1747 | if (attr != 0xFFFFFFFF) { |
| 1748 | if (i & _S_IWRITE) |
| 1749 | attr &= ~FILE_ATTRIBUTE_READONLY; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1750 | else |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1751 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1752 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1753 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1754 | else |
| 1755 | res = 0; |
| 1756 | Py_END_ALLOW_THREADS |
| 1757 | if (!res) |
| 1758 | return win32_error_unicode("chmod", |
| 1759 | PyUnicode_AS_UNICODE(po)); |
| 1760 | Py_INCREF(Py_None); |
| 1761 | return Py_None; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1762 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1763 | /* Drop the argument parsing error as narrow strings |
| 1764 | are also valid. */ |
| 1765 | PyErr_Clear(); |
| 1766 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1767 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1768 | &opath, &i)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1769 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1770 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1771 | Py_BEGIN_ALLOW_THREADS |
| 1772 | attr = GetFileAttributesA(path); |
| 1773 | if (attr != 0xFFFFFFFF) { |
| 1774 | if (i & _S_IWRITE) |
| 1775 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1776 | else |
| 1777 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1778 | res = SetFileAttributesA(path, attr); |
| 1779 | } |
| 1780 | else |
| 1781 | res = 0; |
| 1782 | Py_END_ALLOW_THREADS |
| 1783 | if (!res) { |
| 1784 | win32_error("chmod", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1785 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1786 | return NULL; |
| 1787 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1788 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1789 | Py_INCREF(Py_None); |
| 1790 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1791 | #else /* MS_WINDOWS */ |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1792 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1793 | &opath, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1794 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1795 | path = bytes2str(opath, 1); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1796 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1797 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1798 | Py_END_ALLOW_THREADS |
| 1799 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1800 | return posix_error_with_allocated_filename(opath); |
| 1801 | release_bytes(opath); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1802 | Py_INCREF(Py_None); |
| 1803 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1804 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1807 | #ifdef HAVE_FCHMOD |
| 1808 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1809 | "fchmod(fd, mode)\n\n\ |
| 1810 | Change the access permissions of the file given by file\n\ |
| 1811 | descriptor fd."); |
| 1812 | |
| 1813 | static PyObject * |
| 1814 | posix_fchmod(PyObject *self, PyObject *args) |
| 1815 | { |
| 1816 | int fd, mode, res; |
| 1817 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1818 | return NULL; |
| 1819 | Py_BEGIN_ALLOW_THREADS |
| 1820 | res = fchmod(fd, mode); |
| 1821 | Py_END_ALLOW_THREADS |
| 1822 | if (res < 0) |
| 1823 | return posix_error(); |
| 1824 | Py_RETURN_NONE; |
| 1825 | } |
| 1826 | #endif /* HAVE_FCHMOD */ |
| 1827 | |
| 1828 | #ifdef HAVE_LCHMOD |
| 1829 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1830 | "lchmod(path, mode)\n\n\ |
| 1831 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1832 | affects the link itself rather than the target."); |
| 1833 | |
| 1834 | static PyObject * |
| 1835 | posix_lchmod(PyObject *self, PyObject *args) |
| 1836 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1837 | PyObject *opath; |
| 1838 | char *path; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1839 | int i; |
| 1840 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1841 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 1842 | &opath, &i)) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1843 | return NULL; |
Eric Smith | 86a05ec | 2009-05-05 13:07:30 +0000 | [diff] [blame] | 1844 | path = bytes2str(opath, 1); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1845 | Py_BEGIN_ALLOW_THREADS |
| 1846 | res = lchmod(path, i); |
| 1847 | Py_END_ALLOW_THREADS |
| 1848 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1849 | return posix_error_with_allocated_filename(opath); |
| 1850 | release_bytes(opath); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1851 | Py_RETURN_NONE; |
| 1852 | } |
| 1853 | #endif /* HAVE_LCHMOD */ |
| 1854 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1855 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1856 | #ifdef HAVE_CHFLAGS |
| 1857 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1858 | "chflags(path, flags)\n\n\ |
| 1859 | Set file flags."); |
| 1860 | |
| 1861 | static PyObject * |
| 1862 | posix_chflags(PyObject *self, PyObject *args) |
| 1863 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1864 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1865 | char *path; |
| 1866 | unsigned long flags; |
| 1867 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1868 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 1869 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1870 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1871 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1872 | Py_BEGIN_ALLOW_THREADS |
| 1873 | res = chflags(path, flags); |
| 1874 | Py_END_ALLOW_THREADS |
| 1875 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1876 | return posix_error_with_allocated_filename(opath); |
| 1877 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1878 | Py_INCREF(Py_None); |
| 1879 | return Py_None; |
| 1880 | } |
| 1881 | #endif /* HAVE_CHFLAGS */ |
| 1882 | |
| 1883 | #ifdef HAVE_LCHFLAGS |
| 1884 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1885 | "lchflags(path, flags)\n\n\ |
| 1886 | Set file flags.\n\ |
| 1887 | This function will not follow symbolic links."); |
| 1888 | |
| 1889 | static PyObject * |
| 1890 | posix_lchflags(PyObject *self, PyObject *args) |
| 1891 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1892 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1893 | char *path; |
| 1894 | unsigned long flags; |
| 1895 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1896 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Martin v. Löwis | 4adbc34 | 2009-05-05 17:17:55 +0000 | [diff] [blame] | 1897 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1898 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1899 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1900 | Py_BEGIN_ALLOW_THREADS |
| 1901 | res = lchflags(path, flags); |
| 1902 | Py_END_ALLOW_THREADS |
| 1903 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1904 | return posix_error_with_allocated_filename(opath); |
| 1905 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1906 | Py_INCREF(Py_None); |
| 1907 | return Py_None; |
| 1908 | } |
| 1909 | #endif /* HAVE_LCHFLAGS */ |
| 1910 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1911 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1912 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1913 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1914 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1915 | |
| 1916 | static PyObject * |
| 1917 | posix_chroot(PyObject *self, PyObject *args) |
| 1918 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1919 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1920 | } |
| 1921 | #endif |
| 1922 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1923 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1924 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1925 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1926 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1927 | |
| 1928 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1929 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1930 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1931 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1932 | } |
| 1933 | #endif /* HAVE_FSYNC */ |
| 1934 | |
| 1935 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1936 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1937 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1938 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1939 | #endif |
| 1940 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1941 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1942 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1943 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1944 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1945 | |
| 1946 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1947 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1948 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1949 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1950 | } |
| 1951 | #endif /* HAVE_FDATASYNC */ |
| 1952 | |
| 1953 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1954 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1955 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1956 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1957 | 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] | 1958 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1959 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1960 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1961 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1962 | PyObject *opath; |
| 1963 | char *path; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 1964 | long uid, gid; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1965 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1966 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 1967 | PyUnicode_FSConverter, &opath, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1968 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1969 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1970 | path = bytes2str(opath, 1); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1971 | Py_BEGIN_ALLOW_THREADS |
| 1972 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1973 | Py_END_ALLOW_THREADS |
| 1974 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1975 | return posix_error_with_allocated_filename(opath); |
| 1976 | release_bytes(opath); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1977 | Py_INCREF(Py_None); |
| 1978 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1979 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1980 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1981 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1982 | #ifdef HAVE_FCHOWN |
| 1983 | PyDoc_STRVAR(posix_fchown__doc__, |
| 1984 | "fchown(fd, uid, gid)\n\n\ |
| 1985 | Change the owner and group id of the file given by file descriptor\n\ |
| 1986 | fd to the numeric uid and gid."); |
| 1987 | |
| 1988 | static PyObject * |
| 1989 | posix_fchown(PyObject *self, PyObject *args) |
| 1990 | { |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 1991 | int fd; |
| 1992 | long uid, gid; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1993 | int res; |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 1994 | if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1995 | return NULL; |
| 1996 | Py_BEGIN_ALLOW_THREADS |
| 1997 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 1998 | Py_END_ALLOW_THREADS |
| 1999 | if (res < 0) |
| 2000 | return posix_error(); |
| 2001 | Py_RETURN_NONE; |
| 2002 | } |
| 2003 | #endif /* HAVE_FCHOWN */ |
| 2004 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2005 | #ifdef HAVE_LCHOWN |
| 2006 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2007 | "lchown(path, uid, gid)\n\n\ |
| 2008 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2009 | This function will not follow symbolic links."); |
| 2010 | |
| 2011 | static PyObject * |
| 2012 | posix_lchown(PyObject *self, PyObject *args) |
| 2013 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2014 | PyObject *opath; |
| 2015 | char *path; |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 2016 | long uid, gid; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2017 | int res; |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 2018 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2019 | PyUnicode_FSConverter, &opath, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2020 | &uid, &gid)) |
| 2021 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2022 | path = bytes2str(opath, 1); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2023 | Py_BEGIN_ALLOW_THREADS |
| 2024 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2025 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2026 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2027 | return posix_error_with_allocated_filename(opath); |
| 2028 | release_bytes(opath); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2029 | Py_INCREF(Py_None); |
| 2030 | return Py_None; |
| 2031 | } |
| 2032 | #endif /* HAVE_LCHOWN */ |
| 2033 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2034 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2035 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2036 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2037 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2038 | { |
| 2039 | char buf[1026]; |
| 2040 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2041 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2042 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2043 | if (!use_bytes) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2044 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2045 | wchar_t *wbuf2 = wbuf; |
| 2046 | PyObject *resobj; |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2047 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2048 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2049 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2050 | /* If the buffer is large enough, len does not include the |
| 2051 | terminating \0. If the buffer is too small, len includes |
| 2052 | the space needed for the terminator. */ |
| 2053 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2054 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2055 | if (wbuf2) |
| 2056 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2057 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2058 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2059 | if (!wbuf2) { |
| 2060 | PyErr_NoMemory(); |
| 2061 | return NULL; |
| 2062 | } |
| 2063 | if (!len) { |
| 2064 | if (wbuf2 != wbuf) free(wbuf2); |
| 2065 | return win32_error("getcwdu", NULL); |
| 2066 | } |
| 2067 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2068 | if (wbuf2 != wbuf) free(wbuf2); |
| 2069 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2070 | } |
| 2071 | #endif |
| 2072 | |
| 2073 | Py_BEGIN_ALLOW_THREADS |
| 2074 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2075 | res = _getcwd2(buf, sizeof buf); |
| 2076 | #else |
| 2077 | res = getcwd(buf, sizeof buf); |
| 2078 | #endif |
| 2079 | Py_END_ALLOW_THREADS |
| 2080 | if (res == NULL) |
| 2081 | return posix_error(); |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2082 | if (use_bytes) |
| 2083 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2084 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape"); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2085 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2086 | |
| 2087 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2088 | "getcwd() -> path\n\n\ |
| 2089 | Return a unicode string representing the current working directory."); |
| 2090 | |
| 2091 | static PyObject * |
| 2092 | posix_getcwd_unicode(PyObject *self) |
| 2093 | { |
| 2094 | return posix_getcwd(0); |
| 2095 | } |
| 2096 | |
| 2097 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2098 | "getcwdb() -> path\n\n\ |
| 2099 | Return a bytes string representing the current working directory."); |
| 2100 | |
| 2101 | static PyObject * |
| 2102 | posix_getcwd_bytes(PyObject *self) |
| 2103 | { |
| 2104 | return posix_getcwd(1); |
| 2105 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2106 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2107 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2108 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2109 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2110 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2111 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2112 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2113 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2114 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2115 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2116 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2117 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2118 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2119 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2122 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2123 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2124 | Return a list containing the names of the entries in the directory.\n\ |
| 2125 | \n\ |
| 2126 | path: path of directory to list\n\ |
| 2127 | \n\ |
| 2128 | 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] | 2129 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2130 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2131 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2132 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2133 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2134 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2135 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2136 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2137 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2138 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2139 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2140 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2141 | WIN32_FIND_DATA FileData; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2142 | PyObject *opath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2143 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2144 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2145 | 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] | 2146 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2147 | PyObject *po; |
| 2148 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2149 | WIN32_FIND_DATAW wFileData; |
| 2150 | Py_UNICODE *wnamebuf; |
| 2151 | /* Overallocate for \\*.*\0 */ |
| 2152 | len = PyUnicode_GET_SIZE(po); |
| 2153 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2154 | if (!wnamebuf) { |
| 2155 | PyErr_NoMemory(); |
| 2156 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2157 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2158 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2159 | if (len > 0) { |
| 2160 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2161 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2162 | wnamebuf[len++] = L'\\'; |
| 2163 | wcscpy(wnamebuf + len, L"*.*"); |
| 2164 | } |
| 2165 | if ((d = PyList_New(0)) == NULL) { |
| 2166 | free(wnamebuf); |
| 2167 | return NULL; |
| 2168 | } |
| 2169 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2170 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2171 | int error = GetLastError(); |
| 2172 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2173 | free(wnamebuf); |
| 2174 | return d; |
| 2175 | } |
| 2176 | Py_DECREF(d); |
| 2177 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2178 | free(wnamebuf); |
| 2179 | return NULL; |
| 2180 | } |
| 2181 | do { |
| 2182 | /* Skip over . and .. */ |
| 2183 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2184 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2185 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2186 | if (v == NULL) { |
| 2187 | Py_DECREF(d); |
| 2188 | d = NULL; |
| 2189 | break; |
| 2190 | } |
| 2191 | if (PyList_Append(d, v) != 0) { |
| 2192 | Py_DECREF(v); |
| 2193 | Py_DECREF(d); |
| 2194 | d = NULL; |
| 2195 | break; |
| 2196 | } |
| 2197 | Py_DECREF(v); |
| 2198 | } |
| 2199 | Py_BEGIN_ALLOW_THREADS |
| 2200 | result = FindNextFileW(hFindFile, &wFileData); |
| 2201 | Py_END_ALLOW_THREADS |
| 2202 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2203 | it got to the end of the directory. */ |
| 2204 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2205 | Py_DECREF(d); |
| 2206 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2207 | FindClose(hFindFile); |
| 2208 | free(wnamebuf); |
| 2209 | return NULL; |
| 2210 | } |
| 2211 | } while (result == TRUE); |
| 2212 | |
| 2213 | if (FindClose(hFindFile) == FALSE) { |
| 2214 | Py_DECREF(d); |
| 2215 | win32_error_unicode("FindClose", wnamebuf); |
| 2216 | free(wnamebuf); |
| 2217 | return NULL; |
| 2218 | } |
| 2219 | free(wnamebuf); |
| 2220 | return d; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2221 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2222 | /* Drop the argument parsing error as narrow strings |
| 2223 | are also valid. */ |
| 2224 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2225 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2226 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2227 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2228 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2229 | if (PyObject_Size(opath)+1 > MAX_PATH) { |
| 2230 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2231 | Py_DECREF(opath); |
| 2232 | return NULL; |
| 2233 | } |
| 2234 | strcpy(namebuf, bytes2str(opath, 0)); |
| 2235 | len = PyObject_Size(opath); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2236 | if (len > 0) { |
| 2237 | char ch = namebuf[len-1]; |
| 2238 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2239 | namebuf[len++] = '/'; |
Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2240 | strcpy(namebuf + len, "*.*"); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2241 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2242 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2243 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2244 | return NULL; |
| 2245 | |
| 2246 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2247 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2248 | int error = GetLastError(); |
| 2249 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2250 | return d; |
| 2251 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2252 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2253 | } |
| 2254 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2255 | /* Skip over . and .. */ |
| 2256 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2257 | strcmp(FileData.cFileName, "..") != 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2258 | v = PyBytes_FromString(FileData.cFileName); |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2259 | if (v == NULL) { |
| 2260 | Py_DECREF(d); |
| 2261 | d = NULL; |
| 2262 | break; |
| 2263 | } |
| 2264 | if (PyList_Append(d, v) != 0) { |
| 2265 | Py_DECREF(v); |
| 2266 | Py_DECREF(d); |
| 2267 | d = NULL; |
| 2268 | break; |
| 2269 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2270 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2271 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2272 | Py_BEGIN_ALLOW_THREADS |
| 2273 | result = FindNextFile(hFindFile, &FileData); |
| 2274 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2275 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2276 | it got to the end of the directory. */ |
| 2277 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2278 | Py_DECREF(d); |
| 2279 | win32_error("FindNextFile", namebuf); |
| 2280 | FindClose(hFindFile); |
| 2281 | return NULL; |
| 2282 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2283 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2284 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2285 | if (FindClose(hFindFile) == FALSE) { |
| 2286 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2287 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2288 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2289 | |
| 2290 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2291 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2292 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2293 | |
| 2294 | #ifndef MAX_PATH |
| 2295 | #define MAX_PATH CCHMAXPATH |
| 2296 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2297 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2298 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2299 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2300 | PyObject *d, *v; |
| 2301 | char namebuf[MAX_PATH+5]; |
| 2302 | HDIR hdir = 1; |
| 2303 | ULONG srchcnt = 1; |
| 2304 | FILEFINDBUF3 ep; |
| 2305 | APIRET rc; |
| 2306 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2307 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2308 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2309 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2310 | name = bytes2str(oname); |
| 2311 | len = PyObject_Size(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2312 | if (len >= MAX_PATH) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2313 | release_bytes(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2314 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2315 | return NULL; |
| 2316 | } |
| 2317 | strcpy(namebuf, name); |
| 2318 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2319 | if (*pt == ALTSEP) |
| 2320 | *pt = SEP; |
| 2321 | if (namebuf[len-1] != SEP) |
| 2322 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2323 | strcpy(namebuf + len, "*.*"); |
| 2324 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2325 | if ((d = PyList_New(0)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2326 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2327 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2328 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2329 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2330 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2331 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2332 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2333 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2334 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2335 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2336 | |
| 2337 | if (rc != NO_ERROR) { |
| 2338 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2339 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2342 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2343 | do { |
| 2344 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2345 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2346 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2347 | |
| 2348 | strcpy(namebuf, ep.achName); |
| 2349 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2350 | /* Leave Case of Name Alone -- In Native Form */ |
| 2351 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2352 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2353 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2354 | if (v == NULL) { |
| 2355 | Py_DECREF(d); |
| 2356 | d = NULL; |
| 2357 | break; |
| 2358 | } |
| 2359 | if (PyList_Append(d, v) != 0) { |
| 2360 | Py_DECREF(v); |
| 2361 | Py_DECREF(d); |
| 2362 | d = NULL; |
| 2363 | break; |
| 2364 | } |
| 2365 | Py_DECREF(v); |
| 2366 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2367 | } |
| 2368 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2369 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2370 | return d; |
| 2371 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2372 | PyObject *oname; |
| 2373 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2374 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2375 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2376 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2377 | int arg_is_unicode = 1; |
| 2378 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2379 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2380 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2381 | arg_is_unicode = 0; |
| 2382 | PyErr_Clear(); |
| 2383 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2384 | if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2385 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2386 | name = bytes2str(oname, 1); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2387 | if ((dirp = opendir(name)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2388 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2389 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2390 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2391 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2392 | release_bytes(oname); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2393 | return NULL; |
| 2394 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2395 | for (;;) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2396 | errno = 0; |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2397 | Py_BEGIN_ALLOW_THREADS |
| 2398 | ep = readdir(dirp); |
| 2399 | Py_END_ALLOW_THREADS |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2400 | if (ep == NULL) { |
| 2401 | if (errno == 0) { |
| 2402 | break; |
| 2403 | } else { |
| 2404 | closedir(dirp); |
| 2405 | Py_DECREF(d); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2406 | return posix_error_with_allocated_filename(oname); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2407 | } |
| 2408 | } |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2409 | if (ep->d_name[0] == '.' && |
| 2410 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2411 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2412 | continue; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2413 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2414 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2415 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2416 | d = NULL; |
| 2417 | break; |
| 2418 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2419 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2420 | PyObject *w; |
| 2421 | |
| 2422 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2423 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2424 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2425 | Py_DECREF(v); |
| 2426 | if (w != NULL) |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2427 | v = w; |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2428 | else { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2429 | /* Encoding failed to decode ASCII bytes. |
| 2430 | Raise exception. */ |
| 2431 | Py_DECREF(d); |
| 2432 | d = NULL; |
| 2433 | break; |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2434 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2435 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2436 | if (PyList_Append(d, v) != 0) { |
| 2437 | Py_DECREF(v); |
| 2438 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2439 | d = NULL; |
| 2440 | break; |
| 2441 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2442 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2443 | } |
| 2444 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2445 | release_bytes(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2446 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2447 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2448 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2449 | #endif /* which OS */ |
| 2450 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2451 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2452 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2453 | /* A helper function for abspath on win32 */ |
| 2454 | static PyObject * |
| 2455 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2456 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2457 | PyObject *opath; |
| 2458 | char *path; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2459 | char outbuf[MAX_PATH*2]; |
| 2460 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2461 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2462 | PyUnicodeObject *po; |
| 2463 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2464 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2465 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2466 | Py_UNICODE *wtemp; |
| 2467 | DWORD result; |
| 2468 | PyObject *v; |
| 2469 | result = GetFullPathNameW(wpath, |
| 2470 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2471 | woutbuf, &wtemp); |
| 2472 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2473 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2474 | if (!woutbufp) |
| 2475 | return PyErr_NoMemory(); |
| 2476 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2477 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2478 | if (result) |
| 2479 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2480 | else |
| 2481 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2482 | if (woutbufp != woutbuf) |
| 2483 | free(woutbufp); |
| 2484 | return v; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2485 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2486 | /* Drop the argument parsing error as narrow strings |
| 2487 | are also valid. */ |
| 2488 | PyErr_Clear(); |
| 2489 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2490 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2491 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2492 | PyUnicode_FSConverter, &opath)) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2493 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2494 | path = bytes2str(opath, 1); |
| 2495 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2496 | outbuf, &temp)) { |
| 2497 | win32_error("GetFullPathName", path); |
| 2498 | release_bytes(opath); |
| 2499 | return NULL; |
| 2500 | } |
| 2501 | release_bytes(opath); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2502 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2503 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2504 | Py_FileSystemDefaultEncoding, NULL); |
| 2505 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2506 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2507 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2508 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2510 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2511 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2512 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2513 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2514 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2515 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2516 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2517 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2518 | PyObject *opath; |
| 2519 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2520 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2521 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2522 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2523 | PyUnicodeObject *po; |
| 2524 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 2525 | Py_BEGIN_ALLOW_THREADS |
| 2526 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2527 | it is a simple dereference. */ |
| 2528 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 2529 | Py_END_ALLOW_THREADS |
| 2530 | if (!res) |
| 2531 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 2532 | Py_INCREF(Py_None); |
| 2533 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2534 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2535 | /* Drop the argument parsing error as narrow strings |
| 2536 | are also valid. */ |
| 2537 | PyErr_Clear(); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2538 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2539 | PyUnicode_FSConverter, &opath, &mode)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2540 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2541 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2542 | Py_BEGIN_ALLOW_THREADS |
| 2543 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2544 | it is a simple dereference. */ |
| 2545 | res = CreateDirectoryA(path, NULL); |
| 2546 | Py_END_ALLOW_THREADS |
| 2547 | if (!res) { |
| 2548 | win32_error("mkdir", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2549 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2550 | return NULL; |
| 2551 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2552 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2553 | Py_INCREF(Py_None); |
| 2554 | return Py_None; |
| 2555 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2556 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2557 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2558 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2559 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2560 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2561 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2562 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2563 | res = mkdir(path); |
| 2564 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2565 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2566 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2567 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2568 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2569 | return posix_error_with_allocated_filename(opath); |
| 2570 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2571 | Py_INCREF(Py_None); |
| 2572 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2573 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2576 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2577 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2578 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2579 | #include <sys/resource.h> |
| 2580 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2581 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2582 | |
| 2583 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2584 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2585 | "nice(inc) -> new_priority\n\n\ |
| 2586 | 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] | 2587 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2588 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2589 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2590 | { |
| 2591 | int increment, value; |
| 2592 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2593 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2594 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2595 | |
| 2596 | /* There are two flavours of 'nice': one that returns the new |
| 2597 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2598 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2599 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2600 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2601 | If we are of the nice family that returns the new priority, we |
| 2602 | need to clear errno before the call, and check if errno is filled |
| 2603 | before calling posix_error() on a returnvalue of -1, because the |
| 2604 | -1 may be the actual new priority! */ |
| 2605 | |
| 2606 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2607 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2608 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2609 | if (value == 0) |
| 2610 | value = getpriority(PRIO_PROCESS, 0); |
| 2611 | #endif |
| 2612 | if (value == -1 && errno != 0) |
| 2613 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2614 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2615 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2616 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2617 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2619 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2620 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2621 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2622 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2623 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2624 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2625 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2626 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2627 | PyObject *o1, *o2; |
| 2628 | char *p1, *p2; |
| 2629 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2630 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2631 | goto error; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2632 | if (!convert_to_unicode(&o1)) |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2633 | goto error; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2634 | if (!convert_to_unicode(&o2)) { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2635 | Py_DECREF(o1); |
| 2636 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2637 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2638 | Py_BEGIN_ALLOW_THREADS |
| 2639 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2640 | PyUnicode_AsUnicode(o2)); |
| 2641 | Py_END_ALLOW_THREADS |
| 2642 | Py_DECREF(o1); |
| 2643 | Py_DECREF(o2); |
| 2644 | if (!result) |
| 2645 | return win32_error("rename", NULL); |
| 2646 | Py_INCREF(Py_None); |
| 2647 | return Py_None; |
| 2648 | error: |
| 2649 | PyErr_Clear(); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2650 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2651 | return NULL; |
| 2652 | Py_BEGIN_ALLOW_THREADS |
| 2653 | result = MoveFileA(p1, p2); |
| 2654 | Py_END_ALLOW_THREADS |
| 2655 | if (!result) |
| 2656 | return win32_error("rename", NULL); |
| 2657 | Py_INCREF(Py_None); |
| 2658 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2659 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2660 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2661 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2664 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2665 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2666 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2667 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2668 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2669 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2670 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2671 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2672 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2673 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2674 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2675 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2676 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2680 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2681 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2682 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2683 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2684 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2685 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2686 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2687 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2688 | 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] | 2689 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2690 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2691 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2694 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2695 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2696 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2697 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2698 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2699 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2700 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2701 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2702 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2703 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2704 | #ifdef MS_WINDOWS |
| 2705 | wchar_t *command; |
| 2706 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2707 | return NULL; |
| 2708 | #else |
| 2709 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2710 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2711 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2712 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2713 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2714 | #ifdef MS_WINDOWS |
| 2715 | sts = _wsystem(command); |
| 2716 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2717 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2718 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2719 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2720 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2721 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2722 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2723 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2725 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2726 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2727 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2728 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2729 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2730 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2731 | { |
| 2732 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2733 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2734 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2735 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2736 | if (i < 0) |
| 2737 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2738 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2742 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2743 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2744 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2746 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2747 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2748 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2749 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2750 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2751 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2752 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2753 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2754 | return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2755 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2756 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2757 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2760 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2761 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2762 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2763 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2764 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2765 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2766 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2767 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2768 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2769 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2770 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2771 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2772 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2773 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2774 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2775 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2776 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2777 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2778 | u.sysname, |
| 2779 | u.nodename, |
| 2780 | u.release, |
| 2781 | u.version, |
| 2782 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2783 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2784 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2785 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2786 | static int |
| 2787 | extract_time(PyObject *t, long* sec, long* usec) |
| 2788 | { |
| 2789 | long intval; |
| 2790 | if (PyFloat_Check(t)) { |
| 2791 | double tval = PyFloat_AsDouble(t); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2792 | PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2793 | if (!intobj) |
| 2794 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2795 | intval = PyLong_AsLong(intobj); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2796 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2797 | if (intval == -1 && PyErr_Occurred()) |
| 2798 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2799 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2800 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2801 | if (*usec < 0) |
| 2802 | /* If rounding gave us a negative number, |
| 2803 | truncate. */ |
| 2804 | *usec = 0; |
| 2805 | return 0; |
| 2806 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2807 | intval = PyLong_AsLong(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2808 | if (intval == -1 && PyErr_Occurred()) |
| 2809 | return -1; |
| 2810 | *sec = intval; |
| 2811 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2812 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2813 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2815 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2816 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2817 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2818 | 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] | 2819 | 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] | 2820 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2821 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2822 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2823 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2824 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2825 | PyObject *arg; |
| 2826 | PyUnicodeObject *obwpath; |
| 2827 | wchar_t *wpath = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2828 | PyObject *oapath; |
| 2829 | char *apath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2830 | HANDLE hFile; |
| 2831 | long atimesec, mtimesec, ausec, musec; |
| 2832 | FILETIME atime, mtime; |
| 2833 | PyObject *result = NULL; |
| 2834 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2835 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2836 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2837 | Py_BEGIN_ALLOW_THREADS |
| 2838 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 2839 | NULL, OPEN_EXISTING, |
| 2840 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2841 | Py_END_ALLOW_THREADS |
| 2842 | if (hFile == INVALID_HANDLE_VALUE) |
| 2843 | return win32_error_unicode("utime", wpath); |
| 2844 | } else |
| 2845 | /* Drop the argument parsing error as narrow strings |
| 2846 | are also valid. */ |
| 2847 | PyErr_Clear(); |
| 2848 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2849 | if (!wpath) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2850 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 2851 | PyUnicode_FSConverter, &oapath, &arg)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2852 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2853 | apath = bytes2str(oapath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2854 | Py_BEGIN_ALLOW_THREADS |
| 2855 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2856 | NULL, OPEN_EXISTING, |
| 2857 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2858 | Py_END_ALLOW_THREADS |
| 2859 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2860 | win32_error("utime", apath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2861 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2862 | return NULL; |
| 2863 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2864 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | if (arg == Py_None) { |
| 2868 | SYSTEMTIME now; |
| 2869 | GetSystemTime(&now); |
| 2870 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2871 | !SystemTimeToFileTime(&now, &atime)) { |
| 2872 | win32_error("utime", NULL); |
| 2873 | goto done; |
| 2874 | } |
| 2875 | } |
| 2876 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2877 | PyErr_SetString(PyExc_TypeError, |
| 2878 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2879 | goto done; |
| 2880 | } |
| 2881 | else { |
| 2882 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2883 | &atimesec, &ausec) == -1) |
| 2884 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2885 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2886 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2887 | &mtimesec, &musec) == -1) |
| 2888 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2889 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2890 | } |
| 2891 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2892 | /* Avoid putting the file name into the error here, |
| 2893 | as that may confuse the user into believing that |
| 2894 | something is wrong with the file, when it also |
| 2895 | could be the time stamp that gives a problem. */ |
| 2896 | win32_error("utime", NULL); |
| 2897 | } |
| 2898 | Py_INCREF(Py_None); |
| 2899 | result = Py_None; |
| 2900 | done: |
| 2901 | CloseHandle(hFile); |
| 2902 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2903 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2904 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2905 | PyObject *opath; |
| 2906 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2907 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2908 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2909 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2910 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2911 | #if defined(HAVE_UTIMES) |
| 2912 | struct timeval buf[2]; |
| 2913 | #define ATIME buf[0].tv_sec |
| 2914 | #define MTIME buf[1].tv_sec |
| 2915 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2916 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2917 | struct utimbuf buf; |
| 2918 | #define ATIME buf.actime |
| 2919 | #define MTIME buf.modtime |
| 2920 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2921 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2922 | time_t buf[2]; |
| 2923 | #define ATIME buf[0] |
| 2924 | #define MTIME buf[1] |
| 2925 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2926 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2927 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2928 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2929 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 2930 | PyUnicode_FSConverter, &opath, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2931 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2932 | path = bytes2str(opath, 1); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2933 | if (arg == Py_None) { |
| 2934 | /* optional time values not given */ |
| 2935 | Py_BEGIN_ALLOW_THREADS |
| 2936 | res = utime(path, NULL); |
| 2937 | Py_END_ALLOW_THREADS |
| 2938 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2939 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2940 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2941 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2942 | release_bytes(opath); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2943 | return NULL; |
| 2944 | } |
| 2945 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2946 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2947 | &atime, &ausec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2948 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2949 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2950 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2951 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2952 | &mtime, &musec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2953 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2954 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2955 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2956 | ATIME = atime; |
| 2957 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2958 | #ifdef HAVE_UTIMES |
| 2959 | buf[0].tv_usec = ausec; |
| 2960 | buf[1].tv_usec = musec; |
| 2961 | Py_BEGIN_ALLOW_THREADS |
| 2962 | res = utimes(path, buf); |
| 2963 | Py_END_ALLOW_THREADS |
| 2964 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2965 | Py_BEGIN_ALLOW_THREADS |
| 2966 | res = utime(path, UTIME_ARG); |
| 2967 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2968 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2969 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2970 | if (res < 0) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2971 | return posix_error_with_allocated_filename(opath); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2972 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2973 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2974 | Py_INCREF(Py_None); |
| 2975 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2976 | #undef UTIME_ARG |
| 2977 | #undef ATIME |
| 2978 | #undef MTIME |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2979 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2982 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2983 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2984 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2985 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2986 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2987 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2988 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2989 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2990 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2991 | { |
| 2992 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2993 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2994 | return NULL; |
| 2995 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2996 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2999 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3000 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3001 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3002 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3003 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3004 | for (i = 0; i < count; i++) |
| 3005 | PyMem_Free(array[i]); |
| 3006 | PyMem_DEL(array); |
| 3007 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3008 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3009 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3010 | int fsconvert_strdup(PyObject *o, char**out) |
| 3011 | { |
| 3012 | PyObject *bytes; |
| 3013 | Py_ssize_t size; |
| 3014 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3015 | return 0; |
| 3016 | size = PyObject_Size(bytes); |
| 3017 | *out = PyMem_Malloc(size+1); |
| 3018 | if (!*out) |
| 3019 | return 0; |
| 3020 | /* Don't lock bytes, as we hold the GIL */ |
| 3021 | memcpy(*out, bytes2str(bytes, 0), size+1); |
| 3022 | Py_DECREF(bytes); |
| 3023 | return 1; |
| 3024 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3025 | #endif |
| 3026 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3027 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3028 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3029 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3030 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3031 | Execute an executable path with arguments, replacing current process.\n\ |
| 3032 | \n\ |
| 3033 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3034 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3035 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3036 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3037 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3038 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3039 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3040 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3041 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3042 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3043 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3044 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3045 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 3046 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3047 | argv is a list or tuple of strings. */ |
| 3048 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3049 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3050 | PyUnicode_FSConverter, |
| 3051 | &opath, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3052 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3053 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3054 | if (PyList_Check(argv)) { |
| 3055 | argc = PyList_Size(argv); |
| 3056 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3057 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3058 | else if (PyTuple_Check(argv)) { |
| 3059 | argc = PyTuple_Size(argv); |
| 3060 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3061 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3062 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3063 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3064 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3065 | return NULL; |
| 3066 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3067 | if (argc < 1) { |
| 3068 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3069 | release_bytes(opath); |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3070 | return NULL; |
| 3071 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3072 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3073 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3074 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3075 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3076 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3077 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3078 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3079 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3080 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3081 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3082 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3083 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3084 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3085 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3086 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3087 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3088 | } |
| 3089 | argvlist[argc] = NULL; |
| 3090 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3091 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3092 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3093 | /* If we get here it's definitely an error */ |
| 3094 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3095 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3096 | release_bytes(opath); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3097 | return posix_error(); |
| 3098 | } |
| 3099 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3101 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3102 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3103 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3104 | \n\ |
| 3105 | path: path of executable file\n\ |
| 3106 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3107 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3110 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3111 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3112 | PyObject *opath; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3113 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3114 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3115 | char **argvlist; |
| 3116 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3117 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3118 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3119 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3120 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3121 | |
| 3122 | /* execve has three arguments: (path, argv, env), where |
| 3123 | argv is a list or tuple of strings and env is a dictionary |
| 3124 | like posix.environ. */ |
| 3125 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3126 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3127 | PyUnicode_FSConverter, |
| 3128 | &opath, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3129 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3130 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3131 | if (PyList_Check(argv)) { |
| 3132 | argc = PyList_Size(argv); |
| 3133 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3134 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3135 | else if (PyTuple_Check(argv)) { |
| 3136 | argc = PyTuple_Size(argv); |
| 3137 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3138 | } |
| 3139 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3140 | PyErr_SetString(PyExc_TypeError, |
| 3141 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3142 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3143 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3144 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3145 | PyErr_SetString(PyExc_TypeError, |
| 3146 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3147 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3150 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3151 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3152 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3153 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3154 | } |
| 3155 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3156 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3157 | &argvlist[i])) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3158 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3159 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3160 | goto fail_1; |
| 3161 | } |
| 3162 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3163 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3164 | argvlist[argc] = NULL; |
| 3165 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3166 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3167 | if (i < 0) |
| 3168 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3169 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3170 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3171 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3172 | goto fail_1; |
| 3173 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3174 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3175 | keys = PyMapping_Keys(env); |
| 3176 | vals = PyMapping_Values(env); |
| 3177 | if (!keys || !vals) |
| 3178 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3179 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3180 | PyErr_SetString(PyExc_TypeError, |
| 3181 | "execve(): env.keys() or env.values() is not a list"); |
| 3182 | goto fail_2; |
| 3183 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3184 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3185 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3186 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3187 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3188 | |
| 3189 | key = PyList_GetItem(keys, pos); |
| 3190 | val = PyList_GetItem(vals, pos); |
| 3191 | if (!key || !val) |
| 3192 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3193 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3194 | if (!PyArg_Parse( |
| 3195 | key, |
| 3196 | "s;execve() arg 3 contains a non-string key", |
| 3197 | &k) || |
| 3198 | !PyArg_Parse( |
| 3199 | val, |
| 3200 | "s;execve() arg 3 contains a non-string value", |
| 3201 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3202 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3203 | goto fail_2; |
| 3204 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3205 | |
| 3206 | #if defined(PYOS_OS2) |
| 3207 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3208 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3209 | #endif |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3210 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3211 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3212 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3213 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3214 | goto fail_2; |
| 3215 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3216 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3217 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3218 | #if defined(PYOS_OS2) |
| 3219 | } |
| 3220 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3221 | } |
| 3222 | envlist[envc] = 0; |
| 3223 | |
| 3224 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3225 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3226 | /* If we get here it's definitely an error */ |
| 3227 | |
| 3228 | (void) posix_error(); |
| 3229 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3230 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3231 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3232 | PyMem_DEL(envlist[envc]); |
| 3233 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3234 | fail_1: |
| 3235 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3236 | Py_XDECREF(vals); |
| 3237 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3238 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3239 | release_bytes(opath); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3240 | return NULL; |
| 3241 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3242 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3243 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3244 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3245 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3246 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3247 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3248 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3249 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3250 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3251 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3252 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3253 | |
| 3254 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3255 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3256 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3257 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3258 | char *path; |
| 3259 | PyObject *argv; |
| 3260 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3261 | int mode, i; |
| 3262 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3263 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3264 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3265 | |
| 3266 | /* spawnv has three arguments: (mode, path, argv), where |
| 3267 | argv is a list or tuple of strings. */ |
| 3268 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3269 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 3270 | PyUnicode_FSConverter, |
| 3271 | &opath, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3272 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3273 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3274 | if (PyList_Check(argv)) { |
| 3275 | argc = PyList_Size(argv); |
| 3276 | getitem = PyList_GetItem; |
| 3277 | } |
| 3278 | else if (PyTuple_Check(argv)) { |
| 3279 | argc = PyTuple_Size(argv); |
| 3280 | getitem = PyTuple_GetItem; |
| 3281 | } |
| 3282 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3283 | PyErr_SetString(PyExc_TypeError, |
| 3284 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3285 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3286 | return NULL; |
| 3287 | } |
| 3288 | |
| 3289 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3290 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3291 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3292 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3293 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3294 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3295 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3296 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3297 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3298 | PyErr_SetString( |
| 3299 | PyExc_TypeError, |
| 3300 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3301 | release_bytes(opath); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3302 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3303 | } |
| 3304 | } |
| 3305 | argvlist[argc] = NULL; |
| 3306 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3307 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3308 | Py_BEGIN_ALLOW_THREADS |
| 3309 | spawnval = spawnv(mode, path, argvlist); |
| 3310 | Py_END_ALLOW_THREADS |
| 3311 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3312 | if (mode == _OLD_P_OVERLAY) |
| 3313 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3314 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3315 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3316 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3317 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3318 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3319 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3320 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3321 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3322 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3323 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3324 | return posix_error(); |
| 3325 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3326 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3327 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3328 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3329 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3330 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3334 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3335 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3336 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3337 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3338 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3339 | path: path of executable file\n\ |
| 3340 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3341 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3342 | |
| 3343 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3344 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3345 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3346 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3347 | char *path; |
| 3348 | PyObject *argv, *env; |
| 3349 | char **argvlist; |
| 3350 | char **envlist; |
| 3351 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3352 | int mode, pos, envc; |
| 3353 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3354 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3355 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3356 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3357 | |
| 3358 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3359 | argv is a list or tuple of strings and env is a dictionary |
| 3360 | like posix.environ. */ |
| 3361 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3362 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 3363 | PyUnicode_FSConverter, |
| 3364 | &opath, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3365 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3366 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3367 | if (PyList_Check(argv)) { |
| 3368 | argc = PyList_Size(argv); |
| 3369 | getitem = PyList_GetItem; |
| 3370 | } |
| 3371 | else if (PyTuple_Check(argv)) { |
| 3372 | argc = PyTuple_Size(argv); |
| 3373 | getitem = PyTuple_GetItem; |
| 3374 | } |
| 3375 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3376 | PyErr_SetString(PyExc_TypeError, |
| 3377 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3378 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3379 | } |
| 3380 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3381 | PyErr_SetString(PyExc_TypeError, |
| 3382 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3383 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | argvlist = PyMem_NEW(char *, argc+1); |
| 3387 | if (argvlist == NULL) { |
| 3388 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3389 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3390 | } |
| 3391 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3392 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3393 | &argvlist[i])) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3394 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3395 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3396 | goto fail_1; |
| 3397 | } |
| 3398 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3399 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3400 | argvlist[argc] = NULL; |
| 3401 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3402 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3403 | if (i < 0) |
| 3404 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3405 | envlist = PyMem_NEW(char *, i + 1); |
| 3406 | if (envlist == NULL) { |
| 3407 | PyErr_NoMemory(); |
| 3408 | goto fail_1; |
| 3409 | } |
| 3410 | envc = 0; |
| 3411 | keys = PyMapping_Keys(env); |
| 3412 | vals = PyMapping_Values(env); |
| 3413 | if (!keys || !vals) |
| 3414 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3415 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3416 | PyErr_SetString(PyExc_TypeError, |
| 3417 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3418 | goto fail_2; |
| 3419 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3420 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3421 | for (pos = 0; pos < i; pos++) { |
| 3422 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3423 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3424 | |
| 3425 | key = PyList_GetItem(keys, pos); |
| 3426 | val = PyList_GetItem(vals, pos); |
| 3427 | if (!key || !val) |
| 3428 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3429 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3430 | if (!PyArg_Parse( |
| 3431 | key, |
| 3432 | "s;spawnve() arg 3 contains a non-string key", |
| 3433 | &k) || |
| 3434 | !PyArg_Parse( |
| 3435 | val, |
| 3436 | "s;spawnve() arg 3 contains a non-string value", |
| 3437 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3438 | { |
| 3439 | goto fail_2; |
| 3440 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3441 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3442 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3443 | if (p == NULL) { |
| 3444 | PyErr_NoMemory(); |
| 3445 | goto fail_2; |
| 3446 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3447 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3448 | envlist[envc++] = p; |
| 3449 | } |
| 3450 | envlist[envc] = 0; |
| 3451 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3452 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3453 | Py_BEGIN_ALLOW_THREADS |
| 3454 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3455 | Py_END_ALLOW_THREADS |
| 3456 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3457 | if (mode == _OLD_P_OVERLAY) |
| 3458 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3459 | |
| 3460 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3461 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3462 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3463 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3464 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3465 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3466 | (void) posix_error(); |
| 3467 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3468 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3469 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3470 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3471 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3472 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3473 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3474 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3475 | while (--envc >= 0) |
| 3476 | PyMem_DEL(envlist[envc]); |
| 3477 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3478 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3479 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3480 | Py_XDECREF(vals); |
| 3481 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3482 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3483 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3484 | return res; |
| 3485 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3486 | |
| 3487 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3488 | #if defined(PYOS_OS2) |
| 3489 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3490 | "spawnvp(mode, file, args)\n\n\ |
| 3491 | Execute the program 'file' in a new process, using the environment\n\ |
| 3492 | search path to find the file.\n\ |
| 3493 | \n\ |
| 3494 | mode: mode of process creation\n\ |
| 3495 | file: executable file name\n\ |
| 3496 | args: tuple or list of strings"); |
| 3497 | |
| 3498 | static PyObject * |
| 3499 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3500 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3501 | PyObject *opath; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3502 | char *path; |
| 3503 | PyObject *argv; |
| 3504 | char **argvlist; |
| 3505 | int mode, i, argc; |
| 3506 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3507 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3508 | |
| 3509 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3510 | argv is a list or tuple of strings. */ |
| 3511 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3512 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 3513 | PyUnicode_FSConverter, |
| 3514 | &opath, &argv)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3515 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3516 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3517 | if (PyList_Check(argv)) { |
| 3518 | argc = PyList_Size(argv); |
| 3519 | getitem = PyList_GetItem; |
| 3520 | } |
| 3521 | else if (PyTuple_Check(argv)) { |
| 3522 | argc = PyTuple_Size(argv); |
| 3523 | getitem = PyTuple_GetItem; |
| 3524 | } |
| 3525 | else { |
| 3526 | PyErr_SetString(PyExc_TypeError, |
| 3527 | "spawnvp() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3528 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3529 | return NULL; |
| 3530 | } |
| 3531 | |
| 3532 | argvlist = PyMem_NEW(char *, argc+1); |
| 3533 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3534 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3535 | return PyErr_NoMemory(); |
| 3536 | } |
| 3537 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3538 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3539 | &argvlist[i])) { |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3540 | free_string_array(argvlist, i); |
| 3541 | PyErr_SetString( |
| 3542 | PyExc_TypeError, |
| 3543 | "spawnvp() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3544 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3545 | return NULL; |
| 3546 | } |
| 3547 | } |
| 3548 | argvlist[argc] = NULL; |
| 3549 | |
| 3550 | Py_BEGIN_ALLOW_THREADS |
| 3551 | #if defined(PYCC_GCC) |
| 3552 | spawnval = spawnvp(mode, path, argvlist); |
| 3553 | #else |
| 3554 | spawnval = _spawnvp(mode, path, argvlist); |
| 3555 | #endif |
| 3556 | Py_END_ALLOW_THREADS |
| 3557 | |
| 3558 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3559 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3560 | |
| 3561 | if (spawnval == -1) |
| 3562 | return posix_error(); |
| 3563 | else |
| 3564 | return Py_BuildValue("l", (long) spawnval); |
| 3565 | } |
| 3566 | |
| 3567 | |
| 3568 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3569 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3570 | Execute the program 'file' in a new process, using the environment\n\ |
| 3571 | search path to find the file.\n\ |
| 3572 | \n\ |
| 3573 | mode: mode of process creation\n\ |
| 3574 | file: executable file name\n\ |
| 3575 | args: tuple or list of arguments\n\ |
| 3576 | env: dictionary of strings mapping to strings"); |
| 3577 | |
| 3578 | static PyObject * |
| 3579 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3580 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3581 | PyObject *opath |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3582 | char *path; |
| 3583 | PyObject *argv, *env; |
| 3584 | char **argvlist; |
| 3585 | char **envlist; |
| 3586 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3587 | int mode, i, pos, argc, envc; |
| 3588 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3589 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3590 | int lastarg = 0; |
| 3591 | |
| 3592 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3593 | argv is a list or tuple of strings and env is a dictionary |
| 3594 | like posix.environ. */ |
| 3595 | |
| 3596 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3597 | PyUnicode_FSConverter, |
| 3598 | &opath, &argv, &env)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3599 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3600 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3601 | if (PyList_Check(argv)) { |
| 3602 | argc = PyList_Size(argv); |
| 3603 | getitem = PyList_GetItem; |
| 3604 | } |
| 3605 | else if (PyTuple_Check(argv)) { |
| 3606 | argc = PyTuple_Size(argv); |
| 3607 | getitem = PyTuple_GetItem; |
| 3608 | } |
| 3609 | else { |
| 3610 | PyErr_SetString(PyExc_TypeError, |
| 3611 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3612 | goto fail_0; |
| 3613 | } |
| 3614 | if (!PyMapping_Check(env)) { |
| 3615 | PyErr_SetString(PyExc_TypeError, |
| 3616 | "spawnvpe() arg 3 must be a mapping object"); |
| 3617 | goto fail_0; |
| 3618 | } |
| 3619 | |
| 3620 | argvlist = PyMem_NEW(char *, argc+1); |
| 3621 | if (argvlist == NULL) { |
| 3622 | PyErr_NoMemory(); |
| 3623 | goto fail_0; |
| 3624 | } |
| 3625 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3626 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3627 | &argvlist[i])) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3628 | { |
| 3629 | lastarg = i; |
| 3630 | goto fail_1; |
| 3631 | } |
| 3632 | } |
| 3633 | lastarg = argc; |
| 3634 | argvlist[argc] = NULL; |
| 3635 | |
| 3636 | i = PyMapping_Size(env); |
| 3637 | if (i < 0) |
| 3638 | goto fail_1; |
| 3639 | envlist = PyMem_NEW(char *, i + 1); |
| 3640 | if (envlist == NULL) { |
| 3641 | PyErr_NoMemory(); |
| 3642 | goto fail_1; |
| 3643 | } |
| 3644 | envc = 0; |
| 3645 | keys = PyMapping_Keys(env); |
| 3646 | vals = PyMapping_Values(env); |
| 3647 | if (!keys || !vals) |
| 3648 | goto fail_2; |
| 3649 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3650 | PyErr_SetString(PyExc_TypeError, |
| 3651 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3652 | goto fail_2; |
| 3653 | } |
| 3654 | |
| 3655 | for (pos = 0; pos < i; pos++) { |
| 3656 | char *p, *k, *v; |
| 3657 | size_t len; |
| 3658 | |
| 3659 | key = PyList_GetItem(keys, pos); |
| 3660 | val = PyList_GetItem(vals, pos); |
| 3661 | if (!key || !val) |
| 3662 | goto fail_2; |
| 3663 | |
| 3664 | if (!PyArg_Parse( |
| 3665 | key, |
| 3666 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3667 | &k) || |
| 3668 | !PyArg_Parse( |
| 3669 | val, |
| 3670 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3671 | &v)) |
| 3672 | { |
| 3673 | goto fail_2; |
| 3674 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3675 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3676 | p = PyMem_NEW(char, len); |
| 3677 | if (p == NULL) { |
| 3678 | PyErr_NoMemory(); |
| 3679 | goto fail_2; |
| 3680 | } |
| 3681 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3682 | envlist[envc++] = p; |
| 3683 | } |
| 3684 | envlist[envc] = 0; |
| 3685 | |
| 3686 | Py_BEGIN_ALLOW_THREADS |
| 3687 | #if defined(PYCC_GCC) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3688 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3689 | #else |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3690 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3691 | #endif |
| 3692 | Py_END_ALLOW_THREADS |
| 3693 | |
| 3694 | if (spawnval == -1) |
| 3695 | (void) posix_error(); |
| 3696 | else |
| 3697 | res = Py_BuildValue("l", (long) spawnval); |
| 3698 | |
| 3699 | fail_2: |
| 3700 | while (--envc >= 0) |
| 3701 | PyMem_DEL(envlist[envc]); |
| 3702 | PyMem_DEL(envlist); |
| 3703 | fail_1: |
| 3704 | free_string_array(argvlist, lastarg); |
| 3705 | Py_XDECREF(vals); |
| 3706 | Py_XDECREF(keys); |
| 3707 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3708 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3709 | return res; |
| 3710 | } |
| 3711 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3712 | #endif /* HAVE_SPAWNV */ |
| 3713 | |
| 3714 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3715 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3716 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3717 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3718 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3719 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3720 | 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] | 3721 | |
| 3722 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3723 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3724 | { |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3725 | pid_t pid; |
| 3726 | int result; |
| 3727 | _PyImport_AcquireLock(); |
| 3728 | pid = fork1(); |
| 3729 | result = _PyImport_ReleaseLock(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3730 | if (pid == -1) |
| 3731 | return posix_error(); |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 3732 | if (pid == 0) |
| 3733 | PyOS_AfterFork(); |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3734 | if (result < 0) { |
| 3735 | /* Don't clobber the OSError if the fork failed. */ |
| 3736 | PyErr_SetString(PyExc_RuntimeError, |
| 3737 | "not holding the import lock"); |
| 3738 | return NULL; |
| 3739 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3740 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3741 | } |
| 3742 | #endif |
| 3743 | |
| 3744 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3745 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3746 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3747 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3748 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3749 | 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] | 3750 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3751 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3752 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3753 | { |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3754 | pid_t pid; |
| 3755 | int result; |
| 3756 | _PyImport_AcquireLock(); |
| 3757 | pid = fork(); |
| 3758 | result = _PyImport_ReleaseLock(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3759 | if (pid == -1) |
| 3760 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3761 | if (pid == 0) |
| 3762 | PyOS_AfterFork(); |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3763 | if (result < 0) { |
| 3764 | /* Don't clobber the OSError if the fork failed. */ |
| 3765 | PyErr_SetString(PyExc_RuntimeError, |
| 3766 | "not holding the import lock"); |
| 3767 | return NULL; |
| 3768 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3769 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3770 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3771 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3772 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3773 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3774 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3775 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3776 | #define DEV_PTY_FILE "/dev/ptc" |
| 3777 | #define HAVE_DEV_PTMX |
| 3778 | #else |
| 3779 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3780 | #endif |
| 3781 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3782 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3783 | #ifdef HAVE_PTY_H |
| 3784 | #include <pty.h> |
| 3785 | #else |
| 3786 | #ifdef HAVE_LIBUTIL_H |
| 3787 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3788 | #endif /* HAVE_LIBUTIL_H */ |
| 3789 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3790 | #ifdef HAVE_STROPTS_H |
| 3791 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3792 | #endif |
| 3793 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3794 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3795 | #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] | 3796 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3797 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3798 | 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] | 3799 | |
| 3800 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3801 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3802 | { |
| 3803 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3804 | #ifndef HAVE_OPENPTY |
| 3805 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3806 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3807 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3808 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3809 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3810 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3811 | #endif |
| 3812 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3813 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3814 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3815 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3816 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3817 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3818 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3819 | if (slave_name == NULL) |
| 3820 | return posix_error(); |
| 3821 | |
| 3822 | slave_fd = open(slave_name, O_RDWR); |
| 3823 | if (slave_fd < 0) |
| 3824 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3825 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3826 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3827 | if (master_fd < 0) |
| 3828 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3829 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3830 | /* change permission of slave */ |
| 3831 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3832 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3833 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3834 | } |
| 3835 | /* unlock slave */ |
| 3836 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3837 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3838 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3839 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3840 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3841 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3842 | if (slave_name == NULL) |
| 3843 | return posix_error(); |
| 3844 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3845 | if (slave_fd < 0) |
| 3846 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3847 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3848 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3849 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3850 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3851 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3852 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3853 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3854 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3855 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3856 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3857 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3858 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3859 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3860 | |
| 3861 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3862 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3863 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3864 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3865 | 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] | 3866 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3867 | |
| 3868 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3869 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3870 | { |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3871 | int master_fd = -1, result; |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3872 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3873 | |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3874 | _PyImport_AcquireLock(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3875 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3876 | result = _PyImport_ReleaseLock(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3877 | if (pid == -1) |
| 3878 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3879 | if (pid == 0) |
| 3880 | PyOS_AfterFork(); |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3881 | if (result < 0) { |
| 3882 | /* Don't clobber the OSError if the fork failed. */ |
| 3883 | PyErr_SetString(PyExc_RuntimeError, |
| 3884 | "not holding the import lock"); |
| 3885 | return NULL; |
| 3886 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3887 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3888 | } |
| 3889 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3890 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3891 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3892 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3893 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3894 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3895 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3896 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3897 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3898 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3899 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3900 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3901 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3902 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3903 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3904 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3905 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3906 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3907 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3908 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3909 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3910 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3911 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3912 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3913 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3914 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3915 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3916 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3917 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3918 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3919 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3920 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3921 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3922 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3923 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3924 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3925 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3926 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3927 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3928 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3930 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3931 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3932 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3933 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3934 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3935 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3936 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 3937 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3940 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3941 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3942 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3943 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3944 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3945 | |
| 3946 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3947 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3948 | { |
| 3949 | PyObject *result = NULL; |
| 3950 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3951 | #ifdef NGROUPS_MAX |
| 3952 | #define MAX_GROUPS NGROUPS_MAX |
| 3953 | #else |
| 3954 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3955 | #define MAX_GROUPS 64 |
| 3956 | #endif |
| 3957 | gid_t grouplist[MAX_GROUPS]; |
| 3958 | int n; |
| 3959 | |
| 3960 | n = getgroups(MAX_GROUPS, grouplist); |
| 3961 | if (n < 0) |
| 3962 | posix_error(); |
| 3963 | else { |
| 3964 | result = PyList_New(n); |
| 3965 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3966 | int i; |
| 3967 | for (i = 0; i < n; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3968 | PyObject *o = PyLong_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3969 | if (o == NULL) { |
| 3970 | Py_DECREF(result); |
| 3971 | result = NULL; |
| 3972 | break; |
| 3973 | } |
| 3974 | PyList_SET_ITEM(result, i, o); |
| 3975 | } |
| 3976 | } |
| 3977 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3978 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3979 | return result; |
| 3980 | } |
| 3981 | #endif |
| 3982 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 3983 | #ifdef HAVE_INITGROUPS |
| 3984 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 3985 | "initgroups(username, gid) -> None\n\n\ |
| 3986 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 3987 | the groups of which the specified username is a member, plus the specified\n\ |
| 3988 | group id."); |
| 3989 | |
| 3990 | static PyObject * |
| 3991 | posix_initgroups(PyObject *self, PyObject *args) |
| 3992 | { |
| 3993 | char *username; |
| 3994 | long gid; |
| 3995 | |
| 3996 | if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid)) |
| 3997 | return NULL; |
| 3998 | |
| 3999 | if (initgroups(username, (gid_t) gid) == -1) |
| 4000 | return PyErr_SetFromErrno(PyExc_OSError); |
| 4001 | |
| 4002 | Py_INCREF(Py_None); |
| 4003 | return Py_None; |
| 4004 | } |
| 4005 | #endif |
| 4006 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4007 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4008 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4009 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4010 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4011 | |
| 4012 | static PyObject * |
| 4013 | posix_getpgid(PyObject *self, PyObject *args) |
| 4014 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4015 | pid_t pid, pgid; |
| 4016 | if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid)) |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4017 | return NULL; |
| 4018 | pgid = getpgid(pid); |
| 4019 | if (pgid < 0) |
| 4020 | return posix_error(); |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4021 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4022 | } |
| 4023 | #endif /* HAVE_GETPGID */ |
| 4024 | |
| 4025 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4026 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4027 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4028 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4029 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4030 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4031 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4032 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4033 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4034 | #ifdef GETPGRP_HAVE_ARG |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4035 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4036 | #else /* GETPGRP_HAVE_ARG */ |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4037 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4038 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4039 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4040 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4041 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4042 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4043 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4044 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4045 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4046 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4047 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4048 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4049 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4050 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4051 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4052 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4053 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4054 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4055 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4056 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4057 | Py_INCREF(Py_None); |
| 4058 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4061 | #endif /* HAVE_SETPGRP */ |
| 4062 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4063 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4064 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4065 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4066 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4067 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4068 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4069 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4070 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4071 | return PyLong_FromPid(getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4072 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4073 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4074 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4075 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4076 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4077 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4078 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4079 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4080 | |
| 4081 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4082 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4083 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4084 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4085 | char *name; |
| 4086 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4087 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4088 | errno = 0; |
| 4089 | name = getlogin(); |
| 4090 | if (name == NULL) { |
| 4091 | if (errno) |
| 4092 | posix_error(); |
| 4093 | else |
| 4094 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 4095 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4096 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4097 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 4098 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4099 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4100 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4101 | return result; |
| 4102 | } |
| 4103 | #endif |
| 4104 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4105 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4106 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4107 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4108 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4109 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4110 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4111 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4112 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4113 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4114 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4115 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4116 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4117 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4118 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4119 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4120 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4121 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4122 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4123 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4124 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4125 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4126 | pid_t pid; |
| 4127 | int sig; |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4128 | if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4129 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4130 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4131 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4132 | APIRET rc; |
| 4133 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4134 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4135 | |
| 4136 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4137 | APIRET rc; |
| 4138 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4139 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4140 | |
| 4141 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4142 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4143 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4144 | if (kill(pid, sig) == -1) |
| 4145 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4146 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4147 | Py_INCREF(Py_None); |
| 4148 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4149 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4150 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4151 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4152 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4153 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4154 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4155 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4156 | |
| 4157 | static PyObject * |
| 4158 | posix_killpg(PyObject *self, PyObject *args) |
| 4159 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4160 | int sig; |
| 4161 | pid_t pgid; |
| 4162 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4163 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4164 | take the same type. Moreover, pid_t is always at least as wide as |
| 4165 | int (else compilation of this module fails), which is safe. */ |
| 4166 | if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig)) |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4167 | return NULL; |
| 4168 | if (killpg(pgid, sig) == -1) |
| 4169 | return posix_error(); |
| 4170 | Py_INCREF(Py_None); |
| 4171 | return Py_None; |
| 4172 | } |
| 4173 | #endif |
| 4174 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4175 | #ifdef HAVE_PLOCK |
| 4176 | |
| 4177 | #ifdef HAVE_SYS_LOCK_H |
| 4178 | #include <sys/lock.h> |
| 4179 | #endif |
| 4180 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4181 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4182 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4183 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4185 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4186 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4187 | { |
| 4188 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4189 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4190 | return NULL; |
| 4191 | if (plock(op) == -1) |
| 4192 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4193 | Py_INCREF(Py_None); |
| 4194 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4195 | } |
| 4196 | #endif |
| 4197 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4198 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4199 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4200 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4201 | Set the current process's user id."); |
| 4202 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4203 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4204 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4205 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4206 | long uid_arg; |
| 4207 | uid_t uid; |
| 4208 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4209 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4210 | uid = uid_arg; |
| 4211 | if (uid != uid_arg) { |
| 4212 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4213 | return NULL; |
| 4214 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4215 | if (setuid(uid) < 0) |
| 4216 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4217 | Py_INCREF(Py_None); |
| 4218 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4219 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4220 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4221 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4222 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4223 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4224 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4225 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4226 | Set the current process's effective user id."); |
| 4227 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4228 | static PyObject * |
| 4229 | posix_seteuid (PyObject *self, PyObject *args) |
| 4230 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4231 | long euid_arg; |
| 4232 | uid_t euid; |
| 4233 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4234 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4235 | euid = euid_arg; |
| 4236 | if (euid != euid_arg) { |
| 4237 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4238 | return NULL; |
| 4239 | } |
| 4240 | if (seteuid(euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4241 | return posix_error(); |
| 4242 | } else { |
| 4243 | Py_INCREF(Py_None); |
| 4244 | return Py_None; |
| 4245 | } |
| 4246 | } |
| 4247 | #endif /* HAVE_SETEUID */ |
| 4248 | |
| 4249 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4250 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4251 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4252 | Set the current process's effective group id."); |
| 4253 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4254 | static PyObject * |
| 4255 | posix_setegid (PyObject *self, PyObject *args) |
| 4256 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4257 | long egid_arg; |
| 4258 | gid_t egid; |
| 4259 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4260 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4261 | egid = egid_arg; |
| 4262 | if (egid != egid_arg) { |
| 4263 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4264 | return NULL; |
| 4265 | } |
| 4266 | if (setegid(egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4267 | return posix_error(); |
| 4268 | } else { |
| 4269 | Py_INCREF(Py_None); |
| 4270 | return Py_None; |
| 4271 | } |
| 4272 | } |
| 4273 | #endif /* HAVE_SETEGID */ |
| 4274 | |
| 4275 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4276 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4277 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4278 | Set the current process's real and effective user ids."); |
| 4279 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4280 | static PyObject * |
| 4281 | posix_setreuid (PyObject *self, PyObject *args) |
| 4282 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4283 | long ruid_arg, euid_arg; |
| 4284 | uid_t ruid, euid; |
| 4285 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4286 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4287 | ruid = ruid_arg; |
| 4288 | euid = euid_arg; |
| 4289 | if (euid != euid_arg || ruid != ruid_arg) { |
| 4290 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4291 | return NULL; |
| 4292 | } |
| 4293 | if (setreuid(ruid, euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4294 | return posix_error(); |
| 4295 | } else { |
| 4296 | Py_INCREF(Py_None); |
| 4297 | return Py_None; |
| 4298 | } |
| 4299 | } |
| 4300 | #endif /* HAVE_SETREUID */ |
| 4301 | |
| 4302 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4303 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4304 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4305 | Set the current process's real and effective group ids."); |
| 4306 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4307 | static PyObject * |
| 4308 | posix_setregid (PyObject *self, PyObject *args) |
| 4309 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4310 | long rgid_arg, egid_arg; |
| 4311 | gid_t rgid, egid; |
| 4312 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4313 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4314 | rgid = rgid_arg; |
| 4315 | egid = egid_arg; |
| 4316 | if (egid != egid_arg || rgid != rgid_arg) { |
| 4317 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4318 | return NULL; |
| 4319 | } |
| 4320 | if (setregid(rgid, egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4321 | return posix_error(); |
| 4322 | } else { |
| 4323 | Py_INCREF(Py_None); |
| 4324 | return Py_None; |
| 4325 | } |
| 4326 | } |
| 4327 | #endif /* HAVE_SETREGID */ |
| 4328 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4329 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4330 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4331 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4332 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4334 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4335 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4336 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4337 | long gid_arg; |
| 4338 | gid_t gid; |
| 4339 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4340 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4341 | gid = gid_arg; |
| 4342 | if (gid != gid_arg) { |
| 4343 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4344 | return NULL; |
| 4345 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4346 | if (setgid(gid) < 0) |
| 4347 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4348 | Py_INCREF(Py_None); |
| 4349 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4350 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4351 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4352 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4353 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4354 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4355 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4356 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4357 | |
| 4358 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4359 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4360 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4361 | int i, len; |
| 4362 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4363 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4364 | if (!PySequence_Check(groups)) { |
| 4365 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4366 | return NULL; |
| 4367 | } |
| 4368 | len = PySequence_Size(groups); |
| 4369 | if (len > MAX_GROUPS) { |
| 4370 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4371 | return NULL; |
| 4372 | } |
| 4373 | for(i = 0; i < len; i++) { |
| 4374 | PyObject *elem; |
| 4375 | elem = PySequence_GetItem(groups, i); |
| 4376 | if (!elem) |
| 4377 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4378 | if (!PyLong_Check(elem)) { |
| 4379 | PyErr_SetString(PyExc_TypeError, |
| 4380 | "groups must be integers"); |
| 4381 | Py_DECREF(elem); |
| 4382 | return NULL; |
| 4383 | } else { |
| 4384 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4385 | if (PyErr_Occurred()) { |
| 4386 | PyErr_SetString(PyExc_TypeError, |
| 4387 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4388 | Py_DECREF(elem); |
| 4389 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4390 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4391 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4392 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4393 | if (grouplist[i] != x) { |
| 4394 | PyErr_SetString(PyExc_TypeError, |
| 4395 | "group id too big"); |
| 4396 | Py_DECREF(elem); |
| 4397 | return NULL; |
| 4398 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4399 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4400 | Py_DECREF(elem); |
| 4401 | } |
| 4402 | |
| 4403 | if (setgroups(len, grouplist) < 0) |
| 4404 | return posix_error(); |
| 4405 | Py_INCREF(Py_None); |
| 4406 | return Py_None; |
| 4407 | } |
| 4408 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4409 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4410 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4411 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4412 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4413 | { |
| 4414 | PyObject *result; |
| 4415 | static PyObject *struct_rusage; |
| 4416 | |
| 4417 | if (pid == -1) |
| 4418 | return posix_error(); |
| 4419 | |
| 4420 | if (struct_rusage == NULL) { |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4421 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4422 | if (m == NULL) |
| 4423 | return NULL; |
| 4424 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4425 | Py_DECREF(m); |
| 4426 | if (struct_rusage == NULL) |
| 4427 | return NULL; |
| 4428 | } |
| 4429 | |
| 4430 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4431 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4432 | if (!result) |
| 4433 | return NULL; |
| 4434 | |
| 4435 | #ifndef doubletime |
| 4436 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4437 | #endif |
| 4438 | |
| 4439 | PyStructSequence_SET_ITEM(result, 0, |
| 4440 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4441 | PyStructSequence_SET_ITEM(result, 1, |
| 4442 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4443 | #define SET_INT(result, index, value)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4444 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4445 | SET_INT(result, 2, ru->ru_maxrss); |
| 4446 | SET_INT(result, 3, ru->ru_ixrss); |
| 4447 | SET_INT(result, 4, ru->ru_idrss); |
| 4448 | SET_INT(result, 5, ru->ru_isrss); |
| 4449 | SET_INT(result, 6, ru->ru_minflt); |
| 4450 | SET_INT(result, 7, ru->ru_majflt); |
| 4451 | SET_INT(result, 8, ru->ru_nswap); |
| 4452 | SET_INT(result, 9, ru->ru_inblock); |
| 4453 | SET_INT(result, 10, ru->ru_oublock); |
| 4454 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4455 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4456 | SET_INT(result, 13, ru->ru_nsignals); |
| 4457 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4458 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4459 | #undef SET_INT |
| 4460 | |
| 4461 | if (PyErr_Occurred()) { |
| 4462 | Py_DECREF(result); |
| 4463 | return NULL; |
| 4464 | } |
| 4465 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4466 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4467 | } |
| 4468 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4469 | |
| 4470 | #ifdef HAVE_WAIT3 |
| 4471 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4472 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4473 | Wait for completion of a child process."); |
| 4474 | |
| 4475 | static PyObject * |
| 4476 | posix_wait3(PyObject *self, PyObject *args) |
| 4477 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4478 | pid_t pid; |
| 4479 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4480 | struct rusage ru; |
| 4481 | WAIT_TYPE status; |
| 4482 | WAIT_STATUS_INT(status) = 0; |
| 4483 | |
| 4484 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4485 | return NULL; |
| 4486 | |
| 4487 | Py_BEGIN_ALLOW_THREADS |
| 4488 | pid = wait3(&status, options, &ru); |
| 4489 | Py_END_ALLOW_THREADS |
| 4490 | |
| 4491 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4492 | } |
| 4493 | #endif /* HAVE_WAIT3 */ |
| 4494 | |
| 4495 | #ifdef HAVE_WAIT4 |
| 4496 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4497 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4498 | Wait for completion of a given child process."); |
| 4499 | |
| 4500 | static PyObject * |
| 4501 | posix_wait4(PyObject *self, PyObject *args) |
| 4502 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4503 | pid_t pid; |
| 4504 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4505 | struct rusage ru; |
| 4506 | WAIT_TYPE status; |
| 4507 | WAIT_STATUS_INT(status) = 0; |
| 4508 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4509 | if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4510 | return NULL; |
| 4511 | |
| 4512 | Py_BEGIN_ALLOW_THREADS |
| 4513 | pid = wait4(pid, &status, options, &ru); |
| 4514 | Py_END_ALLOW_THREADS |
| 4515 | |
| 4516 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4517 | } |
| 4518 | #endif /* HAVE_WAIT4 */ |
| 4519 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4520 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4521 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4522 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4523 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4524 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4525 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4526 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4527 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4528 | pid_t pid; |
| 4529 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4530 | WAIT_TYPE status; |
| 4531 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4532 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4533 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4534 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4535 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4536 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4537 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4538 | if (pid == -1) |
| 4539 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4540 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4541 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4542 | } |
| 4543 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4544 | #elif defined(HAVE_CWAIT) |
| 4545 | |
| 4546 | /* 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] | 4547 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4548 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4549 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4550 | |
| 4551 | static PyObject * |
| 4552 | posix_waitpid(PyObject *self, PyObject *args) |
| 4553 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4554 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4555 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4556 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4557 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4558 | return NULL; |
| 4559 | Py_BEGIN_ALLOW_THREADS |
| 4560 | pid = _cwait(&status, pid, options); |
| 4561 | Py_END_ALLOW_THREADS |
| 4562 | if (pid == -1) |
| 4563 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4564 | |
| 4565 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4566 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4567 | } |
| 4568 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4569 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4570 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4571 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4572 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4573 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4574 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4575 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4576 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4577 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4578 | pid_t pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4579 | WAIT_TYPE status; |
| 4580 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4581 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4582 | Py_BEGIN_ALLOW_THREADS |
| 4583 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4584 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4585 | if (pid == -1) |
| 4586 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4587 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4588 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4589 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4590 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4591 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4593 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4594 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4595 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4596 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4597 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4598 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4599 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4600 | #ifdef HAVE_LSTAT |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4601 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4602 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4603 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4604 | 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] | 4605 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4606 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4607 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4608 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4611 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4612 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4613 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4614 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4615 | 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] | 4616 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4617 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4618 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4619 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4620 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4621 | char buf[MAXPATHLEN]; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4622 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4623 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4624 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4625 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4626 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4627 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 4628 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4629 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4630 | path = bytes2str(opath, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4631 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4632 | if (v == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4633 | release_bytes(opath); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4634 | return NULL; |
| 4635 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4636 | |
| 4637 | if (PyUnicode_Check(v)) { |
| 4638 | arg_is_unicode = 1; |
| 4639 | } |
| 4640 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4641 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4642 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4643 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4644 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4645 | if (n < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4646 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4647 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4648 | release_bytes(opath); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4649 | v = PyBytes_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4650 | if (arg_is_unicode) { |
| 4651 | PyObject *w; |
| 4652 | |
| 4653 | w = PyUnicode_FromEncodedObject(v, |
| 4654 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 4655 | "surrogateescape"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4656 | if (w != NULL) { |
| 4657 | Py_DECREF(v); |
| 4658 | v = w; |
| 4659 | } |
| 4660 | else { |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4661 | v = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4662 | } |
| 4663 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4664 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4665 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4666 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4667 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4668 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4669 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4670 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4671 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4672 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4673 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4674 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4675 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4676 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4677 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4678 | } |
| 4679 | #endif /* HAVE_SYMLINK */ |
| 4680 | |
| 4681 | |
| 4682 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4683 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4684 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4685 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4686 | { |
| 4687 | ULONG value = 0; |
| 4688 | |
| 4689 | Py_BEGIN_ALLOW_THREADS |
| 4690 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4691 | Py_END_ALLOW_THREADS |
| 4692 | |
| 4693 | return value; |
| 4694 | } |
| 4695 | |
| 4696 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4697 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4698 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4699 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4700 | return Py_BuildValue("ddddd", |
| 4701 | (double)0 /* t.tms_utime / HZ */, |
| 4702 | (double)0 /* t.tms_stime / HZ */, |
| 4703 | (double)0 /* t.tms_cutime / HZ */, |
| 4704 | (double)0 /* t.tms_cstime / HZ */, |
| 4705 | (double)system_uptime() / 1000); |
| 4706 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4707 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4708 | #define NEED_TICKS_PER_SECOND |
| 4709 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4710 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4711 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4712 | { |
| 4713 | struct tms t; |
| 4714 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4715 | errno = 0; |
| 4716 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4717 | if (c == (clock_t) -1) |
| 4718 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4719 | return Py_BuildValue("ddddd", |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4720 | (double)t.tms_utime / ticks_per_second, |
| 4721 | (double)t.tms_stime / ticks_per_second, |
| 4722 | (double)t.tms_cutime / ticks_per_second, |
| 4723 | (double)t.tms_cstime / ticks_per_second, |
| 4724 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4725 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4726 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4727 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4728 | |
| 4729 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4730 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4731 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4732 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4733 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4734 | { |
| 4735 | FILETIME create, exit, kernel, user; |
| 4736 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4737 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4738 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4739 | /* The fields of a FILETIME structure are the hi and lo part |
| 4740 | of a 64-bit value expressed in 100 nanosecond units. |
| 4741 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4742 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4743 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4744 | return Py_BuildValue( |
| 4745 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4746 | (double)(user.dwHighDateTime*429.4967296 + |
| 4747 | user.dwLowDateTime*1e-7), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 4748 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4749 | kernel.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4750 | (double)0, |
| 4751 | (double)0, |
| 4752 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4753 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4754 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4755 | |
| 4756 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4757 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4758 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4759 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4760 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4761 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4762 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4763 | #ifdef HAVE_GETSID |
| 4764 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4765 | "getsid(pid) -> sid\n\n\ |
| 4766 | Call the system call getsid()."); |
| 4767 | |
| 4768 | static PyObject * |
| 4769 | posix_getsid(PyObject *self, PyObject *args) |
| 4770 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4771 | pid_t pid; |
| 4772 | int sid; |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4773 | if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid)) |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4774 | return NULL; |
| 4775 | sid = getsid(pid); |
| 4776 | if (sid < 0) |
| 4777 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4778 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4779 | } |
| 4780 | #endif /* HAVE_GETSID */ |
| 4781 | |
| 4782 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4783 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4784 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4785 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4786 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4787 | |
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_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4790 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4791 | if (setsid() < 0) |
| 4792 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4793 | Py_INCREF(Py_None); |
| 4794 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4795 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4796 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4797 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4798 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4799 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4800 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4801 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4802 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4803 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4804 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4805 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4806 | pid_t pid; |
| 4807 | int pgrp; |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4808 | if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4809 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4810 | if (setpgid(pid, pgrp) < 0) |
| 4811 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4812 | Py_INCREF(Py_None); |
| 4813 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4814 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4815 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4816 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4817 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4818 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4819 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4820 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4821 | 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] | 4822 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4823 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4824 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4825 | { |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4826 | int fd; |
| 4827 | pid_t pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4828 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4829 | return NULL; |
| 4830 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4831 | if (pgid < 0) |
| 4832 | return posix_error(); |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4833 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4834 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4835 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4836 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4837 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4838 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4839 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4840 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4841 | 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] | 4842 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4843 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4844 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4845 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4846 | int fd; |
| 4847 | pid_t pgid; |
| 4848 | if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4849 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4850 | if (tcsetpgrp(fd, pgid) < 0) |
| 4851 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4852 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4853 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4854 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4855 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4856 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4857 | /* Functions acting on file descriptors */ |
| 4858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4859 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4860 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4861 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4864 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4865 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4866 | PyObject *ofile; |
| 4867 | char *file; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4868 | int flag; |
| 4869 | int mode = 0777; |
| 4870 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4871 | |
| 4872 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 4873 | PyUnicodeObject *po; |
| 4874 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4875 | Py_BEGIN_ALLOW_THREADS |
| 4876 | /* PyUnicode_AS_UNICODE OK without thread |
| 4877 | lock as it is a simple dereference. */ |
| 4878 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4879 | Py_END_ALLOW_THREADS |
| 4880 | if (fd < 0) |
| 4881 | return posix_error(); |
| 4882 | return PyLong_FromLong((long)fd); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4883 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 4884 | /* Drop the argument parsing error as narrow strings |
| 4885 | are also valid. */ |
| 4886 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4887 | #endif |
| 4888 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4889 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 4890 | PyUnicode_FSConverter, &ofile, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4891 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4892 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4893 | file = bytes2str(ofile, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4894 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4895 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4896 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4897 | if (fd < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4898 | return posix_error_with_allocated_filename(ofile); |
| 4899 | release_bytes(ofile); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4900 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4903 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4904 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4905 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4906 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4907 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4908 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4909 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4910 | { |
| 4911 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4912 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4913 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4914 | if (!_PyVerify_fd(fd)) |
| 4915 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4916 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4917 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4918 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4919 | if (res < 0) |
| 4920 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4921 | Py_INCREF(Py_None); |
| 4922 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4925 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4926 | PyDoc_STRVAR(posix_closerange__doc__, |
| 4927 | "closerange(fd_low, fd_high)\n\n\ |
| 4928 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 4929 | |
| 4930 | static PyObject * |
| 4931 | posix_closerange(PyObject *self, PyObject *args) |
| 4932 | { |
| 4933 | int fd_from, fd_to, i; |
| 4934 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 4935 | return NULL; |
| 4936 | Py_BEGIN_ALLOW_THREADS |
| 4937 | for (i = fd_from; i < fd_to; i++) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4938 | if (_PyVerify_fd(i)) |
| 4939 | close(i); |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4940 | Py_END_ALLOW_THREADS |
| 4941 | Py_RETURN_NONE; |
| 4942 | } |
| 4943 | |
| 4944 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4945 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4946 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4947 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4948 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4949 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4950 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4951 | { |
| 4952 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4953 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4954 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4955 | if (!_PyVerify_fd(fd)) |
| 4956 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4957 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4958 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4959 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4960 | if (fd < 0) |
| 4961 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4962 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4965 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4966 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4967 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4968 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4969 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4970 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4971 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4972 | { |
| 4973 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4974 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4975 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4976 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 4977 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4978 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4979 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4980 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4981 | if (res < 0) |
| 4982 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4983 | Py_INCREF(Py_None); |
| 4984 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4988 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4989 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4990 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4991 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4992 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4993 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4994 | { |
| 4995 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4996 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4997 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4998 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4999 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5000 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5001 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5002 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5003 | return NULL; |
| 5004 | #ifdef SEEK_SET |
| 5005 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5006 | switch (how) { |
| 5007 | case 0: how = SEEK_SET; break; |
| 5008 | case 1: how = SEEK_CUR; break; |
| 5009 | case 2: how = SEEK_END; break; |
| 5010 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5011 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5012 | |
| 5013 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5014 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5015 | #else |
| 5016 | pos = PyLong_Check(posobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5017 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5018 | #endif |
| 5019 | if (PyErr_Occurred()) |
| 5020 | return NULL; |
| 5021 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5022 | if (!_PyVerify_fd(fd)) |
| 5023 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5024 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5025 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5026 | res = _lseeki64(fd, pos, how); |
| 5027 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5028 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5029 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5030 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5031 | if (res < 0) |
| 5032 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5033 | |
| 5034 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5035 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5036 | #else |
| 5037 | return PyLong_FromLongLong(res); |
| 5038 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5041 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5042 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5043 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5044 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5045 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5046 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5047 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5048 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 5049 | int fd, size; |
| 5050 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5051 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5052 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5053 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5054 | if (size < 0) { |
| 5055 | errno = EINVAL; |
| 5056 | return posix_error(); |
| 5057 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5058 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5059 | if (buffer == NULL) |
| 5060 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5061 | if (!_PyVerify_fd(fd)) |
| 5062 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5063 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5064 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5065 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5066 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5067 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5068 | return posix_error(); |
| 5069 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5070 | if (n != size) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5071 | _PyBytes_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5072 | return buffer; |
| 5073 | } |
| 5074 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5076 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5077 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5078 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5080 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5081 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5082 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5083 | Py_buffer pbuf; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5084 | int fd; |
| 5085 | Py_ssize_t size; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5086 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 5087 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5088 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5089 | if (!_PyVerify_fd(fd)) |
| 5090 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5091 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5092 | size = write(fd, pbuf.buf, (size_t)pbuf.len); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5093 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5094 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5095 | if (size < 0) |
| 5096 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5097 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5101 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5102 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5103 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5104 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5105 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5106 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5107 | { |
| 5108 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5109 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5110 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5111 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5112 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5113 | #ifdef __VMS |
| 5114 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5115 | fsync(fd); |
| 5116 | #endif |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5117 | if (!_PyVerify_fd(fd)) |
| 5118 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5119 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5120 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5121 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5122 | if (res != 0) { |
| 5123 | #ifdef MS_WINDOWS |
| 5124 | return win32_error("fstat", NULL); |
| 5125 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5126 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5127 | #endif |
| 5128 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5129 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5130 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5133 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5134 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5135 | 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] | 5136 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5137 | |
| 5138 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5139 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5140 | { |
| 5141 | int fd; |
| 5142 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5143 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5144 | if (!_PyVerify_fd(fd)) |
| 5145 | return PyBool_FromLong(0); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5146 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5147 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5148 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5149 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5150 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5151 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5152 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5153 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5154 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5155 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5156 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5157 | #if defined(PYOS_OS2) |
| 5158 | HFILE read, write; |
| 5159 | APIRET rc; |
| 5160 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5161 | Py_BEGIN_ALLOW_THREADS |
| 5162 | rc = DosCreatePipe( &read, &write, 4096); |
| 5163 | Py_END_ALLOW_THREADS |
| 5164 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5165 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5166 | |
| 5167 | return Py_BuildValue("(ii)", read, write); |
| 5168 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5169 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5170 | int fds[2]; |
| 5171 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5172 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5173 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5174 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5175 | if (res != 0) |
| 5176 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5177 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5178 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5179 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5180 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5181 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5182 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5183 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5184 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5185 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5186 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5187 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5188 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5189 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5190 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5191 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5192 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5193 | #endif /* HAVE_PIPE */ |
| 5194 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5195 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5196 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5197 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5198 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5199 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5200 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5201 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5202 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5203 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5204 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5205 | int mode = 0666; |
| 5206 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5207 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5208 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5209 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5210 | res = mkfifo(filename, mode); |
| 5211 | Py_END_ALLOW_THREADS |
| 5212 | if (res < 0) |
| 5213 | return posix_error(); |
| 5214 | Py_INCREF(Py_None); |
| 5215 | return Py_None; |
| 5216 | } |
| 5217 | #endif |
| 5218 | |
| 5219 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5220 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5221 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5222 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5223 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5224 | named filename. mode specifies both the permissions to use and the\n\ |
| 5225 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5226 | 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] | 5227 | device defines the newly created device special file (probably using\n\ |
| 5228 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5229 | |
| 5230 | |
| 5231 | static PyObject * |
| 5232 | posix_mknod(PyObject *self, PyObject *args) |
| 5233 | { |
| 5234 | char *filename; |
| 5235 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5236 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5237 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5238 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5239 | return NULL; |
| 5240 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5241 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5242 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5243 | if (res < 0) |
| 5244 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5245 | Py_INCREF(Py_None); |
| 5246 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5247 | } |
| 5248 | #endif |
| 5249 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5250 | #ifdef HAVE_DEVICE_MACROS |
| 5251 | PyDoc_STRVAR(posix_major__doc__, |
| 5252 | "major(device) -> major number\n\ |
| 5253 | Extracts a device major number from a raw device number."); |
| 5254 | |
| 5255 | static PyObject * |
| 5256 | posix_major(PyObject *self, PyObject *args) |
| 5257 | { |
| 5258 | int device; |
| 5259 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5260 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5261 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5262 | } |
| 5263 | |
| 5264 | PyDoc_STRVAR(posix_minor__doc__, |
| 5265 | "minor(device) -> minor number\n\ |
| 5266 | Extracts a device minor number from a raw device number."); |
| 5267 | |
| 5268 | static PyObject * |
| 5269 | posix_minor(PyObject *self, PyObject *args) |
| 5270 | { |
| 5271 | int device; |
| 5272 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5273 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5274 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5278 | "makedev(major, minor) -> device number\n\ |
| 5279 | Composes a raw device number from the major and minor device numbers."); |
| 5280 | |
| 5281 | static PyObject * |
| 5282 | posix_makedev(PyObject *self, PyObject *args) |
| 5283 | { |
| 5284 | int major, minor; |
| 5285 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5286 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5287 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5288 | } |
| 5289 | #endif /* device macros */ |
| 5290 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5291 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5292 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5293 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5294 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5295 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5296 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5297 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5298 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5299 | { |
| 5300 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5301 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5302 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5303 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5304 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5305 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5306 | return NULL; |
| 5307 | |
| 5308 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5309 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5310 | #else |
| 5311 | length = PyLong_Check(lenobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5312 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5313 | #endif |
| 5314 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5315 | return NULL; |
| 5316 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5317 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5318 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5319 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 9053d75 | 2009-01-19 17:53:36 +0000 | [diff] [blame] | 5320 | if (res < 0) |
| 5321 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5322 | Py_INCREF(Py_None); |
| 5323 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5324 | } |
| 5325 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5326 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5327 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5328 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5329 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5330 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5331 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5332 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5333 | * get re-set with another call for the same key. */ |
| 5334 | static PyObject *posix_putenv_garbage; |
| 5335 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5336 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5337 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5338 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5339 | #ifdef MS_WINDOWS |
| 5340 | wchar_t *s1, *s2; |
| 5341 | wchar_t *newenv; |
| 5342 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5343 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5344 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5345 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5346 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5347 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5348 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5349 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5350 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5351 | if (!PyArg_ParseTuple(args, |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5352 | "uu:putenv", |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5353 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5354 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5355 | #else |
| 5356 | if (!PyArg_ParseTuple(args, |
| 5357 | "O&O&:putenv", |
| 5358 | PyUnicode_FSConverter, &os1, |
| 5359 | PyUnicode_FSConverter, &os2)) |
| 5360 | return NULL; |
| 5361 | s1 = bytes2str(os1, 1); |
| 5362 | s2 = bytes2str(os2, 1); |
| 5363 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5364 | |
| 5365 | #if defined(PYOS_OS2) |
| 5366 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5367 | APIRET rc; |
| 5368 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5369 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5370 | if (rc != NO_ERROR) |
| 5371 | return os2_error(rc); |
| 5372 | |
| 5373 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5374 | APIRET rc; |
| 5375 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5376 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5377 | if (rc != NO_ERROR) |
| 5378 | return os2_error(rc); |
| 5379 | } else { |
| 5380 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5381 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5382 | /* len includes space for a trailing \0; the size arg to |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5383 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5384 | #ifdef MS_WINDOWS |
| 5385 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5386 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5387 | #else |
| 5388 | len = strlen(s1) + strlen(s2) + 2; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5389 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5390 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5391 | if (newstr == NULL) |
| 5392 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5393 | #ifdef MS_WINDOWS |
| 5394 | newenv = PyUnicode_AsUnicode(newstr); |
| 5395 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5396 | if (_wputenv(newenv)) { |
| 5397 | Py_DECREF(newstr); |
| 5398 | posix_error(); |
| 5399 | return NULL; |
| 5400 | } |
| 5401 | #else |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5402 | newenv = PyBytes_AS_STRING(newstr); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5403 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5404 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5405 | Py_DECREF(newstr); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5406 | release_bytes(os1); |
| 5407 | release_bytes(os2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5408 | posix_error(); |
| 5409 | return NULL; |
| 5410 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5411 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5412 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5413 | * this will cause previous value to be collected. This has to |
| 5414 | * happen after the real putenv() call because the old value |
| 5415 | * was still accessible until then. */ |
| 5416 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5417 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5418 | /* really not much we can do; just leak */ |
| 5419 | PyErr_Clear(); |
| 5420 | } |
| 5421 | else { |
| 5422 | Py_DECREF(newstr); |
| 5423 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5424 | |
| 5425 | #if defined(PYOS_OS2) |
| 5426 | } |
| 5427 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5428 | #ifndef MS_WINDOWS |
| 5429 | release_bytes(os1); |
| 5430 | release_bytes(os2); |
| 5431 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5432 | Py_INCREF(Py_None); |
| 5433 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5434 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5435 | #endif /* putenv */ |
| 5436 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5437 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5438 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5439 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5440 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5441 | |
| 5442 | static PyObject * |
| 5443 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5444 | { |
| 5445 | char *s1; |
| 5446 | |
| 5447 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5448 | return NULL; |
| 5449 | |
| 5450 | unsetenv(s1); |
| 5451 | |
| 5452 | /* Remove the key from posix_putenv_garbage; |
| 5453 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5454 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5455 | * old value was still accessible until then. |
| 5456 | */ |
| 5457 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5458 | PyTuple_GET_ITEM(args, 0))) { |
| 5459 | /* really not much we can do; just leak */ |
| 5460 | PyErr_Clear(); |
| 5461 | } |
| 5462 | |
| 5463 | Py_INCREF(Py_None); |
| 5464 | return Py_None; |
| 5465 | } |
| 5466 | #endif /* unsetenv */ |
| 5467 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5469 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5470 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5471 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5472 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5473 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5474 | { |
| 5475 | int code; |
| 5476 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5477 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5478 | return NULL; |
| 5479 | message = strerror(code); |
| 5480 | if (message == NULL) { |
| 5481 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5482 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5483 | return NULL; |
| 5484 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5485 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5486 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5487 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5488 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5489 | #ifdef HAVE_SYS_WAIT_H |
| 5490 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5491 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5492 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5493 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5494 | 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] | 5495 | |
| 5496 | static PyObject * |
| 5497 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5498 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5499 | WAIT_TYPE status; |
| 5500 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5501 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5502 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5503 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5504 | |
| 5505 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5506 | } |
| 5507 | #endif /* WCOREDUMP */ |
| 5508 | |
| 5509 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5510 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5511 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5512 | 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] | 5513 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5514 | |
| 5515 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5516 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5517 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5518 | WAIT_TYPE status; |
| 5519 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5520 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5521 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5522 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5523 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5524 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5525 | } |
| 5526 | #endif /* WIFCONTINUED */ |
| 5527 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5528 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5529 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5530 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5531 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5532 | |
| 5533 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5534 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5535 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5536 | WAIT_TYPE status; |
| 5537 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5538 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5539 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5540 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5541 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5542 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5543 | } |
| 5544 | #endif /* WIFSTOPPED */ |
| 5545 | |
| 5546 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5547 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5548 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5549 | 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] | 5550 | |
| 5551 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5552 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5553 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5554 | WAIT_TYPE status; |
| 5555 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5556 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5557 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5558 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5559 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5560 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5561 | } |
| 5562 | #endif /* WIFSIGNALED */ |
| 5563 | |
| 5564 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5565 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5566 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5567 | 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] | 5568 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5569 | |
| 5570 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5571 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5572 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5573 | WAIT_TYPE status; |
| 5574 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5575 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5576 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5577 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5578 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5579 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5580 | } |
| 5581 | #endif /* WIFEXITED */ |
| 5582 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5583 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5584 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5585 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5586 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5587 | |
| 5588 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5589 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5590 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5591 | WAIT_TYPE status; |
| 5592 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5593 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5594 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5595 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5596 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5597 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5598 | } |
| 5599 | #endif /* WEXITSTATUS */ |
| 5600 | |
| 5601 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5602 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5603 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5604 | 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] | 5605 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5606 | |
| 5607 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5608 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5609 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5610 | WAIT_TYPE status; |
| 5611 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5612 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5613 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5614 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5615 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5616 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5617 | } |
| 5618 | #endif /* WTERMSIG */ |
| 5619 | |
| 5620 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5621 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5622 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5623 | Return the signal that stopped the process that provided\n\ |
| 5624 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5625 | |
| 5626 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5627 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5628 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5629 | WAIT_TYPE status; |
| 5630 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5631 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5632 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5633 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5634 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5635 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5636 | } |
| 5637 | #endif /* WSTOPSIG */ |
| 5638 | |
| 5639 | #endif /* HAVE_SYS_WAIT_H */ |
| 5640 | |
| 5641 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5642 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5643 | #ifdef _SCO_DS |
| 5644 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5645 | needed definitions in sys/statvfs.h */ |
| 5646 | #define _SVID3 |
| 5647 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5648 | #include <sys/statvfs.h> |
| 5649 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5650 | static PyObject* |
| 5651 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5652 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5653 | if (v == NULL) |
| 5654 | return NULL; |
| 5655 | |
| 5656 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5657 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5658 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5659 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5660 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5661 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5662 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5663 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5664 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 5665 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5666 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5667 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5668 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5669 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5670 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5671 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5672 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5673 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5674 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5675 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5676 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5677 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5678 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5679 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5680 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5681 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5682 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5683 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5684 | #endif |
| 5685 | |
| 5686 | return v; |
| 5687 | } |
| 5688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5689 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5690 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5691 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5692 | |
| 5693 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5694 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5695 | { |
| 5696 | int fd, res; |
| 5697 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5698 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5699 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5700 | return NULL; |
| 5701 | Py_BEGIN_ALLOW_THREADS |
| 5702 | res = fstatvfs(fd, &st); |
| 5703 | Py_END_ALLOW_THREADS |
| 5704 | if (res != 0) |
| 5705 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5706 | |
| 5707 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5708 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5709 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5710 | |
| 5711 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5712 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5713 | #include <sys/statvfs.h> |
| 5714 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5715 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5716 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5717 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5718 | |
| 5719 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5720 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5721 | { |
| 5722 | char *path; |
| 5723 | int res; |
| 5724 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5725 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5726 | return NULL; |
| 5727 | Py_BEGIN_ALLOW_THREADS |
| 5728 | res = statvfs(path, &st); |
| 5729 | Py_END_ALLOW_THREADS |
| 5730 | if (res != 0) |
| 5731 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5732 | |
| 5733 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5734 | } |
| 5735 | #endif /* HAVE_STATVFS */ |
| 5736 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5737 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5738 | * It maps strings representing configuration variable names to |
| 5739 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5740 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5741 | * rarely-used constants. There are three separate tables that use |
| 5742 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5743 | * |
| 5744 | * This code is always included, even if none of the interfaces that |
| 5745 | * need it are included. The #if hackery needed to avoid it would be |
| 5746 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5747 | */ |
| 5748 | struct constdef { |
| 5749 | char *name; |
| 5750 | long value; |
| 5751 | }; |
| 5752 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5753 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5754 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5755 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5756 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5757 | if (PyLong_Check(arg)) { |
| 5758 | *valuep = PyLong_AS_LONG(arg); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5759 | return 1; |
| 5760 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5761 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5762 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5763 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5764 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5765 | size_t hi = tablesize; |
| 5766 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5767 | const char *confname; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5768 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5769 | PyErr_SetString(PyExc_TypeError, |
| 5770 | "configuration names must be strings or integers"); |
| 5771 | return 0; |
| 5772 | } |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 5773 | confname = _PyUnicode_AsString(arg); |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5774 | if (confname == NULL) |
| 5775 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5776 | while (lo < hi) { |
| 5777 | mid = (lo + hi) / 2; |
| 5778 | cmp = strcmp(confname, table[mid].name); |
| 5779 | if (cmp < 0) |
| 5780 | hi = mid; |
| 5781 | else if (cmp > 0) |
| 5782 | lo = mid + 1; |
| 5783 | else { |
| 5784 | *valuep = table[mid].value; |
| 5785 | return 1; |
| 5786 | } |
| 5787 | } |
| 5788 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5789 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5790 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5791 | } |
| 5792 | |
| 5793 | |
| 5794 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5795 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5796 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5797 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5798 | #endif |
| 5799 | #ifdef _PC_ABI_ASYNC_IO |
| 5800 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5801 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5802 | #ifdef _PC_ASYNC_IO |
| 5803 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5804 | #endif |
| 5805 | #ifdef _PC_CHOWN_RESTRICTED |
| 5806 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5807 | #endif |
| 5808 | #ifdef _PC_FILESIZEBITS |
| 5809 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5810 | #endif |
| 5811 | #ifdef _PC_LAST |
| 5812 | {"PC_LAST", _PC_LAST}, |
| 5813 | #endif |
| 5814 | #ifdef _PC_LINK_MAX |
| 5815 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5816 | #endif |
| 5817 | #ifdef _PC_MAX_CANON |
| 5818 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5819 | #endif |
| 5820 | #ifdef _PC_MAX_INPUT |
| 5821 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5822 | #endif |
| 5823 | #ifdef _PC_NAME_MAX |
| 5824 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5825 | #endif |
| 5826 | #ifdef _PC_NO_TRUNC |
| 5827 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5828 | #endif |
| 5829 | #ifdef _PC_PATH_MAX |
| 5830 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5831 | #endif |
| 5832 | #ifdef _PC_PIPE_BUF |
| 5833 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5834 | #endif |
| 5835 | #ifdef _PC_PRIO_IO |
| 5836 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5837 | #endif |
| 5838 | #ifdef _PC_SOCK_MAXBUF |
| 5839 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5840 | #endif |
| 5841 | #ifdef _PC_SYNC_IO |
| 5842 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5843 | #endif |
| 5844 | #ifdef _PC_VDISABLE |
| 5845 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5846 | #endif |
| 5847 | }; |
| 5848 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5849 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5850 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5851 | { |
| 5852 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5853 | sizeof(posix_constants_pathconf) |
| 5854 | / sizeof(struct constdef)); |
| 5855 | } |
| 5856 | #endif |
| 5857 | |
| 5858 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5859 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5860 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5861 | 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] | 5862 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5863 | |
| 5864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5865 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5866 | { |
| 5867 | PyObject *result = NULL; |
| 5868 | int name, fd; |
| 5869 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5870 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5871 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5872 | long limit; |
| 5873 | |
| 5874 | errno = 0; |
| 5875 | limit = fpathconf(fd, name); |
| 5876 | if (limit == -1 && errno != 0) |
| 5877 | posix_error(); |
| 5878 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5879 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5880 | } |
| 5881 | return result; |
| 5882 | } |
| 5883 | #endif |
| 5884 | |
| 5885 | |
| 5886 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5887 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5888 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5889 | 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] | 5890 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5891 | |
| 5892 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5893 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5894 | { |
| 5895 | PyObject *result = NULL; |
| 5896 | int name; |
| 5897 | char *path; |
| 5898 | |
| 5899 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5900 | conv_path_confname, &name)) { |
| 5901 | long limit; |
| 5902 | |
| 5903 | errno = 0; |
| 5904 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5905 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5906 | if (errno == EINVAL) |
| 5907 | /* could be a path or name problem */ |
| 5908 | posix_error(); |
| 5909 | else |
| 5910 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5911 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5912 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5913 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5914 | } |
| 5915 | return result; |
| 5916 | } |
| 5917 | #endif |
| 5918 | |
| 5919 | #ifdef HAVE_CONFSTR |
| 5920 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5921 | #ifdef _CS_ARCHITECTURE |
| 5922 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5923 | #endif |
| 5924 | #ifdef _CS_HOSTNAME |
| 5925 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5926 | #endif |
| 5927 | #ifdef _CS_HW_PROVIDER |
| 5928 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5929 | #endif |
| 5930 | #ifdef _CS_HW_SERIAL |
| 5931 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5932 | #endif |
| 5933 | #ifdef _CS_INITTAB_NAME |
| 5934 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5935 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5936 | #ifdef _CS_LFS64_CFLAGS |
| 5937 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5938 | #endif |
| 5939 | #ifdef _CS_LFS64_LDFLAGS |
| 5940 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5941 | #endif |
| 5942 | #ifdef _CS_LFS64_LIBS |
| 5943 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5944 | #endif |
| 5945 | #ifdef _CS_LFS64_LINTFLAGS |
| 5946 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5947 | #endif |
| 5948 | #ifdef _CS_LFS_CFLAGS |
| 5949 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5950 | #endif |
| 5951 | #ifdef _CS_LFS_LDFLAGS |
| 5952 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5953 | #endif |
| 5954 | #ifdef _CS_LFS_LIBS |
| 5955 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5956 | #endif |
| 5957 | #ifdef _CS_LFS_LINTFLAGS |
| 5958 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5959 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5960 | #ifdef _CS_MACHINE |
| 5961 | {"CS_MACHINE", _CS_MACHINE}, |
| 5962 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5963 | #ifdef _CS_PATH |
| 5964 | {"CS_PATH", _CS_PATH}, |
| 5965 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5966 | #ifdef _CS_RELEASE |
| 5967 | {"CS_RELEASE", _CS_RELEASE}, |
| 5968 | #endif |
| 5969 | #ifdef _CS_SRPC_DOMAIN |
| 5970 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5971 | #endif |
| 5972 | #ifdef _CS_SYSNAME |
| 5973 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5974 | #endif |
| 5975 | #ifdef _CS_VERSION |
| 5976 | {"CS_VERSION", _CS_VERSION}, |
| 5977 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5978 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5979 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5980 | #endif |
| 5981 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5982 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5983 | #endif |
| 5984 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5985 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5986 | #endif |
| 5987 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5988 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5989 | #endif |
| 5990 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5991 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5992 | #endif |
| 5993 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5994 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5995 | #endif |
| 5996 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5997 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5998 | #endif |
| 5999 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6000 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6001 | #endif |
| 6002 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6003 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6004 | #endif |
| 6005 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6006 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6007 | #endif |
| 6008 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6009 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6010 | #endif |
| 6011 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6012 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6013 | #endif |
| 6014 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6015 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6016 | #endif |
| 6017 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6018 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6019 | #endif |
| 6020 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6021 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6022 | #endif |
| 6023 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6024 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6025 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6026 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6027 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6028 | #endif |
| 6029 | #ifdef _MIPS_CS_BASE |
| 6030 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6031 | #endif |
| 6032 | #ifdef _MIPS_CS_HOSTID |
| 6033 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6034 | #endif |
| 6035 | #ifdef _MIPS_CS_HW_NAME |
| 6036 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6037 | #endif |
| 6038 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6039 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6040 | #endif |
| 6041 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6042 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6043 | #endif |
| 6044 | #ifdef _MIPS_CS_OSREL_MIN |
| 6045 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6046 | #endif |
| 6047 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6048 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6049 | #endif |
| 6050 | #ifdef _MIPS_CS_OS_NAME |
| 6051 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6052 | #endif |
| 6053 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6054 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6055 | #endif |
| 6056 | #ifdef _MIPS_CS_PROCESSORS |
| 6057 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6058 | #endif |
| 6059 | #ifdef _MIPS_CS_SERIAL |
| 6060 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6061 | #endif |
| 6062 | #ifdef _MIPS_CS_VENDOR |
| 6063 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6064 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6065 | }; |
| 6066 | |
| 6067 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6068 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6069 | { |
| 6070 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6071 | sizeof(posix_constants_confstr) |
| 6072 | / sizeof(struct constdef)); |
| 6073 | } |
| 6074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6075 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6076 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6077 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6078 | |
| 6079 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6080 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6081 | { |
| 6082 | PyObject *result = NULL; |
| 6083 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6084 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6085 | |
| 6086 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6087 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6088 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6089 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6090 | len = confstr(name, buffer, sizeof(buffer)); |
| 6091 | if (len == 0) { |
| 6092 | if (errno) { |
| 6093 | posix_error(); |
| 6094 | } |
| 6095 | else { |
| 6096 | result = Py_None; |
| 6097 | Py_INCREF(Py_None); |
| 6098 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6099 | } |
| 6100 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6101 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6102 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6103 | if (result != NULL) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 6104 | confstr(name, _PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6105 | } |
| 6106 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6107 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6108 | } |
| 6109 | } |
| 6110 | return result; |
| 6111 | } |
| 6112 | #endif |
| 6113 | |
| 6114 | |
| 6115 | #ifdef HAVE_SYSCONF |
| 6116 | static struct constdef posix_constants_sysconf[] = { |
| 6117 | #ifdef _SC_2_CHAR_TERM |
| 6118 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6119 | #endif |
| 6120 | #ifdef _SC_2_C_BIND |
| 6121 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6122 | #endif |
| 6123 | #ifdef _SC_2_C_DEV |
| 6124 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6125 | #endif |
| 6126 | #ifdef _SC_2_C_VERSION |
| 6127 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6128 | #endif |
| 6129 | #ifdef _SC_2_FORT_DEV |
| 6130 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6131 | #endif |
| 6132 | #ifdef _SC_2_FORT_RUN |
| 6133 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6134 | #endif |
| 6135 | #ifdef _SC_2_LOCALEDEF |
| 6136 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6137 | #endif |
| 6138 | #ifdef _SC_2_SW_DEV |
| 6139 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6140 | #endif |
| 6141 | #ifdef _SC_2_UPE |
| 6142 | {"SC_2_UPE", _SC_2_UPE}, |
| 6143 | #endif |
| 6144 | #ifdef _SC_2_VERSION |
| 6145 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6146 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6147 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6148 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6149 | #endif |
| 6150 | #ifdef _SC_ACL |
| 6151 | {"SC_ACL", _SC_ACL}, |
| 6152 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6153 | #ifdef _SC_AIO_LISTIO_MAX |
| 6154 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6155 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6156 | #ifdef _SC_AIO_MAX |
| 6157 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6158 | #endif |
| 6159 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6160 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6161 | #endif |
| 6162 | #ifdef _SC_ARG_MAX |
| 6163 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6164 | #endif |
| 6165 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6166 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6167 | #endif |
| 6168 | #ifdef _SC_ATEXIT_MAX |
| 6169 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6170 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6171 | #ifdef _SC_AUDIT |
| 6172 | {"SC_AUDIT", _SC_AUDIT}, |
| 6173 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6174 | #ifdef _SC_AVPHYS_PAGES |
| 6175 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6176 | #endif |
| 6177 | #ifdef _SC_BC_BASE_MAX |
| 6178 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6179 | #endif |
| 6180 | #ifdef _SC_BC_DIM_MAX |
| 6181 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6182 | #endif |
| 6183 | #ifdef _SC_BC_SCALE_MAX |
| 6184 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6185 | #endif |
| 6186 | #ifdef _SC_BC_STRING_MAX |
| 6187 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6188 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6189 | #ifdef _SC_CAP |
| 6190 | {"SC_CAP", _SC_CAP}, |
| 6191 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6192 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6193 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6194 | #endif |
| 6195 | #ifdef _SC_CHAR_BIT |
| 6196 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6197 | #endif |
| 6198 | #ifdef _SC_CHAR_MAX |
| 6199 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6200 | #endif |
| 6201 | #ifdef _SC_CHAR_MIN |
| 6202 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6203 | #endif |
| 6204 | #ifdef _SC_CHILD_MAX |
| 6205 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6206 | #endif |
| 6207 | #ifdef _SC_CLK_TCK |
| 6208 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6209 | #endif |
| 6210 | #ifdef _SC_COHER_BLKSZ |
| 6211 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6212 | #endif |
| 6213 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6214 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6215 | #endif |
| 6216 | #ifdef _SC_DCACHE_ASSOC |
| 6217 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6218 | #endif |
| 6219 | #ifdef _SC_DCACHE_BLKSZ |
| 6220 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6221 | #endif |
| 6222 | #ifdef _SC_DCACHE_LINESZ |
| 6223 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6224 | #endif |
| 6225 | #ifdef _SC_DCACHE_SZ |
| 6226 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6227 | #endif |
| 6228 | #ifdef _SC_DCACHE_TBLKSZ |
| 6229 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6230 | #endif |
| 6231 | #ifdef _SC_DELAYTIMER_MAX |
| 6232 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6233 | #endif |
| 6234 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6235 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6236 | #endif |
| 6237 | #ifdef _SC_EXPR_NEST_MAX |
| 6238 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6239 | #endif |
| 6240 | #ifdef _SC_FSYNC |
| 6241 | {"SC_FSYNC", _SC_FSYNC}, |
| 6242 | #endif |
| 6243 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6244 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6245 | #endif |
| 6246 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6247 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6248 | #endif |
| 6249 | #ifdef _SC_ICACHE_ASSOC |
| 6250 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6251 | #endif |
| 6252 | #ifdef _SC_ICACHE_BLKSZ |
| 6253 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6254 | #endif |
| 6255 | #ifdef _SC_ICACHE_LINESZ |
| 6256 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6257 | #endif |
| 6258 | #ifdef _SC_ICACHE_SZ |
| 6259 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6260 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6261 | #ifdef _SC_INF |
| 6262 | {"SC_INF", _SC_INF}, |
| 6263 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6264 | #ifdef _SC_INT_MAX |
| 6265 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6266 | #endif |
| 6267 | #ifdef _SC_INT_MIN |
| 6268 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6269 | #endif |
| 6270 | #ifdef _SC_IOV_MAX |
| 6271 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6272 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6273 | #ifdef _SC_IP_SECOPTS |
| 6274 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6275 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6276 | #ifdef _SC_JOB_CONTROL |
| 6277 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6278 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6279 | #ifdef _SC_KERN_POINTERS |
| 6280 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6281 | #endif |
| 6282 | #ifdef _SC_KERN_SIM |
| 6283 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6284 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6285 | #ifdef _SC_LINE_MAX |
| 6286 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6287 | #endif |
| 6288 | #ifdef _SC_LOGIN_NAME_MAX |
| 6289 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6290 | #endif |
| 6291 | #ifdef _SC_LOGNAME_MAX |
| 6292 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6293 | #endif |
| 6294 | #ifdef _SC_LONG_BIT |
| 6295 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6296 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6297 | #ifdef _SC_MAC |
| 6298 | {"SC_MAC", _SC_MAC}, |
| 6299 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6300 | #ifdef _SC_MAPPED_FILES |
| 6301 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6302 | #endif |
| 6303 | #ifdef _SC_MAXPID |
| 6304 | {"SC_MAXPID", _SC_MAXPID}, |
| 6305 | #endif |
| 6306 | #ifdef _SC_MB_LEN_MAX |
| 6307 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6308 | #endif |
| 6309 | #ifdef _SC_MEMLOCK |
| 6310 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6311 | #endif |
| 6312 | #ifdef _SC_MEMLOCK_RANGE |
| 6313 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6314 | #endif |
| 6315 | #ifdef _SC_MEMORY_PROTECTION |
| 6316 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6317 | #endif |
| 6318 | #ifdef _SC_MESSAGE_PASSING |
| 6319 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6320 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6321 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6322 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6323 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6324 | #ifdef _SC_MQ_OPEN_MAX |
| 6325 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6326 | #endif |
| 6327 | #ifdef _SC_MQ_PRIO_MAX |
| 6328 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6329 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6330 | #ifdef _SC_NACLS_MAX |
| 6331 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6332 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6333 | #ifdef _SC_NGROUPS_MAX |
| 6334 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6335 | #endif |
| 6336 | #ifdef _SC_NL_ARGMAX |
| 6337 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6338 | #endif |
| 6339 | #ifdef _SC_NL_LANGMAX |
| 6340 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6341 | #endif |
| 6342 | #ifdef _SC_NL_MSGMAX |
| 6343 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6344 | #endif |
| 6345 | #ifdef _SC_NL_NMAX |
| 6346 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6347 | #endif |
| 6348 | #ifdef _SC_NL_SETMAX |
| 6349 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6350 | #endif |
| 6351 | #ifdef _SC_NL_TEXTMAX |
| 6352 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6353 | #endif |
| 6354 | #ifdef _SC_NPROCESSORS_CONF |
| 6355 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6356 | #endif |
| 6357 | #ifdef _SC_NPROCESSORS_ONLN |
| 6358 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6359 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6360 | #ifdef _SC_NPROC_CONF |
| 6361 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6362 | #endif |
| 6363 | #ifdef _SC_NPROC_ONLN |
| 6364 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6365 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6366 | #ifdef _SC_NZERO |
| 6367 | {"SC_NZERO", _SC_NZERO}, |
| 6368 | #endif |
| 6369 | #ifdef _SC_OPEN_MAX |
| 6370 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6371 | #endif |
| 6372 | #ifdef _SC_PAGESIZE |
| 6373 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6374 | #endif |
| 6375 | #ifdef _SC_PAGE_SIZE |
| 6376 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6377 | #endif |
| 6378 | #ifdef _SC_PASS_MAX |
| 6379 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6380 | #endif |
| 6381 | #ifdef _SC_PHYS_PAGES |
| 6382 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6383 | #endif |
| 6384 | #ifdef _SC_PII |
| 6385 | {"SC_PII", _SC_PII}, |
| 6386 | #endif |
| 6387 | #ifdef _SC_PII_INTERNET |
| 6388 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6389 | #endif |
| 6390 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6391 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6392 | #endif |
| 6393 | #ifdef _SC_PII_INTERNET_STREAM |
| 6394 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6395 | #endif |
| 6396 | #ifdef _SC_PII_OSI |
| 6397 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6398 | #endif |
| 6399 | #ifdef _SC_PII_OSI_CLTS |
| 6400 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6401 | #endif |
| 6402 | #ifdef _SC_PII_OSI_COTS |
| 6403 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6404 | #endif |
| 6405 | #ifdef _SC_PII_OSI_M |
| 6406 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6407 | #endif |
| 6408 | #ifdef _SC_PII_SOCKET |
| 6409 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6410 | #endif |
| 6411 | #ifdef _SC_PII_XTI |
| 6412 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6413 | #endif |
| 6414 | #ifdef _SC_POLL |
| 6415 | {"SC_POLL", _SC_POLL}, |
| 6416 | #endif |
| 6417 | #ifdef _SC_PRIORITIZED_IO |
| 6418 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6419 | #endif |
| 6420 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6421 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6422 | #endif |
| 6423 | #ifdef _SC_REALTIME_SIGNALS |
| 6424 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6425 | #endif |
| 6426 | #ifdef _SC_RE_DUP_MAX |
| 6427 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6428 | #endif |
| 6429 | #ifdef _SC_RTSIG_MAX |
| 6430 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6431 | #endif |
| 6432 | #ifdef _SC_SAVED_IDS |
| 6433 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6434 | #endif |
| 6435 | #ifdef _SC_SCHAR_MAX |
| 6436 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6437 | #endif |
| 6438 | #ifdef _SC_SCHAR_MIN |
| 6439 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6440 | #endif |
| 6441 | #ifdef _SC_SELECT |
| 6442 | {"SC_SELECT", _SC_SELECT}, |
| 6443 | #endif |
| 6444 | #ifdef _SC_SEMAPHORES |
| 6445 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6446 | #endif |
| 6447 | #ifdef _SC_SEM_NSEMS_MAX |
| 6448 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6449 | #endif |
| 6450 | #ifdef _SC_SEM_VALUE_MAX |
| 6451 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6452 | #endif |
| 6453 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6454 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6455 | #endif |
| 6456 | #ifdef _SC_SHRT_MAX |
| 6457 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6458 | #endif |
| 6459 | #ifdef _SC_SHRT_MIN |
| 6460 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6461 | #endif |
| 6462 | #ifdef _SC_SIGQUEUE_MAX |
| 6463 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6464 | #endif |
| 6465 | #ifdef _SC_SIGRT_MAX |
| 6466 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6467 | #endif |
| 6468 | #ifdef _SC_SIGRT_MIN |
| 6469 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6470 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6471 | #ifdef _SC_SOFTPOWER |
| 6472 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6473 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6474 | #ifdef _SC_SPLIT_CACHE |
| 6475 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6476 | #endif |
| 6477 | #ifdef _SC_SSIZE_MAX |
| 6478 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6479 | #endif |
| 6480 | #ifdef _SC_STACK_PROT |
| 6481 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6482 | #endif |
| 6483 | #ifdef _SC_STREAM_MAX |
| 6484 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6485 | #endif |
| 6486 | #ifdef _SC_SYNCHRONIZED_IO |
| 6487 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6488 | #endif |
| 6489 | #ifdef _SC_THREADS |
| 6490 | {"SC_THREADS", _SC_THREADS}, |
| 6491 | #endif |
| 6492 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6493 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6494 | #endif |
| 6495 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6496 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6497 | #endif |
| 6498 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6499 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6500 | #endif |
| 6501 | #ifdef _SC_THREAD_KEYS_MAX |
| 6502 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6503 | #endif |
| 6504 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6505 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6506 | #endif |
| 6507 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6508 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6509 | #endif |
| 6510 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6511 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6512 | #endif |
| 6513 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6514 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6515 | #endif |
| 6516 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6517 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6518 | #endif |
| 6519 | #ifdef _SC_THREAD_STACK_MIN |
| 6520 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6521 | #endif |
| 6522 | #ifdef _SC_THREAD_THREADS_MAX |
| 6523 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6524 | #endif |
| 6525 | #ifdef _SC_TIMERS |
| 6526 | {"SC_TIMERS", _SC_TIMERS}, |
| 6527 | #endif |
| 6528 | #ifdef _SC_TIMER_MAX |
| 6529 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6530 | #endif |
| 6531 | #ifdef _SC_TTY_NAME_MAX |
| 6532 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6533 | #endif |
| 6534 | #ifdef _SC_TZNAME_MAX |
| 6535 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6536 | #endif |
| 6537 | #ifdef _SC_T_IOV_MAX |
| 6538 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6539 | #endif |
| 6540 | #ifdef _SC_UCHAR_MAX |
| 6541 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6542 | #endif |
| 6543 | #ifdef _SC_UINT_MAX |
| 6544 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6545 | #endif |
| 6546 | #ifdef _SC_UIO_MAXIOV |
| 6547 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6548 | #endif |
| 6549 | #ifdef _SC_ULONG_MAX |
| 6550 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6551 | #endif |
| 6552 | #ifdef _SC_USHRT_MAX |
| 6553 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6554 | #endif |
| 6555 | #ifdef _SC_VERSION |
| 6556 | {"SC_VERSION", _SC_VERSION}, |
| 6557 | #endif |
| 6558 | #ifdef _SC_WORD_BIT |
| 6559 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6560 | #endif |
| 6561 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6562 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6563 | #endif |
| 6564 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6565 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6566 | #endif |
| 6567 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6568 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6569 | #endif |
| 6570 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6571 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6572 | #endif |
| 6573 | #ifdef _SC_XOPEN_CRYPT |
| 6574 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6575 | #endif |
| 6576 | #ifdef _SC_XOPEN_ENH_I18N |
| 6577 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6578 | #endif |
| 6579 | #ifdef _SC_XOPEN_LEGACY |
| 6580 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6581 | #endif |
| 6582 | #ifdef _SC_XOPEN_REALTIME |
| 6583 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6584 | #endif |
| 6585 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6586 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6587 | #endif |
| 6588 | #ifdef _SC_XOPEN_SHM |
| 6589 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6590 | #endif |
| 6591 | #ifdef _SC_XOPEN_UNIX |
| 6592 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6593 | #endif |
| 6594 | #ifdef _SC_XOPEN_VERSION |
| 6595 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6596 | #endif |
| 6597 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6598 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6599 | #endif |
| 6600 | #ifdef _SC_XOPEN_XPG2 |
| 6601 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6602 | #endif |
| 6603 | #ifdef _SC_XOPEN_XPG3 |
| 6604 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6605 | #endif |
| 6606 | #ifdef _SC_XOPEN_XPG4 |
| 6607 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6608 | #endif |
| 6609 | }; |
| 6610 | |
| 6611 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6612 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6613 | { |
| 6614 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6615 | sizeof(posix_constants_sysconf) |
| 6616 | / sizeof(struct constdef)); |
| 6617 | } |
| 6618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6619 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6620 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6621 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6622 | |
| 6623 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6624 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6625 | { |
| 6626 | PyObject *result = NULL; |
| 6627 | int name; |
| 6628 | |
| 6629 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6630 | int value; |
| 6631 | |
| 6632 | errno = 0; |
| 6633 | value = sysconf(name); |
| 6634 | if (value == -1 && errno != 0) |
| 6635 | posix_error(); |
| 6636 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6637 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6638 | } |
| 6639 | return result; |
| 6640 | } |
| 6641 | #endif |
| 6642 | |
| 6643 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6644 | /* This code is used to ensure that the tables of configuration value names |
| 6645 | * are in sorted order as required by conv_confname(), and also to build the |
| 6646 | * the exported dictionaries that are used to publish information about the |
| 6647 | * names available on the host platform. |
| 6648 | * |
| 6649 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6650 | * when used, even for platforms we're not able to test on. It also makes |
| 6651 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6652 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6653 | |
| 6654 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6655 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6656 | { |
| 6657 | const struct constdef *c1 = |
| 6658 | (const struct constdef *) v1; |
| 6659 | const struct constdef *c2 = |
| 6660 | (const struct constdef *) v2; |
| 6661 | |
| 6662 | return strcmp(c1->name, c2->name); |
| 6663 | } |
| 6664 | |
| 6665 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6666 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6667 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6668 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6669 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6670 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6671 | |
| 6672 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6673 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6674 | if (d == NULL) |
| 6675 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6676 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6677 | for (i=0; i < tablesize; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6678 | PyObject *o = PyLong_FromLong(table[i].value); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6679 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6680 | Py_XDECREF(o); |
| 6681 | Py_DECREF(d); |
| 6682 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6683 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6684 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6685 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6686 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6687 | } |
| 6688 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6689 | /* Return -1 on failure, 0 on success. */ |
| 6690 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6691 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6692 | { |
| 6693 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6694 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6695 | sizeof(posix_constants_pathconf) |
| 6696 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6697 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6698 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6699 | #endif |
| 6700 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6701 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6702 | sizeof(posix_constants_confstr) |
| 6703 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6704 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6705 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6706 | #endif |
| 6707 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6708 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6709 | sizeof(posix_constants_sysconf) |
| 6710 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6711 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6712 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6713 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6714 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6715 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6716 | |
| 6717 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6718 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6719 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6720 | 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] | 6721 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6722 | |
| 6723 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6724 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6725 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6726 | abort(); |
| 6727 | /*NOTREACHED*/ |
| 6728 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6729 | return NULL; |
| 6730 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6731 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6732 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6733 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6734 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6735 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6736 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6737 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6738 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6739 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6740 | application (if any) its extension is associated.\n\ |
| 6741 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6742 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6743 | \n\ |
| 6744 | startfile returns as soon as the associated application is launched.\n\ |
| 6745 | There is no option to wait for the application to close, and no way\n\ |
| 6746 | to retrieve the application's exit status.\n\ |
| 6747 | \n\ |
| 6748 | The filepath is relative to the current directory. If you want to use\n\ |
| 6749 | 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] | 6750 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6751 | |
| 6752 | static PyObject * |
| 6753 | win32_startfile(PyObject *self, PyObject *args) |
| 6754 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6755 | PyObject *ofilepath; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6756 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6757 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6758 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 6759 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6760 | PyObject *unipath, *woperation = NULL; |
| 6761 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6762 | &unipath, &operation)) { |
| 6763 | PyErr_Clear(); |
| 6764 | goto normal; |
| 6765 | } |
| 6766 | |
| 6767 | if (operation) { |
| 6768 | woperation = PyUnicode_DecodeASCII(operation, |
| 6769 | strlen(operation), NULL); |
| 6770 | if (!woperation) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6771 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6772 | operation = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6773 | goto normal; |
| 6774 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6775 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6776 | |
| 6777 | Py_BEGIN_ALLOW_THREADS |
| 6778 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6779 | PyUnicode_AS_UNICODE(unipath), |
| 6780 | NULL, NULL, SW_SHOWNORMAL); |
| 6781 | Py_END_ALLOW_THREADS |
| 6782 | |
| 6783 | Py_XDECREF(woperation); |
| 6784 | if (rc <= (HINSTANCE)32) { |
| 6785 | PyObject *errval = win32_error_unicode("startfile", |
| 6786 | PyUnicode_AS_UNICODE(unipath)); |
| 6787 | return errval; |
| 6788 | } |
| 6789 | Py_INCREF(Py_None); |
| 6790 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6791 | |
| 6792 | normal: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6793 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 6794 | PyUnicode_FSConverter, &ofilepath, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6795 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6796 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6797 | filepath = bytes2str(ofilepath, 1); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6798 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6799 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6800 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6801 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6802 | if (rc <= (HINSTANCE)32) { |
| 6803 | PyObject *errval = win32_error("startfile", filepath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6804 | release_bytes(ofilepath); |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6805 | return errval; |
| 6806 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6807 | release_bytes(ofilepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6808 | Py_INCREF(Py_None); |
| 6809 | return Py_None; |
| 6810 | } |
| 6811 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6812 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6813 | #ifdef HAVE_GETLOADAVG |
| 6814 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6815 | "getloadavg() -> (float, float, float)\n\n\ |
| 6816 | Return the number of processes in the system run queue averaged over\n\ |
| 6817 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6818 | was unobtainable"); |
| 6819 | |
| 6820 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6821 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6822 | { |
| 6823 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6824 | if (getloadavg(loadavg, 3)!=3) { |
| 6825 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6826 | return NULL; |
| 6827 | } else |
| 6828 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6829 | } |
| 6830 | #endif |
| 6831 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6832 | #ifdef MS_WINDOWS |
| 6833 | |
| 6834 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6835 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6836 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6837 | |
| 6838 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6839 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6840 | DWORD dwFlags ); |
| 6841 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6842 | BYTE *pbBuffer ); |
| 6843 | |
| 6844 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6845 | /* This handle is never explicitly released. Instead, the operating |
| 6846 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6847 | static HCRYPTPROV hCryptProv = 0; |
| 6848 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6849 | static PyObject* |
| 6850 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6851 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6852 | int howMany; |
| 6853 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6854 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6855 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6856 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6857 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6858 | if (howMany < 0) |
| 6859 | return PyErr_Format(PyExc_ValueError, |
| 6860 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6861 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6862 | if (hCryptProv == 0) { |
| 6863 | HINSTANCE hAdvAPI32 = NULL; |
| 6864 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6865 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6866 | /* Obtain handle to the DLL containing CryptoAPI |
| 6867 | This should not fail */ |
| 6868 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6869 | if(hAdvAPI32 == NULL) |
| 6870 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6871 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6872 | /* Obtain pointers to the CryptoAPI functions |
| 6873 | This will fail on some early versions of Win95 */ |
| 6874 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6875 | hAdvAPI32, |
| 6876 | "CryptAcquireContextA"); |
| 6877 | if (pCryptAcquireContext == NULL) |
| 6878 | return PyErr_Format(PyExc_NotImplementedError, |
| 6879 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6880 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6881 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6882 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6883 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6884 | return PyErr_Format(PyExc_NotImplementedError, |
| 6885 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6886 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6887 | /* Acquire context */ |
| 6888 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6889 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6890 | return win32_error("CryptAcquireContext", NULL); |
| 6891 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6892 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6893 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6894 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6895 | if (result != NULL) { |
| 6896 | /* Get random data */ |
Amaury Forgeot d'Arc | a05ada3 | 2008-07-21 21:13:14 +0000 | [diff] [blame] | 6897 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6898 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6899 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6900 | Py_DECREF(result); |
| 6901 | return win32_error("CryptGenRandom", NULL); |
| 6902 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6903 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6904 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6905 | } |
| 6906 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6907 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6908 | PyDoc_STRVAR(device_encoding__doc__, |
| 6909 | "device_encoding(fd) -> str\n\n\ |
| 6910 | Return a string describing the encoding of the device\n\ |
| 6911 | if the output is a terminal; else return None."); |
| 6912 | |
| 6913 | static PyObject * |
| 6914 | device_encoding(PyObject *self, PyObject *args) |
| 6915 | { |
| 6916 | int fd; |
| 6917 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6918 | return NULL; |
Kristján Valur Jónsson | 649170b | 2009-03-24 14:15:49 +0000 | [diff] [blame] | 6919 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6920 | Py_INCREF(Py_None); |
| 6921 | return Py_None; |
| 6922 | } |
| 6923 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6924 | if (fd == 0) { |
| 6925 | char buf[100]; |
| 6926 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6927 | return PyUnicode_FromString(buf); |
| 6928 | } |
| 6929 | if (fd == 1 || fd == 2) { |
| 6930 | char buf[100]; |
| 6931 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6932 | return PyUnicode_FromString(buf); |
| 6933 | } |
| 6934 | #elif defined(CODESET) |
| 6935 | { |
| 6936 | char *codeset = nl_langinfo(CODESET); |
Mark Dickinson | da2706b | 2008-12-11 18:03:03 +0000 | [diff] [blame] | 6937 | if (codeset != NULL && codeset[0] != 0) |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6938 | return PyUnicode_FromString(codeset); |
| 6939 | } |
| 6940 | #endif |
| 6941 | Py_INCREF(Py_None); |
| 6942 | return Py_None; |
| 6943 | } |
| 6944 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6945 | #ifdef __VMS |
| 6946 | /* Use openssl random routine */ |
| 6947 | #include <openssl/rand.h> |
| 6948 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6949 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6950 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6951 | |
| 6952 | static PyObject* |
| 6953 | vms_urandom(PyObject *self, PyObject *args) |
| 6954 | { |
| 6955 | int howMany; |
| 6956 | PyObject* result; |
| 6957 | |
| 6958 | /* Read arguments */ |
| 6959 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6960 | return NULL; |
| 6961 | if (howMany < 0) |
| 6962 | return PyErr_Format(PyExc_ValueError, |
| 6963 | "negative argument not allowed"); |
| 6964 | |
| 6965 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6966 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6967 | if (result != NULL) { |
| 6968 | /* Get random data */ |
| 6969 | if (RAND_pseudo_bytes((unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6970 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6971 | howMany) < 0) { |
| 6972 | Py_DECREF(result); |
| 6973 | return PyErr_Format(PyExc_ValueError, |
| 6974 | "RAND_pseudo_bytes"); |
| 6975 | } |
| 6976 | } |
| 6977 | return result; |
| 6978 | } |
| 6979 | #endif |
| 6980 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 6981 | #ifdef HAVE_SETRESUID |
| 6982 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 6983 | "setresuid(ruid, euid, suid)\n\n\ |
| 6984 | Set the current process's real, effective, and saved user ids."); |
| 6985 | |
| 6986 | static PyObject* |
| 6987 | posix_setresuid (PyObject *self, PyObject *args) |
| 6988 | { |
| 6989 | /* We assume uid_t is no larger than a long. */ |
| 6990 | long ruid, euid, suid; |
| 6991 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 6992 | return NULL; |
| 6993 | if (setresuid(ruid, euid, suid) < 0) |
| 6994 | return posix_error(); |
| 6995 | Py_RETURN_NONE; |
| 6996 | } |
| 6997 | #endif |
| 6998 | |
| 6999 | #ifdef HAVE_SETRESGID |
| 7000 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 7001 | "setresgid(rgid, egid, sgid)\n\n\ |
| 7002 | Set the current process's real, effective, and saved group ids."); |
| 7003 | |
| 7004 | static PyObject* |
| 7005 | posix_setresgid (PyObject *self, PyObject *args) |
| 7006 | { |
| 7007 | /* We assume uid_t is no larger than a long. */ |
| 7008 | long rgid, egid, sgid; |
| 7009 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 7010 | return NULL; |
| 7011 | if (setresgid(rgid, egid, sgid) < 0) |
| 7012 | return posix_error(); |
| 7013 | Py_RETURN_NONE; |
| 7014 | } |
| 7015 | #endif |
| 7016 | |
| 7017 | #ifdef HAVE_GETRESUID |
| 7018 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 7019 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 7020 | Get tuple of the current process's real, effective, and saved user ids."); |
| 7021 | |
| 7022 | static PyObject* |
| 7023 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 7024 | { |
| 7025 | uid_t ruid, euid, suid; |
| 7026 | long l_ruid, l_euid, l_suid; |
| 7027 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 7028 | return posix_error(); |
| 7029 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 7030 | l_ruid = ruid; |
| 7031 | l_euid = euid; |
| 7032 | l_suid = suid; |
| 7033 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
| 7034 | } |
| 7035 | #endif |
| 7036 | |
| 7037 | #ifdef HAVE_GETRESGID |
| 7038 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 7039 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
| 7040 | Get tuple of the current process's real, effective, and saved user ids."); |
| 7041 | |
| 7042 | static PyObject* |
| 7043 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 7044 | { |
| 7045 | uid_t rgid, egid, sgid; |
| 7046 | long l_rgid, l_egid, l_sgid; |
| 7047 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 7048 | return posix_error(); |
| 7049 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 7050 | l_rgid = rgid; |
| 7051 | l_egid = egid; |
| 7052 | l_sgid = sgid; |
| 7053 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
| 7054 | } |
| 7055 | #endif |
| 7056 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7057 | static PyMethodDef posix_methods[] = { |
| 7058 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7059 | #ifdef HAVE_TTYNAME |
| 7060 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7061 | #endif |
| 7062 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7063 | #ifdef HAVE_CHFLAGS |
| 7064 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 7065 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7066 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7067 | #ifdef HAVE_FCHMOD |
| 7068 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
| 7069 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7070 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7071 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7072 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7073 | #ifdef HAVE_LCHMOD |
| 7074 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
| 7075 | #endif /* HAVE_LCHMOD */ |
| 7076 | #ifdef HAVE_FCHOWN |
| 7077 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
| 7078 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7079 | #ifdef HAVE_LCHFLAGS |
| 7080 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 7081 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7082 | #ifdef HAVE_LCHOWN |
| 7083 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7084 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7085 | #ifdef HAVE_CHROOT |
| 7086 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7087 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7088 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7089 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7090 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7091 | #ifdef HAVE_GETCWD |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 7092 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 7093 | METH_NOARGS, posix_getcwd__doc__}, |
| 7094 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 7095 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7096 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7097 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7098 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7099 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7100 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7101 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7102 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7103 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7104 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7105 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7106 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7107 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7108 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7109 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7110 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7111 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7112 | {"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] | 7113 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7114 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7115 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7116 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7117 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7118 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7119 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7120 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7121 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7122 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7123 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7124 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7125 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7126 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7127 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7128 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7129 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7130 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7131 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7132 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7133 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7134 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7135 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7136 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7137 | #if defined(PYOS_OS2) |
| 7138 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7139 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7140 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7141 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7142 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7143 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7144 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7145 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7146 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7147 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7148 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7149 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7150 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7151 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7152 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7153 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7154 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7155 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7156 | #endif /* HAVE_GETEGID */ |
| 7157 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7158 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7159 | #endif /* HAVE_GETEUID */ |
| 7160 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7161 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7162 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7163 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7164 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7165 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7166 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7167 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7168 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7169 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7170 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7171 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7172 | #endif /* HAVE_GETPPID */ |
| 7173 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7174 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7175 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7176 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7177 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7178 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7179 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7180 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7181 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7182 | #ifdef HAVE_KILLPG |
| 7183 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7184 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7185 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7186 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7187 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7188 | #ifdef MS_WINDOWS |
| 7189 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 7190 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7191 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7192 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7193 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7194 | #ifdef HAVE_SETEUID |
| 7195 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7196 | #endif /* HAVE_SETEUID */ |
| 7197 | #ifdef HAVE_SETEGID |
| 7198 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7199 | #endif /* HAVE_SETEGID */ |
| 7200 | #ifdef HAVE_SETREUID |
| 7201 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7202 | #endif /* HAVE_SETREUID */ |
| 7203 | #ifdef HAVE_SETREGID |
| 7204 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7205 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7206 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7207 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7208 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7209 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 7210 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7211 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7212 | #ifdef HAVE_INITGROUPS |
| 7213 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
| 7214 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7215 | #ifdef HAVE_GETPGID |
| 7216 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7217 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7218 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7219 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7220 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7221 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7222 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7223 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7224 | #ifdef HAVE_WAIT3 |
| 7225 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7226 | #endif /* HAVE_WAIT3 */ |
| 7227 | #ifdef HAVE_WAIT4 |
| 7228 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7229 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7230 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7231 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7232 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7233 | #ifdef HAVE_GETSID |
| 7234 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7235 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7236 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7237 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7238 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7239 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7240 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7241 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7242 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7243 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7244 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7245 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7246 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7247 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7248 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7249 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7250 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7251 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7252 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7253 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7254 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7255 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7256 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7257 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7258 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7259 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7260 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7261 | #endif |
| 7262 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7263 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7264 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7265 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7266 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7267 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7268 | #ifdef HAVE_DEVICE_MACROS |
| 7269 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7270 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7271 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7272 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7273 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7274 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7275 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7276 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7277 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7278 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7279 | #ifdef HAVE_UNSETENV |
| 7280 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7281 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7282 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7283 | #ifdef HAVE_FCHDIR |
| 7284 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7285 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7286 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7287 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7288 | #endif |
| 7289 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7290 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7291 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7292 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7293 | #ifdef WCOREDUMP |
| 7294 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7295 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7296 | #ifdef WIFCONTINUED |
| 7297 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7298 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7299 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7300 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7301 | #endif /* WIFSTOPPED */ |
| 7302 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7303 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7304 | #endif /* WIFSIGNALED */ |
| 7305 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7306 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7307 | #endif /* WIFEXITED */ |
| 7308 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7309 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7310 | #endif /* WEXITSTATUS */ |
| 7311 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7312 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7313 | #endif /* WTERMSIG */ |
| 7314 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7315 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7316 | #endif /* WSTOPSIG */ |
| 7317 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7318 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7319 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7320 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7321 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7322 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7323 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7324 | #ifdef HAVE_CONFSTR |
| 7325 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7326 | #endif |
| 7327 | #ifdef HAVE_SYSCONF |
| 7328 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7329 | #endif |
| 7330 | #ifdef HAVE_FPATHCONF |
| 7331 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7332 | #endif |
| 7333 | #ifdef HAVE_PATHCONF |
| 7334 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7335 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7336 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7337 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7338 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7339 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7340 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7341 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7342 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7343 | #ifdef MS_WINDOWS |
| 7344 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7345 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7346 | #ifdef __VMS |
| 7347 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 7348 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 7349 | #ifdef HAVE_SETRESUID |
| 7350 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
| 7351 | #endif |
| 7352 | #ifdef HAVE_SETRESGID |
| 7353 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
| 7354 | #endif |
| 7355 | #ifdef HAVE_GETRESUID |
| 7356 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
| 7357 | #endif |
| 7358 | #ifdef HAVE_GETRESGID |
| 7359 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
| 7360 | #endif |
| 7361 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7362 | {NULL, NULL} /* Sentinel */ |
| 7363 | }; |
| 7364 | |
| 7365 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7366 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7367 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7368 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7369 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7370 | } |
| 7371 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7372 | #if defined(PYOS_OS2) |
| 7373 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7374 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7375 | { |
| 7376 | APIRET rc; |
| 7377 | ULONG values[QSV_MAX+1]; |
| 7378 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7379 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7380 | |
| 7381 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7382 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7383 | Py_END_ALLOW_THREADS |
| 7384 | |
| 7385 | if (rc != NO_ERROR) { |
| 7386 | os2_error(rc); |
| 7387 | return -1; |
| 7388 | } |
| 7389 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7390 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7391 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7392 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7393 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7394 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7395 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7396 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7397 | |
| 7398 | switch (values[QSV_VERSION_MINOR]) { |
| 7399 | case 0: ver = "2.00"; break; |
| 7400 | case 10: ver = "2.10"; break; |
| 7401 | case 11: ver = "2.11"; break; |
| 7402 | case 30: ver = "3.00"; break; |
| 7403 | case 40: ver = "4.00"; break; |
| 7404 | case 50: ver = "5.00"; break; |
| 7405 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7406 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7407 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7408 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7409 | ver = &tmp[0]; |
| 7410 | } |
| 7411 | |
| 7412 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7413 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7414 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7415 | |
| 7416 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7417 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7418 | tmp[1] = ':'; |
| 7419 | tmp[2] = '\0'; |
| 7420 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7421 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7422 | } |
| 7423 | #endif |
| 7424 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7425 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7426 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7427 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7428 | #ifdef F_OK |
| 7429 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7430 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7431 | #ifdef R_OK |
| 7432 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7433 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7434 | #ifdef W_OK |
| 7435 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7436 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7437 | #ifdef X_OK |
| 7438 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7439 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7440 | #ifdef NGROUPS_MAX |
| 7441 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7442 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7443 | #ifdef TMP_MAX |
| 7444 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7445 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7446 | #ifdef WCONTINUED |
| 7447 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7448 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7449 | #ifdef WNOHANG |
| 7450 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7451 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7452 | #ifdef WUNTRACED |
| 7453 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7454 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7455 | #ifdef O_RDONLY |
| 7456 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7457 | #endif |
| 7458 | #ifdef O_WRONLY |
| 7459 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7460 | #endif |
| 7461 | #ifdef O_RDWR |
| 7462 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7463 | #endif |
| 7464 | #ifdef O_NDELAY |
| 7465 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7466 | #endif |
| 7467 | #ifdef O_NONBLOCK |
| 7468 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7469 | #endif |
| 7470 | #ifdef O_APPEND |
| 7471 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7472 | #endif |
| 7473 | #ifdef O_DSYNC |
| 7474 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7475 | #endif |
| 7476 | #ifdef O_RSYNC |
| 7477 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7478 | #endif |
| 7479 | #ifdef O_SYNC |
| 7480 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7481 | #endif |
| 7482 | #ifdef O_NOCTTY |
| 7483 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7484 | #endif |
| 7485 | #ifdef O_CREAT |
| 7486 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7487 | #endif |
| 7488 | #ifdef O_EXCL |
| 7489 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7490 | #endif |
| 7491 | #ifdef O_TRUNC |
| 7492 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7493 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7494 | #ifdef O_BINARY |
| 7495 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7496 | #endif |
| 7497 | #ifdef O_TEXT |
| 7498 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7499 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7500 | #ifdef O_LARGEFILE |
| 7501 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7502 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7503 | #ifdef O_SHLOCK |
| 7504 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7505 | #endif |
| 7506 | #ifdef O_EXLOCK |
| 7507 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7508 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7509 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7510 | /* MS Windows */ |
| 7511 | #ifdef O_NOINHERIT |
| 7512 | /* Don't inherit in child processes. */ |
| 7513 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7514 | #endif |
| 7515 | #ifdef _O_SHORT_LIVED |
| 7516 | /* Optimize for short life (keep in memory). */ |
| 7517 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7518 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7519 | #endif |
| 7520 | #ifdef O_TEMPORARY |
| 7521 | /* Automatically delete when last handle is closed. */ |
| 7522 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7523 | #endif |
| 7524 | #ifdef O_RANDOM |
| 7525 | /* Optimize for random access. */ |
| 7526 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7527 | #endif |
| 7528 | #ifdef O_SEQUENTIAL |
| 7529 | /* Optimize for sequential access. */ |
| 7530 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7531 | #endif |
| 7532 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7533 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7534 | #ifdef O_ASYNC |
| 7535 | /* Send a SIGIO signal whenever input or output |
| 7536 | becomes available on file descriptor */ |
| 7537 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
| 7538 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7539 | #ifdef O_DIRECT |
| 7540 | /* Direct disk access. */ |
| 7541 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7542 | #endif |
| 7543 | #ifdef O_DIRECTORY |
| 7544 | /* Must be a directory. */ |
| 7545 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7546 | #endif |
| 7547 | #ifdef O_NOFOLLOW |
| 7548 | /* Do not follow links. */ |
| 7549 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7550 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7551 | #ifdef O_NOATIME |
| 7552 | /* Do not update the access time. */ |
| 7553 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
| 7554 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7555 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7556 | /* These come from sysexits.h */ |
| 7557 | #ifdef EX_OK |
| 7558 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7559 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7560 | #ifdef EX_USAGE |
| 7561 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7562 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7563 | #ifdef EX_DATAERR |
| 7564 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7565 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7566 | #ifdef EX_NOINPUT |
| 7567 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7568 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7569 | #ifdef EX_NOUSER |
| 7570 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7571 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7572 | #ifdef EX_NOHOST |
| 7573 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7574 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7575 | #ifdef EX_UNAVAILABLE |
| 7576 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7577 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7578 | #ifdef EX_SOFTWARE |
| 7579 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7580 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7581 | #ifdef EX_OSERR |
| 7582 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7583 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7584 | #ifdef EX_OSFILE |
| 7585 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7586 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7587 | #ifdef EX_CANTCREAT |
| 7588 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7589 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7590 | #ifdef EX_IOERR |
| 7591 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7592 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7593 | #ifdef EX_TEMPFAIL |
| 7594 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7595 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7596 | #ifdef EX_PROTOCOL |
| 7597 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7598 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7599 | #ifdef EX_NOPERM |
| 7600 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7601 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7602 | #ifdef EX_CONFIG |
| 7603 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7604 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7605 | #ifdef EX_NOTFOUND |
| 7606 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7607 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7608 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7609 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7610 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7611 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7612 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7613 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7614 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7615 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7616 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7617 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7618 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7619 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7620 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7621 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7622 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7623 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7624 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7625 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7626 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7627 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7628 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7629 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7630 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7631 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7632 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7633 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7634 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7635 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7636 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7637 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7638 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7639 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7640 | #if defined(PYOS_OS2) |
| 7641 | if (insertvalues(d)) return -1; |
| 7642 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7643 | return 0; |
| 7644 | } |
| 7645 | |
| 7646 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7647 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7648 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7649 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7650 | |
| 7651 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7652 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7653 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7654 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7655 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7656 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7657 | #define MODNAME "posix" |
| 7658 | #endif |
| 7659 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7660 | static struct PyModuleDef posixmodule = { |
| 7661 | PyModuleDef_HEAD_INIT, |
| 7662 | MODNAME, |
| 7663 | posix__doc__, |
| 7664 | -1, |
| 7665 | posix_methods, |
| 7666 | NULL, |
| 7667 | NULL, |
| 7668 | NULL, |
| 7669 | NULL |
| 7670 | }; |
| 7671 | |
| 7672 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7673 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7674 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7675 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7676 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7677 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7678 | m = PyModule_Create(&posixmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7679 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7680 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7681 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7682 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7683 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7684 | Py_XINCREF(v); |
| 7685 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7686 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7687 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7688 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7689 | if (all_ins(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7690 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7691 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7692 | if (setup_confname_tables(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7693 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7694 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7695 | Py_INCREF(PyExc_OSError); |
| 7696 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7697 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7698 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7699 | if (posix_putenv_garbage == NULL) |
| 7700 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7701 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7702 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7703 | if (!initialized) { |
| 7704 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7705 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7706 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7707 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7708 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7709 | structseq_new = StatResultType.tp_new; |
| 7710 | StatResultType.tp_new = statresult_new; |
| 7711 | |
| 7712 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7713 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7714 | #ifdef NEED_TICKS_PER_SECOND |
| 7715 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
| 7716 | ticks_per_second = sysconf(_SC_CLK_TCK); |
| 7717 | # elif defined(HZ) |
| 7718 | ticks_per_second = HZ; |
| 7719 | # else |
| 7720 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
| 7721 | # endif |
| 7722 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7723 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7724 | Py_INCREF((PyObject*) &StatResultType); |
| 7725 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7726 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7727 | PyModule_AddObject(m, "statvfs_result", |
| 7728 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7729 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7730 | |
| 7731 | #ifdef __APPLE__ |
| 7732 | /* |
| 7733 | * Step 2 of weak-linking support on Mac OS X. |
| 7734 | * |
| 7735 | * The code below removes functions that are not available on the |
| 7736 | * currently active platform. |
| 7737 | * |
| 7738 | * This block allow one to use a python binary that was build on |
| 7739 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7740 | * OSX 10.4. |
| 7741 | */ |
| 7742 | #ifdef HAVE_FSTATVFS |
| 7743 | if (fstatvfs == NULL) { |
| 7744 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7745 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7746 | } |
| 7747 | } |
| 7748 | #endif /* HAVE_FSTATVFS */ |
| 7749 | |
| 7750 | #ifdef HAVE_STATVFS |
| 7751 | if (statvfs == NULL) { |
| 7752 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7753 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7754 | } |
| 7755 | } |
| 7756 | #endif /* HAVE_STATVFS */ |
| 7757 | |
| 7758 | # ifdef HAVE_LCHOWN |
| 7759 | if (lchown == NULL) { |
| 7760 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7761 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7762 | } |
| 7763 | } |
| 7764 | #endif /* HAVE_LCHOWN */ |
| 7765 | |
| 7766 | |
| 7767 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7768 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7769 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7770 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7771 | |
| 7772 | #ifdef __cplusplus |
| 7773 | } |
| 7774 | #endif |