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 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 762 | #ifdef MS_WINDOWS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 763 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 764 | unicode_file_names(void) |
| 765 | { |
| 766 | static int canusewide = -1; |
| 767 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 768 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 769 | the Windows NT family. */ |
| 770 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 771 | } |
| 772 | return canusewide; |
| 773 | } |
| 774 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 775 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 776 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 777 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 778 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 779 | PyObject *opath1 = NULL; |
| 780 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 781 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 782 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 783 | PyUnicode_FSConverter, &opath1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 784 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 785 | path1 = bytes2str(opath1, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 786 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 787 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 788 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 789 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 790 | return posix_error_with_allocated_filename(opath1); |
| 791 | release_bytes(opath1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 792 | Py_INCREF(Py_None); |
| 793 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 796 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 797 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 798 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 799 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 800 | { |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 801 | PyObject *opath1 = NULL, *opath2 = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 802 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 803 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 804 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 805 | PyUnicode_FSConverter, &opath1, |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 806 | PyUnicode_FSConverter, &opath2)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 807 | return NULL; |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 808 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 809 | path1 = bytes2str(opath1, 1); |
| 810 | path2 = bytes2str(opath2, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 811 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 812 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 813 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 814 | release_bytes(opath1); |
| 815 | release_bytes(opath2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 816 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 817 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 818 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 819 | Py_INCREF(Py_None); |
| 820 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 823 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 824 | static PyObject* |
| 825 | win32_1str(PyObject* args, char* func, |
| 826 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 827 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 828 | { |
| 829 | PyObject *uni; |
| 830 | char *ansi; |
| 831 | BOOL result; |
| 832 | if (unicode_file_names()) { |
| 833 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 834 | PyErr_Clear(); |
| 835 | else { |
| 836 | Py_BEGIN_ALLOW_THREADS |
| 837 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 838 | Py_END_ALLOW_THREADS |
| 839 | if (!result) |
| 840 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 841 | Py_INCREF(Py_None); |
| 842 | return Py_None; |
| 843 | } |
| 844 | } |
| 845 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 846 | return NULL; |
| 847 | Py_BEGIN_ALLOW_THREADS |
| 848 | result = funcA(ansi); |
| 849 | Py_END_ALLOW_THREADS |
| 850 | if (!result) |
| 851 | return win32_error(func, ansi); |
| 852 | Py_INCREF(Py_None); |
| 853 | return Py_None; |
| 854 | |
| 855 | } |
| 856 | |
| 857 | /* This is a reimplementation of the C library's chdir function, |
| 858 | but one that produces Win32 errors instead of DOS error codes. |
| 859 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 860 | it also needs to set "magic" environment variables indicating |
| 861 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 862 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 863 | win32_chdir(LPCSTR path) |
| 864 | { |
| 865 | char new_path[MAX_PATH+1]; |
| 866 | int result; |
| 867 | char env[4] = "=x:"; |
| 868 | |
| 869 | if(!SetCurrentDirectoryA(path)) |
| 870 | return FALSE; |
| 871 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 872 | if (!result) |
| 873 | return FALSE; |
| 874 | /* In the ANSI API, there should not be any paths longer |
| 875 | than MAX_PATH. */ |
| 876 | assert(result <= MAX_PATH+1); |
| 877 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 878 | strncmp(new_path, "//", 2) == 0) |
| 879 | /* UNC path, nothing to do. */ |
| 880 | return TRUE; |
| 881 | env[1] = new_path[0]; |
| 882 | return SetEnvironmentVariableA(env, new_path); |
| 883 | } |
| 884 | |
| 885 | /* The Unicode version differs from the ANSI version |
| 886 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 887 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 888 | win32_wchdir(LPCWSTR path) |
| 889 | { |
| 890 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 891 | int result; |
| 892 | wchar_t env[4] = L"=x:"; |
| 893 | |
| 894 | if(!SetCurrentDirectoryW(path)) |
| 895 | return FALSE; |
| 896 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 897 | if (!result) |
| 898 | return FALSE; |
| 899 | if (result > MAX_PATH+1) { |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 900 | new_path = malloc(result * sizeof(wchar_t)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 901 | if (!new_path) { |
| 902 | SetLastError(ERROR_OUTOFMEMORY); |
| 903 | return FALSE; |
| 904 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 905 | result = GetCurrentDirectoryW(result, new_path); |
| 906 | if (!result) { |
| 907 | free(new_path); |
| 908 | return FALSE; |
| 909 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 910 | } |
| 911 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 912 | wcsncmp(new_path, L"//", 2) == 0) |
| 913 | /* UNC path, nothing to do. */ |
| 914 | return TRUE; |
| 915 | env[1] = new_path[0]; |
| 916 | result = SetEnvironmentVariableW(env, new_path); |
| 917 | if (new_path != _new_path) |
| 918 | free(new_path); |
| 919 | return result; |
| 920 | } |
| 921 | #endif |
| 922 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 923 | #ifdef MS_WINDOWS |
| 924 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 925 | - time stamps are restricted to second resolution |
| 926 | - file modification times suffer from forth-and-back conversions between |
| 927 | UTC and local time |
| 928 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 929 | */ |
| 930 | #define HAVE_STAT_NSEC 1 |
| 931 | |
| 932 | struct win32_stat{ |
| 933 | int st_dev; |
| 934 | __int64 st_ino; |
| 935 | unsigned short st_mode; |
| 936 | int st_nlink; |
| 937 | int st_uid; |
| 938 | int st_gid; |
| 939 | int st_rdev; |
| 940 | __int64 st_size; |
| 941 | int st_atime; |
| 942 | int st_atime_nsec; |
| 943 | int st_mtime; |
| 944 | int st_mtime_nsec; |
| 945 | int st_ctime; |
| 946 | int st_ctime_nsec; |
| 947 | }; |
| 948 | |
| 949 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 950 | |
| 951 | static void |
| 952 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 953 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 954 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 955 | /* Cannot simply cast and dereference in_ptr, |
| 956 | since it might not be aligned properly */ |
| 957 | __int64 in; |
| 958 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 959 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 960 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 961 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 962 | } |
| 963 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 964 | static void |
| 965 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 966 | { |
| 967 | /* XXX endianness */ |
| 968 | __int64 out; |
| 969 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 970 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 971 | memcpy(out_ptr, &out, sizeof(out)); |
| 972 | } |
| 973 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 974 | /* Below, we *know* that ugo+r is 0444 */ |
| 975 | #if _S_IREAD != 0400 |
| 976 | #error Unsupported C library |
| 977 | #endif |
| 978 | static int |
| 979 | attributes_to_mode(DWORD attr) |
| 980 | { |
| 981 | int m = 0; |
| 982 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 983 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 984 | else |
| 985 | m |= _S_IFREG; |
| 986 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 987 | m |= 0444; |
| 988 | else |
| 989 | m |= 0666; |
| 990 | return m; |
| 991 | } |
| 992 | |
| 993 | static int |
| 994 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 995 | { |
| 996 | memset(result, 0, sizeof(*result)); |
| 997 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 998 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 999 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1000 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1001 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1006 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 1007 | static int checked = 0; |
| 1008 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 1009 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 1010 | static void |
| 1011 | check_gfax() |
| 1012 | { |
| 1013 | HINSTANCE hKernel32; |
| 1014 | if (checked) |
| 1015 | return; |
| 1016 | checked = 1; |
| 1017 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1018 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 1019 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 1020 | } |
| 1021 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1022 | static BOOL |
| 1023 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1024 | { |
| 1025 | HANDLE hFindFile; |
| 1026 | WIN32_FIND_DATAA FileData; |
| 1027 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1028 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1029 | return FALSE; |
| 1030 | FindClose(hFindFile); |
| 1031 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1032 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1033 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1034 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1035 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1036 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1037 | return TRUE; |
| 1038 | } |
| 1039 | |
| 1040 | static BOOL |
| 1041 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1042 | { |
| 1043 | HANDLE hFindFile; |
| 1044 | WIN32_FIND_DATAW FileData; |
| 1045 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1046 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1047 | return FALSE; |
| 1048 | FindClose(hFindFile); |
| 1049 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1050 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1051 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1052 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1053 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1054 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1055 | return TRUE; |
| 1056 | } |
| 1057 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1058 | static BOOL WINAPI |
| 1059 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 1060 | GET_FILEEX_INFO_LEVELS level, |
| 1061 | LPVOID pv) |
| 1062 | { |
| 1063 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1064 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1065 | /* First try to use the system's implementation, if that is |
| 1066 | available and either succeeds to gives an error other than |
| 1067 | that it isn't implemented. */ |
| 1068 | check_gfax(); |
| 1069 | if (gfaxa) { |
| 1070 | result = gfaxa(pszFile, level, pv); |
| 1071 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1072 | return result; |
| 1073 | } |
| 1074 | /* It's either not present, or not implemented. |
| 1075 | Emulate using FindFirstFile. */ |
| 1076 | if (level != GetFileExInfoStandard) { |
| 1077 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1078 | return FALSE; |
| 1079 | } |
| 1080 | /* Use GetFileAttributes to validate that the file name |
| 1081 | does not contain wildcards (which FindFirstFile would |
| 1082 | accept). */ |
| 1083 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 1084 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1085 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | static BOOL WINAPI |
| 1089 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 1090 | GET_FILEEX_INFO_LEVELS level, |
| 1091 | LPVOID pv) |
| 1092 | { |
| 1093 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1094 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1095 | /* First try to use the system's implementation, if that is |
| 1096 | available and either succeeds to gives an error other than |
| 1097 | that it isn't implemented. */ |
| 1098 | check_gfax(); |
| 1099 | if (gfaxa) { |
| 1100 | result = gfaxw(pszFile, level, pv); |
| 1101 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1102 | return result; |
| 1103 | } |
| 1104 | /* It's either not present, or not implemented. |
| 1105 | Emulate using FindFirstFile. */ |
| 1106 | if (level != GetFileExInfoStandard) { |
| 1107 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1108 | return FALSE; |
| 1109 | } |
| 1110 | /* Use GetFileAttributes to validate that the file name |
| 1111 | does not contain wildcards (which FindFirstFile would |
| 1112 | accept). */ |
| 1113 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 1114 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1115 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1118 | static int |
| 1119 | win32_stat(const char* path, struct win32_stat *result) |
| 1120 | { |
| 1121 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1122 | int code; |
| 1123 | char *dot; |
| 1124 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1125 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1126 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1127 | /* Protocol violation: we explicitly clear errno, instead of |
| 1128 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1129 | errno = 0; |
| 1130 | return -1; |
| 1131 | } else { |
| 1132 | /* Could not get attributes on open file. Fall back to |
| 1133 | reading the directory. */ |
| 1134 | if (!attributes_from_dir(path, &info)) { |
| 1135 | /* Very strange. This should not fail now */ |
| 1136 | errno = 0; |
| 1137 | return -1; |
| 1138 | } |
| 1139 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1140 | } |
| 1141 | code = attribute_data_to_stat(&info, result); |
| 1142 | if (code != 0) |
| 1143 | return code; |
| 1144 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1145 | dot = strrchr(path, '.'); |
| 1146 | if (dot) { |
| 1147 | if (stricmp(dot, ".bat") == 0 || |
| 1148 | stricmp(dot, ".cmd") == 0 || |
| 1149 | stricmp(dot, ".exe") == 0 || |
| 1150 | stricmp(dot, ".com") == 0) |
| 1151 | result->st_mode |= 0111; |
| 1152 | } |
| 1153 | return code; |
| 1154 | } |
| 1155 | |
| 1156 | static int |
| 1157 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1158 | { |
| 1159 | int code; |
| 1160 | const wchar_t *dot; |
| 1161 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1162 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1163 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1164 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1165 | /* Protocol violation: we explicitly clear errno, instead of |
| 1166 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1167 | errno = 0; |
| 1168 | return -1; |
| 1169 | } else { |
| 1170 | /* Could not get attributes on open file. Fall back to |
| 1171 | reading the directory. */ |
| 1172 | if (!attributes_from_dir_w(path, &info)) { |
| 1173 | /* Very strange. This should not fail now */ |
| 1174 | errno = 0; |
| 1175 | return -1; |
| 1176 | } |
| 1177 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1178 | } |
| 1179 | code = attribute_data_to_stat(&info, result); |
| 1180 | if (code < 0) |
| 1181 | return code; |
| 1182 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1183 | dot = wcsrchr(path, '.'); |
| 1184 | if (dot) { |
| 1185 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1186 | _wcsicmp(dot, L".cmd") == 0 || |
| 1187 | _wcsicmp(dot, L".exe") == 0 || |
| 1188 | _wcsicmp(dot, L".com") == 0) |
| 1189 | result->st_mode |= 0111; |
| 1190 | } |
| 1191 | return code; |
| 1192 | } |
| 1193 | |
| 1194 | static int |
| 1195 | win32_fstat(int file_number, struct win32_stat *result) |
| 1196 | { |
| 1197 | BY_HANDLE_FILE_INFORMATION info; |
| 1198 | HANDLE h; |
| 1199 | int type; |
| 1200 | |
| 1201 | h = (HANDLE)_get_osfhandle(file_number); |
| 1202 | |
| 1203 | /* Protocol violation: we explicitly clear errno, instead of |
| 1204 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1205 | errno = 0; |
| 1206 | |
| 1207 | if (h == INVALID_HANDLE_VALUE) { |
| 1208 | /* This is really a C library error (invalid file handle). |
| 1209 | We set the Win32 error to the closes one matching. */ |
| 1210 | SetLastError(ERROR_INVALID_HANDLE); |
| 1211 | return -1; |
| 1212 | } |
| 1213 | memset(result, 0, sizeof(*result)); |
| 1214 | |
| 1215 | type = GetFileType(h); |
| 1216 | if (type == FILE_TYPE_UNKNOWN) { |
| 1217 | DWORD error = GetLastError(); |
| 1218 | if (error != 0) { |
| 1219 | return -1; |
| 1220 | } |
| 1221 | /* else: valid but unknown file */ |
| 1222 | } |
| 1223 | |
| 1224 | if (type != FILE_TYPE_DISK) { |
| 1225 | if (type == FILE_TYPE_CHAR) |
| 1226 | result->st_mode = _S_IFCHR; |
| 1227 | else if (type == FILE_TYPE_PIPE) |
| 1228 | result->st_mode = _S_IFIFO; |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | if (!GetFileInformationByHandle(h, &info)) { |
| 1233 | return -1; |
| 1234 | } |
| 1235 | |
| 1236 | /* similar to stat() */ |
| 1237 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1238 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1239 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1240 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1241 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1242 | /* specific to fstat() */ |
| 1243 | result->st_nlink = info.nNumberOfLinks; |
| 1244 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | #endif /* MS_WINDOWS */ |
| 1249 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1250 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1251 | "stat_result: Result from stat or lstat.\n\n\ |
| 1252 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1253 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1254 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1255 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1256 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1257 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1258 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1259 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1260 | |
| 1261 | static PyStructSequence_Field stat_result_fields[] = { |
| 1262 | {"st_mode", "protection bits"}, |
| 1263 | {"st_ino", "inode"}, |
| 1264 | {"st_dev", "device"}, |
| 1265 | {"st_nlink", "number of hard links"}, |
| 1266 | {"st_uid", "user ID of owner"}, |
| 1267 | {"st_gid", "group ID of owner"}, |
| 1268 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1269 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1270 | {NULL, "integer time of last access"}, |
| 1271 | {NULL, "integer time of last modification"}, |
| 1272 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1273 | {"st_atime", "time of last access"}, |
| 1274 | {"st_mtime", "time of last modification"}, |
| 1275 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1276 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1277 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1278 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1279 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1280 | {"st_blocks", "number of blocks allocated"}, |
| 1281 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1282 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1283 | {"st_rdev", "device type (if inode device)"}, |
| 1284 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1285 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1286 | {"st_flags", "user defined flags for file"}, |
| 1287 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1288 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1289 | {"st_gen", "generation number"}, |
| 1290 | #endif |
| 1291 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1292 | {"st_birthtime", "time of creation"}, |
| 1293 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1294 | {0} |
| 1295 | }; |
| 1296 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1297 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1298 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1299 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1300 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1301 | #endif |
| 1302 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1303 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1304 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1305 | #else |
| 1306 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1307 | #endif |
| 1308 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1309 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1310 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1311 | #else |
| 1312 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1313 | #endif |
| 1314 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1315 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1316 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1317 | #else |
| 1318 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1319 | #endif |
| 1320 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1321 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1322 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1323 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1324 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1325 | #endif |
| 1326 | |
| 1327 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1328 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1329 | #else |
| 1330 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1331 | #endif |
| 1332 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1333 | static PyStructSequence_Desc stat_result_desc = { |
| 1334 | "stat_result", /* name */ |
| 1335 | stat_result__doc__, /* doc */ |
| 1336 | stat_result_fields, |
| 1337 | 10 |
| 1338 | }; |
| 1339 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1340 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1341 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1342 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1343 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1344 | 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] | 1345 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1346 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1347 | |
| 1348 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1349 | {"f_bsize", }, |
| 1350 | {"f_frsize", }, |
| 1351 | {"f_blocks", }, |
| 1352 | {"f_bfree", }, |
| 1353 | {"f_bavail", }, |
| 1354 | {"f_files", }, |
| 1355 | {"f_ffree", }, |
| 1356 | {"f_favail", }, |
| 1357 | {"f_flag", }, |
| 1358 | {"f_namemax",}, |
| 1359 | {0} |
| 1360 | }; |
| 1361 | |
| 1362 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1363 | "statvfs_result", /* name */ |
| 1364 | statvfs_result__doc__, /* doc */ |
| 1365 | statvfs_result_fields, |
| 1366 | 10 |
| 1367 | }; |
| 1368 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1369 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1370 | static PyTypeObject StatResultType; |
| 1371 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1372 | static newfunc structseq_new; |
| 1373 | |
| 1374 | static PyObject * |
| 1375 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1376 | { |
| 1377 | PyStructSequence *result; |
| 1378 | int i; |
| 1379 | |
| 1380 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1381 | if (!result) |
| 1382 | return NULL; |
| 1383 | /* If we have been initialized from a tuple, |
| 1384 | st_?time might be set to None. Initialize it |
| 1385 | from the int slots. */ |
| 1386 | for (i = 7; i <= 9; i++) { |
| 1387 | if (result->ob_item[i+3] == Py_None) { |
| 1388 | Py_DECREF(Py_None); |
| 1389 | Py_INCREF(result->ob_item[i]); |
| 1390 | result->ob_item[i+3] = result->ob_item[i]; |
| 1391 | } |
| 1392 | } |
| 1393 | return (PyObject*)result; |
| 1394 | } |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1399 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1400 | |
| 1401 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1402 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1403 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1404 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1405 | future calls return ints. \n\ |
| 1406 | If newval is omitted, return the current setting.\n"); |
| 1407 | |
| 1408 | static PyObject* |
| 1409 | stat_float_times(PyObject* self, PyObject *args) |
| 1410 | { |
| 1411 | int newval = -1; |
| 1412 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1413 | return NULL; |
| 1414 | if (newval == -1) |
| 1415 | /* Return old value */ |
| 1416 | return PyBool_FromLong(_stat_float_times); |
| 1417 | _stat_float_times = newval; |
| 1418 | Py_INCREF(Py_None); |
| 1419 | return Py_None; |
| 1420 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1421 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1422 | static void |
| 1423 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1424 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1425 | PyObject *fval,*ival; |
| 1426 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1427 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1428 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1429 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1430 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1431 | if (!ival) |
| 1432 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1433 | if (_stat_float_times) { |
| 1434 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1435 | } else { |
| 1436 | fval = ival; |
| 1437 | Py_INCREF(fval); |
| 1438 | } |
| 1439 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1440 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1443 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1444 | (used by posix_stat() and posix_fstat()) */ |
| 1445 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1446 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1447 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1448 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1449 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1450 | if (v == NULL) |
| 1451 | return NULL; |
| 1452 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1453 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1454 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1455 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1456 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1457 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1458 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1459 | #endif |
| 1460 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1461 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1462 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1463 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1464 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1465 | #endif |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1466 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1467 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1468 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1469 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1470 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1471 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1472 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1473 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1474 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1475 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1476 | #if defined(HAVE_STAT_TV_NSEC) |
| 1477 | ansec = st->st_atim.tv_nsec; |
| 1478 | mnsec = st->st_mtim.tv_nsec; |
| 1479 | cnsec = st->st_ctim.tv_nsec; |
| 1480 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1481 | ansec = st->st_atimespec.tv_nsec; |
| 1482 | mnsec = st->st_mtimespec.tv_nsec; |
| 1483 | cnsec = st->st_ctimespec.tv_nsec; |
| 1484 | #elif defined(HAVE_STAT_NSEC) |
| 1485 | ansec = st->st_atime_nsec; |
| 1486 | mnsec = st->st_mtime_nsec; |
| 1487 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1488 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1489 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1490 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1491 | fill_time(v, 7, st->st_atime, ansec); |
| 1492 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1493 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1494 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1495 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1496 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1497 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1498 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1499 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1500 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1501 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1502 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1503 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1504 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1505 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1506 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1507 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1508 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1509 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1510 | #endif |
| 1511 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1512 | { |
| 1513 | PyObject *val; |
| 1514 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1515 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1516 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1517 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1518 | #else |
| 1519 | bnsec = 0; |
| 1520 | #endif |
| 1521 | if (_stat_float_times) { |
| 1522 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1523 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1524 | val = PyLong_FromLong((long)bsec); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1525 | } |
| 1526 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1527 | val); |
| 1528 | } |
| 1529 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1530 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1531 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1532 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1533 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1534 | |
| 1535 | if (PyErr_Occurred()) { |
| 1536 | Py_DECREF(v); |
| 1537 | return NULL; |
| 1538 | } |
| 1539 | |
| 1540 | return v; |
| 1541 | } |
| 1542 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1543 | #ifdef MS_WINDOWS |
| 1544 | |
| 1545 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1546 | where / can be used in place of \ and the trailing slash is optional. |
| 1547 | Both SERVER and SHARE must have at least one character. |
| 1548 | */ |
| 1549 | |
| 1550 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1551 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1552 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1553 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1554 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1555 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1556 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1557 | IsUNCRootA(char *path, int pathlen) |
| 1558 | { |
| 1559 | #define ISSLASH ISSLASHA |
| 1560 | |
| 1561 | int i, share; |
| 1562 | |
| 1563 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1564 | /* minimum UNCRoot is \\x\y */ |
| 1565 | return FALSE; |
| 1566 | for (i = 2; i < pathlen ; i++) |
| 1567 | if (ISSLASH(path[i])) break; |
| 1568 | if (i == 2 || i == pathlen) |
| 1569 | /* do not allow \\\SHARE or \\SERVER */ |
| 1570 | return FALSE; |
| 1571 | share = i+1; |
| 1572 | for (i = share; i < pathlen; i++) |
| 1573 | if (ISSLASH(path[i])) break; |
| 1574 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1575 | |
| 1576 | #undef ISSLASH |
| 1577 | } |
| 1578 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1579 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1580 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1581 | { |
| 1582 | #define ISSLASH ISSLASHW |
| 1583 | |
| 1584 | int i, share; |
| 1585 | |
| 1586 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1587 | /* minimum UNCRoot is \\x\y */ |
| 1588 | return FALSE; |
| 1589 | for (i = 2; i < pathlen ; i++) |
| 1590 | if (ISSLASH(path[i])) break; |
| 1591 | if (i == 2 || i == pathlen) |
| 1592 | /* do not allow \\\SHARE or \\SERVER */ |
| 1593 | return FALSE; |
| 1594 | share = i+1; |
| 1595 | for (i = share; i < pathlen; i++) |
| 1596 | if (ISSLASH(path[i])) break; |
| 1597 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1598 | |
| 1599 | #undef ISSLASH |
| 1600 | } |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1601 | #endif /* MS_WINDOWS */ |
| 1602 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1603 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1604 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1605 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1606 | #ifdef __VMS |
| 1607 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1608 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1609 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1610 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1611 | char *wformat, |
| 1612 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1613 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1614 | STRUCT_STAT st; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1615 | PyObject *opath; |
| 1616 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1617 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1618 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1619 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1620 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1621 | /* If on wide-character-capable OS see if argument |
| 1622 | is Unicode and if so use wide API. */ |
| 1623 | if (unicode_file_names()) { |
| 1624 | PyUnicodeObject *po; |
| 1625 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1626 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1627 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1628 | Py_BEGIN_ALLOW_THREADS |
| 1629 | /* PyUnicode_AS_UNICODE result OK without |
| 1630 | thread lock as it is a simple dereference. */ |
| 1631 | res = wstatfunc(wpath, &st); |
| 1632 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1633 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1634 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1635 | return win32_error_unicode("stat", wpath); |
| 1636 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1637 | } |
| 1638 | /* Drop the argument parsing error as narrow strings |
| 1639 | are also valid. */ |
| 1640 | PyErr_Clear(); |
| 1641 | } |
| 1642 | #endif |
| 1643 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1644 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1645 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1646 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1647 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1648 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1649 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1650 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1651 | |
| 1652 | if (res != 0) { |
| 1653 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1654 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1655 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1656 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1657 | #endif |
| 1658 | } |
| 1659 | else |
| 1660 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1661 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1662 | release_bytes(opath); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1663 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1666 | /* POSIX methods */ |
| 1667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1668 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1669 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1670 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1671 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1672 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1673 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1674 | 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] | 1675 | |
| 1676 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1677 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1678 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1679 | PyObject *opath; |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1680 | char *path; |
| 1681 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1682 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1683 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1684 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1685 | if (unicode_file_names()) { |
| 1686 | PyUnicodeObject *po; |
| 1687 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1688 | Py_BEGIN_ALLOW_THREADS |
| 1689 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1690 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1691 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1692 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1693 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1694 | } |
| 1695 | /* Drop the argument parsing error as narrow strings |
| 1696 | are also valid. */ |
| 1697 | PyErr_Clear(); |
| 1698 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1699 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1700 | PyUnicode_FSConverter, &opath, &mode)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1701 | return 0; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1702 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1703 | Py_BEGIN_ALLOW_THREADS |
| 1704 | attr = GetFileAttributesA(path); |
| 1705 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1706 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1707 | finish: |
| 1708 | if (attr == 0xFFFFFFFF) |
| 1709 | /* File does not exist, or cannot read attributes */ |
| 1710 | return PyBool_FromLong(0); |
| 1711 | /* Access is possible if either write access wasn't requested, or |
Guido van Rossum | b00324f | 2007-12-04 01:13:14 +0000 | [diff] [blame] | 1712 | the file isn't read-only, or if it's a directory, as there are |
| 1713 | no read-only directories on Windows. */ |
| 1714 | return PyBool_FromLong(!(mode & 2) |
| 1715 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1716 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1717 | #else |
| 1718 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1719 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1720 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1721 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1722 | path = bytes2str(opath, 1); |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1723 | Py_BEGIN_ALLOW_THREADS |
| 1724 | res = access(path, mode); |
| 1725 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1726 | release_bytes(opath); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1727 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1728 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1731 | #ifndef F_OK |
| 1732 | #define F_OK 0 |
| 1733 | #endif |
| 1734 | #ifndef R_OK |
| 1735 | #define R_OK 4 |
| 1736 | #endif |
| 1737 | #ifndef W_OK |
| 1738 | #define W_OK 2 |
| 1739 | #endif |
| 1740 | #ifndef X_OK |
| 1741 | #define X_OK 1 |
| 1742 | #endif |
| 1743 | |
| 1744 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1745 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1746 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1747 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1748 | |
| 1749 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1750 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1751 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1752 | int id; |
| 1753 | char *ret; |
| 1754 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1755 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1756 | return NULL; |
| 1757 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1758 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1759 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1760 | if (id == 0) { |
| 1761 | ret = ttyname(); |
| 1762 | } |
| 1763 | else { |
| 1764 | ret = NULL; |
| 1765 | } |
| 1766 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1767 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1768 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1769 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1770 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1771 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1772 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1773 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1774 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1775 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1776 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1777 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1778 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1779 | |
| 1780 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1781 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1782 | { |
| 1783 | char *ret; |
| 1784 | char buffer[L_ctermid]; |
| 1785 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1786 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1787 | ret = ctermid_r(buffer); |
| 1788 | #else |
| 1789 | ret = ctermid(buffer); |
| 1790 | #endif |
| 1791 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1792 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1793 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1794 | } |
| 1795 | #endif |
| 1796 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1797 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1798 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1799 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1800 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1801 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1802 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1803 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1804 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 1805 | 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] | 1806 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1807 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1808 | #elif defined(__VMS) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1809 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1810 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1811 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1812 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1815 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1816 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1817 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1818 | 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] | 1819 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1820 | |
| 1821 | static PyObject * |
| 1822 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1823 | { |
| 1824 | return posix_fildes(fdobj, fchdir); |
| 1825 | } |
| 1826 | #endif /* HAVE_FCHDIR */ |
| 1827 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1829 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1830 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1831 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1832 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1833 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1834 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1835 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1836 | PyObject *opath = NULL; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1837 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1838 | int i; |
| 1839 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1840 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1841 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1842 | if (unicode_file_names()) { |
| 1843 | PyUnicodeObject *po; |
| 1844 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1845 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1846 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1847 | if (attr != 0xFFFFFFFF) { |
| 1848 | if (i & _S_IWRITE) |
| 1849 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1850 | else |
| 1851 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1852 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1853 | } |
| 1854 | else |
| 1855 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1856 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1857 | if (!res) |
| 1858 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1859 | PyUnicode_AS_UNICODE(po)); |
| 1860 | Py_INCREF(Py_None); |
| 1861 | return Py_None; |
| 1862 | } |
| 1863 | /* Drop the argument parsing error as narrow strings |
| 1864 | are also valid. */ |
| 1865 | PyErr_Clear(); |
| 1866 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1867 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1868 | &opath, &i)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1869 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1870 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1871 | Py_BEGIN_ALLOW_THREADS |
| 1872 | attr = GetFileAttributesA(path); |
| 1873 | if (attr != 0xFFFFFFFF) { |
| 1874 | if (i & _S_IWRITE) |
| 1875 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1876 | else |
| 1877 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1878 | res = SetFileAttributesA(path, attr); |
| 1879 | } |
| 1880 | else |
| 1881 | res = 0; |
| 1882 | Py_END_ALLOW_THREADS |
| 1883 | if (!res) { |
| 1884 | win32_error("chmod", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1885 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1886 | return NULL; |
| 1887 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1888 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1889 | Py_INCREF(Py_None); |
| 1890 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1891 | #else /* MS_WINDOWS */ |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1892 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1893 | &opath, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1894 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1895 | path = bytes2str(opath, 1); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1896 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1897 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1898 | Py_END_ALLOW_THREADS |
| 1899 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1900 | return posix_error_with_allocated_filename(opath); |
| 1901 | release_bytes(opath); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1902 | Py_INCREF(Py_None); |
| 1903 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1904 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1907 | #ifdef HAVE_FCHMOD |
| 1908 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1909 | "fchmod(fd, mode)\n\n\ |
| 1910 | Change the access permissions of the file given by file\n\ |
| 1911 | descriptor fd."); |
| 1912 | |
| 1913 | static PyObject * |
| 1914 | posix_fchmod(PyObject *self, PyObject *args) |
| 1915 | { |
| 1916 | int fd, mode, res; |
| 1917 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1918 | return NULL; |
| 1919 | Py_BEGIN_ALLOW_THREADS |
| 1920 | res = fchmod(fd, mode); |
| 1921 | Py_END_ALLOW_THREADS |
| 1922 | if (res < 0) |
| 1923 | return posix_error(); |
| 1924 | Py_RETURN_NONE; |
| 1925 | } |
| 1926 | #endif /* HAVE_FCHMOD */ |
| 1927 | |
| 1928 | #ifdef HAVE_LCHMOD |
| 1929 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1930 | "lchmod(path, mode)\n\n\ |
| 1931 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1932 | affects the link itself rather than the target."); |
| 1933 | |
| 1934 | static PyObject * |
| 1935 | posix_lchmod(PyObject *self, PyObject *args) |
| 1936 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1937 | PyObject *opath; |
| 1938 | char *path; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1939 | int i; |
| 1940 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1941 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 1942 | &opath, &i)) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1943 | return NULL; |
Eric Smith | 86a05ec | 2009-05-05 13:07:30 +0000 | [diff] [blame] | 1944 | path = bytes2str(opath, 1); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1945 | Py_BEGIN_ALLOW_THREADS |
| 1946 | res = lchmod(path, i); |
| 1947 | Py_END_ALLOW_THREADS |
| 1948 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1949 | return posix_error_with_allocated_filename(opath); |
| 1950 | release_bytes(opath); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1951 | Py_RETURN_NONE; |
| 1952 | } |
| 1953 | #endif /* HAVE_LCHMOD */ |
| 1954 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1955 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1956 | #ifdef HAVE_CHFLAGS |
| 1957 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1958 | "chflags(path, flags)\n\n\ |
| 1959 | Set file flags."); |
| 1960 | |
| 1961 | static PyObject * |
| 1962 | posix_chflags(PyObject *self, PyObject *args) |
| 1963 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1964 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1965 | char *path; |
| 1966 | unsigned long flags; |
| 1967 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1968 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 1969 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1970 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1971 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1972 | Py_BEGIN_ALLOW_THREADS |
| 1973 | res = chflags(path, flags); |
| 1974 | Py_END_ALLOW_THREADS |
| 1975 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1976 | return posix_error_with_allocated_filename(opath); |
| 1977 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1978 | Py_INCREF(Py_None); |
| 1979 | return Py_None; |
| 1980 | } |
| 1981 | #endif /* HAVE_CHFLAGS */ |
| 1982 | |
| 1983 | #ifdef HAVE_LCHFLAGS |
| 1984 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1985 | "lchflags(path, flags)\n\n\ |
| 1986 | Set file flags.\n\ |
| 1987 | This function will not follow symbolic links."); |
| 1988 | |
| 1989 | static PyObject * |
| 1990 | posix_lchflags(PyObject *self, PyObject *args) |
| 1991 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1992 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1993 | char *path; |
| 1994 | unsigned long flags; |
| 1995 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1996 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Martin v. Löwis | 4adbc34 | 2009-05-05 17:17:55 +0000 | [diff] [blame] | 1997 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1998 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1999 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2000 | Py_BEGIN_ALLOW_THREADS |
| 2001 | res = lchflags(path, flags); |
| 2002 | Py_END_ALLOW_THREADS |
| 2003 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2004 | return posix_error_with_allocated_filename(opath); |
| 2005 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2006 | Py_INCREF(Py_None); |
| 2007 | return Py_None; |
| 2008 | } |
| 2009 | #endif /* HAVE_LCHFLAGS */ |
| 2010 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2011 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2012 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2013 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2014 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2015 | |
| 2016 | static PyObject * |
| 2017 | posix_chroot(PyObject *self, PyObject *args) |
| 2018 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2019 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2020 | } |
| 2021 | #endif |
| 2022 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2023 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2024 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2025 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2026 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2027 | |
| 2028 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2029 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2030 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2031 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2032 | } |
| 2033 | #endif /* HAVE_FSYNC */ |
| 2034 | |
| 2035 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2036 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2037 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2038 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2039 | #endif |
| 2040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2041 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2042 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2043 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2044 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2045 | |
| 2046 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2047 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2048 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2049 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2050 | } |
| 2051 | #endif /* HAVE_FDATASYNC */ |
| 2052 | |
| 2053 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2054 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2055 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2056 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2057 | 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] | 2058 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2059 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2060 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2061 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2062 | PyObject *opath; |
| 2063 | char *path; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2064 | long uid, gid; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2065 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2066 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2067 | PyUnicode_FSConverter, &opath, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2068 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2069 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2070 | path = bytes2str(opath, 1); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2071 | Py_BEGIN_ALLOW_THREADS |
| 2072 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2073 | Py_END_ALLOW_THREADS |
| 2074 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2075 | return posix_error_with_allocated_filename(opath); |
| 2076 | release_bytes(opath); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2077 | Py_INCREF(Py_None); |
| 2078 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2079 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2080 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2081 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2082 | #ifdef HAVE_FCHOWN |
| 2083 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2084 | "fchown(fd, uid, gid)\n\n\ |
| 2085 | Change the owner and group id of the file given by file descriptor\n\ |
| 2086 | fd to the numeric uid and gid."); |
| 2087 | |
| 2088 | static PyObject * |
| 2089 | posix_fchown(PyObject *self, PyObject *args) |
| 2090 | { |
| 2091 | int fd, uid, gid; |
| 2092 | int res; |
| 2093 | if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid)) |
| 2094 | return NULL; |
| 2095 | Py_BEGIN_ALLOW_THREADS |
| 2096 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2097 | Py_END_ALLOW_THREADS |
| 2098 | if (res < 0) |
| 2099 | return posix_error(); |
| 2100 | Py_RETURN_NONE; |
| 2101 | } |
| 2102 | #endif /* HAVE_FCHOWN */ |
| 2103 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2104 | #ifdef HAVE_LCHOWN |
| 2105 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2106 | "lchown(path, uid, gid)\n\n\ |
| 2107 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2108 | This function will not follow symbolic links."); |
| 2109 | |
| 2110 | static PyObject * |
| 2111 | posix_lchown(PyObject *self, PyObject *args) |
| 2112 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2113 | PyObject *opath; |
| 2114 | char *path; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2115 | int uid, gid; |
| 2116 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2117 | if (!PyArg_ParseTuple(args, "O&ii:lchown", |
| 2118 | PyUnicode_FSConverter, &opath, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2119 | &uid, &gid)) |
| 2120 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2121 | path = bytes2str(opath, 1); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2122 | Py_BEGIN_ALLOW_THREADS |
| 2123 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2124 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2125 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2126 | return posix_error_with_allocated_filename(opath); |
| 2127 | release_bytes(opath); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2128 | Py_INCREF(Py_None); |
| 2129 | return Py_None; |
| 2130 | } |
| 2131 | #endif /* HAVE_LCHOWN */ |
| 2132 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2133 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2134 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2135 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2136 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2137 | { |
| 2138 | char buf[1026]; |
| 2139 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2140 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2141 | #ifdef MS_WINDOWS |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2142 | if (!use_bytes && unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2143 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2144 | wchar_t *wbuf2 = wbuf; |
| 2145 | PyObject *resobj; |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2146 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2147 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2148 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2149 | /* If the buffer is large enough, len does not include the |
| 2150 | terminating \0. If the buffer is too small, len includes |
| 2151 | the space needed for the terminator. */ |
| 2152 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2153 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2154 | if (wbuf2) |
| 2155 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2156 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2157 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2158 | if (!wbuf2) { |
| 2159 | PyErr_NoMemory(); |
| 2160 | return NULL; |
| 2161 | } |
| 2162 | if (!len) { |
| 2163 | if (wbuf2 != wbuf) free(wbuf2); |
| 2164 | return win32_error("getcwdu", NULL); |
| 2165 | } |
| 2166 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2167 | if (wbuf2 != wbuf) free(wbuf2); |
| 2168 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2169 | } |
| 2170 | #endif |
| 2171 | |
| 2172 | Py_BEGIN_ALLOW_THREADS |
| 2173 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2174 | res = _getcwd2(buf, sizeof buf); |
| 2175 | #else |
| 2176 | res = getcwd(buf, sizeof buf); |
| 2177 | #endif |
| 2178 | Py_END_ALLOW_THREADS |
| 2179 | if (res == NULL) |
| 2180 | return posix_error(); |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2181 | if (use_bytes) |
| 2182 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2183 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape"); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2184 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2185 | |
| 2186 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2187 | "getcwd() -> path\n\n\ |
| 2188 | Return a unicode string representing the current working directory."); |
| 2189 | |
| 2190 | static PyObject * |
| 2191 | posix_getcwd_unicode(PyObject *self) |
| 2192 | { |
| 2193 | return posix_getcwd(0); |
| 2194 | } |
| 2195 | |
| 2196 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2197 | "getcwdb() -> path\n\n\ |
| 2198 | Return a bytes string representing the current working directory."); |
| 2199 | |
| 2200 | static PyObject * |
| 2201 | posix_getcwd_bytes(PyObject *self) |
| 2202 | { |
| 2203 | return posix_getcwd(1); |
| 2204 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2205 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2207 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2208 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2209 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2210 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2211 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2212 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2213 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2214 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2215 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2216 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2217 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2218 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2219 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2221 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2222 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2223 | Return a list containing the names of the entries in the directory.\n\ |
| 2224 | \n\ |
| 2225 | path: path of directory to list\n\ |
| 2226 | \n\ |
| 2227 | 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] | 2228 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2229 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2230 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2231 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2232 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2233 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2234 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2235 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2236 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2237 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2238 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2239 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2240 | WIN32_FIND_DATA FileData; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2241 | PyObject *opath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2242 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2243 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2244 | 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] | 2245 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2246 | /* If on wide-character-capable OS see if argument |
| 2247 | is Unicode and if so use wide API. */ |
| 2248 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2249 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2250 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2251 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2252 | Py_UNICODE *wnamebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2253 | /* Overallocate for \\*.*\0 */ |
| 2254 | len = PyUnicode_GET_SIZE(po); |
| 2255 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2256 | if (!wnamebuf) { |
| 2257 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2258 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2259 | } |
| 2260 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2261 | if (len > 0) { |
| 2262 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2263 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2264 | wnamebuf[len++] = L'\\'; |
| 2265 | wcscpy(wnamebuf + len, L"*.*"); |
| 2266 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2267 | if ((d = PyList_New(0)) == NULL) { |
| 2268 | free(wnamebuf); |
| 2269 | return NULL; |
| 2270 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2271 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2272 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2273 | int error = GetLastError(); |
| 2274 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2275 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2276 | return d; |
| 2277 | } |
| 2278 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2279 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2280 | free(wnamebuf); |
| 2281 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2282 | } |
| 2283 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2284 | /* Skip over . and .. */ |
| 2285 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2286 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2287 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2288 | if (v == NULL) { |
| 2289 | Py_DECREF(d); |
| 2290 | d = NULL; |
| 2291 | break; |
| 2292 | } |
| 2293 | if (PyList_Append(d, v) != 0) { |
| 2294 | Py_DECREF(v); |
| 2295 | Py_DECREF(d); |
| 2296 | d = NULL; |
| 2297 | break; |
| 2298 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2299 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2300 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2301 | Py_BEGIN_ALLOW_THREADS |
| 2302 | result = FindNextFileW(hFindFile, &wFileData); |
| 2303 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2304 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2305 | it got to the end of the directory. */ |
| 2306 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2307 | Py_DECREF(d); |
| 2308 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2309 | FindClose(hFindFile); |
| 2310 | free(wnamebuf); |
| 2311 | return NULL; |
| 2312 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2313 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2314 | |
| 2315 | if (FindClose(hFindFile) == FALSE) { |
| 2316 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2317 | win32_error_unicode("FindClose", wnamebuf); |
| 2318 | free(wnamebuf); |
| 2319 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2320 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2321 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2322 | return d; |
| 2323 | } |
| 2324 | /* Drop the argument parsing error as narrow strings |
| 2325 | are also valid. */ |
| 2326 | PyErr_Clear(); |
| 2327 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2328 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2329 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2330 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2331 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2332 | if (PyObject_Size(opath)+1 > MAX_PATH) { |
| 2333 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2334 | Py_DECREF(opath); |
| 2335 | return NULL; |
| 2336 | } |
| 2337 | strcpy(namebuf, bytes2str(opath, 0)); |
| 2338 | len = PyObject_Size(opath); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2339 | if (len > 0) { |
| 2340 | char ch = namebuf[len-1]; |
| 2341 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2342 | namebuf[len++] = '/'; |
Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2343 | strcpy(namebuf + len, "*.*"); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2344 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2345 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2346 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2347 | return NULL; |
| 2348 | |
| 2349 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2350 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2351 | int error = GetLastError(); |
| 2352 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2353 | return d; |
| 2354 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2355 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2356 | } |
| 2357 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2358 | /* Skip over . and .. */ |
| 2359 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2360 | strcmp(FileData.cFileName, "..") != 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2361 | v = PyBytes_FromString(FileData.cFileName); |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2362 | if (v == NULL) { |
| 2363 | Py_DECREF(d); |
| 2364 | d = NULL; |
| 2365 | break; |
| 2366 | } |
| 2367 | if (PyList_Append(d, v) != 0) { |
| 2368 | Py_DECREF(v); |
| 2369 | Py_DECREF(d); |
| 2370 | d = NULL; |
| 2371 | break; |
| 2372 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2373 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2374 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2375 | Py_BEGIN_ALLOW_THREADS |
| 2376 | result = FindNextFile(hFindFile, &FileData); |
| 2377 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2378 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2379 | it got to the end of the directory. */ |
| 2380 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2381 | Py_DECREF(d); |
| 2382 | win32_error("FindNextFile", namebuf); |
| 2383 | FindClose(hFindFile); |
| 2384 | return NULL; |
| 2385 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2386 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2387 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2388 | if (FindClose(hFindFile) == FALSE) { |
| 2389 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2390 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2391 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2392 | |
| 2393 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2394 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2395 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2396 | |
| 2397 | #ifndef MAX_PATH |
| 2398 | #define MAX_PATH CCHMAXPATH |
| 2399 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2400 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2401 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2402 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2403 | PyObject *d, *v; |
| 2404 | char namebuf[MAX_PATH+5]; |
| 2405 | HDIR hdir = 1; |
| 2406 | ULONG srchcnt = 1; |
| 2407 | FILEFINDBUF3 ep; |
| 2408 | APIRET rc; |
| 2409 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2410 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2411 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2412 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2413 | name = bytes2str(oname); |
| 2414 | len = PyObject_Size(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2415 | if (len >= MAX_PATH) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2416 | release_bytes(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2417 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2418 | return NULL; |
| 2419 | } |
| 2420 | strcpy(namebuf, name); |
| 2421 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2422 | if (*pt == ALTSEP) |
| 2423 | *pt = SEP; |
| 2424 | if (namebuf[len-1] != SEP) |
| 2425 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2426 | strcpy(namebuf + len, "*.*"); |
| 2427 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2428 | if ((d = PyList_New(0)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2429 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2430 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2431 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2432 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2433 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2434 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2435 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2436 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2437 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2438 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2439 | |
| 2440 | if (rc != NO_ERROR) { |
| 2441 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2442 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2445 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2446 | do { |
| 2447 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2448 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2449 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2450 | |
| 2451 | strcpy(namebuf, ep.achName); |
| 2452 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2453 | /* Leave Case of Name Alone -- In Native Form */ |
| 2454 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2455 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2456 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2457 | if (v == NULL) { |
| 2458 | Py_DECREF(d); |
| 2459 | d = NULL; |
| 2460 | break; |
| 2461 | } |
| 2462 | if (PyList_Append(d, v) != 0) { |
| 2463 | Py_DECREF(v); |
| 2464 | Py_DECREF(d); |
| 2465 | d = NULL; |
| 2466 | break; |
| 2467 | } |
| 2468 | Py_DECREF(v); |
| 2469 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2470 | } |
| 2471 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2472 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2473 | return d; |
| 2474 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2475 | PyObject *oname; |
| 2476 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2477 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2478 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2479 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2480 | int arg_is_unicode = 1; |
| 2481 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2482 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2483 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2484 | arg_is_unicode = 0; |
| 2485 | PyErr_Clear(); |
| 2486 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2487 | if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2488 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2489 | name = bytes2str(oname, 1); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2490 | if ((dirp = opendir(name)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2491 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2492 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2493 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2494 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2495 | release_bytes(oname); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2496 | return NULL; |
| 2497 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2498 | for (;;) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2499 | errno = 0; |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2500 | Py_BEGIN_ALLOW_THREADS |
| 2501 | ep = readdir(dirp); |
| 2502 | Py_END_ALLOW_THREADS |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2503 | if (ep == NULL) { |
| 2504 | if (errno == 0) { |
| 2505 | break; |
| 2506 | } else { |
| 2507 | closedir(dirp); |
| 2508 | Py_DECREF(d); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2509 | return posix_error_with_allocated_filename(oname); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2512 | if (ep->d_name[0] == '.' && |
| 2513 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2514 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2515 | continue; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2516 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2517 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2518 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2519 | d = NULL; |
| 2520 | break; |
| 2521 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2522 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2523 | PyObject *w; |
| 2524 | |
| 2525 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2526 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2527 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2528 | Py_DECREF(v); |
| 2529 | if (w != NULL) |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2530 | v = w; |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2531 | else { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2532 | /* Encoding failed to decode ASCII bytes. |
| 2533 | Raise exception. */ |
| 2534 | Py_DECREF(d); |
| 2535 | d = NULL; |
| 2536 | break; |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2537 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2538 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2539 | if (PyList_Append(d, v) != 0) { |
| 2540 | Py_DECREF(v); |
| 2541 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2542 | d = NULL; |
| 2543 | break; |
| 2544 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2545 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2546 | } |
| 2547 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2548 | release_bytes(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2549 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2550 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2551 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2552 | #endif /* which OS */ |
| 2553 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2554 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2555 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2556 | /* A helper function for abspath on win32 */ |
| 2557 | static PyObject * |
| 2558 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2559 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2560 | PyObject *opath; |
| 2561 | char *path; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2562 | char outbuf[MAX_PATH*2]; |
| 2563 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2564 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2565 | if (unicode_file_names()) { |
| 2566 | PyUnicodeObject *po; |
| 2567 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 2568 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2569 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2570 | Py_UNICODE *wtemp; |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 2571 | DWORD result; |
| 2572 | PyObject *v; |
| 2573 | result = GetFullPathNameW(wpath, |
| 2574 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2575 | woutbuf, &wtemp); |
| 2576 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2577 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2578 | if (!woutbufp) |
| 2579 | return PyErr_NoMemory(); |
| 2580 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2581 | } |
| 2582 | if (result) |
| 2583 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2584 | else |
| 2585 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2586 | if (woutbufp != woutbuf) |
| 2587 | free(woutbufp); |
| 2588 | return v; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2589 | } |
| 2590 | /* Drop the argument parsing error as narrow strings |
| 2591 | are also valid. */ |
| 2592 | PyErr_Clear(); |
| 2593 | } |
| 2594 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2595 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2596 | PyUnicode_FSConverter, &opath)) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2597 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2598 | path = bytes2str(opath, 1); |
| 2599 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2600 | outbuf, &temp)) { |
| 2601 | win32_error("GetFullPathName", path); |
| 2602 | release_bytes(opath); |
| 2603 | return NULL; |
| 2604 | } |
| 2605 | release_bytes(opath); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2606 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2607 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2608 | Py_FileSystemDefaultEncoding, NULL); |
| 2609 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2610 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2611 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2612 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2613 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2614 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2615 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2616 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2617 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2618 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2619 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2620 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2621 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2622 | PyObject *opath; |
| 2623 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2624 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2625 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2626 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2627 | if (unicode_file_names()) { |
| 2628 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2629 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2630 | Py_BEGIN_ALLOW_THREADS |
| 2631 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2632 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2633 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2634 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2635 | if (!res) |
| 2636 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2637 | Py_INCREF(Py_None); |
| 2638 | return Py_None; |
| 2639 | } |
| 2640 | /* Drop the argument parsing error as narrow strings |
| 2641 | are also valid. */ |
| 2642 | PyErr_Clear(); |
| 2643 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2644 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2645 | PyUnicode_FSConverter, &opath, &mode)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2646 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2647 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2648 | Py_BEGIN_ALLOW_THREADS |
| 2649 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2650 | it is a simple dereference. */ |
| 2651 | res = CreateDirectoryA(path, NULL); |
| 2652 | Py_END_ALLOW_THREADS |
| 2653 | if (!res) { |
| 2654 | win32_error("mkdir", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2655 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2656 | return NULL; |
| 2657 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2658 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2659 | Py_INCREF(Py_None); |
| 2660 | return Py_None; |
| 2661 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2662 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2663 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2664 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2665 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2666 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2667 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2668 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2669 | res = mkdir(path); |
| 2670 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2671 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2672 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2673 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2674 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2675 | return posix_error_with_allocated_filename(opath); |
| 2676 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2677 | Py_INCREF(Py_None); |
| 2678 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2679 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2682 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2683 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2684 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2685 | #include <sys/resource.h> |
| 2686 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2687 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2688 | |
| 2689 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2690 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2691 | "nice(inc) -> new_priority\n\n\ |
| 2692 | 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] | 2693 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2694 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2695 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2696 | { |
| 2697 | int increment, value; |
| 2698 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2699 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2700 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2701 | |
| 2702 | /* There are two flavours of 'nice': one that returns the new |
| 2703 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2704 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2705 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2706 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2707 | If we are of the nice family that returns the new priority, we |
| 2708 | need to clear errno before the call, and check if errno is filled |
| 2709 | before calling posix_error() on a returnvalue of -1, because the |
| 2710 | -1 may be the actual new priority! */ |
| 2711 | |
| 2712 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2713 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2714 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2715 | if (value == 0) |
| 2716 | value = getpriority(PRIO_PROCESS, 0); |
| 2717 | #endif |
| 2718 | if (value == -1 && errno != 0) |
| 2719 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2720 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2721 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2722 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2723 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2725 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2726 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2727 | Rename a file or directory."); |
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_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2731 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2732 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2733 | PyObject *o1, *o2; |
| 2734 | char *p1, *p2; |
| 2735 | BOOL result; |
| 2736 | if (unicode_file_names()) { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2737 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 2738 | goto error; |
| 2739 | if (!convert_to_unicode(&o1)) |
| 2740 | goto error; |
| 2741 | if (!convert_to_unicode(&o2)) { |
| 2742 | Py_DECREF(o1); |
| 2743 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2744 | } |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2745 | Py_BEGIN_ALLOW_THREADS |
| 2746 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2747 | PyUnicode_AsUnicode(o2)); |
| 2748 | Py_END_ALLOW_THREADS |
| 2749 | Py_DECREF(o1); |
| 2750 | Py_DECREF(o2); |
| 2751 | if (!result) |
| 2752 | return win32_error("rename", NULL); |
| 2753 | Py_INCREF(Py_None); |
| 2754 | return Py_None; |
| 2755 | error: |
| 2756 | PyErr_Clear(); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2757 | } |
| 2758 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2759 | return NULL; |
| 2760 | Py_BEGIN_ALLOW_THREADS |
| 2761 | result = MoveFileA(p1, p2); |
| 2762 | Py_END_ALLOW_THREADS |
| 2763 | if (!result) |
| 2764 | return win32_error("rename", NULL); |
| 2765 | Py_INCREF(Py_None); |
| 2766 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2767 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2768 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2769 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2772 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2773 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2774 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2775 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2776 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2777 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2778 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2779 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2780 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2781 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2782 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2783 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2784 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2787 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2788 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2789 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2790 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2791 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2792 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2793 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2794 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2795 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2796 | 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] | 2797 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2798 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2799 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2802 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2803 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2804 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2805 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2806 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2807 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2808 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2809 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2810 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2811 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2812 | #ifdef MS_WINDOWS |
| 2813 | wchar_t *command; |
| 2814 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2815 | return NULL; |
| 2816 | #else |
| 2817 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2818 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2819 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2820 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2821 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2822 | #ifdef MS_WINDOWS |
| 2823 | sts = _wsystem(command); |
| 2824 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2825 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2826 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2827 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2828 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2829 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2830 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2831 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2833 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2834 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2835 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2836 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2837 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2838 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2839 | { |
| 2840 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2841 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2842 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2843 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2844 | if (i < 0) |
| 2845 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2846 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2849 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2850 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2851 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2852 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2853 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2854 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2855 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2856 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2857 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2858 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2859 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2860 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2861 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2862 | return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2863 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2864 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2865 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2868 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2869 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2870 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2871 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2872 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2873 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2874 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2875 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2876 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2877 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2878 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2879 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2880 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2881 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2882 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2883 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2884 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2885 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2886 | u.sysname, |
| 2887 | u.nodename, |
| 2888 | u.release, |
| 2889 | u.version, |
| 2890 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2891 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2892 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2893 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2894 | static int |
| 2895 | extract_time(PyObject *t, long* sec, long* usec) |
| 2896 | { |
| 2897 | long intval; |
| 2898 | if (PyFloat_Check(t)) { |
| 2899 | double tval = PyFloat_AsDouble(t); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2900 | 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] | 2901 | if (!intobj) |
| 2902 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2903 | intval = PyLong_AsLong(intobj); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2904 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2905 | if (intval == -1 && PyErr_Occurred()) |
| 2906 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2907 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2908 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2909 | if (*usec < 0) |
| 2910 | /* If rounding gave us a negative number, |
| 2911 | truncate. */ |
| 2912 | *usec = 0; |
| 2913 | return 0; |
| 2914 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2915 | intval = PyLong_AsLong(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2916 | if (intval == -1 && PyErr_Occurred()) |
| 2917 | return -1; |
| 2918 | *sec = intval; |
| 2919 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2920 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2921 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2922 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2923 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2924 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2925 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2926 | 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] | 2927 | 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] | 2928 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2929 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2930 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2931 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2932 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2933 | PyObject *arg; |
| 2934 | PyUnicodeObject *obwpath; |
| 2935 | wchar_t *wpath = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2936 | PyObject *oapath; |
| 2937 | char *apath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2938 | HANDLE hFile; |
| 2939 | long atimesec, mtimesec, ausec, musec; |
| 2940 | FILETIME atime, mtime; |
| 2941 | PyObject *result = NULL; |
| 2942 | |
| 2943 | if (unicode_file_names()) { |
| 2944 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2945 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2946 | Py_BEGIN_ALLOW_THREADS |
| 2947 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2948 | NULL, OPEN_EXISTING, |
| 2949 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2950 | Py_END_ALLOW_THREADS |
| 2951 | if (hFile == INVALID_HANDLE_VALUE) |
| 2952 | return win32_error_unicode("utime", wpath); |
| 2953 | } else |
| 2954 | /* Drop the argument parsing error as narrow strings |
| 2955 | are also valid. */ |
| 2956 | PyErr_Clear(); |
| 2957 | } |
| 2958 | if (!wpath) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2959 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 2960 | PyUnicode_FSConverter, &oapath, &arg)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2961 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2962 | apath = bytes2str(oapath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2963 | Py_BEGIN_ALLOW_THREADS |
| 2964 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2965 | NULL, OPEN_EXISTING, |
| 2966 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2967 | Py_END_ALLOW_THREADS |
| 2968 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2969 | win32_error("utime", apath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2970 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2971 | return NULL; |
| 2972 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2973 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | if (arg == Py_None) { |
| 2977 | SYSTEMTIME now; |
| 2978 | GetSystemTime(&now); |
| 2979 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2980 | !SystemTimeToFileTime(&now, &atime)) { |
| 2981 | win32_error("utime", NULL); |
| 2982 | goto done; |
| 2983 | } |
| 2984 | } |
| 2985 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2986 | PyErr_SetString(PyExc_TypeError, |
| 2987 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2988 | goto done; |
| 2989 | } |
| 2990 | else { |
| 2991 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2992 | &atimesec, &ausec) == -1) |
| 2993 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2994 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2995 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2996 | &mtimesec, &musec) == -1) |
| 2997 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2998 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2999 | } |
| 3000 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3001 | /* Avoid putting the file name into the error here, |
| 3002 | as that may confuse the user into believing that |
| 3003 | something is wrong with the file, when it also |
| 3004 | could be the time stamp that gives a problem. */ |
| 3005 | win32_error("utime", NULL); |
| 3006 | } |
| 3007 | Py_INCREF(Py_None); |
| 3008 | result = Py_None; |
| 3009 | done: |
| 3010 | CloseHandle(hFile); |
| 3011 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3012 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3013 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3014 | PyObject *opath; |
| 3015 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3016 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3017 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3018 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3019 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3020 | #if defined(HAVE_UTIMES) |
| 3021 | struct timeval buf[2]; |
| 3022 | #define ATIME buf[0].tv_sec |
| 3023 | #define MTIME buf[1].tv_sec |
| 3024 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3025 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3026 | struct utimbuf buf; |
| 3027 | #define ATIME buf.actime |
| 3028 | #define MTIME buf.modtime |
| 3029 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3030 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3031 | time_t buf[2]; |
| 3032 | #define ATIME buf[0] |
| 3033 | #define MTIME buf[1] |
| 3034 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3035 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3036 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3037 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3038 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3039 | PyUnicode_FSConverter, &opath, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3040 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3041 | path = bytes2str(opath, 1); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3042 | if (arg == Py_None) { |
| 3043 | /* optional time values not given */ |
| 3044 | Py_BEGIN_ALLOW_THREADS |
| 3045 | res = utime(path, NULL); |
| 3046 | Py_END_ALLOW_THREADS |
| 3047 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3048 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3049 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3050 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3051 | release_bytes(opath); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3052 | return NULL; |
| 3053 | } |
| 3054 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3055 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3056 | &atime, &ausec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3057 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3058 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3059 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3060 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3061 | &mtime, &musec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3062 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3063 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3064 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3065 | ATIME = atime; |
| 3066 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3067 | #ifdef HAVE_UTIMES |
| 3068 | buf[0].tv_usec = ausec; |
| 3069 | buf[1].tv_usec = musec; |
| 3070 | Py_BEGIN_ALLOW_THREADS |
| 3071 | res = utimes(path, buf); |
| 3072 | Py_END_ALLOW_THREADS |
| 3073 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3074 | Py_BEGIN_ALLOW_THREADS |
| 3075 | res = utime(path, UTIME_ARG); |
| 3076 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3077 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3078 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 3079 | if (res < 0) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3080 | return posix_error_with_allocated_filename(opath); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 3081 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3082 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3083 | Py_INCREF(Py_None); |
| 3084 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3085 | #undef UTIME_ARG |
| 3086 | #undef ATIME |
| 3087 | #undef MTIME |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3088 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3091 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3092 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3093 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3094 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3095 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3096 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3097 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3098 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3099 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3100 | { |
| 3101 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3102 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3103 | return NULL; |
| 3104 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 3105 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3108 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3109 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3110 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3111 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3112 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3113 | for (i = 0; i < count; i++) |
| 3114 | PyMem_Free(array[i]); |
| 3115 | PyMem_DEL(array); |
| 3116 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3117 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3118 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3119 | int fsconvert_strdup(PyObject *o, char**out) |
| 3120 | { |
| 3121 | PyObject *bytes; |
| 3122 | Py_ssize_t size; |
| 3123 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3124 | return 0; |
| 3125 | size = PyObject_Size(bytes); |
| 3126 | *out = PyMem_Malloc(size+1); |
| 3127 | if (!*out) |
| 3128 | return 0; |
| 3129 | /* Don't lock bytes, as we hold the GIL */ |
| 3130 | memcpy(*out, bytes2str(bytes, 0), size+1); |
| 3131 | Py_DECREF(bytes); |
| 3132 | return 1; |
| 3133 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3134 | #endif |
| 3135 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3136 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3137 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3138 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3139 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3140 | Execute an executable path with arguments, replacing current process.\n\ |
| 3141 | \n\ |
| 3142 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3143 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3144 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3145 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3146 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3147 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3148 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3149 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3150 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3151 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3152 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3153 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3154 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 3155 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3156 | argv is a list or tuple of strings. */ |
| 3157 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3158 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3159 | PyUnicode_FSConverter, |
| 3160 | &opath, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3161 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3162 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3163 | if (PyList_Check(argv)) { |
| 3164 | argc = PyList_Size(argv); |
| 3165 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3166 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3167 | else if (PyTuple_Check(argv)) { |
| 3168 | argc = PyTuple_Size(argv); |
| 3169 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3170 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3171 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3172 | 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] | 3173 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3174 | return NULL; |
| 3175 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3176 | if (argc < 1) { |
| 3177 | 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] | 3178 | release_bytes(opath); |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3179 | return NULL; |
| 3180 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3181 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3182 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3183 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3184 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3185 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3186 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3187 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3188 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3189 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3190 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3191 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3192 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3193 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3194 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3195 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3196 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3197 | } |
| 3198 | argvlist[argc] = NULL; |
| 3199 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3200 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3201 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3202 | /* If we get here it's definitely an error */ |
| 3203 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3204 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3205 | release_bytes(opath); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3206 | return posix_error(); |
| 3207 | } |
| 3208 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3210 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3211 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3212 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3213 | \n\ |
| 3214 | path: path of executable file\n\ |
| 3215 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3216 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3217 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3218 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3219 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3220 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3221 | PyObject *opath; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3222 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3223 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3224 | char **argvlist; |
| 3225 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3226 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3227 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3228 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3229 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3230 | |
| 3231 | /* execve has three arguments: (path, argv, env), where |
| 3232 | argv is a list or tuple of strings and env is a dictionary |
| 3233 | like posix.environ. */ |
| 3234 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3235 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3236 | PyUnicode_FSConverter, |
| 3237 | &opath, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3238 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3239 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3240 | if (PyList_Check(argv)) { |
| 3241 | argc = PyList_Size(argv); |
| 3242 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3243 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3244 | else if (PyTuple_Check(argv)) { |
| 3245 | argc = PyTuple_Size(argv); |
| 3246 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3247 | } |
| 3248 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3249 | PyErr_SetString(PyExc_TypeError, |
| 3250 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3251 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3252 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3253 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3254 | PyErr_SetString(PyExc_TypeError, |
| 3255 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3256 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3259 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3260 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3261 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3262 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3263 | } |
| 3264 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3265 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3266 | &argvlist[i])) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3267 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3268 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3269 | goto fail_1; |
| 3270 | } |
| 3271 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3272 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3273 | argvlist[argc] = NULL; |
| 3274 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3275 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3276 | if (i < 0) |
| 3277 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3278 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3279 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3280 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3281 | goto fail_1; |
| 3282 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3283 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3284 | keys = PyMapping_Keys(env); |
| 3285 | vals = PyMapping_Values(env); |
| 3286 | if (!keys || !vals) |
| 3287 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3288 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3289 | PyErr_SetString(PyExc_TypeError, |
| 3290 | "execve(): env.keys() or env.values() is not a list"); |
| 3291 | goto fail_2; |
| 3292 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3293 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3294 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3295 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3296 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3297 | |
| 3298 | key = PyList_GetItem(keys, pos); |
| 3299 | val = PyList_GetItem(vals, pos); |
| 3300 | if (!key || !val) |
| 3301 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3302 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3303 | if (!PyArg_Parse( |
| 3304 | key, |
| 3305 | "s;execve() arg 3 contains a non-string key", |
| 3306 | &k) || |
| 3307 | !PyArg_Parse( |
| 3308 | val, |
| 3309 | "s;execve() arg 3 contains a non-string value", |
| 3310 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3311 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3312 | goto fail_2; |
| 3313 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3314 | |
| 3315 | #if defined(PYOS_OS2) |
| 3316 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3317 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3318 | #endif |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3319 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3320 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3321 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3322 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3323 | goto fail_2; |
| 3324 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3325 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3326 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3327 | #if defined(PYOS_OS2) |
| 3328 | } |
| 3329 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3330 | } |
| 3331 | envlist[envc] = 0; |
| 3332 | |
| 3333 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3334 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3335 | /* If we get here it's definitely an error */ |
| 3336 | |
| 3337 | (void) posix_error(); |
| 3338 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3339 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3340 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3341 | PyMem_DEL(envlist[envc]); |
| 3342 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3343 | fail_1: |
| 3344 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3345 | Py_XDECREF(vals); |
| 3346 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3347 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3348 | release_bytes(opath); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3349 | return NULL; |
| 3350 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3351 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3352 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3353 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3354 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3355 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3356 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3357 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3358 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3359 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3360 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3361 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3362 | |
| 3363 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3364 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3365 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3366 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3367 | char *path; |
| 3368 | PyObject *argv; |
| 3369 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3370 | int mode, i; |
| 3371 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3372 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3373 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3374 | |
| 3375 | /* spawnv has three arguments: (mode, path, argv), where |
| 3376 | argv is a list or tuple of strings. */ |
| 3377 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3378 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 3379 | PyUnicode_FSConverter, |
| 3380 | &opath, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3381 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3382 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3383 | if (PyList_Check(argv)) { |
| 3384 | argc = PyList_Size(argv); |
| 3385 | getitem = PyList_GetItem; |
| 3386 | } |
| 3387 | else if (PyTuple_Check(argv)) { |
| 3388 | argc = PyTuple_Size(argv); |
| 3389 | getitem = PyTuple_GetItem; |
| 3390 | } |
| 3391 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3392 | PyErr_SetString(PyExc_TypeError, |
| 3393 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3394 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3395 | return NULL; |
| 3396 | } |
| 3397 | |
| 3398 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3399 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3400 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3401 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3402 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3403 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3404 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3405 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3406 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3407 | PyErr_SetString( |
| 3408 | PyExc_TypeError, |
| 3409 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3410 | release_bytes(opath); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3411 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3412 | } |
| 3413 | } |
| 3414 | argvlist[argc] = NULL; |
| 3415 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3416 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3417 | Py_BEGIN_ALLOW_THREADS |
| 3418 | spawnval = spawnv(mode, path, argvlist); |
| 3419 | Py_END_ALLOW_THREADS |
| 3420 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3421 | if (mode == _OLD_P_OVERLAY) |
| 3422 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3423 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3424 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3425 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3426 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3427 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3428 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3429 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3430 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3431 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3432 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3433 | return posix_error(); |
| 3434 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3435 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3436 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3437 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3438 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3439 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3443 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3444 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3445 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3446 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3447 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3448 | path: path of executable file\n\ |
| 3449 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3450 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3451 | |
| 3452 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3453 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3454 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3455 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3456 | char *path; |
| 3457 | PyObject *argv, *env; |
| 3458 | char **argvlist; |
| 3459 | char **envlist; |
| 3460 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3461 | int mode, pos, envc; |
| 3462 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3463 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3464 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3465 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3466 | |
| 3467 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3468 | argv is a list or tuple of strings and env is a dictionary |
| 3469 | like posix.environ. */ |
| 3470 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3471 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 3472 | PyUnicode_FSConverter, |
| 3473 | &opath, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3474 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3475 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3476 | if (PyList_Check(argv)) { |
| 3477 | argc = PyList_Size(argv); |
| 3478 | getitem = PyList_GetItem; |
| 3479 | } |
| 3480 | else if (PyTuple_Check(argv)) { |
| 3481 | argc = PyTuple_Size(argv); |
| 3482 | getitem = PyTuple_GetItem; |
| 3483 | } |
| 3484 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3485 | PyErr_SetString(PyExc_TypeError, |
| 3486 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3487 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3488 | } |
| 3489 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3490 | PyErr_SetString(PyExc_TypeError, |
| 3491 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3492 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | argvlist = PyMem_NEW(char *, argc+1); |
| 3496 | if (argvlist == NULL) { |
| 3497 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3498 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3499 | } |
| 3500 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3501 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3502 | &argvlist[i])) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3503 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3504 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3505 | goto fail_1; |
| 3506 | } |
| 3507 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3508 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3509 | argvlist[argc] = NULL; |
| 3510 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3511 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3512 | if (i < 0) |
| 3513 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3514 | envlist = PyMem_NEW(char *, i + 1); |
| 3515 | if (envlist == NULL) { |
| 3516 | PyErr_NoMemory(); |
| 3517 | goto fail_1; |
| 3518 | } |
| 3519 | envc = 0; |
| 3520 | keys = PyMapping_Keys(env); |
| 3521 | vals = PyMapping_Values(env); |
| 3522 | if (!keys || !vals) |
| 3523 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3524 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3525 | PyErr_SetString(PyExc_TypeError, |
| 3526 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3527 | goto fail_2; |
| 3528 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3529 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3530 | for (pos = 0; pos < i; pos++) { |
| 3531 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3532 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3533 | |
| 3534 | key = PyList_GetItem(keys, pos); |
| 3535 | val = PyList_GetItem(vals, pos); |
| 3536 | if (!key || !val) |
| 3537 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3538 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3539 | if (!PyArg_Parse( |
| 3540 | key, |
| 3541 | "s;spawnve() arg 3 contains a non-string key", |
| 3542 | &k) || |
| 3543 | !PyArg_Parse( |
| 3544 | val, |
| 3545 | "s;spawnve() arg 3 contains a non-string value", |
| 3546 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3547 | { |
| 3548 | goto fail_2; |
| 3549 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3550 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3551 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3552 | if (p == NULL) { |
| 3553 | PyErr_NoMemory(); |
| 3554 | goto fail_2; |
| 3555 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3556 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3557 | envlist[envc++] = p; |
| 3558 | } |
| 3559 | envlist[envc] = 0; |
| 3560 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3561 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3562 | Py_BEGIN_ALLOW_THREADS |
| 3563 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3564 | Py_END_ALLOW_THREADS |
| 3565 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3566 | if (mode == _OLD_P_OVERLAY) |
| 3567 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3568 | |
| 3569 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3570 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3571 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3572 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3573 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3574 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3575 | (void) posix_error(); |
| 3576 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3577 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3578 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3579 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3580 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3581 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3582 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3583 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3584 | while (--envc >= 0) |
| 3585 | PyMem_DEL(envlist[envc]); |
| 3586 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3587 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3588 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3589 | Py_XDECREF(vals); |
| 3590 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3591 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3592 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3593 | return res; |
| 3594 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3595 | |
| 3596 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3597 | #if defined(PYOS_OS2) |
| 3598 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3599 | "spawnvp(mode, file, args)\n\n\ |
| 3600 | Execute the program 'file' in a new process, using the environment\n\ |
| 3601 | search path to find the file.\n\ |
| 3602 | \n\ |
| 3603 | mode: mode of process creation\n\ |
| 3604 | file: executable file name\n\ |
| 3605 | args: tuple or list of strings"); |
| 3606 | |
| 3607 | static PyObject * |
| 3608 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3609 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3610 | PyObject *opath; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3611 | char *path; |
| 3612 | PyObject *argv; |
| 3613 | char **argvlist; |
| 3614 | int mode, i, argc; |
| 3615 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3616 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3617 | |
| 3618 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3619 | argv is a list or tuple of strings. */ |
| 3620 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3621 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 3622 | PyUnicode_FSConverter, |
| 3623 | &opath, &argv)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3624 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3625 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3626 | if (PyList_Check(argv)) { |
| 3627 | argc = PyList_Size(argv); |
| 3628 | getitem = PyList_GetItem; |
| 3629 | } |
| 3630 | else if (PyTuple_Check(argv)) { |
| 3631 | argc = PyTuple_Size(argv); |
| 3632 | getitem = PyTuple_GetItem; |
| 3633 | } |
| 3634 | else { |
| 3635 | PyErr_SetString(PyExc_TypeError, |
| 3636 | "spawnvp() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3637 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3638 | return NULL; |
| 3639 | } |
| 3640 | |
| 3641 | argvlist = PyMem_NEW(char *, argc+1); |
| 3642 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3643 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3644 | return PyErr_NoMemory(); |
| 3645 | } |
| 3646 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3647 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3648 | &argvlist[i])) { |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3649 | free_string_array(argvlist, i); |
| 3650 | PyErr_SetString( |
| 3651 | PyExc_TypeError, |
| 3652 | "spawnvp() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3653 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3654 | return NULL; |
| 3655 | } |
| 3656 | } |
| 3657 | argvlist[argc] = NULL; |
| 3658 | |
| 3659 | Py_BEGIN_ALLOW_THREADS |
| 3660 | #if defined(PYCC_GCC) |
| 3661 | spawnval = spawnvp(mode, path, argvlist); |
| 3662 | #else |
| 3663 | spawnval = _spawnvp(mode, path, argvlist); |
| 3664 | #endif |
| 3665 | Py_END_ALLOW_THREADS |
| 3666 | |
| 3667 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3668 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3669 | |
| 3670 | if (spawnval == -1) |
| 3671 | return posix_error(); |
| 3672 | else |
| 3673 | return Py_BuildValue("l", (long) spawnval); |
| 3674 | } |
| 3675 | |
| 3676 | |
| 3677 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3678 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3679 | Execute the program 'file' in a new process, using the environment\n\ |
| 3680 | search path to find the file.\n\ |
| 3681 | \n\ |
| 3682 | mode: mode of process creation\n\ |
| 3683 | file: executable file name\n\ |
| 3684 | args: tuple or list of arguments\n\ |
| 3685 | env: dictionary of strings mapping to strings"); |
| 3686 | |
| 3687 | static PyObject * |
| 3688 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3689 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3690 | PyObject *opath |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3691 | char *path; |
| 3692 | PyObject *argv, *env; |
| 3693 | char **argvlist; |
| 3694 | char **envlist; |
| 3695 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3696 | int mode, i, pos, argc, envc; |
| 3697 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3698 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3699 | int lastarg = 0; |
| 3700 | |
| 3701 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3702 | argv is a list or tuple of strings and env is a dictionary |
| 3703 | like posix.environ. */ |
| 3704 | |
| 3705 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3706 | PyUnicode_FSConverter, |
| 3707 | &opath, &argv, &env)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3708 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3709 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3710 | if (PyList_Check(argv)) { |
| 3711 | argc = PyList_Size(argv); |
| 3712 | getitem = PyList_GetItem; |
| 3713 | } |
| 3714 | else if (PyTuple_Check(argv)) { |
| 3715 | argc = PyTuple_Size(argv); |
| 3716 | getitem = PyTuple_GetItem; |
| 3717 | } |
| 3718 | else { |
| 3719 | PyErr_SetString(PyExc_TypeError, |
| 3720 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3721 | goto fail_0; |
| 3722 | } |
| 3723 | if (!PyMapping_Check(env)) { |
| 3724 | PyErr_SetString(PyExc_TypeError, |
| 3725 | "spawnvpe() arg 3 must be a mapping object"); |
| 3726 | goto fail_0; |
| 3727 | } |
| 3728 | |
| 3729 | argvlist = PyMem_NEW(char *, argc+1); |
| 3730 | if (argvlist == NULL) { |
| 3731 | PyErr_NoMemory(); |
| 3732 | goto fail_0; |
| 3733 | } |
| 3734 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3735 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3736 | &argvlist[i])) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3737 | { |
| 3738 | lastarg = i; |
| 3739 | goto fail_1; |
| 3740 | } |
| 3741 | } |
| 3742 | lastarg = argc; |
| 3743 | argvlist[argc] = NULL; |
| 3744 | |
| 3745 | i = PyMapping_Size(env); |
| 3746 | if (i < 0) |
| 3747 | goto fail_1; |
| 3748 | envlist = PyMem_NEW(char *, i + 1); |
| 3749 | if (envlist == NULL) { |
| 3750 | PyErr_NoMemory(); |
| 3751 | goto fail_1; |
| 3752 | } |
| 3753 | envc = 0; |
| 3754 | keys = PyMapping_Keys(env); |
| 3755 | vals = PyMapping_Values(env); |
| 3756 | if (!keys || !vals) |
| 3757 | goto fail_2; |
| 3758 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3759 | PyErr_SetString(PyExc_TypeError, |
| 3760 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3761 | goto fail_2; |
| 3762 | } |
| 3763 | |
| 3764 | for (pos = 0; pos < i; pos++) { |
| 3765 | char *p, *k, *v; |
| 3766 | size_t len; |
| 3767 | |
| 3768 | key = PyList_GetItem(keys, pos); |
| 3769 | val = PyList_GetItem(vals, pos); |
| 3770 | if (!key || !val) |
| 3771 | goto fail_2; |
| 3772 | |
| 3773 | if (!PyArg_Parse( |
| 3774 | key, |
| 3775 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3776 | &k) || |
| 3777 | !PyArg_Parse( |
| 3778 | val, |
| 3779 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3780 | &v)) |
| 3781 | { |
| 3782 | goto fail_2; |
| 3783 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3784 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3785 | p = PyMem_NEW(char, len); |
| 3786 | if (p == NULL) { |
| 3787 | PyErr_NoMemory(); |
| 3788 | goto fail_2; |
| 3789 | } |
| 3790 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3791 | envlist[envc++] = p; |
| 3792 | } |
| 3793 | envlist[envc] = 0; |
| 3794 | |
| 3795 | Py_BEGIN_ALLOW_THREADS |
| 3796 | #if defined(PYCC_GCC) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3797 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3798 | #else |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3799 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3800 | #endif |
| 3801 | Py_END_ALLOW_THREADS |
| 3802 | |
| 3803 | if (spawnval == -1) |
| 3804 | (void) posix_error(); |
| 3805 | else |
| 3806 | res = Py_BuildValue("l", (long) spawnval); |
| 3807 | |
| 3808 | fail_2: |
| 3809 | while (--envc >= 0) |
| 3810 | PyMem_DEL(envlist[envc]); |
| 3811 | PyMem_DEL(envlist); |
| 3812 | fail_1: |
| 3813 | free_string_array(argvlist, lastarg); |
| 3814 | Py_XDECREF(vals); |
| 3815 | Py_XDECREF(keys); |
| 3816 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3817 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3818 | return res; |
| 3819 | } |
| 3820 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3821 | #endif /* HAVE_SPAWNV */ |
| 3822 | |
| 3823 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3824 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3825 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3826 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3827 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3828 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3829 | 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] | 3830 | |
| 3831 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3832 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3833 | { |
Benjamin Peterson | 5183856 | 2009-10-04 20:35:30 +0000 | [diff] [blame] | 3834 | pid_t pid; |
| 3835 | int result; |
| 3836 | _PyImport_AcquireLock(); |
| 3837 | pid = fork1(); |
| 3838 | result = _PyImport_ReleaseLock(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3839 | if (pid == -1) |
| 3840 | return posix_error(); |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 3841 | if (pid == 0) |
| 3842 | PyOS_AfterFork(); |
Benjamin Peterson | 5183856 | 2009-10-04 20:35:30 +0000 | [diff] [blame] | 3843 | if (result < 0) { |
| 3844 | /* Don't clobber the OSError if the fork failed. */ |
| 3845 | PyErr_SetString(PyExc_RuntimeError, |
| 3846 | "not holding the import lock"); |
| 3847 | return NULL; |
| 3848 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3849 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3850 | } |
| 3851 | #endif |
| 3852 | |
| 3853 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3854 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3855 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3856 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3857 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3858 | 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] | 3859 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3860 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3861 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3862 | { |
Benjamin Peterson | 5183856 | 2009-10-04 20:35:30 +0000 | [diff] [blame] | 3863 | pid_t pid; |
| 3864 | int result; |
| 3865 | _PyImport_AcquireLock(); |
| 3866 | pid = fork(); |
| 3867 | result = _PyImport_ReleaseLock(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3868 | if (pid == -1) |
| 3869 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3870 | if (pid == 0) |
| 3871 | PyOS_AfterFork(); |
Benjamin Peterson | 5183856 | 2009-10-04 20:35:30 +0000 | [diff] [blame] | 3872 | if (result < 0) { |
| 3873 | /* Don't clobber the OSError if the fork failed. */ |
| 3874 | PyErr_SetString(PyExc_RuntimeError, |
| 3875 | "not holding the import lock"); |
| 3876 | return NULL; |
| 3877 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3878 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3879 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3880 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3881 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3882 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3883 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3884 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3885 | #define DEV_PTY_FILE "/dev/ptc" |
| 3886 | #define HAVE_DEV_PTMX |
| 3887 | #else |
| 3888 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3889 | #endif |
| 3890 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3891 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3892 | #ifdef HAVE_PTY_H |
| 3893 | #include <pty.h> |
| 3894 | #else |
| 3895 | #ifdef HAVE_LIBUTIL_H |
| 3896 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3897 | #endif /* HAVE_LIBUTIL_H */ |
| 3898 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3899 | #ifdef HAVE_STROPTS_H |
| 3900 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3901 | #endif |
| 3902 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3903 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3904 | #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] | 3905 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3906 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3907 | 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] | 3908 | |
| 3909 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3910 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3911 | { |
| 3912 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3913 | #ifndef HAVE_OPENPTY |
| 3914 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3915 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3916 | #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] | 3917 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3918 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3919 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3920 | #endif |
| 3921 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3922 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3923 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3924 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3925 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3926 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3927 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3928 | if (slave_name == NULL) |
| 3929 | return posix_error(); |
| 3930 | |
| 3931 | slave_fd = open(slave_name, O_RDWR); |
| 3932 | if (slave_fd < 0) |
| 3933 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3934 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3935 | 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] | 3936 | if (master_fd < 0) |
| 3937 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3938 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3939 | /* change permission of slave */ |
| 3940 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3941 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3942 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3943 | } |
| 3944 | /* unlock slave */ |
| 3945 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3946 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3947 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3948 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3949 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3950 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3951 | if (slave_name == NULL) |
| 3952 | return posix_error(); |
| 3953 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3954 | if (slave_fd < 0) |
| 3955 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3956 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3957 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3958 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3959 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3960 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3961 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3962 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3963 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3964 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3965 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3966 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3967 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3968 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3969 | |
| 3970 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3971 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3972 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3973 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3974 | 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] | 3975 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3976 | |
| 3977 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3978 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3979 | { |
Benjamin Peterson | 5183856 | 2009-10-04 20:35:30 +0000 | [diff] [blame] | 3980 | int master_fd = -1, result; |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3981 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3982 | |
Benjamin Peterson | 5183856 | 2009-10-04 20:35:30 +0000 | [diff] [blame] | 3983 | _PyImport_AcquireLock(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3984 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
Benjamin Peterson | 5183856 | 2009-10-04 20:35:30 +0000 | [diff] [blame] | 3985 | result = _PyImport_ReleaseLock(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3986 | if (pid == -1) |
| 3987 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3988 | if (pid == 0) |
| 3989 | PyOS_AfterFork(); |
Benjamin Peterson | 5183856 | 2009-10-04 20:35:30 +0000 | [diff] [blame] | 3990 | if (result < 0) { |
| 3991 | /* Don't clobber the OSError if the fork failed. */ |
| 3992 | PyErr_SetString(PyExc_RuntimeError, |
| 3993 | "not holding the import lock"); |
| 3994 | return NULL; |
| 3995 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3996 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3997 | } |
| 3998 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3999 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4000 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4001 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4002 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4003 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4004 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4005 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4006 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4007 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4008 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4009 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4010 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4011 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4012 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4013 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4014 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4015 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4016 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4017 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4018 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4019 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4020 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4021 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4022 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4023 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4024 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4025 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4026 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4027 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4028 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4029 | Return the current process's 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_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4033 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4034 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4035 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4036 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4037 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4038 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4039 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4040 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4041 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4042 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4043 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4044 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4045 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4046 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4049 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4050 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4051 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4052 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4053 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4054 | |
| 4055 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4056 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4057 | { |
| 4058 | PyObject *result = NULL; |
| 4059 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4060 | #ifdef NGROUPS_MAX |
| 4061 | #define MAX_GROUPS NGROUPS_MAX |
| 4062 | #else |
| 4063 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 4064 | #define MAX_GROUPS 64 |
| 4065 | #endif |
| 4066 | gid_t grouplist[MAX_GROUPS]; |
| 4067 | int n; |
| 4068 | |
| 4069 | n = getgroups(MAX_GROUPS, grouplist); |
| 4070 | if (n < 0) |
| 4071 | posix_error(); |
| 4072 | else { |
| 4073 | result = PyList_New(n); |
| 4074 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4075 | int i; |
| 4076 | for (i = 0; i < n; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4077 | PyObject *o = PyLong_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4078 | if (o == NULL) { |
| 4079 | Py_DECREF(result); |
| 4080 | result = NULL; |
| 4081 | break; |
| 4082 | } |
| 4083 | PyList_SET_ITEM(result, i, o); |
| 4084 | } |
| 4085 | } |
| 4086 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4087 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4088 | return result; |
| 4089 | } |
| 4090 | #endif |
| 4091 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4092 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4093 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4094 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4095 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4096 | |
| 4097 | static PyObject * |
| 4098 | posix_getpgid(PyObject *self, PyObject *args) |
| 4099 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4100 | pid_t pid, pgid; |
| 4101 | if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid)) |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4102 | return NULL; |
| 4103 | pgid = getpgid(pid); |
| 4104 | if (pgid < 0) |
| 4105 | return posix_error(); |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4106 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4107 | } |
| 4108 | #endif /* HAVE_GETPGID */ |
| 4109 | |
| 4110 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4111 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4112 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4113 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4114 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4115 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4116 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4117 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4118 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4119 | #ifdef GETPGRP_HAVE_ARG |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4120 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4121 | #else /* GETPGRP_HAVE_ARG */ |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4122 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4123 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4124 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4125 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4126 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4127 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4128 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4129 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4130 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4131 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4132 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4133 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4134 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4135 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4136 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4137 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4138 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4139 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4140 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4141 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4142 | Py_INCREF(Py_None); |
| 4143 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4146 | #endif /* HAVE_SETPGRP */ |
| 4147 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4148 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4149 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4150 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4151 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4152 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4153 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4154 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4155 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4156 | return PyLong_FromPid(getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4157 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4158 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4159 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4160 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4161 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4162 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4163 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4164 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4165 | |
| 4166 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4167 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4168 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4169 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4170 | char *name; |
| 4171 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4172 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4173 | errno = 0; |
| 4174 | name = getlogin(); |
| 4175 | if (name == NULL) { |
| 4176 | if (errno) |
| 4177 | posix_error(); |
| 4178 | else |
| 4179 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 4180 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4181 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4182 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 4183 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4184 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4185 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4186 | return result; |
| 4187 | } |
| 4188 | #endif |
| 4189 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4190 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4191 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4192 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4193 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4194 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4195 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4196 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4197 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4198 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4199 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4200 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4201 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4202 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4203 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4204 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4205 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4206 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4207 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4208 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4209 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4210 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4211 | pid_t pid; |
| 4212 | int sig; |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4213 | if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4214 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4215 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4216 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4217 | APIRET rc; |
| 4218 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4219 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4220 | |
| 4221 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4222 | APIRET rc; |
| 4223 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4224 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4225 | |
| 4226 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4227 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4228 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4229 | if (kill(pid, sig) == -1) |
| 4230 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4231 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4232 | Py_INCREF(Py_None); |
| 4233 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4234 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4235 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4236 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4237 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4238 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4239 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4240 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4241 | |
| 4242 | static PyObject * |
| 4243 | posix_killpg(PyObject *self, PyObject *args) |
| 4244 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4245 | int sig; |
| 4246 | pid_t pgid; |
| 4247 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4248 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4249 | take the same type. Moreover, pid_t is always at least as wide as |
| 4250 | int (else compilation of this module fails), which is safe. */ |
| 4251 | if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig)) |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4252 | return NULL; |
| 4253 | if (killpg(pgid, sig) == -1) |
| 4254 | return posix_error(); |
| 4255 | Py_INCREF(Py_None); |
| 4256 | return Py_None; |
| 4257 | } |
| 4258 | #endif |
| 4259 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4260 | #ifdef HAVE_PLOCK |
| 4261 | |
| 4262 | #ifdef HAVE_SYS_LOCK_H |
| 4263 | #include <sys/lock.h> |
| 4264 | #endif |
| 4265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4266 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4267 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4268 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4269 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4270 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4271 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4272 | { |
| 4273 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4274 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4275 | return NULL; |
| 4276 | if (plock(op) == -1) |
| 4277 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4278 | Py_INCREF(Py_None); |
| 4279 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4280 | } |
| 4281 | #endif |
| 4282 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4283 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4284 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4285 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4286 | Set the current process's user id."); |
| 4287 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4288 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4289 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4290 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4291 | long uid_arg; |
| 4292 | uid_t uid; |
| 4293 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4294 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4295 | uid = uid_arg; |
| 4296 | if (uid != uid_arg) { |
| 4297 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4298 | return NULL; |
| 4299 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4300 | if (setuid(uid) < 0) |
| 4301 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4302 | Py_INCREF(Py_None); |
| 4303 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4304 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4305 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4306 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4307 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4308 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4309 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4310 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4311 | Set the current process's effective user id."); |
| 4312 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4313 | static PyObject * |
| 4314 | posix_seteuid (PyObject *self, PyObject *args) |
| 4315 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4316 | long euid_arg; |
| 4317 | uid_t euid; |
| 4318 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4319 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4320 | euid = euid_arg; |
| 4321 | if (euid != euid_arg) { |
| 4322 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4323 | return NULL; |
| 4324 | } |
| 4325 | if (seteuid(euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4326 | return posix_error(); |
| 4327 | } else { |
| 4328 | Py_INCREF(Py_None); |
| 4329 | return Py_None; |
| 4330 | } |
| 4331 | } |
| 4332 | #endif /* HAVE_SETEUID */ |
| 4333 | |
| 4334 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4335 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4336 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4337 | Set the current process's effective group id."); |
| 4338 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4339 | static PyObject * |
| 4340 | posix_setegid (PyObject *self, PyObject *args) |
| 4341 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4342 | long egid_arg; |
| 4343 | gid_t egid; |
| 4344 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4345 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4346 | egid = egid_arg; |
| 4347 | if (egid != egid_arg) { |
| 4348 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4349 | return NULL; |
| 4350 | } |
| 4351 | if (setegid(egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4352 | return posix_error(); |
| 4353 | } else { |
| 4354 | Py_INCREF(Py_None); |
| 4355 | return Py_None; |
| 4356 | } |
| 4357 | } |
| 4358 | #endif /* HAVE_SETEGID */ |
| 4359 | |
| 4360 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4361 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4362 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4363 | Set the current process's real and effective user ids."); |
| 4364 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4365 | static PyObject * |
| 4366 | posix_setreuid (PyObject *self, PyObject *args) |
| 4367 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4368 | long ruid_arg, euid_arg; |
| 4369 | uid_t ruid, euid; |
| 4370 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4371 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4372 | ruid = ruid_arg; |
| 4373 | euid = euid_arg; |
| 4374 | if (euid != euid_arg || ruid != ruid_arg) { |
| 4375 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4376 | return NULL; |
| 4377 | } |
| 4378 | if (setreuid(ruid, euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4379 | return posix_error(); |
| 4380 | } else { |
| 4381 | Py_INCREF(Py_None); |
| 4382 | return Py_None; |
| 4383 | } |
| 4384 | } |
| 4385 | #endif /* HAVE_SETREUID */ |
| 4386 | |
| 4387 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4388 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4389 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4390 | Set the current process's real and effective group ids."); |
| 4391 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4392 | static PyObject * |
| 4393 | posix_setregid (PyObject *self, PyObject *args) |
| 4394 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4395 | long rgid_arg, egid_arg; |
| 4396 | gid_t rgid, egid; |
| 4397 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4398 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4399 | rgid = rgid_arg; |
| 4400 | egid = egid_arg; |
| 4401 | if (egid != egid_arg || rgid != rgid_arg) { |
| 4402 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4403 | return NULL; |
| 4404 | } |
| 4405 | if (setregid(rgid, egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4406 | return posix_error(); |
| 4407 | } else { |
| 4408 | Py_INCREF(Py_None); |
| 4409 | return Py_None; |
| 4410 | } |
| 4411 | } |
| 4412 | #endif /* HAVE_SETREGID */ |
| 4413 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4414 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4415 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4416 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4417 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4418 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4419 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4420 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4421 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4422 | long gid_arg; |
| 4423 | gid_t gid; |
| 4424 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4425 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4426 | gid = gid_arg; |
| 4427 | if (gid != gid_arg) { |
| 4428 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4429 | return NULL; |
| 4430 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4431 | if (setgid(gid) < 0) |
| 4432 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4433 | Py_INCREF(Py_None); |
| 4434 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4435 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4436 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4437 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4438 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4439 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4440 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4441 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4442 | |
| 4443 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4444 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4445 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4446 | int i, len; |
| 4447 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4448 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4449 | if (!PySequence_Check(groups)) { |
| 4450 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4451 | return NULL; |
| 4452 | } |
| 4453 | len = PySequence_Size(groups); |
| 4454 | if (len > MAX_GROUPS) { |
| 4455 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4456 | return NULL; |
| 4457 | } |
| 4458 | for(i = 0; i < len; i++) { |
| 4459 | PyObject *elem; |
| 4460 | elem = PySequence_GetItem(groups, i); |
| 4461 | if (!elem) |
| 4462 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4463 | if (!PyLong_Check(elem)) { |
| 4464 | PyErr_SetString(PyExc_TypeError, |
| 4465 | "groups must be integers"); |
| 4466 | Py_DECREF(elem); |
| 4467 | return NULL; |
| 4468 | } else { |
| 4469 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4470 | if (PyErr_Occurred()) { |
| 4471 | PyErr_SetString(PyExc_TypeError, |
| 4472 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4473 | Py_DECREF(elem); |
| 4474 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4475 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4476 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4477 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4478 | if (grouplist[i] != x) { |
| 4479 | PyErr_SetString(PyExc_TypeError, |
| 4480 | "group id too big"); |
| 4481 | Py_DECREF(elem); |
| 4482 | return NULL; |
| 4483 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4484 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4485 | Py_DECREF(elem); |
| 4486 | } |
| 4487 | |
| 4488 | if (setgroups(len, grouplist) < 0) |
| 4489 | return posix_error(); |
| 4490 | Py_INCREF(Py_None); |
| 4491 | return Py_None; |
| 4492 | } |
| 4493 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4494 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4495 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4496 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4497 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4498 | { |
| 4499 | PyObject *result; |
| 4500 | static PyObject *struct_rusage; |
| 4501 | |
| 4502 | if (pid == -1) |
| 4503 | return posix_error(); |
| 4504 | |
| 4505 | if (struct_rusage == NULL) { |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4506 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4507 | if (m == NULL) |
| 4508 | return NULL; |
| 4509 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4510 | Py_DECREF(m); |
| 4511 | if (struct_rusage == NULL) |
| 4512 | return NULL; |
| 4513 | } |
| 4514 | |
| 4515 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4516 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4517 | if (!result) |
| 4518 | return NULL; |
| 4519 | |
| 4520 | #ifndef doubletime |
| 4521 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4522 | #endif |
| 4523 | |
| 4524 | PyStructSequence_SET_ITEM(result, 0, |
| 4525 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4526 | PyStructSequence_SET_ITEM(result, 1, |
| 4527 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4528 | #define SET_INT(result, index, value)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4529 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4530 | SET_INT(result, 2, ru->ru_maxrss); |
| 4531 | SET_INT(result, 3, ru->ru_ixrss); |
| 4532 | SET_INT(result, 4, ru->ru_idrss); |
| 4533 | SET_INT(result, 5, ru->ru_isrss); |
| 4534 | SET_INT(result, 6, ru->ru_minflt); |
| 4535 | SET_INT(result, 7, ru->ru_majflt); |
| 4536 | SET_INT(result, 8, ru->ru_nswap); |
| 4537 | SET_INT(result, 9, ru->ru_inblock); |
| 4538 | SET_INT(result, 10, ru->ru_oublock); |
| 4539 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4540 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4541 | SET_INT(result, 13, ru->ru_nsignals); |
| 4542 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4543 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4544 | #undef SET_INT |
| 4545 | |
| 4546 | if (PyErr_Occurred()) { |
| 4547 | Py_DECREF(result); |
| 4548 | return NULL; |
| 4549 | } |
| 4550 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4551 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4552 | } |
| 4553 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4554 | |
| 4555 | #ifdef HAVE_WAIT3 |
| 4556 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4557 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4558 | Wait for completion of a child process."); |
| 4559 | |
| 4560 | static PyObject * |
| 4561 | posix_wait3(PyObject *self, PyObject *args) |
| 4562 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4563 | pid_t pid; |
| 4564 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4565 | struct rusage ru; |
| 4566 | WAIT_TYPE status; |
| 4567 | WAIT_STATUS_INT(status) = 0; |
| 4568 | |
| 4569 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4570 | return NULL; |
| 4571 | |
| 4572 | Py_BEGIN_ALLOW_THREADS |
| 4573 | pid = wait3(&status, options, &ru); |
| 4574 | Py_END_ALLOW_THREADS |
| 4575 | |
| 4576 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4577 | } |
| 4578 | #endif /* HAVE_WAIT3 */ |
| 4579 | |
| 4580 | #ifdef HAVE_WAIT4 |
| 4581 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4582 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4583 | Wait for completion of a given child process."); |
| 4584 | |
| 4585 | static PyObject * |
| 4586 | posix_wait4(PyObject *self, PyObject *args) |
| 4587 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4588 | pid_t pid; |
| 4589 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4590 | struct rusage ru; |
| 4591 | WAIT_TYPE status; |
| 4592 | WAIT_STATUS_INT(status) = 0; |
| 4593 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4594 | if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4595 | return NULL; |
| 4596 | |
| 4597 | Py_BEGIN_ALLOW_THREADS |
| 4598 | pid = wait4(pid, &status, options, &ru); |
| 4599 | Py_END_ALLOW_THREADS |
| 4600 | |
| 4601 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4602 | } |
| 4603 | #endif /* HAVE_WAIT4 */ |
| 4604 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4605 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4606 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4607 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4608 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4609 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4610 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4611 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4612 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4613 | pid_t pid; |
| 4614 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4615 | WAIT_TYPE status; |
| 4616 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4617 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4618 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4619 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4620 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4621 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4622 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4623 | if (pid == -1) |
| 4624 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4625 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4626 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4629 | #elif defined(HAVE_CWAIT) |
| 4630 | |
| 4631 | /* 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] | 4632 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4633 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4634 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4635 | |
| 4636 | static PyObject * |
| 4637 | posix_waitpid(PyObject *self, PyObject *args) |
| 4638 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4639 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4640 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4641 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4642 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4643 | return NULL; |
| 4644 | Py_BEGIN_ALLOW_THREADS |
| 4645 | pid = _cwait(&status, pid, options); |
| 4646 | Py_END_ALLOW_THREADS |
| 4647 | if (pid == -1) |
| 4648 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4649 | |
| 4650 | /* 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] | 4651 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4652 | } |
| 4653 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4654 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4655 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4656 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4657 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4658 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4659 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4660 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4661 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4662 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4663 | pid_t pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4664 | WAIT_TYPE status; |
| 4665 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4666 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4667 | Py_BEGIN_ALLOW_THREADS |
| 4668 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4669 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4670 | if (pid == -1) |
| 4671 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4672 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4673 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4674 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4675 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4676 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4677 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4678 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4679 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4680 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4681 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4682 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4683 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4684 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4685 | #ifdef HAVE_LSTAT |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4686 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4687 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4688 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4689 | 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] | 4690 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4691 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4692 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4693 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4696 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4697 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4698 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4699 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4700 | 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] | 4701 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4702 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4703 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4704 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4705 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4706 | char buf[MAXPATHLEN]; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4707 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4708 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4709 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4710 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4711 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4712 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 4713 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4714 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4715 | path = bytes2str(opath, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4716 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4717 | if (v == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4718 | release_bytes(opath); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4719 | return NULL; |
| 4720 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4721 | |
| 4722 | if (PyUnicode_Check(v)) { |
| 4723 | arg_is_unicode = 1; |
| 4724 | } |
| 4725 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4726 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4727 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4728 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4729 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4730 | if (n < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4731 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4732 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4733 | release_bytes(opath); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4734 | v = PyBytes_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4735 | if (arg_is_unicode) { |
| 4736 | PyObject *w; |
| 4737 | |
| 4738 | w = PyUnicode_FromEncodedObject(v, |
| 4739 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 4740 | "surrogateescape"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4741 | if (w != NULL) { |
| 4742 | Py_DECREF(v); |
| 4743 | v = w; |
| 4744 | } |
| 4745 | else { |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4746 | v = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4747 | } |
| 4748 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4749 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4750 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4751 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4752 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4753 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4754 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4755 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4756 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4757 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4758 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4759 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4760 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4761 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4762 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4763 | } |
| 4764 | #endif /* HAVE_SYMLINK */ |
| 4765 | |
| 4766 | |
| 4767 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4768 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4769 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4770 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4771 | { |
| 4772 | ULONG value = 0; |
| 4773 | |
| 4774 | Py_BEGIN_ALLOW_THREADS |
| 4775 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4776 | Py_END_ALLOW_THREADS |
| 4777 | |
| 4778 | return value; |
| 4779 | } |
| 4780 | |
| 4781 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4782 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4783 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4784 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4785 | return Py_BuildValue("ddddd", |
| 4786 | (double)0 /* t.tms_utime / HZ */, |
| 4787 | (double)0 /* t.tms_stime / HZ */, |
| 4788 | (double)0 /* t.tms_cutime / HZ */, |
| 4789 | (double)0 /* t.tms_cstime / HZ */, |
| 4790 | (double)system_uptime() / 1000); |
| 4791 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4792 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4793 | #define NEED_TICKS_PER_SECOND |
| 4794 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4795 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4796 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4797 | { |
| 4798 | struct tms t; |
| 4799 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4800 | errno = 0; |
| 4801 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4802 | if (c == (clock_t) -1) |
| 4803 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4804 | return Py_BuildValue("ddddd", |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4805 | (double)t.tms_utime / ticks_per_second, |
| 4806 | (double)t.tms_stime / ticks_per_second, |
| 4807 | (double)t.tms_cutime / ticks_per_second, |
| 4808 | (double)t.tms_cstime / ticks_per_second, |
| 4809 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4810 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4811 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4812 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4813 | |
| 4814 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4815 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4816 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4817 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4818 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4819 | { |
| 4820 | FILETIME create, exit, kernel, user; |
| 4821 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4822 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4823 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4824 | /* The fields of a FILETIME structure are the hi and lo part |
| 4825 | of a 64-bit value expressed in 100 nanosecond units. |
| 4826 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4827 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4828 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4829 | return Py_BuildValue( |
| 4830 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4831 | (double)(user.dwHighDateTime*429.4967296 + |
| 4832 | user.dwLowDateTime*1e-7), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 4833 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4834 | kernel.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4835 | (double)0, |
| 4836 | (double)0, |
| 4837 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4838 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4839 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4840 | |
| 4841 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4842 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4843 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4844 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4845 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4846 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4847 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4848 | #ifdef HAVE_GETSID |
| 4849 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4850 | "getsid(pid) -> sid\n\n\ |
| 4851 | Call the system call getsid()."); |
| 4852 | |
| 4853 | static PyObject * |
| 4854 | posix_getsid(PyObject *self, PyObject *args) |
| 4855 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4856 | pid_t pid; |
| 4857 | int sid; |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4858 | if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid)) |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4859 | return NULL; |
| 4860 | sid = getsid(pid); |
| 4861 | if (sid < 0) |
| 4862 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4863 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4864 | } |
| 4865 | #endif /* HAVE_GETSID */ |
| 4866 | |
| 4867 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4868 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4869 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4870 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4871 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4872 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4873 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4874 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4875 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4876 | if (setsid() < 0) |
| 4877 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4878 | Py_INCREF(Py_None); |
| 4879 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4880 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4881 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4882 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4883 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4884 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4885 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4886 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4887 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4888 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4889 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4890 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4891 | pid_t pid; |
| 4892 | int pgrp; |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4893 | if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4894 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4895 | if (setpgid(pid, pgrp) < 0) |
| 4896 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4897 | Py_INCREF(Py_None); |
| 4898 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4899 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4900 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4901 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4902 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4903 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4904 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4905 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4906 | 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] | 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_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4910 | { |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4911 | int fd; |
| 4912 | pid_t pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4913 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4914 | return NULL; |
| 4915 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4916 | if (pgid < 0) |
| 4917 | return posix_error(); |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4918 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4919 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4920 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4921 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4922 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4923 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4924 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4925 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4926 | 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] | 4927 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4928 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4929 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4930 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4931 | int fd; |
| 4932 | pid_t pgid; |
| 4933 | if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4934 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4935 | if (tcsetpgrp(fd, pgid) < 0) |
| 4936 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4937 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4938 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4939 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4940 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4941 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4942 | /* Functions acting on file descriptors */ |
| 4943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4944 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4945 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4946 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4947 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4948 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4949 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4950 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4951 | PyObject *ofile; |
| 4952 | char *file; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4953 | int flag; |
| 4954 | int mode = 0777; |
| 4955 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4956 | |
| 4957 | #ifdef MS_WINDOWS |
| 4958 | if (unicode_file_names()) { |
| 4959 | PyUnicodeObject *po; |
| 4960 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4961 | Py_BEGIN_ALLOW_THREADS |
| 4962 | /* PyUnicode_AS_UNICODE OK without thread |
| 4963 | lock as it is a simple dereference. */ |
| 4964 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4965 | Py_END_ALLOW_THREADS |
| 4966 | if (fd < 0) |
| 4967 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4968 | return PyLong_FromLong((long)fd); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4969 | } |
| 4970 | /* Drop the argument parsing error as narrow strings |
| 4971 | are also valid. */ |
| 4972 | PyErr_Clear(); |
| 4973 | } |
| 4974 | #endif |
| 4975 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4976 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 4977 | PyUnicode_FSConverter, &ofile, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4978 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4979 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4980 | file = bytes2str(ofile, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4981 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4982 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4983 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4984 | if (fd < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4985 | return posix_error_with_allocated_filename(ofile); |
| 4986 | release_bytes(ofile); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4987 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4990 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4991 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4992 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4993 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4994 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4995 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4996 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4997 | { |
| 4998 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4999 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5000 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5001 | if (!_PyVerify_fd(fd)) |
| 5002 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5003 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5004 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5005 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5006 | if (res < 0) |
| 5007 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5008 | Py_INCREF(Py_None); |
| 5009 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5012 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5013 | PyDoc_STRVAR(posix_closerange__doc__, |
| 5014 | "closerange(fd_low, fd_high)\n\n\ |
| 5015 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 5016 | |
| 5017 | static PyObject * |
| 5018 | posix_closerange(PyObject *self, PyObject *args) |
| 5019 | { |
| 5020 | int fd_from, fd_to, i; |
| 5021 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 5022 | return NULL; |
| 5023 | Py_BEGIN_ALLOW_THREADS |
| 5024 | for (i = fd_from; i < fd_to; i++) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5025 | if (_PyVerify_fd(i)) |
| 5026 | close(i); |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5027 | Py_END_ALLOW_THREADS |
| 5028 | Py_RETURN_NONE; |
| 5029 | } |
| 5030 | |
| 5031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5032 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5033 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5034 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5035 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5036 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5037 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5038 | { |
| 5039 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5040 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5041 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5042 | if (!_PyVerify_fd(fd)) |
| 5043 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5044 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5045 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5046 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5047 | if (fd < 0) |
| 5048 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5049 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5053 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5054 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5055 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5056 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5057 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5058 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5059 | { |
| 5060 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5061 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5062 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5063 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 5064 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5065 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5066 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5067 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5068 | if (res < 0) |
| 5069 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5070 | Py_INCREF(Py_None); |
| 5071 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5075 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5076 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5077 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5078 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5079 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5080 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5081 | { |
| 5082 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5083 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5084 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5085 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5086 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5087 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5088 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5089 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5090 | return NULL; |
| 5091 | #ifdef SEEK_SET |
| 5092 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5093 | switch (how) { |
| 5094 | case 0: how = SEEK_SET; break; |
| 5095 | case 1: how = SEEK_CUR; break; |
| 5096 | case 2: how = SEEK_END; break; |
| 5097 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5098 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5099 | |
| 5100 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5101 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5102 | #else |
| 5103 | pos = PyLong_Check(posobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5104 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5105 | #endif |
| 5106 | if (PyErr_Occurred()) |
| 5107 | return NULL; |
| 5108 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5109 | if (!_PyVerify_fd(fd)) |
| 5110 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5111 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5112 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5113 | res = _lseeki64(fd, pos, how); |
| 5114 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5115 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5116 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5117 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5118 | if (res < 0) |
| 5119 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5120 | |
| 5121 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5122 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5123 | #else |
| 5124 | return PyLong_FromLongLong(res); |
| 5125 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5128 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5129 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5130 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5131 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5132 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5133 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5134 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5135 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 5136 | int fd, size; |
| 5137 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5138 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5139 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5140 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5141 | if (size < 0) { |
| 5142 | errno = EINVAL; |
| 5143 | return posix_error(); |
| 5144 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5145 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5146 | if (buffer == NULL) |
| 5147 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5148 | if (!_PyVerify_fd(fd)) |
| 5149 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5150 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5151 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5152 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5153 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5154 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5155 | return posix_error(); |
| 5156 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5157 | if (n != size) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5158 | _PyBytes_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5159 | return buffer; |
| 5160 | } |
| 5161 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5162 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5163 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5164 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5165 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5166 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5167 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5168 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5169 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5170 | Py_buffer pbuf; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5171 | int fd; |
| 5172 | Py_ssize_t size; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5173 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 5174 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5175 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5176 | if (!_PyVerify_fd(fd)) |
| 5177 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5178 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5179 | size = write(fd, pbuf.buf, (size_t)pbuf.len); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5180 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5181 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5182 | if (size < 0) |
| 5183 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5184 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5187 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5188 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5189 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5190 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5191 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5192 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5193 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5194 | { |
| 5195 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5196 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5197 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5198 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5199 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5200 | #ifdef __VMS |
| 5201 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5202 | fsync(fd); |
| 5203 | #endif |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5204 | if (!_PyVerify_fd(fd)) |
| 5205 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5206 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5207 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5208 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5209 | if (res != 0) { |
| 5210 | #ifdef MS_WINDOWS |
| 5211 | return win32_error("fstat", NULL); |
| 5212 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5213 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5214 | #endif |
| 5215 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5216 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5217 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5218 | } |
| 5219 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5220 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5221 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5222 | 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] | 5223 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5224 | |
| 5225 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5226 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5227 | { |
| 5228 | int fd; |
| 5229 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5230 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5231 | if (!_PyVerify_fd(fd)) |
| 5232 | return PyBool_FromLong(0); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5233 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5234 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5235 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5236 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5237 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5238 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5239 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5240 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5241 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5242 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5243 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5244 | #if defined(PYOS_OS2) |
| 5245 | HFILE read, write; |
| 5246 | APIRET rc; |
| 5247 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5248 | Py_BEGIN_ALLOW_THREADS |
| 5249 | rc = DosCreatePipe( &read, &write, 4096); |
| 5250 | Py_END_ALLOW_THREADS |
| 5251 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5252 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5253 | |
| 5254 | return Py_BuildValue("(ii)", read, write); |
| 5255 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5256 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5257 | int fds[2]; |
| 5258 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5259 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5260 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5261 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5262 | if (res != 0) |
| 5263 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5264 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5265 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5266 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5267 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5268 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5269 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5270 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5271 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5272 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5273 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5274 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5275 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5276 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5277 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5278 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5279 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5280 | #endif /* HAVE_PIPE */ |
| 5281 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5282 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5283 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5284 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5285 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5286 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5287 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5288 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5289 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5290 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5291 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5292 | int mode = 0666; |
| 5293 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5294 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5295 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5296 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5297 | res = mkfifo(filename, mode); |
| 5298 | Py_END_ALLOW_THREADS |
| 5299 | if (res < 0) |
| 5300 | return posix_error(); |
| 5301 | Py_INCREF(Py_None); |
| 5302 | return Py_None; |
| 5303 | } |
| 5304 | #endif |
| 5305 | |
| 5306 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5307 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5308 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5309 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5310 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5311 | named filename. mode specifies both the permissions to use and the\n\ |
| 5312 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5313 | 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] | 5314 | device defines the newly created device special file (probably using\n\ |
| 5315 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5316 | |
| 5317 | |
| 5318 | static PyObject * |
| 5319 | posix_mknod(PyObject *self, PyObject *args) |
| 5320 | { |
| 5321 | char *filename; |
| 5322 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5323 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5324 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5325 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5326 | return NULL; |
| 5327 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5328 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5329 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5330 | if (res < 0) |
| 5331 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5332 | Py_INCREF(Py_None); |
| 5333 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5334 | } |
| 5335 | #endif |
| 5336 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5337 | #ifdef HAVE_DEVICE_MACROS |
| 5338 | PyDoc_STRVAR(posix_major__doc__, |
| 5339 | "major(device) -> major number\n\ |
| 5340 | Extracts a device major number from a raw device number."); |
| 5341 | |
| 5342 | static PyObject * |
| 5343 | posix_major(PyObject *self, PyObject *args) |
| 5344 | { |
| 5345 | int device; |
| 5346 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5347 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5348 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
| 5351 | PyDoc_STRVAR(posix_minor__doc__, |
| 5352 | "minor(device) -> minor number\n\ |
| 5353 | Extracts a device minor number from a raw device number."); |
| 5354 | |
| 5355 | static PyObject * |
| 5356 | posix_minor(PyObject *self, PyObject *args) |
| 5357 | { |
| 5358 | int device; |
| 5359 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5360 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5361 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5365 | "makedev(major, minor) -> device number\n\ |
| 5366 | Composes a raw device number from the major and minor device numbers."); |
| 5367 | |
| 5368 | static PyObject * |
| 5369 | posix_makedev(PyObject *self, PyObject *args) |
| 5370 | { |
| 5371 | int major, minor; |
| 5372 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5373 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5374 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5375 | } |
| 5376 | #endif /* device macros */ |
| 5377 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5378 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5379 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5380 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5381 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5382 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5383 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5384 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5385 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5386 | { |
| 5387 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5388 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5389 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5390 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5391 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5392 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5393 | return NULL; |
| 5394 | |
| 5395 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5396 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5397 | #else |
| 5398 | length = PyLong_Check(lenobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5399 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5400 | #endif |
| 5401 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5402 | return NULL; |
| 5403 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5404 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5405 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5406 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 9053d75 | 2009-01-19 17:53:36 +0000 | [diff] [blame] | 5407 | if (res < 0) |
| 5408 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5409 | Py_INCREF(Py_None); |
| 5410 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5411 | } |
| 5412 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5413 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5414 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5415 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5416 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5417 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5418 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5419 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5420 | * get re-set with another call for the same key. */ |
| 5421 | static PyObject *posix_putenv_garbage; |
| 5422 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5423 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5424 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5425 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5426 | #ifdef MS_WINDOWS |
| 5427 | wchar_t *s1, *s2; |
| 5428 | wchar_t *newenv; |
| 5429 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5430 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5431 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5432 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5433 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5434 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5435 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5436 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5437 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5438 | if (!PyArg_ParseTuple(args, |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5439 | "uu:putenv", |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5440 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5441 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5442 | #else |
| 5443 | if (!PyArg_ParseTuple(args, |
| 5444 | "O&O&:putenv", |
| 5445 | PyUnicode_FSConverter, &os1, |
| 5446 | PyUnicode_FSConverter, &os2)) |
| 5447 | return NULL; |
| 5448 | s1 = bytes2str(os1, 1); |
| 5449 | s2 = bytes2str(os2, 1); |
| 5450 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5451 | |
| 5452 | #if defined(PYOS_OS2) |
| 5453 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5454 | APIRET rc; |
| 5455 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5456 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5457 | if (rc != NO_ERROR) |
| 5458 | return os2_error(rc); |
| 5459 | |
| 5460 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5461 | APIRET rc; |
| 5462 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5463 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5464 | if (rc != NO_ERROR) |
| 5465 | return os2_error(rc); |
| 5466 | } else { |
| 5467 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5468 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5469 | /* len includes space for a trailing \0; the size arg to |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5470 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5471 | #ifdef MS_WINDOWS |
| 5472 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5473 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5474 | #else |
| 5475 | len = strlen(s1) + strlen(s2) + 2; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5476 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5477 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5478 | if (newstr == NULL) |
| 5479 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5480 | #ifdef MS_WINDOWS |
| 5481 | newenv = PyUnicode_AsUnicode(newstr); |
| 5482 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5483 | if (_wputenv(newenv)) { |
| 5484 | Py_DECREF(newstr); |
| 5485 | posix_error(); |
| 5486 | return NULL; |
| 5487 | } |
| 5488 | #else |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5489 | newenv = PyBytes_AS_STRING(newstr); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5490 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5491 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5492 | Py_DECREF(newstr); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5493 | release_bytes(os1); |
| 5494 | release_bytes(os2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5495 | posix_error(); |
| 5496 | return NULL; |
| 5497 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5498 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5499 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5500 | * this will cause previous value to be collected. This has to |
| 5501 | * happen after the real putenv() call because the old value |
| 5502 | * was still accessible until then. */ |
| 5503 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5504 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5505 | /* really not much we can do; just leak */ |
| 5506 | PyErr_Clear(); |
| 5507 | } |
| 5508 | else { |
| 5509 | Py_DECREF(newstr); |
| 5510 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5511 | |
| 5512 | #if defined(PYOS_OS2) |
| 5513 | } |
| 5514 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5515 | #ifndef MS_WINDOWS |
| 5516 | release_bytes(os1); |
| 5517 | release_bytes(os2); |
| 5518 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5519 | Py_INCREF(Py_None); |
| 5520 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5521 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5522 | #endif /* putenv */ |
| 5523 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5524 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5525 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5526 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5527 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5528 | |
| 5529 | static PyObject * |
| 5530 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5531 | { |
| 5532 | char *s1; |
| 5533 | |
| 5534 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5535 | return NULL; |
| 5536 | |
| 5537 | unsetenv(s1); |
| 5538 | |
| 5539 | /* Remove the key from posix_putenv_garbage; |
| 5540 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5541 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5542 | * old value was still accessible until then. |
| 5543 | */ |
| 5544 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5545 | PyTuple_GET_ITEM(args, 0))) { |
| 5546 | /* really not much we can do; just leak */ |
| 5547 | PyErr_Clear(); |
| 5548 | } |
| 5549 | |
| 5550 | Py_INCREF(Py_None); |
| 5551 | return Py_None; |
| 5552 | } |
| 5553 | #endif /* unsetenv */ |
| 5554 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5555 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5556 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5557 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5558 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5559 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5560 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5561 | { |
| 5562 | int code; |
| 5563 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5564 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5565 | return NULL; |
| 5566 | message = strerror(code); |
| 5567 | if (message == NULL) { |
| 5568 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5569 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5570 | return NULL; |
| 5571 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5572 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5573 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5574 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5575 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5576 | #ifdef HAVE_SYS_WAIT_H |
| 5577 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5578 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5579 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5580 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5581 | 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] | 5582 | |
| 5583 | static PyObject * |
| 5584 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5585 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5586 | WAIT_TYPE status; |
| 5587 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5588 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5589 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5590 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5591 | |
| 5592 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5593 | } |
| 5594 | #endif /* WCOREDUMP */ |
| 5595 | |
| 5596 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5597 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5598 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5599 | 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] | 5600 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5601 | |
| 5602 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5603 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5604 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5605 | WAIT_TYPE status; |
| 5606 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5607 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5608 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5609 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5610 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5611 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5612 | } |
| 5613 | #endif /* WIFCONTINUED */ |
| 5614 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5615 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5616 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5617 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5618 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5619 | |
| 5620 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5621 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5622 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5623 | WAIT_TYPE status; |
| 5624 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5625 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5626 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5627 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5628 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5629 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5630 | } |
| 5631 | #endif /* WIFSTOPPED */ |
| 5632 | |
| 5633 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5634 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5635 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5636 | 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] | 5637 | |
| 5638 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5639 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5640 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5641 | WAIT_TYPE status; |
| 5642 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5643 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5644 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5645 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5646 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5647 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5648 | } |
| 5649 | #endif /* WIFSIGNALED */ |
| 5650 | |
| 5651 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5652 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5653 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5654 | 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] | 5655 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5656 | |
| 5657 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5658 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5659 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5660 | WAIT_TYPE status; |
| 5661 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5662 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5663 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5664 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5665 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5666 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5667 | } |
| 5668 | #endif /* WIFEXITED */ |
| 5669 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5670 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5671 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5672 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5673 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5674 | |
| 5675 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5676 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5677 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5678 | WAIT_TYPE status; |
| 5679 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5680 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5681 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5682 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5683 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5684 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5685 | } |
| 5686 | #endif /* WEXITSTATUS */ |
| 5687 | |
| 5688 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5689 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5690 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5691 | 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] | 5692 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5693 | |
| 5694 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5695 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5696 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5697 | WAIT_TYPE status; |
| 5698 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5699 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5700 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5701 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5702 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5703 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5704 | } |
| 5705 | #endif /* WTERMSIG */ |
| 5706 | |
| 5707 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5708 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5709 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5710 | Return the signal that stopped the process that provided\n\ |
| 5711 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5712 | |
| 5713 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5714 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5715 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5716 | WAIT_TYPE status; |
| 5717 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5718 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5719 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5720 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5721 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5722 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5723 | } |
| 5724 | #endif /* WSTOPSIG */ |
| 5725 | |
| 5726 | #endif /* HAVE_SYS_WAIT_H */ |
| 5727 | |
| 5728 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5729 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5730 | #ifdef _SCO_DS |
| 5731 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5732 | needed definitions in sys/statvfs.h */ |
| 5733 | #define _SVID3 |
| 5734 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5735 | #include <sys/statvfs.h> |
| 5736 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5737 | static PyObject* |
| 5738 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5739 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5740 | if (v == NULL) |
| 5741 | return NULL; |
| 5742 | |
| 5743 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5744 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5745 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5746 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5747 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5748 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5749 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5750 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5751 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 5752 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5753 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5754 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5755 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5756 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5757 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5758 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5759 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5760 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5761 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5762 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5763 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5764 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5765 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5766 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5767 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5768 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5769 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5770 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5771 | #endif |
| 5772 | |
| 5773 | return v; |
| 5774 | } |
| 5775 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5776 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5777 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5778 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5779 | |
| 5780 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5781 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5782 | { |
| 5783 | int fd, res; |
| 5784 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5785 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5786 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5787 | return NULL; |
| 5788 | Py_BEGIN_ALLOW_THREADS |
| 5789 | res = fstatvfs(fd, &st); |
| 5790 | Py_END_ALLOW_THREADS |
| 5791 | if (res != 0) |
| 5792 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5793 | |
| 5794 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5795 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5796 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5797 | |
| 5798 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5799 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5800 | #include <sys/statvfs.h> |
| 5801 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5802 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5803 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5804 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5805 | |
| 5806 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5807 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5808 | { |
| 5809 | char *path; |
| 5810 | int res; |
| 5811 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5812 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5813 | return NULL; |
| 5814 | Py_BEGIN_ALLOW_THREADS |
| 5815 | res = statvfs(path, &st); |
| 5816 | Py_END_ALLOW_THREADS |
| 5817 | if (res != 0) |
| 5818 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5819 | |
| 5820 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5821 | } |
| 5822 | #endif /* HAVE_STATVFS */ |
| 5823 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5824 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5825 | * It maps strings representing configuration variable names to |
| 5826 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5827 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5828 | * rarely-used constants. There are three separate tables that use |
| 5829 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5830 | * |
| 5831 | * This code is always included, even if none of the interfaces that |
| 5832 | * need it are included. The #if hackery needed to avoid it would be |
| 5833 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5834 | */ |
| 5835 | struct constdef { |
| 5836 | char *name; |
| 5837 | long value; |
| 5838 | }; |
| 5839 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5840 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5841 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5842 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5843 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5844 | if (PyLong_Check(arg)) { |
| 5845 | *valuep = PyLong_AS_LONG(arg); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5846 | return 1; |
| 5847 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5848 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5849 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5850 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5851 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5852 | size_t hi = tablesize; |
| 5853 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5854 | const char *confname; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5855 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5856 | PyErr_SetString(PyExc_TypeError, |
| 5857 | "configuration names must be strings or integers"); |
| 5858 | return 0; |
| 5859 | } |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 5860 | confname = _PyUnicode_AsString(arg); |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5861 | if (confname == NULL) |
| 5862 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5863 | while (lo < hi) { |
| 5864 | mid = (lo + hi) / 2; |
| 5865 | cmp = strcmp(confname, table[mid].name); |
| 5866 | if (cmp < 0) |
| 5867 | hi = mid; |
| 5868 | else if (cmp > 0) |
| 5869 | lo = mid + 1; |
| 5870 | else { |
| 5871 | *valuep = table[mid].value; |
| 5872 | return 1; |
| 5873 | } |
| 5874 | } |
| 5875 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5876 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5877 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | |
| 5881 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5882 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5883 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5884 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5885 | #endif |
| 5886 | #ifdef _PC_ABI_ASYNC_IO |
| 5887 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5888 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5889 | #ifdef _PC_ASYNC_IO |
| 5890 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5891 | #endif |
| 5892 | #ifdef _PC_CHOWN_RESTRICTED |
| 5893 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5894 | #endif |
| 5895 | #ifdef _PC_FILESIZEBITS |
| 5896 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5897 | #endif |
| 5898 | #ifdef _PC_LAST |
| 5899 | {"PC_LAST", _PC_LAST}, |
| 5900 | #endif |
| 5901 | #ifdef _PC_LINK_MAX |
| 5902 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5903 | #endif |
| 5904 | #ifdef _PC_MAX_CANON |
| 5905 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5906 | #endif |
| 5907 | #ifdef _PC_MAX_INPUT |
| 5908 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5909 | #endif |
| 5910 | #ifdef _PC_NAME_MAX |
| 5911 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5912 | #endif |
| 5913 | #ifdef _PC_NO_TRUNC |
| 5914 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5915 | #endif |
| 5916 | #ifdef _PC_PATH_MAX |
| 5917 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5918 | #endif |
| 5919 | #ifdef _PC_PIPE_BUF |
| 5920 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5921 | #endif |
| 5922 | #ifdef _PC_PRIO_IO |
| 5923 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5924 | #endif |
| 5925 | #ifdef _PC_SOCK_MAXBUF |
| 5926 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5927 | #endif |
| 5928 | #ifdef _PC_SYNC_IO |
| 5929 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5930 | #endif |
| 5931 | #ifdef _PC_VDISABLE |
| 5932 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5933 | #endif |
| 5934 | }; |
| 5935 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5936 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5937 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5938 | { |
| 5939 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5940 | sizeof(posix_constants_pathconf) |
| 5941 | / sizeof(struct constdef)); |
| 5942 | } |
| 5943 | #endif |
| 5944 | |
| 5945 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5946 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5947 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5948 | 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] | 5949 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5950 | |
| 5951 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5952 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5953 | { |
| 5954 | PyObject *result = NULL; |
| 5955 | int name, fd; |
| 5956 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5957 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5958 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5959 | long limit; |
| 5960 | |
| 5961 | errno = 0; |
| 5962 | limit = fpathconf(fd, name); |
| 5963 | if (limit == -1 && errno != 0) |
| 5964 | posix_error(); |
| 5965 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5966 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5967 | } |
| 5968 | return result; |
| 5969 | } |
| 5970 | #endif |
| 5971 | |
| 5972 | |
| 5973 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5974 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5975 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5976 | 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] | 5977 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5978 | |
| 5979 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5980 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5981 | { |
| 5982 | PyObject *result = NULL; |
| 5983 | int name; |
| 5984 | char *path; |
| 5985 | |
| 5986 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5987 | conv_path_confname, &name)) { |
| 5988 | long limit; |
| 5989 | |
| 5990 | errno = 0; |
| 5991 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5992 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5993 | if (errno == EINVAL) |
| 5994 | /* could be a path or name problem */ |
| 5995 | posix_error(); |
| 5996 | else |
| 5997 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5998 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5999 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6000 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6001 | } |
| 6002 | return result; |
| 6003 | } |
| 6004 | #endif |
| 6005 | |
| 6006 | #ifdef HAVE_CONFSTR |
| 6007 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6008 | #ifdef _CS_ARCHITECTURE |
| 6009 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6010 | #endif |
| 6011 | #ifdef _CS_HOSTNAME |
| 6012 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6013 | #endif |
| 6014 | #ifdef _CS_HW_PROVIDER |
| 6015 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6016 | #endif |
| 6017 | #ifdef _CS_HW_SERIAL |
| 6018 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6019 | #endif |
| 6020 | #ifdef _CS_INITTAB_NAME |
| 6021 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6022 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6023 | #ifdef _CS_LFS64_CFLAGS |
| 6024 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6025 | #endif |
| 6026 | #ifdef _CS_LFS64_LDFLAGS |
| 6027 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6028 | #endif |
| 6029 | #ifdef _CS_LFS64_LIBS |
| 6030 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6031 | #endif |
| 6032 | #ifdef _CS_LFS64_LINTFLAGS |
| 6033 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6034 | #endif |
| 6035 | #ifdef _CS_LFS_CFLAGS |
| 6036 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6037 | #endif |
| 6038 | #ifdef _CS_LFS_LDFLAGS |
| 6039 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6040 | #endif |
| 6041 | #ifdef _CS_LFS_LIBS |
| 6042 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6043 | #endif |
| 6044 | #ifdef _CS_LFS_LINTFLAGS |
| 6045 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6046 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6047 | #ifdef _CS_MACHINE |
| 6048 | {"CS_MACHINE", _CS_MACHINE}, |
| 6049 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6050 | #ifdef _CS_PATH |
| 6051 | {"CS_PATH", _CS_PATH}, |
| 6052 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6053 | #ifdef _CS_RELEASE |
| 6054 | {"CS_RELEASE", _CS_RELEASE}, |
| 6055 | #endif |
| 6056 | #ifdef _CS_SRPC_DOMAIN |
| 6057 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6058 | #endif |
| 6059 | #ifdef _CS_SYSNAME |
| 6060 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6061 | #endif |
| 6062 | #ifdef _CS_VERSION |
| 6063 | {"CS_VERSION", _CS_VERSION}, |
| 6064 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6065 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6066 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6067 | #endif |
| 6068 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6069 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6070 | #endif |
| 6071 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6072 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6073 | #endif |
| 6074 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6075 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6076 | #endif |
| 6077 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6078 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6079 | #endif |
| 6080 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6081 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6082 | #endif |
| 6083 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6084 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6085 | #endif |
| 6086 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6087 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6088 | #endif |
| 6089 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6090 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6091 | #endif |
| 6092 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6093 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6094 | #endif |
| 6095 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6096 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6097 | #endif |
| 6098 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6099 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6100 | #endif |
| 6101 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6102 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6103 | #endif |
| 6104 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6105 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6106 | #endif |
| 6107 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6108 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6109 | #endif |
| 6110 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6111 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6112 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6113 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6114 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6115 | #endif |
| 6116 | #ifdef _MIPS_CS_BASE |
| 6117 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6118 | #endif |
| 6119 | #ifdef _MIPS_CS_HOSTID |
| 6120 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6121 | #endif |
| 6122 | #ifdef _MIPS_CS_HW_NAME |
| 6123 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6124 | #endif |
| 6125 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6126 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6127 | #endif |
| 6128 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6129 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6130 | #endif |
| 6131 | #ifdef _MIPS_CS_OSREL_MIN |
| 6132 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6133 | #endif |
| 6134 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6135 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6136 | #endif |
| 6137 | #ifdef _MIPS_CS_OS_NAME |
| 6138 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6139 | #endif |
| 6140 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6141 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6142 | #endif |
| 6143 | #ifdef _MIPS_CS_PROCESSORS |
| 6144 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6145 | #endif |
| 6146 | #ifdef _MIPS_CS_SERIAL |
| 6147 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6148 | #endif |
| 6149 | #ifdef _MIPS_CS_VENDOR |
| 6150 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6151 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6152 | }; |
| 6153 | |
| 6154 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6155 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6156 | { |
| 6157 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6158 | sizeof(posix_constants_confstr) |
| 6159 | / sizeof(struct constdef)); |
| 6160 | } |
| 6161 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6162 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6163 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6164 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6165 | |
| 6166 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6167 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6168 | { |
| 6169 | PyObject *result = NULL; |
| 6170 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6171 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6172 | |
| 6173 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6174 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6175 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6176 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6177 | len = confstr(name, buffer, sizeof(buffer)); |
| 6178 | if (len == 0) { |
| 6179 | if (errno) { |
| 6180 | posix_error(); |
| 6181 | } |
| 6182 | else { |
| 6183 | result = Py_None; |
| 6184 | Py_INCREF(Py_None); |
| 6185 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6186 | } |
| 6187 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6188 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6189 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6190 | if (result != NULL) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 6191 | confstr(name, _PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6192 | } |
| 6193 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6194 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6195 | } |
| 6196 | } |
| 6197 | return result; |
| 6198 | } |
| 6199 | #endif |
| 6200 | |
| 6201 | |
| 6202 | #ifdef HAVE_SYSCONF |
| 6203 | static struct constdef posix_constants_sysconf[] = { |
| 6204 | #ifdef _SC_2_CHAR_TERM |
| 6205 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6206 | #endif |
| 6207 | #ifdef _SC_2_C_BIND |
| 6208 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6209 | #endif |
| 6210 | #ifdef _SC_2_C_DEV |
| 6211 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6212 | #endif |
| 6213 | #ifdef _SC_2_C_VERSION |
| 6214 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6215 | #endif |
| 6216 | #ifdef _SC_2_FORT_DEV |
| 6217 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6218 | #endif |
| 6219 | #ifdef _SC_2_FORT_RUN |
| 6220 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6221 | #endif |
| 6222 | #ifdef _SC_2_LOCALEDEF |
| 6223 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6224 | #endif |
| 6225 | #ifdef _SC_2_SW_DEV |
| 6226 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6227 | #endif |
| 6228 | #ifdef _SC_2_UPE |
| 6229 | {"SC_2_UPE", _SC_2_UPE}, |
| 6230 | #endif |
| 6231 | #ifdef _SC_2_VERSION |
| 6232 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6233 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6234 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6235 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6236 | #endif |
| 6237 | #ifdef _SC_ACL |
| 6238 | {"SC_ACL", _SC_ACL}, |
| 6239 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6240 | #ifdef _SC_AIO_LISTIO_MAX |
| 6241 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6242 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6243 | #ifdef _SC_AIO_MAX |
| 6244 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6245 | #endif |
| 6246 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6247 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6248 | #endif |
| 6249 | #ifdef _SC_ARG_MAX |
| 6250 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6251 | #endif |
| 6252 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6253 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6254 | #endif |
| 6255 | #ifdef _SC_ATEXIT_MAX |
| 6256 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6257 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6258 | #ifdef _SC_AUDIT |
| 6259 | {"SC_AUDIT", _SC_AUDIT}, |
| 6260 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6261 | #ifdef _SC_AVPHYS_PAGES |
| 6262 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6263 | #endif |
| 6264 | #ifdef _SC_BC_BASE_MAX |
| 6265 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6266 | #endif |
| 6267 | #ifdef _SC_BC_DIM_MAX |
| 6268 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6269 | #endif |
| 6270 | #ifdef _SC_BC_SCALE_MAX |
| 6271 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6272 | #endif |
| 6273 | #ifdef _SC_BC_STRING_MAX |
| 6274 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6275 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6276 | #ifdef _SC_CAP |
| 6277 | {"SC_CAP", _SC_CAP}, |
| 6278 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6279 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6280 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6281 | #endif |
| 6282 | #ifdef _SC_CHAR_BIT |
| 6283 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6284 | #endif |
| 6285 | #ifdef _SC_CHAR_MAX |
| 6286 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6287 | #endif |
| 6288 | #ifdef _SC_CHAR_MIN |
| 6289 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6290 | #endif |
| 6291 | #ifdef _SC_CHILD_MAX |
| 6292 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6293 | #endif |
| 6294 | #ifdef _SC_CLK_TCK |
| 6295 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6296 | #endif |
| 6297 | #ifdef _SC_COHER_BLKSZ |
| 6298 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6299 | #endif |
| 6300 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6301 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6302 | #endif |
| 6303 | #ifdef _SC_DCACHE_ASSOC |
| 6304 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6305 | #endif |
| 6306 | #ifdef _SC_DCACHE_BLKSZ |
| 6307 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6308 | #endif |
| 6309 | #ifdef _SC_DCACHE_LINESZ |
| 6310 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6311 | #endif |
| 6312 | #ifdef _SC_DCACHE_SZ |
| 6313 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6314 | #endif |
| 6315 | #ifdef _SC_DCACHE_TBLKSZ |
| 6316 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6317 | #endif |
| 6318 | #ifdef _SC_DELAYTIMER_MAX |
| 6319 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6320 | #endif |
| 6321 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6322 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6323 | #endif |
| 6324 | #ifdef _SC_EXPR_NEST_MAX |
| 6325 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6326 | #endif |
| 6327 | #ifdef _SC_FSYNC |
| 6328 | {"SC_FSYNC", _SC_FSYNC}, |
| 6329 | #endif |
| 6330 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6331 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6332 | #endif |
| 6333 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6334 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6335 | #endif |
| 6336 | #ifdef _SC_ICACHE_ASSOC |
| 6337 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6338 | #endif |
| 6339 | #ifdef _SC_ICACHE_BLKSZ |
| 6340 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6341 | #endif |
| 6342 | #ifdef _SC_ICACHE_LINESZ |
| 6343 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6344 | #endif |
| 6345 | #ifdef _SC_ICACHE_SZ |
| 6346 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6347 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6348 | #ifdef _SC_INF |
| 6349 | {"SC_INF", _SC_INF}, |
| 6350 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6351 | #ifdef _SC_INT_MAX |
| 6352 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6353 | #endif |
| 6354 | #ifdef _SC_INT_MIN |
| 6355 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6356 | #endif |
| 6357 | #ifdef _SC_IOV_MAX |
| 6358 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6359 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6360 | #ifdef _SC_IP_SECOPTS |
| 6361 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6362 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6363 | #ifdef _SC_JOB_CONTROL |
| 6364 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6365 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6366 | #ifdef _SC_KERN_POINTERS |
| 6367 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6368 | #endif |
| 6369 | #ifdef _SC_KERN_SIM |
| 6370 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6371 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6372 | #ifdef _SC_LINE_MAX |
| 6373 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6374 | #endif |
| 6375 | #ifdef _SC_LOGIN_NAME_MAX |
| 6376 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6377 | #endif |
| 6378 | #ifdef _SC_LOGNAME_MAX |
| 6379 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6380 | #endif |
| 6381 | #ifdef _SC_LONG_BIT |
| 6382 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6383 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6384 | #ifdef _SC_MAC |
| 6385 | {"SC_MAC", _SC_MAC}, |
| 6386 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6387 | #ifdef _SC_MAPPED_FILES |
| 6388 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6389 | #endif |
| 6390 | #ifdef _SC_MAXPID |
| 6391 | {"SC_MAXPID", _SC_MAXPID}, |
| 6392 | #endif |
| 6393 | #ifdef _SC_MB_LEN_MAX |
| 6394 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6395 | #endif |
| 6396 | #ifdef _SC_MEMLOCK |
| 6397 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6398 | #endif |
| 6399 | #ifdef _SC_MEMLOCK_RANGE |
| 6400 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6401 | #endif |
| 6402 | #ifdef _SC_MEMORY_PROTECTION |
| 6403 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6404 | #endif |
| 6405 | #ifdef _SC_MESSAGE_PASSING |
| 6406 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6407 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6408 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6409 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6410 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6411 | #ifdef _SC_MQ_OPEN_MAX |
| 6412 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6413 | #endif |
| 6414 | #ifdef _SC_MQ_PRIO_MAX |
| 6415 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6416 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6417 | #ifdef _SC_NACLS_MAX |
| 6418 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6419 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6420 | #ifdef _SC_NGROUPS_MAX |
| 6421 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6422 | #endif |
| 6423 | #ifdef _SC_NL_ARGMAX |
| 6424 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6425 | #endif |
| 6426 | #ifdef _SC_NL_LANGMAX |
| 6427 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6428 | #endif |
| 6429 | #ifdef _SC_NL_MSGMAX |
| 6430 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6431 | #endif |
| 6432 | #ifdef _SC_NL_NMAX |
| 6433 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6434 | #endif |
| 6435 | #ifdef _SC_NL_SETMAX |
| 6436 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6437 | #endif |
| 6438 | #ifdef _SC_NL_TEXTMAX |
| 6439 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6440 | #endif |
| 6441 | #ifdef _SC_NPROCESSORS_CONF |
| 6442 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6443 | #endif |
| 6444 | #ifdef _SC_NPROCESSORS_ONLN |
| 6445 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6446 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6447 | #ifdef _SC_NPROC_CONF |
| 6448 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6449 | #endif |
| 6450 | #ifdef _SC_NPROC_ONLN |
| 6451 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6452 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6453 | #ifdef _SC_NZERO |
| 6454 | {"SC_NZERO", _SC_NZERO}, |
| 6455 | #endif |
| 6456 | #ifdef _SC_OPEN_MAX |
| 6457 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6458 | #endif |
| 6459 | #ifdef _SC_PAGESIZE |
| 6460 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6461 | #endif |
| 6462 | #ifdef _SC_PAGE_SIZE |
| 6463 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6464 | #endif |
| 6465 | #ifdef _SC_PASS_MAX |
| 6466 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6467 | #endif |
| 6468 | #ifdef _SC_PHYS_PAGES |
| 6469 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6470 | #endif |
| 6471 | #ifdef _SC_PII |
| 6472 | {"SC_PII", _SC_PII}, |
| 6473 | #endif |
| 6474 | #ifdef _SC_PII_INTERNET |
| 6475 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6476 | #endif |
| 6477 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6478 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6479 | #endif |
| 6480 | #ifdef _SC_PII_INTERNET_STREAM |
| 6481 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6482 | #endif |
| 6483 | #ifdef _SC_PII_OSI |
| 6484 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6485 | #endif |
| 6486 | #ifdef _SC_PII_OSI_CLTS |
| 6487 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6488 | #endif |
| 6489 | #ifdef _SC_PII_OSI_COTS |
| 6490 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6491 | #endif |
| 6492 | #ifdef _SC_PII_OSI_M |
| 6493 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6494 | #endif |
| 6495 | #ifdef _SC_PII_SOCKET |
| 6496 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6497 | #endif |
| 6498 | #ifdef _SC_PII_XTI |
| 6499 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6500 | #endif |
| 6501 | #ifdef _SC_POLL |
| 6502 | {"SC_POLL", _SC_POLL}, |
| 6503 | #endif |
| 6504 | #ifdef _SC_PRIORITIZED_IO |
| 6505 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6506 | #endif |
| 6507 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6508 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6509 | #endif |
| 6510 | #ifdef _SC_REALTIME_SIGNALS |
| 6511 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6512 | #endif |
| 6513 | #ifdef _SC_RE_DUP_MAX |
| 6514 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6515 | #endif |
| 6516 | #ifdef _SC_RTSIG_MAX |
| 6517 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6518 | #endif |
| 6519 | #ifdef _SC_SAVED_IDS |
| 6520 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6521 | #endif |
| 6522 | #ifdef _SC_SCHAR_MAX |
| 6523 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6524 | #endif |
| 6525 | #ifdef _SC_SCHAR_MIN |
| 6526 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6527 | #endif |
| 6528 | #ifdef _SC_SELECT |
| 6529 | {"SC_SELECT", _SC_SELECT}, |
| 6530 | #endif |
| 6531 | #ifdef _SC_SEMAPHORES |
| 6532 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6533 | #endif |
| 6534 | #ifdef _SC_SEM_NSEMS_MAX |
| 6535 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6536 | #endif |
| 6537 | #ifdef _SC_SEM_VALUE_MAX |
| 6538 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6539 | #endif |
| 6540 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6541 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6542 | #endif |
| 6543 | #ifdef _SC_SHRT_MAX |
| 6544 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6545 | #endif |
| 6546 | #ifdef _SC_SHRT_MIN |
| 6547 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6548 | #endif |
| 6549 | #ifdef _SC_SIGQUEUE_MAX |
| 6550 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6551 | #endif |
| 6552 | #ifdef _SC_SIGRT_MAX |
| 6553 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6554 | #endif |
| 6555 | #ifdef _SC_SIGRT_MIN |
| 6556 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6557 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6558 | #ifdef _SC_SOFTPOWER |
| 6559 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6560 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6561 | #ifdef _SC_SPLIT_CACHE |
| 6562 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6563 | #endif |
| 6564 | #ifdef _SC_SSIZE_MAX |
| 6565 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6566 | #endif |
| 6567 | #ifdef _SC_STACK_PROT |
| 6568 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6569 | #endif |
| 6570 | #ifdef _SC_STREAM_MAX |
| 6571 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6572 | #endif |
| 6573 | #ifdef _SC_SYNCHRONIZED_IO |
| 6574 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6575 | #endif |
| 6576 | #ifdef _SC_THREADS |
| 6577 | {"SC_THREADS", _SC_THREADS}, |
| 6578 | #endif |
| 6579 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6580 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6581 | #endif |
| 6582 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6583 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6584 | #endif |
| 6585 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6586 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6587 | #endif |
| 6588 | #ifdef _SC_THREAD_KEYS_MAX |
| 6589 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6590 | #endif |
| 6591 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6592 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6593 | #endif |
| 6594 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6595 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6596 | #endif |
| 6597 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6598 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6599 | #endif |
| 6600 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6601 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6602 | #endif |
| 6603 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6604 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6605 | #endif |
| 6606 | #ifdef _SC_THREAD_STACK_MIN |
| 6607 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6608 | #endif |
| 6609 | #ifdef _SC_THREAD_THREADS_MAX |
| 6610 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6611 | #endif |
| 6612 | #ifdef _SC_TIMERS |
| 6613 | {"SC_TIMERS", _SC_TIMERS}, |
| 6614 | #endif |
| 6615 | #ifdef _SC_TIMER_MAX |
| 6616 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6617 | #endif |
| 6618 | #ifdef _SC_TTY_NAME_MAX |
| 6619 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6620 | #endif |
| 6621 | #ifdef _SC_TZNAME_MAX |
| 6622 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6623 | #endif |
| 6624 | #ifdef _SC_T_IOV_MAX |
| 6625 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6626 | #endif |
| 6627 | #ifdef _SC_UCHAR_MAX |
| 6628 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6629 | #endif |
| 6630 | #ifdef _SC_UINT_MAX |
| 6631 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6632 | #endif |
| 6633 | #ifdef _SC_UIO_MAXIOV |
| 6634 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6635 | #endif |
| 6636 | #ifdef _SC_ULONG_MAX |
| 6637 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6638 | #endif |
| 6639 | #ifdef _SC_USHRT_MAX |
| 6640 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6641 | #endif |
| 6642 | #ifdef _SC_VERSION |
| 6643 | {"SC_VERSION", _SC_VERSION}, |
| 6644 | #endif |
| 6645 | #ifdef _SC_WORD_BIT |
| 6646 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6647 | #endif |
| 6648 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6649 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6650 | #endif |
| 6651 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6652 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6653 | #endif |
| 6654 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6655 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6656 | #endif |
| 6657 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6658 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6659 | #endif |
| 6660 | #ifdef _SC_XOPEN_CRYPT |
| 6661 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6662 | #endif |
| 6663 | #ifdef _SC_XOPEN_ENH_I18N |
| 6664 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6665 | #endif |
| 6666 | #ifdef _SC_XOPEN_LEGACY |
| 6667 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6668 | #endif |
| 6669 | #ifdef _SC_XOPEN_REALTIME |
| 6670 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6671 | #endif |
| 6672 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6673 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6674 | #endif |
| 6675 | #ifdef _SC_XOPEN_SHM |
| 6676 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6677 | #endif |
| 6678 | #ifdef _SC_XOPEN_UNIX |
| 6679 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6680 | #endif |
| 6681 | #ifdef _SC_XOPEN_VERSION |
| 6682 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6683 | #endif |
| 6684 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6685 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6686 | #endif |
| 6687 | #ifdef _SC_XOPEN_XPG2 |
| 6688 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6689 | #endif |
| 6690 | #ifdef _SC_XOPEN_XPG3 |
| 6691 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6692 | #endif |
| 6693 | #ifdef _SC_XOPEN_XPG4 |
| 6694 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6695 | #endif |
| 6696 | }; |
| 6697 | |
| 6698 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6699 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6700 | { |
| 6701 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6702 | sizeof(posix_constants_sysconf) |
| 6703 | / sizeof(struct constdef)); |
| 6704 | } |
| 6705 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6706 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6707 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6708 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6709 | |
| 6710 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6711 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6712 | { |
| 6713 | PyObject *result = NULL; |
| 6714 | int name; |
| 6715 | |
| 6716 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6717 | int value; |
| 6718 | |
| 6719 | errno = 0; |
| 6720 | value = sysconf(name); |
| 6721 | if (value == -1 && errno != 0) |
| 6722 | posix_error(); |
| 6723 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6724 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6725 | } |
| 6726 | return result; |
| 6727 | } |
| 6728 | #endif |
| 6729 | |
| 6730 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6731 | /* This code is used to ensure that the tables of configuration value names |
| 6732 | * are in sorted order as required by conv_confname(), and also to build the |
| 6733 | * the exported dictionaries that are used to publish information about the |
| 6734 | * names available on the host platform. |
| 6735 | * |
| 6736 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6737 | * when used, even for platforms we're not able to test on. It also makes |
| 6738 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6739 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6740 | |
| 6741 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6742 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6743 | { |
| 6744 | const struct constdef *c1 = |
| 6745 | (const struct constdef *) v1; |
| 6746 | const struct constdef *c2 = |
| 6747 | (const struct constdef *) v2; |
| 6748 | |
| 6749 | return strcmp(c1->name, c2->name); |
| 6750 | } |
| 6751 | |
| 6752 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6753 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6754 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6755 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6756 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6757 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6758 | |
| 6759 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6760 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6761 | if (d == NULL) |
| 6762 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6763 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6764 | for (i=0; i < tablesize; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6765 | PyObject *o = PyLong_FromLong(table[i].value); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6766 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6767 | Py_XDECREF(o); |
| 6768 | Py_DECREF(d); |
| 6769 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6770 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6771 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6772 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6773 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6776 | /* Return -1 on failure, 0 on success. */ |
| 6777 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6778 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6779 | { |
| 6780 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6781 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6782 | sizeof(posix_constants_pathconf) |
| 6783 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6784 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6785 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6786 | #endif |
| 6787 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6788 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6789 | sizeof(posix_constants_confstr) |
| 6790 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6791 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6792 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6793 | #endif |
| 6794 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6795 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6796 | sizeof(posix_constants_sysconf) |
| 6797 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6798 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6799 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6800 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6801 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6802 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6803 | |
| 6804 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6805 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6806 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6807 | 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] | 6808 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6809 | |
| 6810 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6811 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6812 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6813 | abort(); |
| 6814 | /*NOTREACHED*/ |
| 6815 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6816 | return NULL; |
| 6817 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6818 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6819 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6820 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6821 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6822 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6823 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6824 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6825 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6826 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6827 | application (if any) its extension is associated.\n\ |
| 6828 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6829 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6830 | \n\ |
| 6831 | startfile returns as soon as the associated application is launched.\n\ |
| 6832 | There is no option to wait for the application to close, and no way\n\ |
| 6833 | to retrieve the application's exit status.\n\ |
| 6834 | \n\ |
| 6835 | The filepath is relative to the current directory. If you want to use\n\ |
| 6836 | 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] | 6837 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6838 | |
| 6839 | static PyObject * |
| 6840 | win32_startfile(PyObject *self, PyObject *args) |
| 6841 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6842 | PyObject *ofilepath; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6843 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6844 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6845 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 6846 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6847 | if (unicode_file_names()) { |
| 6848 | PyObject *unipath, *woperation = NULL; |
| 6849 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6850 | &unipath, &operation)) { |
| 6851 | PyErr_Clear(); |
| 6852 | goto normal; |
| 6853 | } |
| 6854 | |
| 6855 | |
| 6856 | if (operation) { |
| 6857 | woperation = PyUnicode_DecodeASCII(operation, |
| 6858 | strlen(operation), NULL); |
| 6859 | if (!woperation) { |
| 6860 | PyErr_Clear(); |
| 6861 | operation = NULL; |
| 6862 | goto normal; |
| 6863 | } |
| 6864 | } |
| 6865 | |
| 6866 | Py_BEGIN_ALLOW_THREADS |
| 6867 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6868 | PyUnicode_AS_UNICODE(unipath), |
| 6869 | NULL, NULL, SW_SHOWNORMAL); |
| 6870 | Py_END_ALLOW_THREADS |
| 6871 | |
| 6872 | Py_XDECREF(woperation); |
| 6873 | if (rc <= (HINSTANCE)32) { |
| 6874 | PyObject *errval = win32_error_unicode("startfile", |
| 6875 | PyUnicode_AS_UNICODE(unipath)); |
| 6876 | return errval; |
| 6877 | } |
| 6878 | Py_INCREF(Py_None); |
| 6879 | return Py_None; |
| 6880 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6881 | |
| 6882 | normal: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6883 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 6884 | PyUnicode_FSConverter, &ofilepath, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6885 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6886 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6887 | filepath = bytes2str(ofilepath, 1); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6888 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6889 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6890 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6891 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6892 | if (rc <= (HINSTANCE)32) { |
| 6893 | PyObject *errval = win32_error("startfile", filepath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6894 | release_bytes(ofilepath); |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6895 | return errval; |
| 6896 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6897 | release_bytes(ofilepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6898 | Py_INCREF(Py_None); |
| 6899 | return Py_None; |
| 6900 | } |
| 6901 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6902 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6903 | #ifdef HAVE_GETLOADAVG |
| 6904 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6905 | "getloadavg() -> (float, float, float)\n\n\ |
| 6906 | Return the number of processes in the system run queue averaged over\n\ |
| 6907 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6908 | was unobtainable"); |
| 6909 | |
| 6910 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6911 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6912 | { |
| 6913 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6914 | if (getloadavg(loadavg, 3)!=3) { |
| 6915 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6916 | return NULL; |
| 6917 | } else |
| 6918 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6919 | } |
| 6920 | #endif |
| 6921 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6922 | #ifdef MS_WINDOWS |
| 6923 | |
| 6924 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6925 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6926 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6927 | |
| 6928 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6929 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6930 | DWORD dwFlags ); |
| 6931 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6932 | BYTE *pbBuffer ); |
| 6933 | |
| 6934 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6935 | /* This handle is never explicitly released. Instead, the operating |
| 6936 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6937 | static HCRYPTPROV hCryptProv = 0; |
| 6938 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6939 | static PyObject* |
| 6940 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6941 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6942 | int howMany; |
| 6943 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6944 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6945 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6946 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6947 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6948 | if (howMany < 0) |
| 6949 | return PyErr_Format(PyExc_ValueError, |
| 6950 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6951 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6952 | if (hCryptProv == 0) { |
| 6953 | HINSTANCE hAdvAPI32 = NULL; |
| 6954 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6955 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6956 | /* Obtain handle to the DLL containing CryptoAPI |
| 6957 | This should not fail */ |
| 6958 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6959 | if(hAdvAPI32 == NULL) |
| 6960 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6961 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6962 | /* Obtain pointers to the CryptoAPI functions |
| 6963 | This will fail on some early versions of Win95 */ |
| 6964 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6965 | hAdvAPI32, |
| 6966 | "CryptAcquireContextA"); |
| 6967 | if (pCryptAcquireContext == NULL) |
| 6968 | return PyErr_Format(PyExc_NotImplementedError, |
| 6969 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6970 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6971 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6972 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6973 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6974 | return PyErr_Format(PyExc_NotImplementedError, |
| 6975 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6976 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6977 | /* Acquire context */ |
| 6978 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6979 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6980 | return win32_error("CryptAcquireContext", NULL); |
| 6981 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6982 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6983 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6984 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6985 | if (result != NULL) { |
| 6986 | /* Get random data */ |
Amaury Forgeot d'Arc | a05ada3 | 2008-07-21 21:13:14 +0000 | [diff] [blame] | 6987 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6988 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6989 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6990 | Py_DECREF(result); |
| 6991 | return win32_error("CryptGenRandom", NULL); |
| 6992 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6993 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6994 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6995 | } |
| 6996 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6997 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6998 | PyDoc_STRVAR(device_encoding__doc__, |
| 6999 | "device_encoding(fd) -> str\n\n\ |
| 7000 | Return a string describing the encoding of the device\n\ |
| 7001 | if the output is a terminal; else return None."); |
| 7002 | |
| 7003 | static PyObject * |
| 7004 | device_encoding(PyObject *self, PyObject *args) |
| 7005 | { |
| 7006 | int fd; |
| 7007 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 7008 | return NULL; |
Kristján Valur Jónsson | 649170b | 2009-03-24 14:15:49 +0000 | [diff] [blame] | 7009 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7010 | Py_INCREF(Py_None); |
| 7011 | return Py_None; |
| 7012 | } |
| 7013 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 7014 | if (fd == 0) { |
| 7015 | char buf[100]; |
| 7016 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 7017 | return PyUnicode_FromString(buf); |
| 7018 | } |
| 7019 | if (fd == 1 || fd == 2) { |
| 7020 | char buf[100]; |
| 7021 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 7022 | return PyUnicode_FromString(buf); |
| 7023 | } |
| 7024 | #elif defined(CODESET) |
| 7025 | { |
| 7026 | char *codeset = nl_langinfo(CODESET); |
Mark Dickinson | da2706b | 2008-12-11 18:03:03 +0000 | [diff] [blame] | 7027 | if (codeset != NULL && codeset[0] != 0) |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7028 | return PyUnicode_FromString(codeset); |
| 7029 | } |
| 7030 | #endif |
| 7031 | Py_INCREF(Py_None); |
| 7032 | return Py_None; |
| 7033 | } |
| 7034 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7035 | #ifdef __VMS |
| 7036 | /* Use openssl random routine */ |
| 7037 | #include <openssl/rand.h> |
| 7038 | PyDoc_STRVAR(vms_urandom__doc__, |
| 7039 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 7040 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7041 | |
| 7042 | static PyObject* |
| 7043 | vms_urandom(PyObject *self, PyObject *args) |
| 7044 | { |
| 7045 | int howMany; |
| 7046 | PyObject* result; |
| 7047 | |
| 7048 | /* Read arguments */ |
| 7049 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7050 | return NULL; |
| 7051 | if (howMany < 0) |
| 7052 | return PyErr_Format(PyExc_ValueError, |
| 7053 | "negative argument not allowed"); |
| 7054 | |
| 7055 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7056 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7057 | if (result != NULL) { |
| 7058 | /* Get random data */ |
| 7059 | if (RAND_pseudo_bytes((unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7060 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7061 | howMany) < 0) { |
| 7062 | Py_DECREF(result); |
| 7063 | return PyErr_Format(PyExc_ValueError, |
| 7064 | "RAND_pseudo_bytes"); |
| 7065 | } |
| 7066 | } |
| 7067 | return result; |
| 7068 | } |
| 7069 | #endif |
| 7070 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7071 | static PyMethodDef posix_methods[] = { |
| 7072 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7073 | #ifdef HAVE_TTYNAME |
| 7074 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7075 | #endif |
| 7076 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7077 | #ifdef HAVE_CHFLAGS |
| 7078 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 7079 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7080 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7081 | #ifdef HAVE_FCHMOD |
| 7082 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
| 7083 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7084 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7085 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7086 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7087 | #ifdef HAVE_LCHMOD |
| 7088 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
| 7089 | #endif /* HAVE_LCHMOD */ |
| 7090 | #ifdef HAVE_FCHOWN |
| 7091 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
| 7092 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7093 | #ifdef HAVE_LCHFLAGS |
| 7094 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 7095 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7096 | #ifdef HAVE_LCHOWN |
| 7097 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7098 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7099 | #ifdef HAVE_CHROOT |
| 7100 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7101 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7102 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7103 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7104 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7105 | #ifdef HAVE_GETCWD |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 7106 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 7107 | METH_NOARGS, posix_getcwd__doc__}, |
| 7108 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 7109 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7110 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7111 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7112 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7113 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7114 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7115 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7116 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7117 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7118 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7119 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7120 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7121 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7122 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7123 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7124 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7125 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7126 | {"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] | 7127 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7128 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7129 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7130 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7131 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7132 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7133 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7134 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7135 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7136 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7137 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7138 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7139 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7140 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7141 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7142 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7143 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7144 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7145 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7146 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7147 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7148 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7149 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7150 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7151 | #if defined(PYOS_OS2) |
| 7152 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7153 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7154 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7155 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7156 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7157 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7158 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7159 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7160 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7161 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7162 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7163 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7164 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7165 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7166 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7167 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7168 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7169 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7170 | #endif /* HAVE_GETEGID */ |
| 7171 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7172 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7173 | #endif /* HAVE_GETEUID */ |
| 7174 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7175 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7176 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7177 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7178 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7179 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7180 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7181 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7182 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7183 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7184 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7185 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7186 | #endif /* HAVE_GETPPID */ |
| 7187 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7188 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7189 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7190 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7191 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7192 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7193 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7194 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7195 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7196 | #ifdef HAVE_KILLPG |
| 7197 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7198 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7199 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7200 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7201 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7202 | #ifdef MS_WINDOWS |
| 7203 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 7204 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7205 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7206 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7207 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7208 | #ifdef HAVE_SETEUID |
| 7209 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7210 | #endif /* HAVE_SETEUID */ |
| 7211 | #ifdef HAVE_SETEGID |
| 7212 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7213 | #endif /* HAVE_SETEGID */ |
| 7214 | #ifdef HAVE_SETREUID |
| 7215 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7216 | #endif /* HAVE_SETREUID */ |
| 7217 | #ifdef HAVE_SETREGID |
| 7218 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7219 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7220 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7221 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7222 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7223 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 7224 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7225 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7226 | #ifdef HAVE_GETPGID |
| 7227 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7228 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7229 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7230 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7231 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7232 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7233 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7234 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7235 | #ifdef HAVE_WAIT3 |
| 7236 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7237 | #endif /* HAVE_WAIT3 */ |
| 7238 | #ifdef HAVE_WAIT4 |
| 7239 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7240 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7241 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7242 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7243 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7244 | #ifdef HAVE_GETSID |
| 7245 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7246 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7247 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7248 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7249 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7250 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7251 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7252 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7253 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7254 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7255 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7256 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7257 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7258 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7259 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7260 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7261 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7262 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7263 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7264 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7265 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7266 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7267 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7268 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7269 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7270 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7271 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7272 | #endif |
| 7273 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7274 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7275 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7276 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7277 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7278 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7279 | #ifdef HAVE_DEVICE_MACROS |
| 7280 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7281 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7282 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7283 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7284 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7285 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7286 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7287 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7288 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7289 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7290 | #ifdef HAVE_UNSETENV |
| 7291 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7292 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7293 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7294 | #ifdef HAVE_FCHDIR |
| 7295 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7296 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7297 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7298 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7299 | #endif |
| 7300 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7301 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7302 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7303 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7304 | #ifdef WCOREDUMP |
| 7305 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7306 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7307 | #ifdef WIFCONTINUED |
| 7308 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7309 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7310 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7311 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7312 | #endif /* WIFSTOPPED */ |
| 7313 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7314 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7315 | #endif /* WIFSIGNALED */ |
| 7316 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7317 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7318 | #endif /* WIFEXITED */ |
| 7319 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7320 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7321 | #endif /* WEXITSTATUS */ |
| 7322 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7323 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7324 | #endif /* WTERMSIG */ |
| 7325 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7326 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7327 | #endif /* WSTOPSIG */ |
| 7328 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7329 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7330 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7331 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7332 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7333 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7334 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7335 | #ifdef HAVE_CONFSTR |
| 7336 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7337 | #endif |
| 7338 | #ifdef HAVE_SYSCONF |
| 7339 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7340 | #endif |
| 7341 | #ifdef HAVE_FPATHCONF |
| 7342 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7343 | #endif |
| 7344 | #ifdef HAVE_PATHCONF |
| 7345 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7346 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7347 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7348 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7349 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7350 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7351 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7352 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7353 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7354 | #ifdef MS_WINDOWS |
| 7355 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7356 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7357 | #ifdef __VMS |
| 7358 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 7359 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7360 | {NULL, NULL} /* Sentinel */ |
| 7361 | }; |
| 7362 | |
| 7363 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7364 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7365 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7366 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7367 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7368 | } |
| 7369 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7370 | #if defined(PYOS_OS2) |
| 7371 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7372 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7373 | { |
| 7374 | APIRET rc; |
| 7375 | ULONG values[QSV_MAX+1]; |
| 7376 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7377 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7378 | |
| 7379 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7380 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7381 | Py_END_ALLOW_THREADS |
| 7382 | |
| 7383 | if (rc != NO_ERROR) { |
| 7384 | os2_error(rc); |
| 7385 | return -1; |
| 7386 | } |
| 7387 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7388 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7389 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7390 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7391 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7392 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7393 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7394 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7395 | |
| 7396 | switch (values[QSV_VERSION_MINOR]) { |
| 7397 | case 0: ver = "2.00"; break; |
| 7398 | case 10: ver = "2.10"; break; |
| 7399 | case 11: ver = "2.11"; break; |
| 7400 | case 30: ver = "3.00"; break; |
| 7401 | case 40: ver = "4.00"; break; |
| 7402 | case 50: ver = "5.00"; break; |
| 7403 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7404 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7405 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7406 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7407 | ver = &tmp[0]; |
| 7408 | } |
| 7409 | |
| 7410 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7411 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7412 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7413 | |
| 7414 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7415 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7416 | tmp[1] = ':'; |
| 7417 | tmp[2] = '\0'; |
| 7418 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7419 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7420 | } |
| 7421 | #endif |
| 7422 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7423 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7424 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7425 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7426 | #ifdef F_OK |
| 7427 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7428 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7429 | #ifdef R_OK |
| 7430 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7431 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7432 | #ifdef W_OK |
| 7433 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7434 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7435 | #ifdef X_OK |
| 7436 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7437 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7438 | #ifdef NGROUPS_MAX |
| 7439 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7440 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7441 | #ifdef TMP_MAX |
| 7442 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7443 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7444 | #ifdef WCONTINUED |
| 7445 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7446 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7447 | #ifdef WNOHANG |
| 7448 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7449 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7450 | #ifdef WUNTRACED |
| 7451 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7452 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7453 | #ifdef O_RDONLY |
| 7454 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7455 | #endif |
| 7456 | #ifdef O_WRONLY |
| 7457 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7458 | #endif |
| 7459 | #ifdef O_RDWR |
| 7460 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7461 | #endif |
| 7462 | #ifdef O_NDELAY |
| 7463 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7464 | #endif |
| 7465 | #ifdef O_NONBLOCK |
| 7466 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7467 | #endif |
| 7468 | #ifdef O_APPEND |
| 7469 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7470 | #endif |
| 7471 | #ifdef O_DSYNC |
| 7472 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7473 | #endif |
| 7474 | #ifdef O_RSYNC |
| 7475 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7476 | #endif |
| 7477 | #ifdef O_SYNC |
| 7478 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7479 | #endif |
| 7480 | #ifdef O_NOCTTY |
| 7481 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7482 | #endif |
| 7483 | #ifdef O_CREAT |
| 7484 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7485 | #endif |
| 7486 | #ifdef O_EXCL |
| 7487 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7488 | #endif |
| 7489 | #ifdef O_TRUNC |
| 7490 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7491 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7492 | #ifdef O_BINARY |
| 7493 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7494 | #endif |
| 7495 | #ifdef O_TEXT |
| 7496 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7497 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7498 | #ifdef O_LARGEFILE |
| 7499 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7500 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7501 | #ifdef O_SHLOCK |
| 7502 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7503 | #endif |
| 7504 | #ifdef O_EXLOCK |
| 7505 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7506 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7507 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7508 | /* MS Windows */ |
| 7509 | #ifdef O_NOINHERIT |
| 7510 | /* Don't inherit in child processes. */ |
| 7511 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7512 | #endif |
| 7513 | #ifdef _O_SHORT_LIVED |
| 7514 | /* Optimize for short life (keep in memory). */ |
| 7515 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7516 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7517 | #endif |
| 7518 | #ifdef O_TEMPORARY |
| 7519 | /* Automatically delete when last handle is closed. */ |
| 7520 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7521 | #endif |
| 7522 | #ifdef O_RANDOM |
| 7523 | /* Optimize for random access. */ |
| 7524 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7525 | #endif |
| 7526 | #ifdef O_SEQUENTIAL |
| 7527 | /* Optimize for sequential access. */ |
| 7528 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7529 | #endif |
| 7530 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7531 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7532 | #ifdef O_ASYNC |
| 7533 | /* Send a SIGIO signal whenever input or output |
| 7534 | becomes available on file descriptor */ |
| 7535 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
| 7536 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7537 | #ifdef O_DIRECT |
| 7538 | /* Direct disk access. */ |
| 7539 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7540 | #endif |
| 7541 | #ifdef O_DIRECTORY |
| 7542 | /* Must be a directory. */ |
| 7543 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7544 | #endif |
| 7545 | #ifdef O_NOFOLLOW |
| 7546 | /* Do not follow links. */ |
| 7547 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7548 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7549 | #ifdef O_NOATIME |
| 7550 | /* Do not update the access time. */ |
| 7551 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
| 7552 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7553 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7554 | /* These come from sysexits.h */ |
| 7555 | #ifdef EX_OK |
| 7556 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7557 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7558 | #ifdef EX_USAGE |
| 7559 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7560 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7561 | #ifdef EX_DATAERR |
| 7562 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7563 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7564 | #ifdef EX_NOINPUT |
| 7565 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7566 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7567 | #ifdef EX_NOUSER |
| 7568 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7569 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7570 | #ifdef EX_NOHOST |
| 7571 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7572 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7573 | #ifdef EX_UNAVAILABLE |
| 7574 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7575 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7576 | #ifdef EX_SOFTWARE |
| 7577 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7578 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7579 | #ifdef EX_OSERR |
| 7580 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7581 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7582 | #ifdef EX_OSFILE |
| 7583 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7584 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7585 | #ifdef EX_CANTCREAT |
| 7586 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7587 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7588 | #ifdef EX_IOERR |
| 7589 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7590 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7591 | #ifdef EX_TEMPFAIL |
| 7592 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7593 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7594 | #ifdef EX_PROTOCOL |
| 7595 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7596 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7597 | #ifdef EX_NOPERM |
| 7598 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7599 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7600 | #ifdef EX_CONFIG |
| 7601 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7602 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7603 | #ifdef EX_NOTFOUND |
| 7604 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7605 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7606 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7607 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7608 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7609 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7610 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7611 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7612 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7613 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7614 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7615 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7616 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7617 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7618 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7619 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7620 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7621 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7622 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7623 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7624 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7625 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7626 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7627 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7628 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7629 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7630 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7631 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7632 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7633 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7634 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7635 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7636 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7637 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7638 | #if defined(PYOS_OS2) |
| 7639 | if (insertvalues(d)) return -1; |
| 7640 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7641 | return 0; |
| 7642 | } |
| 7643 | |
| 7644 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7645 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7646 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7647 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7648 | |
| 7649 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7650 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7651 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7652 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7653 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7654 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7655 | #define MODNAME "posix" |
| 7656 | #endif |
| 7657 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7658 | static struct PyModuleDef posixmodule = { |
| 7659 | PyModuleDef_HEAD_INIT, |
| 7660 | MODNAME, |
| 7661 | posix__doc__, |
| 7662 | -1, |
| 7663 | posix_methods, |
| 7664 | NULL, |
| 7665 | NULL, |
| 7666 | NULL, |
| 7667 | NULL |
| 7668 | }; |
| 7669 | |
| 7670 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7671 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7672 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7673 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7674 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7675 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7676 | m = PyModule_Create(&posixmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7677 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7678 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7679 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7680 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7681 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7682 | Py_XINCREF(v); |
| 7683 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7684 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7685 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7686 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7687 | if (all_ins(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7688 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7689 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7690 | if (setup_confname_tables(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7691 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7692 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7693 | Py_INCREF(PyExc_OSError); |
| 7694 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7695 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7696 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7697 | if (posix_putenv_garbage == NULL) |
| 7698 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7699 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7700 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7701 | if (!initialized) { |
| 7702 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7703 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7704 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7705 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7706 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7707 | structseq_new = StatResultType.tp_new; |
| 7708 | StatResultType.tp_new = statresult_new; |
| 7709 | |
| 7710 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7711 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7712 | #ifdef NEED_TICKS_PER_SECOND |
| 7713 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
| 7714 | ticks_per_second = sysconf(_SC_CLK_TCK); |
| 7715 | # elif defined(HZ) |
| 7716 | ticks_per_second = HZ; |
| 7717 | # else |
| 7718 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
| 7719 | # endif |
| 7720 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7721 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7722 | Py_INCREF((PyObject*) &StatResultType); |
| 7723 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7724 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7725 | PyModule_AddObject(m, "statvfs_result", |
| 7726 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7727 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7728 | |
| 7729 | #ifdef __APPLE__ |
| 7730 | /* |
| 7731 | * Step 2 of weak-linking support on Mac OS X. |
| 7732 | * |
| 7733 | * The code below removes functions that are not available on the |
| 7734 | * currently active platform. |
| 7735 | * |
| 7736 | * This block allow one to use a python binary that was build on |
| 7737 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7738 | * OSX 10.4. |
| 7739 | */ |
| 7740 | #ifdef HAVE_FSTATVFS |
| 7741 | if (fstatvfs == NULL) { |
| 7742 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7743 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7744 | } |
| 7745 | } |
| 7746 | #endif /* HAVE_FSTATVFS */ |
| 7747 | |
| 7748 | #ifdef HAVE_STATVFS |
| 7749 | if (statvfs == NULL) { |
| 7750 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7751 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7752 | } |
| 7753 | } |
| 7754 | #endif /* HAVE_STATVFS */ |
| 7755 | |
| 7756 | # ifdef HAVE_LCHOWN |
| 7757 | if (lchown == NULL) { |
| 7758 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7759 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7760 | } |
| 7761 | } |
| 7762 | #endif /* HAVE_LCHOWN */ |
| 7763 | |
| 7764 | |
| 7765 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7766 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7767 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7768 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7769 | |
| 7770 | #ifdef __cplusplus |
| 7771 | } |
| 7772 | #endif |