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" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 267 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 268 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 270 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 272 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 273 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 274 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 275 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 276 | #define MAXPATHLEN PATH_MAX |
| 277 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 278 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 279 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 280 | #endif /* MAXPATHLEN */ |
| 281 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 282 | #ifdef UNION_WAIT |
| 283 | /* Emulate some macros on systems that have a union instead of macros */ |
| 284 | |
| 285 | #ifndef WIFEXITED |
| 286 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 287 | #endif |
| 288 | |
| 289 | #ifndef WEXITSTATUS |
| 290 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 291 | #endif |
| 292 | |
| 293 | #ifndef WTERMSIG |
| 294 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 295 | #endif |
| 296 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 297 | #define WAIT_TYPE union wait |
| 298 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 299 | |
| 300 | #else /* !UNION_WAIT */ |
| 301 | #define WAIT_TYPE int |
| 302 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 303 | #endif /* UNION_WAIT */ |
| 304 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 305 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 306 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 307 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 308 | #define USE_CTERMID_R |
| 309 | #endif |
| 310 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 311 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 312 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 313 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 314 | # define STAT win32_stat |
| 315 | # define FSTAT win32_fstat |
| 316 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 317 | #else |
| 318 | # define STAT stat |
| 319 | # define FSTAT fstat |
| 320 | # define STRUCT_STAT struct stat |
| 321 | #endif |
| 322 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 323 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 324 | #include <sys/mkdev.h> |
| 325 | #else |
| 326 | #if defined(MAJOR_IN_SYSMACROS) |
| 327 | #include <sys/sysmacros.h> |
| 328 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 329 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 330 | #include <sys/mkdev.h> |
| 331 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 332 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 333 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 334 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 335 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 336 | * valid and throw an assertion if it isn't. |
| 337 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 338 | * an assertion can be useful, but it does contradict the POSIX standard |
| 339 | * which for write(2) states: |
| 340 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 341 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 342 | * writing." |
| 343 | * Furthermore, python allows the user to enter any old integer |
| 344 | * as a fd and should merely raise a python exception on error. |
| 345 | * The Microsoft CRT doesn't provide an official way to check for the |
| 346 | * validity of a file descriptor, but we can emulate its internal behaviour |
| 347 | * by using the exported __pinfo data member and knowledge of the |
| 348 | * internal structures involved. |
| 349 | * The structures below must be updated for each version of visual studio |
| 350 | * according to the file internal.h in the CRT source, until MS comes |
| 351 | * up with a less hacky way to do this. |
| 352 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 353 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 354 | */ |
| 355 | #if _MSC_VER >= 1500 /* VS 2008 */ |
| 356 | typedef struct { |
| 357 | intptr_t osfhnd; |
| 358 | char osfile; |
| 359 | char pipech; |
| 360 | int lockinitflag; |
| 361 | CRITICAL_SECTION lock; |
| 362 | #ifndef _SAFECRT_IMPL |
| 363 | char textmode : 7; |
| 364 | char unicode : 1; |
| 365 | char pipech2[2]; |
| 366 | __int64 startpos; |
| 367 | BOOL utf8translations; |
| 368 | char dbcsBuffer; |
| 369 | BOOL dbcsBufferUsed; |
| 370 | #endif /* _SAFECRT_IMPL */ |
| 371 | } ioinfo; |
| 372 | #elif _MSC_VER >= 1400 /* VS 2005 */ |
| 373 | typedef struct { |
| 374 | intptr_t osfhnd; |
| 375 | char osfile; |
| 376 | char pipech; |
| 377 | int lockinitflag; |
| 378 | CRITICAL_SECTION lock; |
| 379 | #ifndef _SAFECRT_IMPL |
| 380 | char textmode : 7; |
| 381 | char unicode : 1; |
| 382 | char pipech2[2]; |
| 383 | __int64 startpos; |
| 384 | BOOL utf8translations; |
Kristján Valur Jónsson | 45ed72d | 2009-03-03 06:52:34 +0000 | [diff] [blame] | 385 | #ifndef _DEBUG |
| 386 | /* padding hack. 8 byte extra length observed at |
| 387 | * runtime, for 32 and 64 bits when not in _DEBUG |
| 388 | */ |
| 389 | __int32 _padding[2]; |
| 390 | #endif |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 391 | #endif /* _SAFECRT_IMPL */ |
| 392 | } ioinfo; |
| 393 | #endif |
| 394 | |
| 395 | extern __declspec(dllimport) ioinfo * __pioinfo[]; |
| 396 | #define IOINFO_L2E 5 |
| 397 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 398 | #define IOINFO_ARRAYS 64 |
| 399 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 400 | #define FOPEN 0x01 |
| 401 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 402 | |
| 403 | /* This function emulates what the windows CRT does to validate file handles */ |
| 404 | int |
| 405 | _PyVerify_fd(int fd) |
| 406 | { |
| 407 | const int i1 = fd >> IOINFO_L2E; |
| 408 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
| 409 | |
| 410 | /* See that it isn't a special CLEAR fileno */ |
| 411 | if (fd != _NO_CONSOLE_FILENO) { |
| 412 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 413 | * we check pointer validity and other info |
| 414 | */ |
| 415 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 416 | /* finally, check that the file is open */ |
| 417 | if (__pioinfo[i1][i2].osfile & FOPEN) |
| 418 | return 1; |
| 419 | } |
| 420 | } |
| 421 | errno = EBADF; |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 426 | static int |
| 427 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 428 | { |
| 429 | if (!_PyVerify_fd(fd1)) |
| 430 | return 0; |
| 431 | if (fd2 == _NO_CONSOLE_FILENO) |
| 432 | return 0; |
| 433 | if ((unsigned)fd2 < _NHANDLE_) |
| 434 | return 1; |
| 435 | else |
| 436 | return 0; |
| 437 | } |
| 438 | #else |
| 439 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 440 | #define _PyVerify_fd_dup2(A, B) (1) |
| 441 | #endif |
| 442 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 443 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 444 | #ifdef WITH_NEXT_FRAMEWORK |
| 445 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 446 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 447 | */ |
| 448 | #include <crt_externs.h> |
| 449 | static char **environ; |
| 450 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 451 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 452 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 454 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 455 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 456 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 457 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 458 | #ifdef MS_WINDOWS |
| 459 | wchar_t **e; |
| 460 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 461 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 462 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 463 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 464 | if (d == NULL) |
| 465 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 466 | #ifdef WITH_NEXT_FRAMEWORK |
| 467 | if (environ == NULL) |
| 468 | environ = *_NSGetEnviron(); |
| 469 | #endif |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 470 | #ifdef MS_WINDOWS |
| 471 | /* _wenviron must be initialized in this way if the program is started |
| 472 | through main() instead of wmain(). */ |
| 473 | _wgetenv(L""); |
| 474 | if (_wenviron == NULL) |
| 475 | return d; |
| 476 | /* This part ignores errors */ |
| 477 | for (e = _wenviron; *e != NULL; e++) { |
| 478 | PyObject *k; |
| 479 | PyObject *v; |
| 480 | wchar_t *p = wcschr(*e, L'='); |
| 481 | if (p == NULL) |
| 482 | continue; |
| 483 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 484 | if (k == NULL) { |
| 485 | PyErr_Clear(); |
| 486 | continue; |
| 487 | } |
| 488 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 489 | if (v == NULL) { |
| 490 | PyErr_Clear(); |
| 491 | Py_DECREF(k); |
| 492 | continue; |
| 493 | } |
| 494 | if (PyDict_GetItem(d, k) == NULL) { |
| 495 | if (PyDict_SetItem(d, k, v) != 0) |
| 496 | PyErr_Clear(); |
| 497 | } |
| 498 | Py_DECREF(k); |
| 499 | Py_DECREF(v); |
| 500 | } |
| 501 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 502 | if (environ == NULL) |
| 503 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 504 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 505 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 506 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 507 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 508 | char *p = strchr(*e, '='); |
| 509 | if (p == NULL) |
| 510 | continue; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 511 | k = PyUnicode_FromStringAndSize(*e, (int)(p-*e)); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 512 | if (k == NULL) { |
| 513 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 514 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 515 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 516 | v = PyUnicode_FromString(p+1); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 517 | if (v == NULL) { |
| 518 | PyErr_Clear(); |
| 519 | Py_DECREF(k); |
| 520 | continue; |
| 521 | } |
| 522 | if (PyDict_GetItem(d, k) == NULL) { |
| 523 | if (PyDict_SetItem(d, k, v) != 0) |
| 524 | PyErr_Clear(); |
| 525 | } |
| 526 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 527 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 528 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 529 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 530 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 531 | { |
| 532 | APIRET rc; |
| 533 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 534 | |
| 535 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 536 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 537 | PyObject *v = PyBytes_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 538 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 539 | Py_DECREF(v); |
| 540 | } |
| 541 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 542 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 543 | PyObject *v = PyBytes_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 544 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 545 | Py_DECREF(v); |
| 546 | } |
| 547 | } |
| 548 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 549 | return d; |
| 550 | } |
| 551 | |
| 552 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 553 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 554 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 555 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 556 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 557 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 558 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 559 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 560 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 561 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 562 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 563 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 566 | #ifdef Py_WIN_WIDE_FILENAMES |
| 567 | static PyObject * |
| 568 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 569 | { |
| 570 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 571 | } |
| 572 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 573 | |
| 574 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 575 | static PyObject * |
| 576 | posix_error_with_allocated_filename(char* name) |
| 577 | { |
| 578 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 579 | PyMem_Free(name); |
| 580 | return rc; |
| 581 | } |
| 582 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 583 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 584 | static PyObject * |
| 585 | win32_error(char* function, char* filename) |
| 586 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 587 | /* XXX We should pass the function name along in the future. |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 588 | (winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 589 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 590 | Windows error object, which is non-trivial. |
| 591 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 592 | errno = GetLastError(); |
| 593 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 594 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 595 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 596 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 597 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 598 | |
| 599 | #ifdef Py_WIN_WIDE_FILENAMES |
| 600 | static PyObject * |
| 601 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 602 | { |
| 603 | /* XXX - see win32_error for comments on 'function' */ |
| 604 | errno = GetLastError(); |
| 605 | if (filename) |
| 606 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 607 | else |
| 608 | return PyErr_SetFromWindowsErr(errno); |
| 609 | } |
| 610 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 611 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 612 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 613 | { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 614 | if (PyUnicode_CheckExact(*param)) |
| 615 | Py_INCREF(*param); |
| 616 | else if (PyUnicode_Check(*param)) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 617 | /* For a Unicode subtype that's not a Unicode object, |
| 618 | return a true Unicode object with the same data. */ |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 619 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 620 | PyUnicode_GET_SIZE(*param)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 621 | else |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 622 | *param = PyUnicode_FromEncodedObject(*param, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 623 | Py_FileSystemDefaultEncoding, |
| 624 | "strict"); |
| 625 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 629 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 630 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 632 | #if defined(PYOS_OS2) |
| 633 | /********************************************************************** |
| 634 | * Helper Function to Trim and Format OS/2 Messages |
| 635 | **********************************************************************/ |
| 636 | static void |
| 637 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 638 | { |
| 639 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 640 | |
| 641 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 642 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 643 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 644 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 645 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 646 | } |
| 647 | |
| 648 | /* Add Optional Reason Text */ |
| 649 | if (reason) { |
| 650 | strcat(msgbuf, " : "); |
| 651 | strcat(msgbuf, reason); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /********************************************************************** |
| 656 | * Decode an OS/2 Operating System Error Code |
| 657 | * |
| 658 | * A convenience function to lookup an OS/2 error code and return a |
| 659 | * text message we can use to raise a Python exception. |
| 660 | * |
| 661 | * Notes: |
| 662 | * The messages for errors returned from the OS/2 kernel reside in |
| 663 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 664 | * |
| 665 | **********************************************************************/ |
| 666 | static char * |
| 667 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 668 | { |
| 669 | APIRET rc; |
| 670 | ULONG msglen; |
| 671 | |
| 672 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 673 | Py_BEGIN_ALLOW_THREADS |
| 674 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 675 | errorcode, "oso001.msg", &msglen); |
| 676 | Py_END_ALLOW_THREADS |
| 677 | |
| 678 | if (rc == NO_ERROR) |
| 679 | os2_formatmsg(msgbuf, msglen, reason); |
| 680 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 681 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 682 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 683 | |
| 684 | return msgbuf; |
| 685 | } |
| 686 | |
| 687 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 688 | errors are not in a global variable e.g. 'errno' nor are |
| 689 | they congruent with posix error numbers. */ |
| 690 | |
| 691 | static PyObject * os2_error(int code) |
| 692 | { |
| 693 | char text[1024]; |
| 694 | PyObject *v; |
| 695 | |
| 696 | os2_strerror(text, sizeof(text), code, ""); |
| 697 | |
| 698 | v = Py_BuildValue("(is)", code, text); |
| 699 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 700 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 701 | Py_DECREF(v); |
| 702 | } |
| 703 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 704 | } |
| 705 | |
| 706 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 707 | |
| 708 | /* POSIX generic methods */ |
| 709 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 710 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 711 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 712 | { |
| 713 | int fd; |
| 714 | int res; |
| 715 | fd = PyObject_AsFileDescriptor(fdobj); |
| 716 | if (fd < 0) |
| 717 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 718 | if (!_PyVerify_fd(fd)) |
| 719 | return posix_error(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 720 | Py_BEGIN_ALLOW_THREADS |
| 721 | res = (*func)(fd); |
| 722 | Py_END_ALLOW_THREADS |
| 723 | if (res < 0) |
| 724 | return posix_error(); |
| 725 | Py_INCREF(Py_None); |
| 726 | return Py_None; |
| 727 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 728 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 729 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 730 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 731 | unicode_file_names(void) |
| 732 | { |
| 733 | static int canusewide = -1; |
| 734 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 735 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 736 | the Windows NT family. */ |
| 737 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 738 | } |
| 739 | return canusewide; |
| 740 | } |
| 741 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 742 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 743 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 744 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 745 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 746 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 747 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 748 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 749 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 750 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 751 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 752 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 753 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 754 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 755 | return posix_error_with_allocated_filename(path1); |
| 756 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 757 | Py_INCREF(Py_None); |
| 758 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 761 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 762 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 763 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 764 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 765 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 766 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 767 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 768 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 769 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 770 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 771 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 772 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 773 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 774 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 775 | PyMem_Free(path1); |
| 776 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 777 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 778 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 779 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 780 | Py_INCREF(Py_None); |
| 781 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 784 | #ifdef Py_WIN_WIDE_FILENAMES |
| 785 | static PyObject* |
| 786 | win32_1str(PyObject* args, char* func, |
| 787 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 788 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 789 | { |
| 790 | PyObject *uni; |
| 791 | char *ansi; |
| 792 | BOOL result; |
| 793 | if (unicode_file_names()) { |
| 794 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 795 | PyErr_Clear(); |
| 796 | else { |
| 797 | Py_BEGIN_ALLOW_THREADS |
| 798 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 799 | Py_END_ALLOW_THREADS |
| 800 | if (!result) |
| 801 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 802 | Py_INCREF(Py_None); |
| 803 | return Py_None; |
| 804 | } |
| 805 | } |
| 806 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 807 | return NULL; |
| 808 | Py_BEGIN_ALLOW_THREADS |
| 809 | result = funcA(ansi); |
| 810 | Py_END_ALLOW_THREADS |
| 811 | if (!result) |
| 812 | return win32_error(func, ansi); |
| 813 | Py_INCREF(Py_None); |
| 814 | return Py_None; |
| 815 | |
| 816 | } |
| 817 | |
| 818 | /* This is a reimplementation of the C library's chdir function, |
| 819 | but one that produces Win32 errors instead of DOS error codes. |
| 820 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 821 | it also needs to set "magic" environment variables indicating |
| 822 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 823 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 824 | win32_chdir(LPCSTR path) |
| 825 | { |
| 826 | char new_path[MAX_PATH+1]; |
| 827 | int result; |
| 828 | char env[4] = "=x:"; |
| 829 | |
| 830 | if(!SetCurrentDirectoryA(path)) |
| 831 | return FALSE; |
| 832 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 833 | if (!result) |
| 834 | return FALSE; |
| 835 | /* In the ANSI API, there should not be any paths longer |
| 836 | than MAX_PATH. */ |
| 837 | assert(result <= MAX_PATH+1); |
| 838 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 839 | strncmp(new_path, "//", 2) == 0) |
| 840 | /* UNC path, nothing to do. */ |
| 841 | return TRUE; |
| 842 | env[1] = new_path[0]; |
| 843 | return SetEnvironmentVariableA(env, new_path); |
| 844 | } |
| 845 | |
| 846 | /* The Unicode version differs from the ANSI version |
| 847 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 848 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 849 | win32_wchdir(LPCWSTR path) |
| 850 | { |
| 851 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 852 | int result; |
| 853 | wchar_t env[4] = L"=x:"; |
| 854 | |
| 855 | if(!SetCurrentDirectoryW(path)) |
| 856 | return FALSE; |
| 857 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 858 | if (!result) |
| 859 | return FALSE; |
| 860 | if (result > MAX_PATH+1) { |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 861 | new_path = malloc(result * sizeof(wchar_t)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 862 | if (!new_path) { |
| 863 | SetLastError(ERROR_OUTOFMEMORY); |
| 864 | return FALSE; |
| 865 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 866 | result = GetCurrentDirectoryW(result, new_path); |
| 867 | if (!result) { |
| 868 | free(new_path); |
| 869 | return FALSE; |
| 870 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 871 | } |
| 872 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 873 | wcsncmp(new_path, L"//", 2) == 0) |
| 874 | /* UNC path, nothing to do. */ |
| 875 | return TRUE; |
| 876 | env[1] = new_path[0]; |
| 877 | result = SetEnvironmentVariableW(env, new_path); |
| 878 | if (new_path != _new_path) |
| 879 | free(new_path); |
| 880 | return result; |
| 881 | } |
| 882 | #endif |
| 883 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 884 | #ifdef MS_WINDOWS |
| 885 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 886 | - time stamps are restricted to second resolution |
| 887 | - file modification times suffer from forth-and-back conversions between |
| 888 | UTC and local time |
| 889 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 890 | */ |
| 891 | #define HAVE_STAT_NSEC 1 |
| 892 | |
| 893 | struct win32_stat{ |
| 894 | int st_dev; |
| 895 | __int64 st_ino; |
| 896 | unsigned short st_mode; |
| 897 | int st_nlink; |
| 898 | int st_uid; |
| 899 | int st_gid; |
| 900 | int st_rdev; |
| 901 | __int64 st_size; |
| 902 | int st_atime; |
| 903 | int st_atime_nsec; |
| 904 | int st_mtime; |
| 905 | int st_mtime_nsec; |
| 906 | int st_ctime; |
| 907 | int st_ctime_nsec; |
| 908 | }; |
| 909 | |
| 910 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 911 | |
| 912 | static void |
| 913 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 914 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 915 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 916 | /* Cannot simply cast and dereference in_ptr, |
| 917 | since it might not be aligned properly */ |
| 918 | __int64 in; |
| 919 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 920 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 921 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 922 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 923 | } |
| 924 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 925 | static void |
| 926 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 927 | { |
| 928 | /* XXX endianness */ |
| 929 | __int64 out; |
| 930 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 931 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 932 | memcpy(out_ptr, &out, sizeof(out)); |
| 933 | } |
| 934 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 935 | /* Below, we *know* that ugo+r is 0444 */ |
| 936 | #if _S_IREAD != 0400 |
| 937 | #error Unsupported C library |
| 938 | #endif |
| 939 | static int |
| 940 | attributes_to_mode(DWORD attr) |
| 941 | { |
| 942 | int m = 0; |
| 943 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 944 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 945 | else |
| 946 | m |= _S_IFREG; |
| 947 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 948 | m |= 0444; |
| 949 | else |
| 950 | m |= 0666; |
| 951 | return m; |
| 952 | } |
| 953 | |
| 954 | static int |
| 955 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 956 | { |
| 957 | memset(result, 0, sizeof(*result)); |
| 958 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 959 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 960 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 961 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 962 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 967 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 968 | static int checked = 0; |
| 969 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 970 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 971 | static void |
| 972 | check_gfax() |
| 973 | { |
| 974 | HINSTANCE hKernel32; |
| 975 | if (checked) |
| 976 | return; |
| 977 | checked = 1; |
| 978 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 979 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 980 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 981 | } |
| 982 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 983 | static BOOL |
| 984 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 985 | { |
| 986 | HANDLE hFindFile; |
| 987 | WIN32_FIND_DATAA FileData; |
| 988 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 989 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 990 | return FALSE; |
| 991 | FindClose(hFindFile); |
| 992 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 993 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 994 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 995 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 996 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 997 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 998 | return TRUE; |
| 999 | } |
| 1000 | |
| 1001 | static BOOL |
| 1002 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1003 | { |
| 1004 | HANDLE hFindFile; |
| 1005 | WIN32_FIND_DATAW FileData; |
| 1006 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1007 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1008 | return FALSE; |
| 1009 | FindClose(hFindFile); |
| 1010 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1011 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1012 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1013 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1014 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1015 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1016 | return TRUE; |
| 1017 | } |
| 1018 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1019 | static BOOL WINAPI |
| 1020 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 1021 | GET_FILEEX_INFO_LEVELS level, |
| 1022 | LPVOID pv) |
| 1023 | { |
| 1024 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1025 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1026 | /* First try to use the system's implementation, if that is |
| 1027 | available and either succeeds to gives an error other than |
| 1028 | that it isn't implemented. */ |
| 1029 | check_gfax(); |
| 1030 | if (gfaxa) { |
| 1031 | result = gfaxa(pszFile, level, pv); |
| 1032 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1033 | return result; |
| 1034 | } |
| 1035 | /* It's either not present, or not implemented. |
| 1036 | Emulate using FindFirstFile. */ |
| 1037 | if (level != GetFileExInfoStandard) { |
| 1038 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1039 | return FALSE; |
| 1040 | } |
| 1041 | /* Use GetFileAttributes to validate that the file name |
| 1042 | does not contain wildcards (which FindFirstFile would |
| 1043 | accept). */ |
| 1044 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 1045 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1046 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static BOOL WINAPI |
| 1050 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 1051 | GET_FILEEX_INFO_LEVELS level, |
| 1052 | LPVOID pv) |
| 1053 | { |
| 1054 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1055 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1056 | /* First try to use the system's implementation, if that is |
| 1057 | available and either succeeds to gives an error other than |
| 1058 | that it isn't implemented. */ |
| 1059 | check_gfax(); |
| 1060 | if (gfaxa) { |
| 1061 | result = gfaxw(pszFile, level, pv); |
| 1062 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1063 | return result; |
| 1064 | } |
| 1065 | /* It's either not present, or not implemented. |
| 1066 | Emulate using FindFirstFile. */ |
| 1067 | if (level != GetFileExInfoStandard) { |
| 1068 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1069 | return FALSE; |
| 1070 | } |
| 1071 | /* Use GetFileAttributes to validate that the file name |
| 1072 | does not contain wildcards (which FindFirstFile would |
| 1073 | accept). */ |
| 1074 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 1075 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1076 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1079 | static int |
| 1080 | win32_stat(const char* path, struct win32_stat *result) |
| 1081 | { |
| 1082 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1083 | int code; |
| 1084 | char *dot; |
| 1085 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1086 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1087 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1088 | /* Protocol violation: we explicitly clear errno, instead of |
| 1089 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1090 | errno = 0; |
| 1091 | return -1; |
| 1092 | } else { |
| 1093 | /* Could not get attributes on open file. Fall back to |
| 1094 | reading the directory. */ |
| 1095 | if (!attributes_from_dir(path, &info)) { |
| 1096 | /* Very strange. This should not fail now */ |
| 1097 | errno = 0; |
| 1098 | return -1; |
| 1099 | } |
| 1100 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1101 | } |
| 1102 | code = attribute_data_to_stat(&info, result); |
| 1103 | if (code != 0) |
| 1104 | return code; |
| 1105 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1106 | dot = strrchr(path, '.'); |
| 1107 | if (dot) { |
| 1108 | if (stricmp(dot, ".bat") == 0 || |
| 1109 | stricmp(dot, ".cmd") == 0 || |
| 1110 | stricmp(dot, ".exe") == 0 || |
| 1111 | stricmp(dot, ".com") == 0) |
| 1112 | result->st_mode |= 0111; |
| 1113 | } |
| 1114 | return code; |
| 1115 | } |
| 1116 | |
| 1117 | static int |
| 1118 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1119 | { |
| 1120 | int code; |
| 1121 | const wchar_t *dot; |
| 1122 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1123 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1124 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1125 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1126 | /* Protocol violation: we explicitly clear errno, instead of |
| 1127 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1128 | errno = 0; |
| 1129 | return -1; |
| 1130 | } else { |
| 1131 | /* Could not get attributes on open file. Fall back to |
| 1132 | reading the directory. */ |
| 1133 | if (!attributes_from_dir_w(path, &info)) { |
| 1134 | /* Very strange. This should not fail now */ |
| 1135 | errno = 0; |
| 1136 | return -1; |
| 1137 | } |
| 1138 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1139 | } |
| 1140 | code = attribute_data_to_stat(&info, result); |
| 1141 | if (code < 0) |
| 1142 | return code; |
| 1143 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1144 | dot = wcsrchr(path, '.'); |
| 1145 | if (dot) { |
| 1146 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1147 | _wcsicmp(dot, L".cmd") == 0 || |
| 1148 | _wcsicmp(dot, L".exe") == 0 || |
| 1149 | _wcsicmp(dot, L".com") == 0) |
| 1150 | result->st_mode |= 0111; |
| 1151 | } |
| 1152 | return code; |
| 1153 | } |
| 1154 | |
| 1155 | static int |
| 1156 | win32_fstat(int file_number, struct win32_stat *result) |
| 1157 | { |
| 1158 | BY_HANDLE_FILE_INFORMATION info; |
| 1159 | HANDLE h; |
| 1160 | int type; |
| 1161 | |
| 1162 | h = (HANDLE)_get_osfhandle(file_number); |
| 1163 | |
| 1164 | /* Protocol violation: we explicitly clear errno, instead of |
| 1165 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1166 | errno = 0; |
| 1167 | |
| 1168 | if (h == INVALID_HANDLE_VALUE) { |
| 1169 | /* This is really a C library error (invalid file handle). |
| 1170 | We set the Win32 error to the closes one matching. */ |
| 1171 | SetLastError(ERROR_INVALID_HANDLE); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | memset(result, 0, sizeof(*result)); |
| 1175 | |
| 1176 | type = GetFileType(h); |
| 1177 | if (type == FILE_TYPE_UNKNOWN) { |
| 1178 | DWORD error = GetLastError(); |
| 1179 | if (error != 0) { |
| 1180 | return -1; |
| 1181 | } |
| 1182 | /* else: valid but unknown file */ |
| 1183 | } |
| 1184 | |
| 1185 | if (type != FILE_TYPE_DISK) { |
| 1186 | if (type == FILE_TYPE_CHAR) |
| 1187 | result->st_mode = _S_IFCHR; |
| 1188 | else if (type == FILE_TYPE_PIPE) |
| 1189 | result->st_mode = _S_IFIFO; |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | if (!GetFileInformationByHandle(h, &info)) { |
| 1194 | return -1; |
| 1195 | } |
| 1196 | |
| 1197 | /* similar to stat() */ |
| 1198 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1199 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1200 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1201 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1202 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1203 | /* specific to fstat() */ |
| 1204 | result->st_nlink = info.nNumberOfLinks; |
| 1205 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | #endif /* MS_WINDOWS */ |
| 1210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1211 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1212 | "stat_result: Result from stat or lstat.\n\n\ |
| 1213 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1214 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1215 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1216 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1217 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1218 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1219 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1220 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1221 | |
| 1222 | static PyStructSequence_Field stat_result_fields[] = { |
| 1223 | {"st_mode", "protection bits"}, |
| 1224 | {"st_ino", "inode"}, |
| 1225 | {"st_dev", "device"}, |
| 1226 | {"st_nlink", "number of hard links"}, |
| 1227 | {"st_uid", "user ID of owner"}, |
| 1228 | {"st_gid", "group ID of owner"}, |
| 1229 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1230 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1231 | {NULL, "integer time of last access"}, |
| 1232 | {NULL, "integer time of last modification"}, |
| 1233 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1234 | {"st_atime", "time of last access"}, |
| 1235 | {"st_mtime", "time of last modification"}, |
| 1236 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1237 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1238 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1239 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1240 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1241 | {"st_blocks", "number of blocks allocated"}, |
| 1242 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1243 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1244 | {"st_rdev", "device type (if inode device)"}, |
| 1245 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1246 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1247 | {"st_flags", "user defined flags for file"}, |
| 1248 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1249 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1250 | {"st_gen", "generation number"}, |
| 1251 | #endif |
| 1252 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1253 | {"st_birthtime", "time of creation"}, |
| 1254 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1255 | {0} |
| 1256 | }; |
| 1257 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1258 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1259 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1260 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1261 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1262 | #endif |
| 1263 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1264 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1265 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1266 | #else |
| 1267 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1268 | #endif |
| 1269 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1270 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1271 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1272 | #else |
| 1273 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1274 | #endif |
| 1275 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1276 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1277 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1278 | #else |
| 1279 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1280 | #endif |
| 1281 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1282 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1283 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1284 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1285 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1286 | #endif |
| 1287 | |
| 1288 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1289 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1290 | #else |
| 1291 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1292 | #endif |
| 1293 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1294 | static PyStructSequence_Desc stat_result_desc = { |
| 1295 | "stat_result", /* name */ |
| 1296 | stat_result__doc__, /* doc */ |
| 1297 | stat_result_fields, |
| 1298 | 10 |
| 1299 | }; |
| 1300 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1301 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1302 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1303 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1304 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1305 | 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] | 1306 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1307 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1308 | |
| 1309 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1310 | {"f_bsize", }, |
| 1311 | {"f_frsize", }, |
| 1312 | {"f_blocks", }, |
| 1313 | {"f_bfree", }, |
| 1314 | {"f_bavail", }, |
| 1315 | {"f_files", }, |
| 1316 | {"f_ffree", }, |
| 1317 | {"f_favail", }, |
| 1318 | {"f_flag", }, |
| 1319 | {"f_namemax",}, |
| 1320 | {0} |
| 1321 | }; |
| 1322 | |
| 1323 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1324 | "statvfs_result", /* name */ |
| 1325 | statvfs_result__doc__, /* doc */ |
| 1326 | statvfs_result_fields, |
| 1327 | 10 |
| 1328 | }; |
| 1329 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1330 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1331 | static PyTypeObject StatResultType; |
| 1332 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1333 | static newfunc structseq_new; |
| 1334 | |
| 1335 | static PyObject * |
| 1336 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1337 | { |
| 1338 | PyStructSequence *result; |
| 1339 | int i; |
| 1340 | |
| 1341 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1342 | if (!result) |
| 1343 | return NULL; |
| 1344 | /* If we have been initialized from a tuple, |
| 1345 | st_?time might be set to None. Initialize it |
| 1346 | from the int slots. */ |
| 1347 | for (i = 7; i <= 9; i++) { |
| 1348 | if (result->ob_item[i+3] == Py_None) { |
| 1349 | Py_DECREF(Py_None); |
| 1350 | Py_INCREF(result->ob_item[i]); |
| 1351 | result->ob_item[i+3] = result->ob_item[i]; |
| 1352 | } |
| 1353 | } |
| 1354 | return (PyObject*)result; |
| 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1360 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1361 | |
| 1362 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1363 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1364 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1365 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1366 | future calls return ints. \n\ |
| 1367 | If newval is omitted, return the current setting.\n"); |
| 1368 | |
| 1369 | static PyObject* |
| 1370 | stat_float_times(PyObject* self, PyObject *args) |
| 1371 | { |
| 1372 | int newval = -1; |
| 1373 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1374 | return NULL; |
| 1375 | if (newval == -1) |
| 1376 | /* Return old value */ |
| 1377 | return PyBool_FromLong(_stat_float_times); |
| 1378 | _stat_float_times = newval; |
| 1379 | Py_INCREF(Py_None); |
| 1380 | return Py_None; |
| 1381 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1382 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1383 | static void |
| 1384 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1385 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1386 | PyObject *fval,*ival; |
| 1387 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1388 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1389 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1390 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1391 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1392 | if (!ival) |
| 1393 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1394 | if (_stat_float_times) { |
| 1395 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1396 | } else { |
| 1397 | fval = ival; |
| 1398 | Py_INCREF(fval); |
| 1399 | } |
| 1400 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1401 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1404 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1405 | (used by posix_stat() and posix_fstat()) */ |
| 1406 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1407 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1408 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1409 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1410 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1411 | if (v == NULL) |
| 1412 | return NULL; |
| 1413 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1414 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1415 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1416 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1417 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1418 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1419 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1420 | #endif |
| 1421 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1422 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1423 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1424 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1425 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1426 | #endif |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1427 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1428 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1429 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1430 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1431 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1432 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1433 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1434 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1435 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1436 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1437 | #if defined(HAVE_STAT_TV_NSEC) |
| 1438 | ansec = st->st_atim.tv_nsec; |
| 1439 | mnsec = st->st_mtim.tv_nsec; |
| 1440 | cnsec = st->st_ctim.tv_nsec; |
| 1441 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1442 | ansec = st->st_atimespec.tv_nsec; |
| 1443 | mnsec = st->st_mtimespec.tv_nsec; |
| 1444 | cnsec = st->st_ctimespec.tv_nsec; |
| 1445 | #elif defined(HAVE_STAT_NSEC) |
| 1446 | ansec = st->st_atime_nsec; |
| 1447 | mnsec = st->st_mtime_nsec; |
| 1448 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1449 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1450 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1451 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1452 | fill_time(v, 7, st->st_atime, ansec); |
| 1453 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1454 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1455 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1456 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1457 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1458 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1459 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1460 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1461 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1462 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1463 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1464 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1465 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1466 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1467 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1468 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1469 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1470 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1471 | #endif |
| 1472 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1473 | { |
| 1474 | PyObject *val; |
| 1475 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1476 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1477 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1478 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1479 | #else |
| 1480 | bnsec = 0; |
| 1481 | #endif |
| 1482 | if (_stat_float_times) { |
| 1483 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1484 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1485 | val = PyLong_FromLong((long)bsec); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1486 | } |
| 1487 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1488 | val); |
| 1489 | } |
| 1490 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1491 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1492 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1493 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1494 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1495 | |
| 1496 | if (PyErr_Occurred()) { |
| 1497 | Py_DECREF(v); |
| 1498 | return NULL; |
| 1499 | } |
| 1500 | |
| 1501 | return v; |
| 1502 | } |
| 1503 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1504 | #ifdef MS_WINDOWS |
| 1505 | |
| 1506 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1507 | where / can be used in place of \ and the trailing slash is optional. |
| 1508 | Both SERVER and SHARE must have at least one character. |
| 1509 | */ |
| 1510 | |
| 1511 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1512 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1513 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1514 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1515 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1516 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1517 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1518 | IsUNCRootA(char *path, int pathlen) |
| 1519 | { |
| 1520 | #define ISSLASH ISSLASHA |
| 1521 | |
| 1522 | int i, share; |
| 1523 | |
| 1524 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1525 | /* minimum UNCRoot is \\x\y */ |
| 1526 | return FALSE; |
| 1527 | for (i = 2; i < pathlen ; i++) |
| 1528 | if (ISSLASH(path[i])) break; |
| 1529 | if (i == 2 || i == pathlen) |
| 1530 | /* do not allow \\\SHARE or \\SERVER */ |
| 1531 | return FALSE; |
| 1532 | share = i+1; |
| 1533 | for (i = share; i < pathlen; i++) |
| 1534 | if (ISSLASH(path[i])) break; |
| 1535 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1536 | |
| 1537 | #undef ISSLASH |
| 1538 | } |
| 1539 | |
| 1540 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1541 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1542 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1543 | { |
| 1544 | #define ISSLASH ISSLASHW |
| 1545 | |
| 1546 | int i, share; |
| 1547 | |
| 1548 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1549 | /* minimum UNCRoot is \\x\y */ |
| 1550 | return FALSE; |
| 1551 | for (i = 2; i < pathlen ; i++) |
| 1552 | if (ISSLASH(path[i])) break; |
| 1553 | if (i == 2 || i == pathlen) |
| 1554 | /* do not allow \\\SHARE or \\SERVER */ |
| 1555 | return FALSE; |
| 1556 | share = i+1; |
| 1557 | for (i = share; i < pathlen; i++) |
| 1558 | if (ISSLASH(path[i])) break; |
| 1559 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1560 | |
| 1561 | #undef ISSLASH |
| 1562 | } |
| 1563 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1564 | #endif /* MS_WINDOWS */ |
| 1565 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1566 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1567 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1568 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1569 | #ifdef __VMS |
| 1570 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1571 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1572 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1573 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1574 | char *wformat, |
| 1575 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1576 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1577 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1578 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1579 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1580 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1581 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1582 | |
| 1583 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1584 | /* If on wide-character-capable OS see if argument |
| 1585 | is Unicode and if so use wide API. */ |
| 1586 | if (unicode_file_names()) { |
| 1587 | PyUnicodeObject *po; |
| 1588 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1589 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1590 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1591 | Py_BEGIN_ALLOW_THREADS |
| 1592 | /* PyUnicode_AS_UNICODE result OK without |
| 1593 | thread lock as it is a simple dereference. */ |
| 1594 | res = wstatfunc(wpath, &st); |
| 1595 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1596 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1597 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1598 | return win32_error_unicode("stat", wpath); |
| 1599 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1600 | } |
| 1601 | /* Drop the argument parsing error as narrow strings |
| 1602 | are also valid. */ |
| 1603 | PyErr_Clear(); |
| 1604 | } |
| 1605 | #endif |
| 1606 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1607 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1608 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1609 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1610 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1611 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1612 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1613 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1614 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (res != 0) { |
| 1617 | #ifdef MS_WINDOWS |
| 1618 | result = win32_error("stat", pathfree); |
| 1619 | #else |
| 1620 | result = posix_error_with_filename(pathfree); |
| 1621 | #endif |
| 1622 | } |
| 1623 | else |
| 1624 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1625 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1626 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1627 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1630 | /* POSIX methods */ |
| 1631 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1632 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1633 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1634 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1635 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1636 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1637 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1638 | 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] | 1639 | |
| 1640 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1641 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1642 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1643 | char *path; |
| 1644 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1645 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1646 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1647 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1648 | if (unicode_file_names()) { |
| 1649 | PyUnicodeObject *po; |
| 1650 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1651 | Py_BEGIN_ALLOW_THREADS |
| 1652 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1653 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1654 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1655 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1656 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1657 | } |
| 1658 | /* Drop the argument parsing error as narrow strings |
| 1659 | are also valid. */ |
| 1660 | PyErr_Clear(); |
| 1661 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1662 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1663 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1664 | return 0; |
| 1665 | Py_BEGIN_ALLOW_THREADS |
| 1666 | attr = GetFileAttributesA(path); |
| 1667 | Py_END_ALLOW_THREADS |
| 1668 | PyMem_Free(path); |
| 1669 | finish: |
| 1670 | if (attr == 0xFFFFFFFF) |
| 1671 | /* File does not exist, or cannot read attributes */ |
| 1672 | return PyBool_FromLong(0); |
| 1673 | /* Access is possible if either write access wasn't requested, or |
Guido van Rossum | b00324f | 2007-12-04 01:13:14 +0000 | [diff] [blame] | 1674 | the file isn't read-only, or if it's a directory, as there are |
| 1675 | no read-only directories on Windows. */ |
| 1676 | return PyBool_FromLong(!(mode & 2) |
| 1677 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1678 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1679 | #else |
| 1680 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1681 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1682 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1683 | return NULL; |
| 1684 | Py_BEGIN_ALLOW_THREADS |
| 1685 | res = access(path, mode); |
| 1686 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1687 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1688 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1689 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1692 | #ifndef F_OK |
| 1693 | #define F_OK 0 |
| 1694 | #endif |
| 1695 | #ifndef R_OK |
| 1696 | #define R_OK 4 |
| 1697 | #endif |
| 1698 | #ifndef W_OK |
| 1699 | #define W_OK 2 |
| 1700 | #endif |
| 1701 | #ifndef X_OK |
| 1702 | #define X_OK 1 |
| 1703 | #endif |
| 1704 | |
| 1705 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1706 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1707 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1708 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1709 | |
| 1710 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1711 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1712 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1713 | int id; |
| 1714 | char *ret; |
| 1715 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1716 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1717 | return NULL; |
| 1718 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1719 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1720 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1721 | if (id == 0) { |
| 1722 | ret = ttyname(); |
| 1723 | } |
| 1724 | else { |
| 1725 | ret = NULL; |
| 1726 | } |
| 1727 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1728 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1729 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1730 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1731 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1732 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1733 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1734 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1735 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1736 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1737 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1738 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1739 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1740 | |
| 1741 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1742 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1743 | { |
| 1744 | char *ret; |
| 1745 | char buffer[L_ctermid]; |
| 1746 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1747 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1748 | ret = ctermid_r(buffer); |
| 1749 | #else |
| 1750 | ret = ctermid(buffer); |
| 1751 | #endif |
| 1752 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1753 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1754 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1755 | } |
| 1756 | #endif |
| 1757 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1758 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1759 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1760 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1761 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1762 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1763 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1764 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1765 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 1766 | 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] | 1767 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1768 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1769 | #elif defined(__VMS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1770 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1771 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1772 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1773 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1776 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1777 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1778 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1779 | 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] | 1780 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1781 | |
| 1782 | static PyObject * |
| 1783 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1784 | { |
| 1785 | return posix_fildes(fdobj, fchdir); |
| 1786 | } |
| 1787 | #endif /* HAVE_FCHDIR */ |
| 1788 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1789 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1790 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1791 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1792 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1793 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1794 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1795 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1796 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1797 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1798 | int i; |
| 1799 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1800 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1801 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1802 | if (unicode_file_names()) { |
| 1803 | PyUnicodeObject *po; |
| 1804 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1805 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1806 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1807 | if (attr != 0xFFFFFFFF) { |
| 1808 | if (i & _S_IWRITE) |
| 1809 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1810 | else |
| 1811 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1812 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1813 | } |
| 1814 | else |
| 1815 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1816 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1817 | if (!res) |
| 1818 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1819 | PyUnicode_AS_UNICODE(po)); |
| 1820 | Py_INCREF(Py_None); |
| 1821 | return Py_None; |
| 1822 | } |
| 1823 | /* Drop the argument parsing error as narrow strings |
| 1824 | are also valid. */ |
| 1825 | PyErr_Clear(); |
| 1826 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1827 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1828 | &path, &i)) |
| 1829 | return NULL; |
| 1830 | Py_BEGIN_ALLOW_THREADS |
| 1831 | attr = GetFileAttributesA(path); |
| 1832 | if (attr != 0xFFFFFFFF) { |
| 1833 | if (i & _S_IWRITE) |
| 1834 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1835 | else |
| 1836 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1837 | res = SetFileAttributesA(path, attr); |
| 1838 | } |
| 1839 | else |
| 1840 | res = 0; |
| 1841 | Py_END_ALLOW_THREADS |
| 1842 | if (!res) { |
| 1843 | win32_error("chmod", path); |
| 1844 | PyMem_Free(path); |
| 1845 | return NULL; |
| 1846 | } |
| 1847 | PyMem_Free(path); |
| 1848 | Py_INCREF(Py_None); |
| 1849 | return Py_None; |
| 1850 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1851 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1852 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1853 | return NULL; |
| 1854 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1855 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1856 | Py_END_ALLOW_THREADS |
| 1857 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1858 | return posix_error_with_allocated_filename(path); |
| 1859 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1860 | Py_INCREF(Py_None); |
| 1861 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1862 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1865 | #ifdef HAVE_FCHMOD |
| 1866 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1867 | "fchmod(fd, mode)\n\n\ |
| 1868 | Change the access permissions of the file given by file\n\ |
| 1869 | descriptor fd."); |
| 1870 | |
| 1871 | static PyObject * |
| 1872 | posix_fchmod(PyObject *self, PyObject *args) |
| 1873 | { |
| 1874 | int fd, mode, res; |
| 1875 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1876 | return NULL; |
| 1877 | Py_BEGIN_ALLOW_THREADS |
| 1878 | res = fchmod(fd, mode); |
| 1879 | Py_END_ALLOW_THREADS |
| 1880 | if (res < 0) |
| 1881 | return posix_error(); |
| 1882 | Py_RETURN_NONE; |
| 1883 | } |
| 1884 | #endif /* HAVE_FCHMOD */ |
| 1885 | |
| 1886 | #ifdef HAVE_LCHMOD |
| 1887 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1888 | "lchmod(path, mode)\n\n\ |
| 1889 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1890 | affects the link itself rather than the target."); |
| 1891 | |
| 1892 | static PyObject * |
| 1893 | posix_lchmod(PyObject *self, PyObject *args) |
| 1894 | { |
| 1895 | char *path = NULL; |
| 1896 | int i; |
| 1897 | int res; |
| 1898 | if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding, |
| 1899 | &path, &i)) |
| 1900 | return NULL; |
| 1901 | Py_BEGIN_ALLOW_THREADS |
| 1902 | res = lchmod(path, i); |
| 1903 | Py_END_ALLOW_THREADS |
| 1904 | if (res < 0) |
| 1905 | return posix_error_with_allocated_filename(path); |
| 1906 | PyMem_Free(path); |
| 1907 | Py_RETURN_NONE; |
| 1908 | } |
| 1909 | #endif /* HAVE_LCHMOD */ |
| 1910 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1911 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1912 | #ifdef HAVE_CHFLAGS |
| 1913 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1914 | "chflags(path, flags)\n\n\ |
| 1915 | Set file flags."); |
| 1916 | |
| 1917 | static PyObject * |
| 1918 | posix_chflags(PyObject *self, PyObject *args) |
| 1919 | { |
| 1920 | char *path; |
| 1921 | unsigned long flags; |
| 1922 | int res; |
| 1923 | if (!PyArg_ParseTuple(args, "etk:chflags", |
| 1924 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1925 | return NULL; |
| 1926 | Py_BEGIN_ALLOW_THREADS |
| 1927 | res = chflags(path, flags); |
| 1928 | Py_END_ALLOW_THREADS |
| 1929 | if (res < 0) |
| 1930 | return posix_error_with_allocated_filename(path); |
| 1931 | PyMem_Free(path); |
| 1932 | Py_INCREF(Py_None); |
| 1933 | return Py_None; |
| 1934 | } |
| 1935 | #endif /* HAVE_CHFLAGS */ |
| 1936 | |
| 1937 | #ifdef HAVE_LCHFLAGS |
| 1938 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1939 | "lchflags(path, flags)\n\n\ |
| 1940 | Set file flags.\n\ |
| 1941 | This function will not follow symbolic links."); |
| 1942 | |
| 1943 | static PyObject * |
| 1944 | posix_lchflags(PyObject *self, PyObject *args) |
| 1945 | { |
| 1946 | char *path; |
| 1947 | unsigned long flags; |
| 1948 | int res; |
| 1949 | if (!PyArg_ParseTuple(args, "etk:lchflags", |
| 1950 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1951 | return NULL; |
| 1952 | Py_BEGIN_ALLOW_THREADS |
| 1953 | res = lchflags(path, flags); |
| 1954 | Py_END_ALLOW_THREADS |
| 1955 | if (res < 0) |
| 1956 | return posix_error_with_allocated_filename(path); |
| 1957 | PyMem_Free(path); |
| 1958 | Py_INCREF(Py_None); |
| 1959 | return Py_None; |
| 1960 | } |
| 1961 | #endif /* HAVE_LCHFLAGS */ |
| 1962 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1963 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1964 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1965 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1966 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1967 | |
| 1968 | static PyObject * |
| 1969 | posix_chroot(PyObject *self, PyObject *args) |
| 1970 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1971 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1972 | } |
| 1973 | #endif |
| 1974 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1975 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1976 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1977 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1978 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1979 | |
| 1980 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1981 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1982 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1983 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1984 | } |
| 1985 | #endif /* HAVE_FSYNC */ |
| 1986 | |
| 1987 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1988 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1989 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1990 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1991 | #endif |
| 1992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1993 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1994 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1995 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1996 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1997 | |
| 1998 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1999 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2000 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2001 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2002 | } |
| 2003 | #endif /* HAVE_FDATASYNC */ |
| 2004 | |
| 2005 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2006 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2007 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2008 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2009 | 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] | 2010 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2011 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2012 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2013 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2014 | char *path = NULL; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2015 | long uid, gid; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2016 | int res; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2017 | if (!PyArg_ParseTuple(args, "etll:chown", |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2018 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2019 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2020 | return NULL; |
| 2021 | Py_BEGIN_ALLOW_THREADS |
| 2022 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2023 | Py_END_ALLOW_THREADS |
| 2024 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2025 | return posix_error_with_allocated_filename(path); |
| 2026 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2027 | Py_INCREF(Py_None); |
| 2028 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2029 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2030 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2031 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2032 | #ifdef HAVE_FCHOWN |
| 2033 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2034 | "fchown(fd, uid, gid)\n\n\ |
| 2035 | Change the owner and group id of the file given by file descriptor\n\ |
| 2036 | fd to the numeric uid and gid."); |
| 2037 | |
| 2038 | static PyObject * |
| 2039 | posix_fchown(PyObject *self, PyObject *args) |
| 2040 | { |
| 2041 | int fd, uid, gid; |
| 2042 | int res; |
| 2043 | if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid)) |
| 2044 | return NULL; |
| 2045 | Py_BEGIN_ALLOW_THREADS |
| 2046 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2047 | Py_END_ALLOW_THREADS |
| 2048 | if (res < 0) |
| 2049 | return posix_error(); |
| 2050 | Py_RETURN_NONE; |
| 2051 | } |
| 2052 | #endif /* HAVE_FCHOWN */ |
| 2053 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2054 | #ifdef HAVE_LCHOWN |
| 2055 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2056 | "lchown(path, uid, gid)\n\n\ |
| 2057 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2058 | This function will not follow symbolic links."); |
| 2059 | |
| 2060 | static PyObject * |
| 2061 | posix_lchown(PyObject *self, PyObject *args) |
| 2062 | { |
| 2063 | char *path = NULL; |
| 2064 | int uid, gid; |
| 2065 | int res; |
| 2066 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 2067 | Py_FileSystemDefaultEncoding, &path, |
| 2068 | &uid, &gid)) |
| 2069 | return NULL; |
| 2070 | Py_BEGIN_ALLOW_THREADS |
| 2071 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2072 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2073 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2074 | return posix_error_with_allocated_filename(path); |
| 2075 | PyMem_Free(path); |
| 2076 | Py_INCREF(Py_None); |
| 2077 | return Py_None; |
| 2078 | } |
| 2079 | #endif /* HAVE_LCHOWN */ |
| 2080 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2081 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2082 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2083 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2084 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2085 | { |
| 2086 | char buf[1026]; |
| 2087 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2088 | |
| 2089 | #ifdef Py_WIN_WIDE_FILENAMES |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2090 | if (!use_bytes && unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2091 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2092 | wchar_t *wbuf2 = wbuf; |
| 2093 | PyObject *resobj; |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2094 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2095 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2096 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2097 | /* If the buffer is large enough, len does not include the |
| 2098 | terminating \0. If the buffer is too small, len includes |
| 2099 | the space needed for the terminator. */ |
| 2100 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2101 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2102 | if (wbuf2) |
| 2103 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2104 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2105 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2106 | if (!wbuf2) { |
| 2107 | PyErr_NoMemory(); |
| 2108 | return NULL; |
| 2109 | } |
| 2110 | if (!len) { |
| 2111 | if (wbuf2 != wbuf) free(wbuf2); |
| 2112 | return win32_error("getcwdu", NULL); |
| 2113 | } |
| 2114 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2115 | if (wbuf2 != wbuf) free(wbuf2); |
| 2116 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2117 | } |
| 2118 | #endif |
| 2119 | |
| 2120 | Py_BEGIN_ALLOW_THREADS |
| 2121 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2122 | res = _getcwd2(buf, sizeof buf); |
| 2123 | #else |
| 2124 | res = getcwd(buf, sizeof buf); |
| 2125 | #endif |
| 2126 | Py_END_ALLOW_THREADS |
| 2127 | if (res == NULL) |
| 2128 | return posix_error(); |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2129 | if (use_bytes) |
| 2130 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2131 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 2132 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2133 | |
| 2134 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2135 | "getcwd() -> path\n\n\ |
| 2136 | Return a unicode string representing the current working directory."); |
| 2137 | |
| 2138 | static PyObject * |
| 2139 | posix_getcwd_unicode(PyObject *self) |
| 2140 | { |
| 2141 | return posix_getcwd(0); |
| 2142 | } |
| 2143 | |
| 2144 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2145 | "getcwdb() -> path\n\n\ |
| 2146 | Return a bytes string representing the current working directory."); |
| 2147 | |
| 2148 | static PyObject * |
| 2149 | posix_getcwd_bytes(PyObject *self) |
| 2150 | { |
| 2151 | return posix_getcwd(1); |
| 2152 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2153 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2154 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2155 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2156 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2157 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2158 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2159 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2160 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2161 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2162 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2163 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2164 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2165 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2166 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2167 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2169 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2170 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2171 | Return a list containing the names of the entries in the directory.\n\ |
| 2172 | \n\ |
| 2173 | path: path of directory to list\n\ |
| 2174 | \n\ |
| 2175 | 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] | 2176 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2177 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2178 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2179 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2180 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2181 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2182 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2183 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2185 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2186 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2187 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2188 | WIN32_FIND_DATA FileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2189 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2190 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2191 | 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] | 2192 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2193 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2194 | /* If on wide-character-capable OS see if argument |
| 2195 | is Unicode and if so use wide API. */ |
| 2196 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2197 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2198 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2199 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2200 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2201 | Py_UNICODE wch; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2202 | /* Overallocate for \\*.*\0 */ |
| 2203 | len = PyUnicode_GET_SIZE(po); |
| 2204 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2205 | if (!wnamebuf) { |
| 2206 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2207 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2208 | } |
| 2209 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2210 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 2211 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2212 | wnamebuf[len++] = L'\\'; |
| 2213 | wcscpy(wnamebuf + len, L"*.*"); |
| 2214 | if ((d = PyList_New(0)) == NULL) { |
| 2215 | free(wnamebuf); |
| 2216 | return NULL; |
| 2217 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2218 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2219 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2220 | int error = GetLastError(); |
| 2221 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2222 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2223 | return d; |
| 2224 | } |
| 2225 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2226 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2227 | free(wnamebuf); |
| 2228 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2229 | } |
| 2230 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2231 | /* Skip over . and .. */ |
| 2232 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2233 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2234 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2235 | if (v == NULL) { |
| 2236 | Py_DECREF(d); |
| 2237 | d = NULL; |
| 2238 | break; |
| 2239 | } |
| 2240 | if (PyList_Append(d, v) != 0) { |
| 2241 | Py_DECREF(v); |
| 2242 | Py_DECREF(d); |
| 2243 | d = NULL; |
| 2244 | break; |
| 2245 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2246 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2247 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2248 | Py_BEGIN_ALLOW_THREADS |
| 2249 | result = FindNextFileW(hFindFile, &wFileData); |
| 2250 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2251 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2252 | it got to the end of the directory. */ |
| 2253 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2254 | Py_DECREF(d); |
| 2255 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2256 | FindClose(hFindFile); |
| 2257 | free(wnamebuf); |
| 2258 | return NULL; |
| 2259 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2260 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2261 | |
| 2262 | if (FindClose(hFindFile) == FALSE) { |
| 2263 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2264 | win32_error_unicode("FindClose", wnamebuf); |
| 2265 | free(wnamebuf); |
| 2266 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2267 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2268 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2269 | return d; |
| 2270 | } |
| 2271 | /* Drop the argument parsing error as narrow strings |
| 2272 | are also valid. */ |
| 2273 | PyErr_Clear(); |
| 2274 | } |
| 2275 | #endif |
| 2276 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2277 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2278 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2279 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2280 | if (len > 0) { |
| 2281 | char ch = namebuf[len-1]; |
| 2282 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2283 | namebuf[len++] = '/'; |
| 2284 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2285 | strcpy(namebuf + len, "*.*"); |
| 2286 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2287 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2288 | return NULL; |
| 2289 | |
| 2290 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2291 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2292 | int error = GetLastError(); |
| 2293 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2294 | return d; |
| 2295 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2296 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2297 | } |
| 2298 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2299 | /* Skip over . and .. */ |
| 2300 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2301 | strcmp(FileData.cFileName, "..") != 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2302 | v = PyBytes_FromString(FileData.cFileName); |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2303 | if (v == NULL) { |
| 2304 | Py_DECREF(d); |
| 2305 | d = NULL; |
| 2306 | break; |
| 2307 | } |
| 2308 | if (PyList_Append(d, v) != 0) { |
| 2309 | Py_DECREF(v); |
| 2310 | Py_DECREF(d); |
| 2311 | d = NULL; |
| 2312 | break; |
| 2313 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2314 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2315 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2316 | Py_BEGIN_ALLOW_THREADS |
| 2317 | result = FindNextFile(hFindFile, &FileData); |
| 2318 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2319 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2320 | it got to the end of the directory. */ |
| 2321 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2322 | Py_DECREF(d); |
| 2323 | win32_error("FindNextFile", namebuf); |
| 2324 | FindClose(hFindFile); |
| 2325 | return NULL; |
| 2326 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2327 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2328 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2329 | if (FindClose(hFindFile) == FALSE) { |
| 2330 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2331 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2332 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2333 | |
| 2334 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2335 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2336 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2337 | |
| 2338 | #ifndef MAX_PATH |
| 2339 | #define MAX_PATH CCHMAXPATH |
| 2340 | #endif |
| 2341 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2342 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2343 | PyObject *d, *v; |
| 2344 | char namebuf[MAX_PATH+5]; |
| 2345 | HDIR hdir = 1; |
| 2346 | ULONG srchcnt = 1; |
| 2347 | FILEFINDBUF3 ep; |
| 2348 | APIRET rc; |
| 2349 | |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 2350 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 2351 | Py_FileSystemDefaultEncoding, &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2352 | return NULL; |
| 2353 | if (len >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2354 | PyMem_Free(name); |
| 2355 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2356 | return NULL; |
| 2357 | } |
| 2358 | strcpy(namebuf, name); |
| 2359 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2360 | if (*pt == ALTSEP) |
| 2361 | *pt = SEP; |
| 2362 | if (namebuf[len-1] != SEP) |
| 2363 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2364 | strcpy(namebuf + len, "*.*"); |
| 2365 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2366 | if ((d = PyList_New(0)) == NULL) { |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2367 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2368 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2369 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2370 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2371 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2372 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2373 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2374 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2375 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2376 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2377 | |
| 2378 | if (rc != NO_ERROR) { |
| 2379 | errno = ENOENT; |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2380 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2383 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2384 | do { |
| 2385 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2386 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2387 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2388 | |
| 2389 | strcpy(namebuf, ep.achName); |
| 2390 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2391 | /* Leave Case of Name Alone -- In Native Form */ |
| 2392 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2393 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2394 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2395 | if (v == NULL) { |
| 2396 | Py_DECREF(d); |
| 2397 | d = NULL; |
| 2398 | break; |
| 2399 | } |
| 2400 | if (PyList_Append(d, v) != 0) { |
| 2401 | Py_DECREF(v); |
| 2402 | Py_DECREF(d); |
| 2403 | d = NULL; |
| 2404 | break; |
| 2405 | } |
| 2406 | Py_DECREF(v); |
| 2407 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2408 | } |
| 2409 | |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2410 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2411 | return d; |
| 2412 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2413 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2414 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2415 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2416 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2417 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2418 | int arg_is_unicode = 1; |
| 2419 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2420 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2421 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2422 | arg_is_unicode = 0; |
| 2423 | PyErr_Clear(); |
| 2424 | } |
| 2425 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2426 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2427 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2428 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2429 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2430 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2431 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2432 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2433 | return NULL; |
| 2434 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2435 | for (;;) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2436 | errno = 0; |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2437 | Py_BEGIN_ALLOW_THREADS |
| 2438 | ep = readdir(dirp); |
| 2439 | Py_END_ALLOW_THREADS |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2440 | if (ep == NULL) { |
| 2441 | if (errno == 0) { |
| 2442 | break; |
| 2443 | } else { |
| 2444 | closedir(dirp); |
| 2445 | Py_DECREF(d); |
| 2446 | return posix_error_with_allocated_filename(name); |
| 2447 | } |
| 2448 | } |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2449 | if (ep->d_name[0] == '.' && |
| 2450 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2451 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2452 | continue; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2453 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2454 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2455 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2456 | d = NULL; |
| 2457 | break; |
| 2458 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2459 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2460 | PyObject *w; |
| 2461 | |
| 2462 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2463 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2464 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2465 | if (w != NULL) { |
| 2466 | Py_DECREF(v); |
| 2467 | v = w; |
| 2468 | } |
| 2469 | else { |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2470 | /* Ignore undecodable filenames, as discussed |
| 2471 | * in issue 3187. To include these, |
| 2472 | * use getcwdb(). */ |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2473 | PyErr_Clear(); |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2474 | Py_DECREF(v); |
| 2475 | continue; |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2476 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2477 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2478 | if (PyList_Append(d, v) != 0) { |
| 2479 | Py_DECREF(v); |
| 2480 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2481 | d = NULL; |
| 2482 | break; |
| 2483 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2484 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2485 | } |
| 2486 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2487 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2488 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2489 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2490 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2491 | #endif /* which OS */ |
| 2492 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2493 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2494 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2495 | /* A helper function for abspath on win32 */ |
| 2496 | static PyObject * |
| 2497 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2498 | { |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 2499 | /* assume encoded strings won't more than double no of chars */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2500 | char inbuf[MAX_PATH*2]; |
| 2501 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2502 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2503 | char outbuf[MAX_PATH*2]; |
| 2504 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2505 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2506 | if (unicode_file_names()) { |
| 2507 | PyUnicodeObject *po; |
| 2508 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 2509 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2510 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2511 | Py_UNICODE *wtemp; |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 2512 | DWORD result; |
| 2513 | PyObject *v; |
| 2514 | result = GetFullPathNameW(wpath, |
| 2515 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2516 | woutbuf, &wtemp); |
| 2517 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2518 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2519 | if (!woutbufp) |
| 2520 | return PyErr_NoMemory(); |
| 2521 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2522 | } |
| 2523 | if (result) |
| 2524 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2525 | else |
| 2526 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2527 | if (woutbufp != woutbuf) |
| 2528 | free(woutbufp); |
| 2529 | return v; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2530 | } |
| 2531 | /* Drop the argument parsing error as narrow strings |
| 2532 | are also valid. */ |
| 2533 | PyErr_Clear(); |
| 2534 | } |
| 2535 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2536 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2537 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2538 | &insize)) |
| 2539 | return NULL; |
| 2540 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2541 | outbuf, &temp)) |
| 2542 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2543 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2544 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2545 | Py_FileSystemDefaultEncoding, NULL); |
| 2546 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2547 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2548 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2549 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2550 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2551 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2552 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2553 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2554 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2555 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2556 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2557 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2558 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2559 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2560 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2561 | |
| 2562 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2563 | if (unicode_file_names()) { |
| 2564 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2565 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2566 | Py_BEGIN_ALLOW_THREADS |
| 2567 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2568 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2569 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2570 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2571 | if (!res) |
| 2572 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2573 | Py_INCREF(Py_None); |
| 2574 | return Py_None; |
| 2575 | } |
| 2576 | /* Drop the argument parsing error as narrow strings |
| 2577 | are also valid. */ |
| 2578 | PyErr_Clear(); |
| 2579 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2580 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2581 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2582 | return NULL; |
| 2583 | Py_BEGIN_ALLOW_THREADS |
| 2584 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2585 | it is a simple dereference. */ |
| 2586 | res = CreateDirectoryA(path, NULL); |
| 2587 | Py_END_ALLOW_THREADS |
| 2588 | if (!res) { |
| 2589 | win32_error("mkdir", path); |
| 2590 | PyMem_Free(path); |
| 2591 | return NULL; |
| 2592 | } |
| 2593 | PyMem_Free(path); |
| 2594 | Py_INCREF(Py_None); |
| 2595 | return Py_None; |
| 2596 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2597 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2598 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2599 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2600 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2601 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2602 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2603 | res = mkdir(path); |
| 2604 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2605 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2606 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2607 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2608 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2609 | return posix_error_with_allocated_filename(path); |
| 2610 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2611 | Py_INCREF(Py_None); |
| 2612 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2613 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2616 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2617 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2618 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2619 | #include <sys/resource.h> |
| 2620 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2621 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2622 | |
| 2623 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2624 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2625 | "nice(inc) -> new_priority\n\n\ |
| 2626 | 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] | 2627 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2628 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2629 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2630 | { |
| 2631 | int increment, value; |
| 2632 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2633 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2634 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2635 | |
| 2636 | /* There are two flavours of 'nice': one that returns the new |
| 2637 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2638 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2639 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2640 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2641 | If we are of the nice family that returns the new priority, we |
| 2642 | need to clear errno before the call, and check if errno is filled |
| 2643 | before calling posix_error() on a returnvalue of -1, because the |
| 2644 | -1 may be the actual new priority! */ |
| 2645 | |
| 2646 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2647 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2648 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2649 | if (value == 0) |
| 2650 | value = getpriority(PRIO_PROCESS, 0); |
| 2651 | #endif |
| 2652 | if (value == -1 && errno != 0) |
| 2653 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2654 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2655 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2656 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2657 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2658 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2659 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2660 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2661 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2662 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2663 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2664 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2665 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2666 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2667 | PyObject *o1, *o2; |
| 2668 | char *p1, *p2; |
| 2669 | BOOL result; |
| 2670 | if (unicode_file_names()) { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2671 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 2672 | goto error; |
| 2673 | if (!convert_to_unicode(&o1)) |
| 2674 | goto error; |
| 2675 | if (!convert_to_unicode(&o2)) { |
| 2676 | Py_DECREF(o1); |
| 2677 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2678 | } |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2679 | Py_BEGIN_ALLOW_THREADS |
| 2680 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2681 | PyUnicode_AsUnicode(o2)); |
| 2682 | Py_END_ALLOW_THREADS |
| 2683 | Py_DECREF(o1); |
| 2684 | Py_DECREF(o2); |
| 2685 | if (!result) |
| 2686 | return win32_error("rename", NULL); |
| 2687 | Py_INCREF(Py_None); |
| 2688 | return Py_None; |
| 2689 | error: |
| 2690 | PyErr_Clear(); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2691 | } |
| 2692 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2693 | return NULL; |
| 2694 | Py_BEGIN_ALLOW_THREADS |
| 2695 | result = MoveFileA(p1, p2); |
| 2696 | Py_END_ALLOW_THREADS |
| 2697 | if (!result) |
| 2698 | return win32_error("rename", NULL); |
| 2699 | Py_INCREF(Py_None); |
| 2700 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2701 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2702 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2703 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2707 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2708 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2709 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2710 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2711 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2712 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2713 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2714 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2715 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2716 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2717 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2718 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2721 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2722 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2723 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2724 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2725 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2726 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2727 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2728 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2729 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2730 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2731 | #else |
| 2732 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2733 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2736 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2737 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2738 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2739 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2740 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2741 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2742 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2743 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2744 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2745 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2746 | #ifdef MS_WINDOWS |
| 2747 | wchar_t *command; |
| 2748 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2749 | return NULL; |
| 2750 | #else |
| 2751 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2752 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2753 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2754 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2755 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2756 | #ifdef MS_WINDOWS |
| 2757 | sts = _wsystem(command); |
| 2758 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2759 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2760 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2761 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2762 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2763 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2764 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2765 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2766 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2767 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2768 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2769 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2770 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2771 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2772 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2773 | { |
| 2774 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2775 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2776 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2777 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2778 | if (i < 0) |
| 2779 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2780 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2784 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2785 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2786 | Remove a file (same as remove(path))."); |
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_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2789 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2790 | Remove a file (same as unlink(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_unlink(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 |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2796 | return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2797 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2798 | return posix_1str(args, "et:remove", unlink); |
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 | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2803 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2804 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2805 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2806 | Return a tuple identifying the current operating system."); |
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 * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2809 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2810 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2811 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2812 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2813 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2814 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2815 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2816 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2817 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2818 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2819 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2820 | u.sysname, |
| 2821 | u.nodename, |
| 2822 | u.release, |
| 2823 | u.version, |
| 2824 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2825 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2826 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2827 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2828 | static int |
| 2829 | extract_time(PyObject *t, long* sec, long* usec) |
| 2830 | { |
| 2831 | long intval; |
| 2832 | if (PyFloat_Check(t)) { |
| 2833 | double tval = PyFloat_AsDouble(t); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2834 | 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] | 2835 | if (!intobj) |
| 2836 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2837 | intval = PyLong_AsLong(intobj); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2838 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2839 | if (intval == -1 && PyErr_Occurred()) |
| 2840 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2841 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2842 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2843 | if (*usec < 0) |
| 2844 | /* If rounding gave us a negative number, |
| 2845 | truncate. */ |
| 2846 | *usec = 0; |
| 2847 | return 0; |
| 2848 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2849 | intval = PyLong_AsLong(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2850 | if (intval == -1 && PyErr_Occurred()) |
| 2851 | return -1; |
| 2852 | *sec = intval; |
| 2853 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2854 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2855 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2857 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2858 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2859 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2860 | 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] | 2861 | 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] | 2862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2864 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2865 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2866 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2867 | PyObject *arg; |
| 2868 | PyUnicodeObject *obwpath; |
| 2869 | wchar_t *wpath = NULL; |
| 2870 | char *apath = NULL; |
| 2871 | HANDLE hFile; |
| 2872 | long atimesec, mtimesec, ausec, musec; |
| 2873 | FILETIME atime, mtime; |
| 2874 | PyObject *result = NULL; |
| 2875 | |
| 2876 | if (unicode_file_names()) { |
| 2877 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2878 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2879 | Py_BEGIN_ALLOW_THREADS |
| 2880 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2881 | NULL, OPEN_EXISTING, |
| 2882 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2883 | Py_END_ALLOW_THREADS |
| 2884 | if (hFile == INVALID_HANDLE_VALUE) |
| 2885 | return win32_error_unicode("utime", wpath); |
| 2886 | } else |
| 2887 | /* Drop the argument parsing error as narrow strings |
| 2888 | are also valid. */ |
| 2889 | PyErr_Clear(); |
| 2890 | } |
| 2891 | if (!wpath) { |
| 2892 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2893 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2894 | return NULL; |
| 2895 | Py_BEGIN_ALLOW_THREADS |
| 2896 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2897 | NULL, OPEN_EXISTING, |
| 2898 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2899 | Py_END_ALLOW_THREADS |
| 2900 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2901 | win32_error("utime", apath); |
| 2902 | PyMem_Free(apath); |
| 2903 | return NULL; |
| 2904 | } |
| 2905 | PyMem_Free(apath); |
| 2906 | } |
| 2907 | |
| 2908 | if (arg == Py_None) { |
| 2909 | SYSTEMTIME now; |
| 2910 | GetSystemTime(&now); |
| 2911 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2912 | !SystemTimeToFileTime(&now, &atime)) { |
| 2913 | win32_error("utime", NULL); |
| 2914 | goto done; |
| 2915 | } |
| 2916 | } |
| 2917 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2918 | PyErr_SetString(PyExc_TypeError, |
| 2919 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2920 | goto done; |
| 2921 | } |
| 2922 | else { |
| 2923 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2924 | &atimesec, &ausec) == -1) |
| 2925 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2926 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2927 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2928 | &mtimesec, &musec) == -1) |
| 2929 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2930 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2931 | } |
| 2932 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2933 | /* Avoid putting the file name into the error here, |
| 2934 | as that may confuse the user into believing that |
| 2935 | something is wrong with the file, when it also |
| 2936 | could be the time stamp that gives a problem. */ |
| 2937 | win32_error("utime", NULL); |
| 2938 | } |
| 2939 | Py_INCREF(Py_None); |
| 2940 | result = Py_None; |
| 2941 | done: |
| 2942 | CloseHandle(hFile); |
| 2943 | return result; |
| 2944 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2945 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2946 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2947 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2948 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2949 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2950 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2951 | #if defined(HAVE_UTIMES) |
| 2952 | struct timeval buf[2]; |
| 2953 | #define ATIME buf[0].tv_sec |
| 2954 | #define MTIME buf[1].tv_sec |
| 2955 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2956 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2957 | struct utimbuf buf; |
| 2958 | #define ATIME buf.actime |
| 2959 | #define MTIME buf.modtime |
| 2960 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2961 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2962 | time_t buf[2]; |
| 2963 | #define ATIME buf[0] |
| 2964 | #define MTIME buf[1] |
| 2965 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2966 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2967 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2968 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2969 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2970 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2971 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2972 | if (arg == Py_None) { |
| 2973 | /* optional time values not given */ |
| 2974 | Py_BEGIN_ALLOW_THREADS |
| 2975 | res = utime(path, NULL); |
| 2976 | Py_END_ALLOW_THREADS |
| 2977 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2978 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2979 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2980 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2981 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2982 | return NULL; |
| 2983 | } |
| 2984 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2985 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2986 | &atime, &ausec) == -1) { |
| 2987 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2988 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2989 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2990 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2991 | &mtime, &musec) == -1) { |
| 2992 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2993 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2994 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2995 | ATIME = atime; |
| 2996 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2997 | #ifdef HAVE_UTIMES |
| 2998 | buf[0].tv_usec = ausec; |
| 2999 | buf[1].tv_usec = musec; |
| 3000 | Py_BEGIN_ALLOW_THREADS |
| 3001 | res = utimes(path, buf); |
| 3002 | Py_END_ALLOW_THREADS |
| 3003 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3004 | Py_BEGIN_ALLOW_THREADS |
| 3005 | res = utime(path, UTIME_ARG); |
| 3006 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3007 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3008 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 3009 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3010 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 3011 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3012 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3013 | Py_INCREF(Py_None); |
| 3014 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3015 | #undef UTIME_ARG |
| 3016 | #undef ATIME |
| 3017 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3018 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3021 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3022 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3023 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3024 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3025 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3026 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3027 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3028 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3029 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3030 | { |
| 3031 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3032 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3033 | return NULL; |
| 3034 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 3035 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3038 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3039 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3040 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3041 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3042 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3043 | for (i = 0; i < count; i++) |
| 3044 | PyMem_Free(array[i]); |
| 3045 | PyMem_DEL(array); |
| 3046 | } |
| 3047 | #endif |
| 3048 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3049 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3050 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3051 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3052 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3053 | Execute an executable path with arguments, replacing current process.\n\ |
| 3054 | \n\ |
| 3055 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3056 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3057 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3058 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3059 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3060 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3061 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3062 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3063 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3064 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3065 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3066 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 3067 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3068 | argv is a list or tuple of strings. */ |
| 3069 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3070 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 3071 | Py_FileSystemDefaultEncoding, |
| 3072 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3073 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3074 | if (PyList_Check(argv)) { |
| 3075 | argc = PyList_Size(argv); |
| 3076 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3077 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3078 | else if (PyTuple_Check(argv)) { |
| 3079 | argc = PyTuple_Size(argv); |
| 3080 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3081 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3082 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3083 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3084 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3085 | return NULL; |
| 3086 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3087 | if (argc < 1) { |
| 3088 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 3089 | PyMem_Free(path); |
| 3090 | return NULL; |
| 3091 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3092 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3093 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3094 | if (argvlist == NULL) { |
| 3095 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3096 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3097 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3098 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3099 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3100 | Py_FileSystemDefaultEncoding, |
| 3101 | &argvlist[i])) { |
| 3102 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3103 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3104 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3105 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3106 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3107 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3108 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3109 | } |
| 3110 | argvlist[argc] = NULL; |
| 3111 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3112 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3113 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3114 | /* If we get here it's definitely an error */ |
| 3115 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3116 | free_string_array(argvlist, argc); |
| 3117 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3118 | return posix_error(); |
| 3119 | } |
| 3120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3122 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3123 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3124 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3125 | \n\ |
| 3126 | path: path of executable file\n\ |
| 3127 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3128 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3129 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3130 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3131 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3132 | { |
| 3133 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3134 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3135 | char **argvlist; |
| 3136 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3137 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3138 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3139 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3140 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3141 | |
| 3142 | /* execve has three arguments: (path, argv, env), where |
| 3143 | argv is a list or tuple of strings and env is a dictionary |
| 3144 | like posix.environ. */ |
| 3145 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3146 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 3147 | Py_FileSystemDefaultEncoding, |
| 3148 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3149 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3150 | if (PyList_Check(argv)) { |
| 3151 | argc = PyList_Size(argv); |
| 3152 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3153 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3154 | else if (PyTuple_Check(argv)) { |
| 3155 | argc = PyTuple_Size(argv); |
| 3156 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3157 | } |
| 3158 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3159 | PyErr_SetString(PyExc_TypeError, |
| 3160 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3161 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3162 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3163 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3164 | PyErr_SetString(PyExc_TypeError, |
| 3165 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3166 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3169 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3170 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3171 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3172 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3173 | } |
| 3174 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3175 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3176 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 3177 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3178 | &argvlist[i])) |
| 3179 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3180 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3181 | goto fail_1; |
| 3182 | } |
| 3183 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3184 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3185 | argvlist[argc] = NULL; |
| 3186 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3187 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3188 | if (i < 0) |
| 3189 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3190 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3191 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3192 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3193 | goto fail_1; |
| 3194 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3195 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3196 | keys = PyMapping_Keys(env); |
| 3197 | vals = PyMapping_Values(env); |
| 3198 | if (!keys || !vals) |
| 3199 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3200 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3201 | PyErr_SetString(PyExc_TypeError, |
| 3202 | "execve(): env.keys() or env.values() is not a list"); |
| 3203 | goto fail_2; |
| 3204 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3205 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3206 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3207 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3208 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3209 | |
| 3210 | key = PyList_GetItem(keys, pos); |
| 3211 | val = PyList_GetItem(vals, pos); |
| 3212 | if (!key || !val) |
| 3213 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3214 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3215 | if (!PyArg_Parse( |
| 3216 | key, |
| 3217 | "s;execve() arg 3 contains a non-string key", |
| 3218 | &k) || |
| 3219 | !PyArg_Parse( |
| 3220 | val, |
| 3221 | "s;execve() arg 3 contains a non-string value", |
| 3222 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3223 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3224 | goto fail_2; |
| 3225 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3226 | |
| 3227 | #if defined(PYOS_OS2) |
| 3228 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3229 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3230 | #endif |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3231 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3232 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3233 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3234 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3235 | goto fail_2; |
| 3236 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3237 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3238 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3239 | #if defined(PYOS_OS2) |
| 3240 | } |
| 3241 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3242 | } |
| 3243 | envlist[envc] = 0; |
| 3244 | |
| 3245 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3246 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3247 | /* If we get here it's definitely an error */ |
| 3248 | |
| 3249 | (void) posix_error(); |
| 3250 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3251 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3252 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3253 | PyMem_DEL(envlist[envc]); |
| 3254 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3255 | fail_1: |
| 3256 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3257 | Py_XDECREF(vals); |
| 3258 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3259 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3260 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3261 | return NULL; |
| 3262 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3263 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3264 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3265 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3266 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3267 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3268 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3269 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3270 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3271 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3272 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3273 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3274 | |
| 3275 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3276 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3277 | { |
| 3278 | char *path; |
| 3279 | PyObject *argv; |
| 3280 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3281 | int mode, i; |
| 3282 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3283 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3284 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3285 | |
| 3286 | /* spawnv has three arguments: (mode, path, argv), where |
| 3287 | argv is a list or tuple of strings. */ |
| 3288 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3289 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3290 | Py_FileSystemDefaultEncoding, |
| 3291 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3292 | return NULL; |
| 3293 | if (PyList_Check(argv)) { |
| 3294 | argc = PyList_Size(argv); |
| 3295 | getitem = PyList_GetItem; |
| 3296 | } |
| 3297 | else if (PyTuple_Check(argv)) { |
| 3298 | argc = PyTuple_Size(argv); |
| 3299 | getitem = PyTuple_GetItem; |
| 3300 | } |
| 3301 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3302 | PyErr_SetString(PyExc_TypeError, |
| 3303 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3304 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3305 | return NULL; |
| 3306 | } |
| 3307 | |
| 3308 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3309 | if (argvlist == NULL) { |
| 3310 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3311 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3312 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3313 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3314 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3315 | Py_FileSystemDefaultEncoding, |
| 3316 | &argvlist[i])) { |
| 3317 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3318 | PyErr_SetString( |
| 3319 | PyExc_TypeError, |
| 3320 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3321 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3322 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | argvlist[argc] = NULL; |
| 3326 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3327 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3328 | Py_BEGIN_ALLOW_THREADS |
| 3329 | spawnval = spawnv(mode, path, argvlist); |
| 3330 | Py_END_ALLOW_THREADS |
| 3331 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3332 | if (mode == _OLD_P_OVERLAY) |
| 3333 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3334 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3335 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3336 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3337 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3338 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3339 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3340 | free_string_array(argvlist, argc); |
| 3341 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3342 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3343 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3344 | return posix_error(); |
| 3345 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3346 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3347 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3348 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3349 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3350 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3354 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3355 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3356 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3357 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3358 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3359 | path: path of executable file\n\ |
| 3360 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3361 | env: dictionary of strings mapping to 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_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3365 | { |
| 3366 | char *path; |
| 3367 | PyObject *argv, *env; |
| 3368 | char **argvlist; |
| 3369 | char **envlist; |
| 3370 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3371 | int mode, pos, envc; |
| 3372 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3373 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3374 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3375 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3376 | |
| 3377 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3378 | argv is a list or tuple of strings and env is a dictionary |
| 3379 | like posix.environ. */ |
| 3380 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3381 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3382 | Py_FileSystemDefaultEncoding, |
| 3383 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3384 | return NULL; |
| 3385 | if (PyList_Check(argv)) { |
| 3386 | argc = PyList_Size(argv); |
| 3387 | getitem = PyList_GetItem; |
| 3388 | } |
| 3389 | else if (PyTuple_Check(argv)) { |
| 3390 | argc = PyTuple_Size(argv); |
| 3391 | getitem = PyTuple_GetItem; |
| 3392 | } |
| 3393 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3394 | PyErr_SetString(PyExc_TypeError, |
| 3395 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3396 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3397 | } |
| 3398 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3399 | PyErr_SetString(PyExc_TypeError, |
| 3400 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3401 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | argvlist = PyMem_NEW(char *, argc+1); |
| 3405 | if (argvlist == NULL) { |
| 3406 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3407 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3408 | } |
| 3409 | for (i = 0; i < argc; i++) { |
| 3410 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3411 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3412 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3413 | &argvlist[i])) |
| 3414 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3415 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3416 | goto fail_1; |
| 3417 | } |
| 3418 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3419 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3420 | argvlist[argc] = NULL; |
| 3421 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3422 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3423 | if (i < 0) |
| 3424 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3425 | envlist = PyMem_NEW(char *, i + 1); |
| 3426 | if (envlist == NULL) { |
| 3427 | PyErr_NoMemory(); |
| 3428 | goto fail_1; |
| 3429 | } |
| 3430 | envc = 0; |
| 3431 | keys = PyMapping_Keys(env); |
| 3432 | vals = PyMapping_Values(env); |
| 3433 | if (!keys || !vals) |
| 3434 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3435 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3436 | PyErr_SetString(PyExc_TypeError, |
| 3437 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3438 | goto fail_2; |
| 3439 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3440 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3441 | for (pos = 0; pos < i; pos++) { |
| 3442 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3443 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3444 | |
| 3445 | key = PyList_GetItem(keys, pos); |
| 3446 | val = PyList_GetItem(vals, pos); |
| 3447 | if (!key || !val) |
| 3448 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3449 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3450 | if (!PyArg_Parse( |
| 3451 | key, |
| 3452 | "s;spawnve() arg 3 contains a non-string key", |
| 3453 | &k) || |
| 3454 | !PyArg_Parse( |
| 3455 | val, |
| 3456 | "s;spawnve() arg 3 contains a non-string value", |
| 3457 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3458 | { |
| 3459 | goto fail_2; |
| 3460 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3461 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3462 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3463 | if (p == NULL) { |
| 3464 | PyErr_NoMemory(); |
| 3465 | goto fail_2; |
| 3466 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3467 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3468 | envlist[envc++] = p; |
| 3469 | } |
| 3470 | envlist[envc] = 0; |
| 3471 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3472 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3473 | Py_BEGIN_ALLOW_THREADS |
| 3474 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3475 | Py_END_ALLOW_THREADS |
| 3476 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3477 | if (mode == _OLD_P_OVERLAY) |
| 3478 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3479 | |
| 3480 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3481 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3482 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3483 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3484 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3485 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3486 | (void) posix_error(); |
| 3487 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3488 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3489 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3490 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3491 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3492 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3493 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3494 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3495 | while (--envc >= 0) |
| 3496 | PyMem_DEL(envlist[envc]); |
| 3497 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3498 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3499 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3500 | Py_XDECREF(vals); |
| 3501 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3502 | fail_0: |
| 3503 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3504 | return res; |
| 3505 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3506 | |
| 3507 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3508 | #if defined(PYOS_OS2) |
| 3509 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3510 | "spawnvp(mode, file, args)\n\n\ |
| 3511 | Execute the program 'file' in a new process, using the environment\n\ |
| 3512 | search path to find the file.\n\ |
| 3513 | \n\ |
| 3514 | mode: mode of process creation\n\ |
| 3515 | file: executable file name\n\ |
| 3516 | args: tuple or list of strings"); |
| 3517 | |
| 3518 | static PyObject * |
| 3519 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3520 | { |
| 3521 | char *path; |
| 3522 | PyObject *argv; |
| 3523 | char **argvlist; |
| 3524 | int mode, i, argc; |
| 3525 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3526 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3527 | |
| 3528 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3529 | argv is a list or tuple of strings. */ |
| 3530 | |
| 3531 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3532 | Py_FileSystemDefaultEncoding, |
| 3533 | &path, &argv)) |
| 3534 | return NULL; |
| 3535 | if (PyList_Check(argv)) { |
| 3536 | argc = PyList_Size(argv); |
| 3537 | getitem = PyList_GetItem; |
| 3538 | } |
| 3539 | else if (PyTuple_Check(argv)) { |
| 3540 | argc = PyTuple_Size(argv); |
| 3541 | getitem = PyTuple_GetItem; |
| 3542 | } |
| 3543 | else { |
| 3544 | PyErr_SetString(PyExc_TypeError, |
| 3545 | "spawnvp() arg 2 must be a tuple or list"); |
| 3546 | PyMem_Free(path); |
| 3547 | return NULL; |
| 3548 | } |
| 3549 | |
| 3550 | argvlist = PyMem_NEW(char *, argc+1); |
| 3551 | if (argvlist == NULL) { |
| 3552 | PyMem_Free(path); |
| 3553 | return PyErr_NoMemory(); |
| 3554 | } |
| 3555 | for (i = 0; i < argc; i++) { |
| 3556 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3557 | Py_FileSystemDefaultEncoding, |
| 3558 | &argvlist[i])) { |
| 3559 | free_string_array(argvlist, i); |
| 3560 | PyErr_SetString( |
| 3561 | PyExc_TypeError, |
| 3562 | "spawnvp() arg 2 must contain only strings"); |
| 3563 | PyMem_Free(path); |
| 3564 | return NULL; |
| 3565 | } |
| 3566 | } |
| 3567 | argvlist[argc] = NULL; |
| 3568 | |
| 3569 | Py_BEGIN_ALLOW_THREADS |
| 3570 | #if defined(PYCC_GCC) |
| 3571 | spawnval = spawnvp(mode, path, argvlist); |
| 3572 | #else |
| 3573 | spawnval = _spawnvp(mode, path, argvlist); |
| 3574 | #endif |
| 3575 | Py_END_ALLOW_THREADS |
| 3576 | |
| 3577 | free_string_array(argvlist, argc); |
| 3578 | PyMem_Free(path); |
| 3579 | |
| 3580 | if (spawnval == -1) |
| 3581 | return posix_error(); |
| 3582 | else |
| 3583 | return Py_BuildValue("l", (long) spawnval); |
| 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3588 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3589 | Execute the program 'file' in a new process, using the environment\n\ |
| 3590 | search path to find the file.\n\ |
| 3591 | \n\ |
| 3592 | mode: mode of process creation\n\ |
| 3593 | file: executable file name\n\ |
| 3594 | args: tuple or list of arguments\n\ |
| 3595 | env: dictionary of strings mapping to strings"); |
| 3596 | |
| 3597 | static PyObject * |
| 3598 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3599 | { |
| 3600 | char *path; |
| 3601 | PyObject *argv, *env; |
| 3602 | char **argvlist; |
| 3603 | char **envlist; |
| 3604 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3605 | int mode, i, pos, argc, envc; |
| 3606 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3607 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3608 | int lastarg = 0; |
| 3609 | |
| 3610 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3611 | argv is a list or tuple of strings and env is a dictionary |
| 3612 | like posix.environ. */ |
| 3613 | |
| 3614 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3615 | Py_FileSystemDefaultEncoding, |
| 3616 | &path, &argv, &env)) |
| 3617 | return NULL; |
| 3618 | if (PyList_Check(argv)) { |
| 3619 | argc = PyList_Size(argv); |
| 3620 | getitem = PyList_GetItem; |
| 3621 | } |
| 3622 | else if (PyTuple_Check(argv)) { |
| 3623 | argc = PyTuple_Size(argv); |
| 3624 | getitem = PyTuple_GetItem; |
| 3625 | } |
| 3626 | else { |
| 3627 | PyErr_SetString(PyExc_TypeError, |
| 3628 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3629 | goto fail_0; |
| 3630 | } |
| 3631 | if (!PyMapping_Check(env)) { |
| 3632 | PyErr_SetString(PyExc_TypeError, |
| 3633 | "spawnvpe() arg 3 must be a mapping object"); |
| 3634 | goto fail_0; |
| 3635 | } |
| 3636 | |
| 3637 | argvlist = PyMem_NEW(char *, argc+1); |
| 3638 | if (argvlist == NULL) { |
| 3639 | PyErr_NoMemory(); |
| 3640 | goto fail_0; |
| 3641 | } |
| 3642 | for (i = 0; i < argc; i++) { |
| 3643 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3644 | "et;spawnvpe() arg 2 must contain only strings", |
| 3645 | Py_FileSystemDefaultEncoding, |
| 3646 | &argvlist[i])) |
| 3647 | { |
| 3648 | lastarg = i; |
| 3649 | goto fail_1; |
| 3650 | } |
| 3651 | } |
| 3652 | lastarg = argc; |
| 3653 | argvlist[argc] = NULL; |
| 3654 | |
| 3655 | i = PyMapping_Size(env); |
| 3656 | if (i < 0) |
| 3657 | goto fail_1; |
| 3658 | envlist = PyMem_NEW(char *, i + 1); |
| 3659 | if (envlist == NULL) { |
| 3660 | PyErr_NoMemory(); |
| 3661 | goto fail_1; |
| 3662 | } |
| 3663 | envc = 0; |
| 3664 | keys = PyMapping_Keys(env); |
| 3665 | vals = PyMapping_Values(env); |
| 3666 | if (!keys || !vals) |
| 3667 | goto fail_2; |
| 3668 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3669 | PyErr_SetString(PyExc_TypeError, |
| 3670 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3671 | goto fail_2; |
| 3672 | } |
| 3673 | |
| 3674 | for (pos = 0; pos < i; pos++) { |
| 3675 | char *p, *k, *v; |
| 3676 | size_t len; |
| 3677 | |
| 3678 | key = PyList_GetItem(keys, pos); |
| 3679 | val = PyList_GetItem(vals, pos); |
| 3680 | if (!key || !val) |
| 3681 | goto fail_2; |
| 3682 | |
| 3683 | if (!PyArg_Parse( |
| 3684 | key, |
| 3685 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3686 | &k) || |
| 3687 | !PyArg_Parse( |
| 3688 | val, |
| 3689 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3690 | &v)) |
| 3691 | { |
| 3692 | goto fail_2; |
| 3693 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3694 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3695 | p = PyMem_NEW(char, len); |
| 3696 | if (p == NULL) { |
| 3697 | PyErr_NoMemory(); |
| 3698 | goto fail_2; |
| 3699 | } |
| 3700 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3701 | envlist[envc++] = p; |
| 3702 | } |
| 3703 | envlist[envc] = 0; |
| 3704 | |
| 3705 | Py_BEGIN_ALLOW_THREADS |
| 3706 | #if defined(PYCC_GCC) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3707 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3708 | #else |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3709 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3710 | #endif |
| 3711 | Py_END_ALLOW_THREADS |
| 3712 | |
| 3713 | if (spawnval == -1) |
| 3714 | (void) posix_error(); |
| 3715 | else |
| 3716 | res = Py_BuildValue("l", (long) spawnval); |
| 3717 | |
| 3718 | fail_2: |
| 3719 | while (--envc >= 0) |
| 3720 | PyMem_DEL(envlist[envc]); |
| 3721 | PyMem_DEL(envlist); |
| 3722 | fail_1: |
| 3723 | free_string_array(argvlist, lastarg); |
| 3724 | Py_XDECREF(vals); |
| 3725 | Py_XDECREF(keys); |
| 3726 | fail_0: |
| 3727 | PyMem_Free(path); |
| 3728 | return res; |
| 3729 | } |
| 3730 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3731 | #endif /* HAVE_SPAWNV */ |
| 3732 | |
| 3733 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3734 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3735 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3736 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3737 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3738 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3739 | 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] | 3740 | |
| 3741 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3742 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3743 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3744 | pid_t pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3745 | if (pid == -1) |
| 3746 | return posix_error(); |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 3747 | if (pid == 0) |
| 3748 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3749 | return PyLong_FromLong(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3750 | } |
| 3751 | #endif |
| 3752 | |
| 3753 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3754 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3755 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3756 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3757 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3758 | 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] | 3759 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3760 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3761 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3762 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3763 | pid_t pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3764 | if (pid == -1) |
| 3765 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3766 | if (pid == 0) |
| 3767 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3768 | return PyLong_FromLong(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3769 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3770 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3771 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3772 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3773 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3774 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3775 | #define DEV_PTY_FILE "/dev/ptc" |
| 3776 | #define HAVE_DEV_PTMX |
| 3777 | #else |
| 3778 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3779 | #endif |
| 3780 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3781 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3782 | #ifdef HAVE_PTY_H |
| 3783 | #include <pty.h> |
| 3784 | #else |
| 3785 | #ifdef HAVE_LIBUTIL_H |
| 3786 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3787 | #endif /* HAVE_LIBUTIL_H */ |
| 3788 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3789 | #ifdef HAVE_STROPTS_H |
| 3790 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3791 | #endif |
| 3792 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3793 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3794 | #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] | 3795 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3796 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3797 | 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] | 3798 | |
| 3799 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3800 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3801 | { |
| 3802 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3803 | #ifndef HAVE_OPENPTY |
| 3804 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3805 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3806 | #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] | 3807 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3808 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3809 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3810 | #endif |
| 3811 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3812 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3813 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3814 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3815 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3816 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3817 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3818 | if (slave_name == NULL) |
| 3819 | return posix_error(); |
| 3820 | |
| 3821 | slave_fd = open(slave_name, O_RDWR); |
| 3822 | if (slave_fd < 0) |
| 3823 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3824 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3825 | 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] | 3826 | if (master_fd < 0) |
| 3827 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3828 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3829 | /* change permission of slave */ |
| 3830 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3831 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3832 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3833 | } |
| 3834 | /* unlock slave */ |
| 3835 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3836 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3837 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3838 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3839 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3840 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3841 | if (slave_name == NULL) |
| 3842 | return posix_error(); |
| 3843 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3844 | if (slave_fd < 0) |
| 3845 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3846 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3847 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3848 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3849 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3850 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3851 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3852 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3853 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3854 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3855 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3856 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3857 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3858 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3859 | |
| 3860 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3861 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3862 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3863 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3864 | 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] | 3865 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3866 | |
| 3867 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3868 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3869 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3870 | int master_fd = -1; |
| 3871 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3872 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3873 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3874 | if (pid == -1) |
| 3875 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3876 | if (pid == 0) |
| 3877 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3878 | return Py_BuildValue("(li)", pid, master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3879 | } |
| 3880 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3881 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3882 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3883 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3884 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3885 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3886 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3887 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3888 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3889 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3890 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3891 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3892 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3893 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3894 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3895 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3896 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3897 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3898 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3899 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3900 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3901 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3902 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3903 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3904 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3905 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3906 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3907 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3908 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3909 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3910 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3911 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3912 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3913 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3914 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3915 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3916 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3917 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3918 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3919 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3920 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3921 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3922 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3923 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3924 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3925 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3926 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3927 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3928 | return PyLong_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3931 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3932 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3933 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3934 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3935 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3936 | |
| 3937 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3938 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3939 | { |
| 3940 | PyObject *result = NULL; |
| 3941 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3942 | #ifdef NGROUPS_MAX |
| 3943 | #define MAX_GROUPS NGROUPS_MAX |
| 3944 | #else |
| 3945 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3946 | #define MAX_GROUPS 64 |
| 3947 | #endif |
| 3948 | gid_t grouplist[MAX_GROUPS]; |
| 3949 | int n; |
| 3950 | |
| 3951 | n = getgroups(MAX_GROUPS, grouplist); |
| 3952 | if (n < 0) |
| 3953 | posix_error(); |
| 3954 | else { |
| 3955 | result = PyList_New(n); |
| 3956 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3957 | int i; |
| 3958 | for (i = 0; i < n; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3959 | PyObject *o = PyLong_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3960 | if (o == NULL) { |
| 3961 | Py_DECREF(result); |
| 3962 | result = NULL; |
| 3963 | break; |
| 3964 | } |
| 3965 | PyList_SET_ITEM(result, i, o); |
| 3966 | } |
| 3967 | } |
| 3968 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3969 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3970 | return result; |
| 3971 | } |
| 3972 | #endif |
| 3973 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3974 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3975 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3976 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3977 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3978 | |
| 3979 | static PyObject * |
| 3980 | posix_getpgid(PyObject *self, PyObject *args) |
| 3981 | { |
| 3982 | int pid, pgid; |
| 3983 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3984 | return NULL; |
| 3985 | pgid = getpgid(pid); |
| 3986 | if (pgid < 0) |
| 3987 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3988 | return PyLong_FromLong((long)pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3989 | } |
| 3990 | #endif /* HAVE_GETPGID */ |
| 3991 | |
| 3992 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3993 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3994 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3995 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3996 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3997 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3998 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3999 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4000 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4001 | #ifdef GETPGRP_HAVE_ARG |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4002 | return PyLong_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4003 | #else /* GETPGRP_HAVE_ARG */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4004 | return PyLong_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4005 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4006 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4007 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4008 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4009 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4010 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4011 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4012 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4013 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4014 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4015 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4016 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4017 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4018 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4019 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4020 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4021 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4022 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4023 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4024 | Py_INCREF(Py_None); |
| 4025 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4028 | #endif /* HAVE_SETPGRP */ |
| 4029 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4030 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4031 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4032 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4033 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4034 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4035 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4036 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4037 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4038 | return PyLong_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4039 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4040 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4041 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4042 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4043 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4044 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4045 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4046 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4047 | |
| 4048 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4049 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4050 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4051 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4052 | char *name; |
| 4053 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4054 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4055 | errno = 0; |
| 4056 | name = getlogin(); |
| 4057 | if (name == NULL) { |
| 4058 | if (errno) |
| 4059 | posix_error(); |
| 4060 | else |
| 4061 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 4062 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4063 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4064 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 4065 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4066 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4067 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4068 | return result; |
| 4069 | } |
| 4070 | #endif |
| 4071 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4072 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4073 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4074 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4075 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4076 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4077 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4078 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4079 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4080 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4081 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4082 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4083 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4084 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4085 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4086 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4087 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4088 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4089 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4090 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4091 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4092 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4093 | pid_t pid; |
| 4094 | int sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4095 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4096 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4097 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4098 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4099 | APIRET rc; |
| 4100 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4101 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4102 | |
| 4103 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4104 | APIRET rc; |
| 4105 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4106 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4107 | |
| 4108 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4109 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4110 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4111 | if (kill(pid, sig) == -1) |
| 4112 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4113 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4114 | Py_INCREF(Py_None); |
| 4115 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4116 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4117 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4118 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4119 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4120 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4121 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4122 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4123 | |
| 4124 | static PyObject * |
| 4125 | posix_killpg(PyObject *self, PyObject *args) |
| 4126 | { |
| 4127 | int pgid, sig; |
| 4128 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 4129 | return NULL; |
| 4130 | if (killpg(pgid, sig) == -1) |
| 4131 | return posix_error(); |
| 4132 | Py_INCREF(Py_None); |
| 4133 | return Py_None; |
| 4134 | } |
| 4135 | #endif |
| 4136 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4137 | #ifdef HAVE_PLOCK |
| 4138 | |
| 4139 | #ifdef HAVE_SYS_LOCK_H |
| 4140 | #include <sys/lock.h> |
| 4141 | #endif |
| 4142 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4143 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4144 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4145 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4146 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4147 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4148 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4149 | { |
| 4150 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4151 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4152 | return NULL; |
| 4153 | if (plock(op) == -1) |
| 4154 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4155 | Py_INCREF(Py_None); |
| 4156 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4157 | } |
| 4158 | #endif |
| 4159 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4160 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4161 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4162 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4163 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4164 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4165 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4166 | Set the current process's user id."); |
| 4167 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4168 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4169 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4170 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4171 | long uid_arg; |
| 4172 | uid_t uid; |
| 4173 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4174 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4175 | uid = uid_arg; |
| 4176 | if (uid != uid_arg) { |
| 4177 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4178 | return NULL; |
| 4179 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4180 | if (setuid(uid) < 0) |
| 4181 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4182 | Py_INCREF(Py_None); |
| 4183 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4184 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4185 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4186 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4187 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4188 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4189 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4190 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4191 | Set the current process's effective user id."); |
| 4192 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4193 | static PyObject * |
| 4194 | posix_seteuid (PyObject *self, PyObject *args) |
| 4195 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4196 | long euid_arg; |
| 4197 | uid_t euid; |
| 4198 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4199 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4200 | euid = euid_arg; |
| 4201 | if (euid != euid_arg) { |
| 4202 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4203 | return NULL; |
| 4204 | } |
| 4205 | if (seteuid(euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4206 | return posix_error(); |
| 4207 | } else { |
| 4208 | Py_INCREF(Py_None); |
| 4209 | return Py_None; |
| 4210 | } |
| 4211 | } |
| 4212 | #endif /* HAVE_SETEUID */ |
| 4213 | |
| 4214 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4215 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4216 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4217 | Set the current process's effective group id."); |
| 4218 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4219 | static PyObject * |
| 4220 | posix_setegid (PyObject *self, PyObject *args) |
| 4221 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4222 | long egid_arg; |
| 4223 | gid_t egid; |
| 4224 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4225 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4226 | egid = egid_arg; |
| 4227 | if (egid != egid_arg) { |
| 4228 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4229 | return NULL; |
| 4230 | } |
| 4231 | if (setegid(egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4232 | return posix_error(); |
| 4233 | } else { |
| 4234 | Py_INCREF(Py_None); |
| 4235 | return Py_None; |
| 4236 | } |
| 4237 | } |
| 4238 | #endif /* HAVE_SETEGID */ |
| 4239 | |
| 4240 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4241 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4242 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4243 | Set the current process's real and effective user ids."); |
| 4244 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4245 | static PyObject * |
| 4246 | posix_setreuid (PyObject *self, PyObject *args) |
| 4247 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4248 | long ruid_arg, euid_arg; |
| 4249 | uid_t ruid, euid; |
| 4250 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4251 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4252 | ruid = ruid_arg; |
| 4253 | euid = euid_arg; |
| 4254 | if (euid != euid_arg || ruid != ruid_arg) { |
| 4255 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4256 | return NULL; |
| 4257 | } |
| 4258 | if (setreuid(ruid, euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4259 | return posix_error(); |
| 4260 | } else { |
| 4261 | Py_INCREF(Py_None); |
| 4262 | return Py_None; |
| 4263 | } |
| 4264 | } |
| 4265 | #endif /* HAVE_SETREUID */ |
| 4266 | |
| 4267 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4268 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4269 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4270 | Set the current process's real and effective group ids."); |
| 4271 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4272 | static PyObject * |
| 4273 | posix_setregid (PyObject *self, PyObject *args) |
| 4274 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4275 | long rgid_arg, egid_arg; |
| 4276 | gid_t rgid, egid; |
| 4277 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4278 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4279 | rgid = rgid_arg; |
| 4280 | egid = egid_arg; |
| 4281 | if (egid != egid_arg || rgid != rgid_arg) { |
| 4282 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4283 | return NULL; |
| 4284 | } |
| 4285 | if (setregid(rgid, egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4286 | return posix_error(); |
| 4287 | } else { |
| 4288 | Py_INCREF(Py_None); |
| 4289 | return Py_None; |
| 4290 | } |
| 4291 | } |
| 4292 | #endif /* HAVE_SETREGID */ |
| 4293 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4294 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4295 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4296 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4297 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4298 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4299 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4300 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4301 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4302 | long gid_arg; |
| 4303 | gid_t gid; |
| 4304 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4305 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4306 | gid = gid_arg; |
| 4307 | if (gid != gid_arg) { |
| 4308 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4309 | return NULL; |
| 4310 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4311 | if (setgid(gid) < 0) |
| 4312 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4313 | Py_INCREF(Py_None); |
| 4314 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4315 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4316 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4317 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4318 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4319 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4320 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4321 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4322 | |
| 4323 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4324 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4325 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4326 | int i, len; |
| 4327 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4328 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4329 | if (!PySequence_Check(groups)) { |
| 4330 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4331 | return NULL; |
| 4332 | } |
| 4333 | len = PySequence_Size(groups); |
| 4334 | if (len > MAX_GROUPS) { |
| 4335 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4336 | return NULL; |
| 4337 | } |
| 4338 | for(i = 0; i < len; i++) { |
| 4339 | PyObject *elem; |
| 4340 | elem = PySequence_GetItem(groups, i); |
| 4341 | if (!elem) |
| 4342 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4343 | if (!PyLong_Check(elem)) { |
| 4344 | PyErr_SetString(PyExc_TypeError, |
| 4345 | "groups must be integers"); |
| 4346 | Py_DECREF(elem); |
| 4347 | return NULL; |
| 4348 | } else { |
| 4349 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4350 | if (PyErr_Occurred()) { |
| 4351 | PyErr_SetString(PyExc_TypeError, |
| 4352 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4353 | Py_DECREF(elem); |
| 4354 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4355 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4356 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4357 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4358 | if (grouplist[i] != x) { |
| 4359 | PyErr_SetString(PyExc_TypeError, |
| 4360 | "group id too big"); |
| 4361 | Py_DECREF(elem); |
| 4362 | return NULL; |
| 4363 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4364 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4365 | Py_DECREF(elem); |
| 4366 | } |
| 4367 | |
| 4368 | if (setgroups(len, grouplist) < 0) |
| 4369 | return posix_error(); |
| 4370 | Py_INCREF(Py_None); |
| 4371 | return Py_None; |
| 4372 | } |
| 4373 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4374 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4375 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4376 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4377 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4378 | { |
| 4379 | PyObject *result; |
| 4380 | static PyObject *struct_rusage; |
| 4381 | |
| 4382 | if (pid == -1) |
| 4383 | return posix_error(); |
| 4384 | |
| 4385 | if (struct_rusage == NULL) { |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4386 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4387 | if (m == NULL) |
| 4388 | return NULL; |
| 4389 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4390 | Py_DECREF(m); |
| 4391 | if (struct_rusage == NULL) |
| 4392 | return NULL; |
| 4393 | } |
| 4394 | |
| 4395 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4396 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4397 | if (!result) |
| 4398 | return NULL; |
| 4399 | |
| 4400 | #ifndef doubletime |
| 4401 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4402 | #endif |
| 4403 | |
| 4404 | PyStructSequence_SET_ITEM(result, 0, |
| 4405 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4406 | PyStructSequence_SET_ITEM(result, 1, |
| 4407 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4408 | #define SET_INT(result, index, value)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4409 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4410 | SET_INT(result, 2, ru->ru_maxrss); |
| 4411 | SET_INT(result, 3, ru->ru_ixrss); |
| 4412 | SET_INT(result, 4, ru->ru_idrss); |
| 4413 | SET_INT(result, 5, ru->ru_isrss); |
| 4414 | SET_INT(result, 6, ru->ru_minflt); |
| 4415 | SET_INT(result, 7, ru->ru_majflt); |
| 4416 | SET_INT(result, 8, ru->ru_nswap); |
| 4417 | SET_INT(result, 9, ru->ru_inblock); |
| 4418 | SET_INT(result, 10, ru->ru_oublock); |
| 4419 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4420 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4421 | SET_INT(result, 13, ru->ru_nsignals); |
| 4422 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4423 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4424 | #undef SET_INT |
| 4425 | |
| 4426 | if (PyErr_Occurred()) { |
| 4427 | Py_DECREF(result); |
| 4428 | return NULL; |
| 4429 | } |
| 4430 | |
| 4431 | return Py_BuildValue("iiN", pid, status, result); |
| 4432 | } |
| 4433 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4434 | |
| 4435 | #ifdef HAVE_WAIT3 |
| 4436 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4437 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4438 | Wait for completion of a child process."); |
| 4439 | |
| 4440 | static PyObject * |
| 4441 | posix_wait3(PyObject *self, PyObject *args) |
| 4442 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4443 | pid_t pid; |
| 4444 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4445 | struct rusage ru; |
| 4446 | WAIT_TYPE status; |
| 4447 | WAIT_STATUS_INT(status) = 0; |
| 4448 | |
| 4449 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4450 | return NULL; |
| 4451 | |
| 4452 | Py_BEGIN_ALLOW_THREADS |
| 4453 | pid = wait3(&status, options, &ru); |
| 4454 | Py_END_ALLOW_THREADS |
| 4455 | |
| 4456 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4457 | } |
| 4458 | #endif /* HAVE_WAIT3 */ |
| 4459 | |
| 4460 | #ifdef HAVE_WAIT4 |
| 4461 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4462 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4463 | Wait for completion of a given child process."); |
| 4464 | |
| 4465 | static PyObject * |
| 4466 | posix_wait4(PyObject *self, PyObject *args) |
| 4467 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4468 | pid_t pid; |
| 4469 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4470 | struct rusage ru; |
| 4471 | WAIT_TYPE status; |
| 4472 | WAIT_STATUS_INT(status) = 0; |
| 4473 | |
| 4474 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4475 | return NULL; |
| 4476 | |
| 4477 | Py_BEGIN_ALLOW_THREADS |
| 4478 | pid = wait4(pid, &status, options, &ru); |
| 4479 | Py_END_ALLOW_THREADS |
| 4480 | |
| 4481 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4482 | } |
| 4483 | #endif /* HAVE_WAIT4 */ |
| 4484 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4485 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4486 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4487 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4488 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4489 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4490 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4491 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4492 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4493 | pid_t pid; |
| 4494 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4495 | WAIT_TYPE status; |
| 4496 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4497 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4498 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4499 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4500 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4501 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4502 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4503 | if (pid == -1) |
| 4504 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4505 | |
| 4506 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4509 | #elif defined(HAVE_CWAIT) |
| 4510 | |
| 4511 | /* 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] | 4512 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4513 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4514 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4515 | |
| 4516 | static PyObject * |
| 4517 | posix_waitpid(PyObject *self, PyObject *args) |
| 4518 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4519 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4520 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4521 | |
| 4522 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4523 | return NULL; |
| 4524 | Py_BEGIN_ALLOW_THREADS |
| 4525 | pid = _cwait(&status, pid, options); |
| 4526 | Py_END_ALLOW_THREADS |
| 4527 | if (pid == -1) |
| 4528 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4529 | |
| 4530 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4531 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4532 | } |
| 4533 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4534 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4535 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4536 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4537 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4538 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4539 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4540 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4541 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4542 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4543 | pid_t pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4544 | WAIT_TYPE status; |
| 4545 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4546 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4547 | Py_BEGIN_ALLOW_THREADS |
| 4548 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4549 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4550 | if (pid == -1) |
| 4551 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4552 | |
| 4553 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4554 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4555 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4556 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4558 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4559 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4560 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4561 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4562 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4563 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4564 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4565 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4566 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4567 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4568 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4569 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4570 | #else |
| 4571 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4572 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4573 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4576 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4577 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4578 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4579 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4580 | 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] | 4581 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4582 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4583 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4584 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4585 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4586 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4587 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4588 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4589 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4590 | |
| 4591 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 4592 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4593 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4594 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4595 | if (v == NULL) { |
| 4596 | PyMem_Free(path); |
| 4597 | return NULL; |
| 4598 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4599 | |
| 4600 | if (PyUnicode_Check(v)) { |
| 4601 | arg_is_unicode = 1; |
| 4602 | } |
| 4603 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4604 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4605 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4606 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4607 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4608 | if (n < 0) |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4609 | return posix_error_with_allocated_filename(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4610 | |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4611 | PyMem_Free(path); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4612 | v = PyBytes_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4613 | if (arg_is_unicode) { |
| 4614 | PyObject *w; |
| 4615 | |
| 4616 | w = PyUnicode_FromEncodedObject(v, |
| 4617 | Py_FileSystemDefaultEncoding, |
| 4618 | "strict"); |
| 4619 | if (w != NULL) { |
| 4620 | Py_DECREF(v); |
| 4621 | v = w; |
| 4622 | } |
| 4623 | else { |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4624 | v = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4625 | } |
| 4626 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4627 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4628 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4629 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4630 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4631 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4632 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4633 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4634 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4635 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4636 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4637 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4638 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4639 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4640 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4641 | } |
| 4642 | #endif /* HAVE_SYMLINK */ |
| 4643 | |
| 4644 | |
| 4645 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4646 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4647 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4648 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4649 | { |
| 4650 | ULONG value = 0; |
| 4651 | |
| 4652 | Py_BEGIN_ALLOW_THREADS |
| 4653 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4654 | Py_END_ALLOW_THREADS |
| 4655 | |
| 4656 | return value; |
| 4657 | } |
| 4658 | |
| 4659 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4660 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4661 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4662 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4663 | return Py_BuildValue("ddddd", |
| 4664 | (double)0 /* t.tms_utime / HZ */, |
| 4665 | (double)0 /* t.tms_stime / HZ */, |
| 4666 | (double)0 /* t.tms_cutime / HZ */, |
| 4667 | (double)0 /* t.tms_cstime / HZ */, |
| 4668 | (double)system_uptime() / 1000); |
| 4669 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4670 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4671 | #define NEED_TICKS_PER_SECOND |
| 4672 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4673 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4674 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4675 | { |
| 4676 | struct tms t; |
| 4677 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4678 | errno = 0; |
| 4679 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4680 | if (c == (clock_t) -1) |
| 4681 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4682 | return Py_BuildValue("ddddd", |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4683 | (double)t.tms_utime / ticks_per_second, |
| 4684 | (double)t.tms_stime / ticks_per_second, |
| 4685 | (double)t.tms_cutime / ticks_per_second, |
| 4686 | (double)t.tms_cstime / ticks_per_second, |
| 4687 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4688 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4689 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4690 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4691 | |
| 4692 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4693 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4694 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4695 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4696 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4697 | { |
| 4698 | FILETIME create, exit, kernel, user; |
| 4699 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4700 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4701 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4702 | /* The fields of a FILETIME structure are the hi and lo part |
| 4703 | of a 64-bit value expressed in 100 nanosecond units. |
| 4704 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4705 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4706 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4707 | return Py_BuildValue( |
| 4708 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4709 | (double)(user.dwHighDateTime*429.4967296 + |
| 4710 | user.dwLowDateTime*1e-7), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 4711 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4712 | kernel.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4713 | (double)0, |
| 4714 | (double)0, |
| 4715 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4716 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4717 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4718 | |
| 4719 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4720 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4721 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4722 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4723 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4724 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4725 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4726 | #ifdef HAVE_GETSID |
| 4727 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4728 | "getsid(pid) -> sid\n\n\ |
| 4729 | Call the system call getsid()."); |
| 4730 | |
| 4731 | static PyObject * |
| 4732 | posix_getsid(PyObject *self, PyObject *args) |
| 4733 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4734 | pid_t pid; |
| 4735 | int sid; |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4736 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4737 | return NULL; |
| 4738 | sid = getsid(pid); |
| 4739 | if (sid < 0) |
| 4740 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4741 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4742 | } |
| 4743 | #endif /* HAVE_GETSID */ |
| 4744 | |
| 4745 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4746 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4747 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4748 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4749 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4750 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4751 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4752 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4753 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4754 | if (setsid() < 0) |
| 4755 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4756 | Py_INCREF(Py_None); |
| 4757 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4758 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4759 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4760 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4761 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4762 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4763 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4764 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4765 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4766 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4767 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4768 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4769 | pid_t pid; |
| 4770 | int pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4771 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4772 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4773 | if (setpgid(pid, pgrp) < 0) |
| 4774 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4775 | Py_INCREF(Py_None); |
| 4776 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4777 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4778 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4779 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4780 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4781 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4782 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4783 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4784 | 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] | 4785 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4786 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4787 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4788 | { |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4789 | int fd; |
| 4790 | pid_t pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4791 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4792 | return NULL; |
| 4793 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4794 | if (pgid < 0) |
| 4795 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4796 | return PyLong_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4797 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4798 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4799 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4800 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4801 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4802 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4803 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4804 | 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] | 4805 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4806 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4807 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4808 | { |
| 4809 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4810 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4811 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4812 | if (tcsetpgrp(fd, pgid) < 0) |
| 4813 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4814 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4815 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4816 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4817 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4818 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4819 | /* Functions acting on file descriptors */ |
| 4820 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4821 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4822 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4823 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4824 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4825 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4826 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4827 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4828 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4829 | int flag; |
| 4830 | int mode = 0777; |
| 4831 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4832 | |
| 4833 | #ifdef MS_WINDOWS |
| 4834 | if (unicode_file_names()) { |
| 4835 | PyUnicodeObject *po; |
| 4836 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4837 | Py_BEGIN_ALLOW_THREADS |
| 4838 | /* PyUnicode_AS_UNICODE OK without thread |
| 4839 | lock as it is a simple dereference. */ |
| 4840 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4841 | Py_END_ALLOW_THREADS |
| 4842 | if (fd < 0) |
| 4843 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4844 | return PyLong_FromLong((long)fd); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4845 | } |
| 4846 | /* Drop the argument parsing error as narrow strings |
| 4847 | are also valid. */ |
| 4848 | PyErr_Clear(); |
| 4849 | } |
| 4850 | #endif |
| 4851 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4852 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4853 | Py_FileSystemDefaultEncoding, &file, |
| 4854 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4855 | return NULL; |
| 4856 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4857 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4858 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4859 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4860 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4861 | return posix_error_with_allocated_filename(file); |
| 4862 | PyMem_Free(file); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4863 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4866 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4867 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4868 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4869 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4870 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4871 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4872 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4873 | { |
| 4874 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4875 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4876 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4877 | if (!_PyVerify_fd(fd)) |
| 4878 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4879 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4880 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4881 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4882 | if (res < 0) |
| 4883 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4884 | Py_INCREF(Py_None); |
| 4885 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4888 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4889 | PyDoc_STRVAR(posix_closerange__doc__, |
| 4890 | "closerange(fd_low, fd_high)\n\n\ |
| 4891 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 4892 | |
| 4893 | static PyObject * |
| 4894 | posix_closerange(PyObject *self, PyObject *args) |
| 4895 | { |
| 4896 | int fd_from, fd_to, i; |
| 4897 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 4898 | return NULL; |
| 4899 | Py_BEGIN_ALLOW_THREADS |
| 4900 | for (i = fd_from; i < fd_to; i++) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4901 | if (_PyVerify_fd(i)) |
| 4902 | close(i); |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4903 | Py_END_ALLOW_THREADS |
| 4904 | Py_RETURN_NONE; |
| 4905 | } |
| 4906 | |
| 4907 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4908 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4909 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4910 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4911 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4912 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4913 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4914 | { |
| 4915 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4916 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4917 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4918 | if (!_PyVerify_fd(fd)) |
| 4919 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4920 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4921 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4922 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4923 | if (fd < 0) |
| 4924 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4925 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4929 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4930 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4931 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4934 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4935 | { |
| 4936 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4937 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4938 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4939 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 4940 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4941 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4942 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4943 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4944 | if (res < 0) |
| 4945 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4946 | Py_INCREF(Py_None); |
| 4947 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4950 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4951 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4952 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4953 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4954 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4955 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4956 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4957 | { |
| 4958 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4959 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4960 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4961 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4962 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4963 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4964 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4965 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4966 | return NULL; |
| 4967 | #ifdef SEEK_SET |
| 4968 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4969 | switch (how) { |
| 4970 | case 0: how = SEEK_SET; break; |
| 4971 | case 1: how = SEEK_CUR; break; |
| 4972 | case 2: how = SEEK_END; break; |
| 4973 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4974 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4975 | |
| 4976 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4977 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4978 | #else |
| 4979 | pos = PyLong_Check(posobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4980 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4981 | #endif |
| 4982 | if (PyErr_Occurred()) |
| 4983 | return NULL; |
| 4984 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4985 | if (!_PyVerify_fd(fd)) |
| 4986 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4987 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4988 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4989 | res = _lseeki64(fd, pos, how); |
| 4990 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4991 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4992 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4993 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4994 | if (res < 0) |
| 4995 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4996 | |
| 4997 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4998 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4999 | #else |
| 5000 | return PyLong_FromLongLong(res); |
| 5001 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5004 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5005 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5006 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5007 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5008 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5009 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5010 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5011 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 5012 | int fd, size; |
| 5013 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5014 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5015 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5016 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5017 | if (size < 0) { |
| 5018 | errno = EINVAL; |
| 5019 | return posix_error(); |
| 5020 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5021 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5022 | if (buffer == NULL) |
| 5023 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5024 | if (!_PyVerify_fd(fd)) |
| 5025 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5026 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5027 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5028 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5029 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5030 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5031 | return posix_error(); |
| 5032 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5033 | if (n != size) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5034 | _PyBytes_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5035 | return buffer; |
| 5036 | } |
| 5037 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5038 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5039 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5040 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5041 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5042 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5043 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5044 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5045 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5046 | Py_buffer pbuf; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5047 | int fd; |
| 5048 | Py_ssize_t size; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5049 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 5050 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5051 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5052 | if (!_PyVerify_fd(fd)) |
| 5053 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5054 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5055 | size = write(fd, pbuf.buf, (size_t)pbuf.len); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5056 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5057 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5058 | if (size < 0) |
| 5059 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5060 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5063 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5064 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5065 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5066 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5067 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5068 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5069 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5070 | { |
| 5071 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5072 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5073 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5074 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5075 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5076 | #ifdef __VMS |
| 5077 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5078 | fsync(fd); |
| 5079 | #endif |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5080 | if (!_PyVerify_fd(fd)) |
| 5081 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5082 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5083 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5084 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5085 | if (res != 0) { |
| 5086 | #ifdef MS_WINDOWS |
| 5087 | return win32_error("fstat", NULL); |
| 5088 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5089 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5090 | #endif |
| 5091 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5092 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5093 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5094 | } |
| 5095 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5096 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5097 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5098 | 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] | 5099 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5100 | |
| 5101 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5102 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5103 | { |
| 5104 | int fd; |
| 5105 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5106 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5107 | if (!_PyVerify_fd(fd)) |
| 5108 | return PyBool_FromLong(0); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5109 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5110 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5111 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5112 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5113 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5114 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5115 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5116 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5117 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5118 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5119 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5120 | #if defined(PYOS_OS2) |
| 5121 | HFILE read, write; |
| 5122 | APIRET rc; |
| 5123 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5124 | Py_BEGIN_ALLOW_THREADS |
| 5125 | rc = DosCreatePipe( &read, &write, 4096); |
| 5126 | Py_END_ALLOW_THREADS |
| 5127 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5128 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5129 | |
| 5130 | return Py_BuildValue("(ii)", read, write); |
| 5131 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5132 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5133 | int fds[2]; |
| 5134 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5135 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5136 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5137 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5138 | if (res != 0) |
| 5139 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5140 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5141 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5142 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5143 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5144 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5145 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5146 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5147 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5148 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5149 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5150 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5151 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5152 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5153 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5154 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5155 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5156 | #endif /* HAVE_PIPE */ |
| 5157 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5158 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5159 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5160 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5161 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5162 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5163 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5164 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5165 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5166 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5167 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5168 | int mode = 0666; |
| 5169 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5170 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5171 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5172 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5173 | res = mkfifo(filename, mode); |
| 5174 | Py_END_ALLOW_THREADS |
| 5175 | if (res < 0) |
| 5176 | return posix_error(); |
| 5177 | Py_INCREF(Py_None); |
| 5178 | return Py_None; |
| 5179 | } |
| 5180 | #endif |
| 5181 | |
| 5182 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5183 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5184 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5185 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5186 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5187 | named filename. mode specifies both the permissions to use and the\n\ |
| 5188 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5189 | 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] | 5190 | device defines the newly created device special file (probably using\n\ |
| 5191 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5192 | |
| 5193 | |
| 5194 | static PyObject * |
| 5195 | posix_mknod(PyObject *self, PyObject *args) |
| 5196 | { |
| 5197 | char *filename; |
| 5198 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5199 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5200 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5201 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5202 | return NULL; |
| 5203 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5204 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5205 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5206 | if (res < 0) |
| 5207 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5208 | Py_INCREF(Py_None); |
| 5209 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5210 | } |
| 5211 | #endif |
| 5212 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5213 | #ifdef HAVE_DEVICE_MACROS |
| 5214 | PyDoc_STRVAR(posix_major__doc__, |
| 5215 | "major(device) -> major number\n\ |
| 5216 | Extracts a device major number from a raw device number."); |
| 5217 | |
| 5218 | static PyObject * |
| 5219 | posix_major(PyObject *self, PyObject *args) |
| 5220 | { |
| 5221 | int device; |
| 5222 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5223 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5224 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | PyDoc_STRVAR(posix_minor__doc__, |
| 5228 | "minor(device) -> minor number\n\ |
| 5229 | Extracts a device minor number from a raw device number."); |
| 5230 | |
| 5231 | static PyObject * |
| 5232 | posix_minor(PyObject *self, PyObject *args) |
| 5233 | { |
| 5234 | int device; |
| 5235 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5236 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5237 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
| 5240 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5241 | "makedev(major, minor) -> device number\n\ |
| 5242 | Composes a raw device number from the major and minor device numbers."); |
| 5243 | |
| 5244 | static PyObject * |
| 5245 | posix_makedev(PyObject *self, PyObject *args) |
| 5246 | { |
| 5247 | int major, minor; |
| 5248 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5249 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5250 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5251 | } |
| 5252 | #endif /* device macros */ |
| 5253 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5254 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5255 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5256 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5257 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5258 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5259 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5260 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5261 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5262 | { |
| 5263 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5264 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5265 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5266 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5267 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5268 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5269 | return NULL; |
| 5270 | |
| 5271 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5272 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5273 | #else |
| 5274 | length = PyLong_Check(lenobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5275 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5276 | #endif |
| 5277 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5278 | return NULL; |
| 5279 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5280 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5281 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5282 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 9053d75 | 2009-01-19 17:53:36 +0000 | [diff] [blame] | 5283 | if (res < 0) |
| 5284 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5285 | Py_INCREF(Py_None); |
| 5286 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5287 | } |
| 5288 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5289 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5290 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5291 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5292 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5293 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5294 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5295 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5296 | * get re-set with another call for the same key. */ |
| 5297 | static PyObject *posix_putenv_garbage; |
| 5298 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5299 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5300 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5301 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5302 | #ifdef MS_WINDOWS |
| 5303 | wchar_t *s1, *s2; |
| 5304 | wchar_t *newenv; |
| 5305 | #else |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5306 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5307 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5308 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5309 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5310 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5311 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5312 | if (!PyArg_ParseTuple(args, |
| 5313 | #ifdef MS_WINDOWS |
| 5314 | "uu:putenv", |
| 5315 | #else |
| 5316 | "ss:putenv", |
| 5317 | #endif |
| 5318 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5319 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5320 | |
| 5321 | #if defined(PYOS_OS2) |
| 5322 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5323 | APIRET rc; |
| 5324 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5325 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5326 | if (rc != NO_ERROR) |
| 5327 | return os2_error(rc); |
| 5328 | |
| 5329 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5330 | APIRET rc; |
| 5331 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5332 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5333 | if (rc != NO_ERROR) |
| 5334 | return os2_error(rc); |
| 5335 | } else { |
| 5336 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5337 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5338 | /* len includes space for a trailing \0; the size arg to |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5339 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5340 | #ifdef MS_WINDOWS |
| 5341 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5342 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5343 | #else |
| 5344 | len = strlen(s1) + strlen(s2) + 2; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5345 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5346 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5347 | if (newstr == NULL) |
| 5348 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5349 | #ifdef MS_WINDOWS |
| 5350 | newenv = PyUnicode_AsUnicode(newstr); |
| 5351 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5352 | if (_wputenv(newenv)) { |
| 5353 | Py_DECREF(newstr); |
| 5354 | posix_error(); |
| 5355 | return NULL; |
| 5356 | } |
| 5357 | #else |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5358 | newenv = PyBytes_AS_STRING(newstr); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5359 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5360 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5361 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5362 | posix_error(); |
| 5363 | return NULL; |
| 5364 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5365 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5366 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5367 | * this will cause previous value to be collected. This has to |
| 5368 | * happen after the real putenv() call because the old value |
| 5369 | * was still accessible until then. */ |
| 5370 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5371 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5372 | /* really not much we can do; just leak */ |
| 5373 | PyErr_Clear(); |
| 5374 | } |
| 5375 | else { |
| 5376 | Py_DECREF(newstr); |
| 5377 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5378 | |
| 5379 | #if defined(PYOS_OS2) |
| 5380 | } |
| 5381 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5382 | Py_INCREF(Py_None); |
| 5383 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5384 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5385 | #endif /* putenv */ |
| 5386 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5387 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5388 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5389 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5390 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5391 | |
| 5392 | static PyObject * |
| 5393 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5394 | { |
| 5395 | char *s1; |
| 5396 | |
| 5397 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5398 | return NULL; |
| 5399 | |
| 5400 | unsetenv(s1); |
| 5401 | |
| 5402 | /* Remove the key from posix_putenv_garbage; |
| 5403 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5404 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5405 | * old value was still accessible until then. |
| 5406 | */ |
| 5407 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5408 | PyTuple_GET_ITEM(args, 0))) { |
| 5409 | /* really not much we can do; just leak */ |
| 5410 | PyErr_Clear(); |
| 5411 | } |
| 5412 | |
| 5413 | Py_INCREF(Py_None); |
| 5414 | return Py_None; |
| 5415 | } |
| 5416 | #endif /* unsetenv */ |
| 5417 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5418 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5419 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5420 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5421 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5422 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5423 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5424 | { |
| 5425 | int code; |
| 5426 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5427 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5428 | return NULL; |
| 5429 | message = strerror(code); |
| 5430 | if (message == NULL) { |
| 5431 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5432 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5433 | return NULL; |
| 5434 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5435 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5436 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5437 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5438 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5439 | #ifdef HAVE_SYS_WAIT_H |
| 5440 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5441 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5442 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5443 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5444 | 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] | 5445 | |
| 5446 | static PyObject * |
| 5447 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5448 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5449 | WAIT_TYPE status; |
| 5450 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5451 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5452 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5453 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5454 | |
| 5455 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5456 | } |
| 5457 | #endif /* WCOREDUMP */ |
| 5458 | |
| 5459 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5460 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5461 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5462 | 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] | 5463 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5464 | |
| 5465 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5466 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5467 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5468 | WAIT_TYPE status; |
| 5469 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5470 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5471 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5472 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5473 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5474 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5475 | } |
| 5476 | #endif /* WIFCONTINUED */ |
| 5477 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5478 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5479 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5480 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5481 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5482 | |
| 5483 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5484 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5485 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5486 | WAIT_TYPE status; |
| 5487 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5488 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5489 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5490 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5491 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5492 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5493 | } |
| 5494 | #endif /* WIFSTOPPED */ |
| 5495 | |
| 5496 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5497 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5498 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5499 | 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] | 5500 | |
| 5501 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5502 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5503 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5504 | WAIT_TYPE status; |
| 5505 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5506 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5507 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5508 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5509 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5510 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5511 | } |
| 5512 | #endif /* WIFSIGNALED */ |
| 5513 | |
| 5514 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5515 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5516 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5517 | 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] | 5518 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5519 | |
| 5520 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5521 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5522 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5523 | WAIT_TYPE status; |
| 5524 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5525 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5526 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5527 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5528 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5529 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5530 | } |
| 5531 | #endif /* WIFEXITED */ |
| 5532 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5533 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5534 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5535 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5536 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5537 | |
| 5538 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5539 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5540 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5541 | WAIT_TYPE status; |
| 5542 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5543 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5544 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5545 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5546 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5547 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5548 | } |
| 5549 | #endif /* WEXITSTATUS */ |
| 5550 | |
| 5551 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5552 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5553 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5554 | 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] | 5555 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5556 | |
| 5557 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5558 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5559 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5560 | WAIT_TYPE status; |
| 5561 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5562 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5563 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5564 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5565 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5566 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5567 | } |
| 5568 | #endif /* WTERMSIG */ |
| 5569 | |
| 5570 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5571 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5572 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5573 | Return the signal that stopped the process that provided\n\ |
| 5574 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5575 | |
| 5576 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5577 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5578 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5579 | WAIT_TYPE status; |
| 5580 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5581 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5582 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5583 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5584 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5585 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5586 | } |
| 5587 | #endif /* WSTOPSIG */ |
| 5588 | |
| 5589 | #endif /* HAVE_SYS_WAIT_H */ |
| 5590 | |
| 5591 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5592 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5593 | #ifdef _SCO_DS |
| 5594 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5595 | needed definitions in sys/statvfs.h */ |
| 5596 | #define _SVID3 |
| 5597 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5598 | #include <sys/statvfs.h> |
| 5599 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5600 | static PyObject* |
| 5601 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5602 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5603 | if (v == NULL) |
| 5604 | return NULL; |
| 5605 | |
| 5606 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5607 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5608 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5609 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5610 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5611 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5612 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5613 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5614 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 5615 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5616 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5617 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5618 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5619 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5620 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5621 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5622 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5623 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5624 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5625 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5626 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5627 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5628 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5629 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5630 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5631 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5632 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5633 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5634 | #endif |
| 5635 | |
| 5636 | return v; |
| 5637 | } |
| 5638 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5639 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5640 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5641 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5642 | |
| 5643 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5644 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5645 | { |
| 5646 | int fd, res; |
| 5647 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5648 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5649 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5650 | return NULL; |
| 5651 | Py_BEGIN_ALLOW_THREADS |
| 5652 | res = fstatvfs(fd, &st); |
| 5653 | Py_END_ALLOW_THREADS |
| 5654 | if (res != 0) |
| 5655 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5656 | |
| 5657 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5658 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5659 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5660 | |
| 5661 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5662 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5663 | #include <sys/statvfs.h> |
| 5664 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5665 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5666 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5667 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5668 | |
| 5669 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5670 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5671 | { |
| 5672 | char *path; |
| 5673 | int res; |
| 5674 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5675 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5676 | return NULL; |
| 5677 | Py_BEGIN_ALLOW_THREADS |
| 5678 | res = statvfs(path, &st); |
| 5679 | Py_END_ALLOW_THREADS |
| 5680 | if (res != 0) |
| 5681 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5682 | |
| 5683 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5684 | } |
| 5685 | #endif /* HAVE_STATVFS */ |
| 5686 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5687 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5688 | * It maps strings representing configuration variable names to |
| 5689 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5690 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5691 | * rarely-used constants. There are three separate tables that use |
| 5692 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5693 | * |
| 5694 | * This code is always included, even if none of the interfaces that |
| 5695 | * need it are included. The #if hackery needed to avoid it would be |
| 5696 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5697 | */ |
| 5698 | struct constdef { |
| 5699 | char *name; |
| 5700 | long value; |
| 5701 | }; |
| 5702 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5703 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5704 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5705 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5706 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5707 | if (PyLong_Check(arg)) { |
| 5708 | *valuep = PyLong_AS_LONG(arg); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5709 | return 1; |
| 5710 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5711 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5712 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5713 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5714 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5715 | size_t hi = tablesize; |
| 5716 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5717 | const char *confname; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5718 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5719 | PyErr_SetString(PyExc_TypeError, |
| 5720 | "configuration names must be strings or integers"); |
| 5721 | return 0; |
| 5722 | } |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 5723 | confname = _PyUnicode_AsString(arg); |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5724 | if (confname == NULL) |
| 5725 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5726 | while (lo < hi) { |
| 5727 | mid = (lo + hi) / 2; |
| 5728 | cmp = strcmp(confname, table[mid].name); |
| 5729 | if (cmp < 0) |
| 5730 | hi = mid; |
| 5731 | else if (cmp > 0) |
| 5732 | lo = mid + 1; |
| 5733 | else { |
| 5734 | *valuep = table[mid].value; |
| 5735 | return 1; |
| 5736 | } |
| 5737 | } |
| 5738 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5739 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5740 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | |
| 5744 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5745 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5746 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5747 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5748 | #endif |
| 5749 | #ifdef _PC_ABI_ASYNC_IO |
| 5750 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5751 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5752 | #ifdef _PC_ASYNC_IO |
| 5753 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5754 | #endif |
| 5755 | #ifdef _PC_CHOWN_RESTRICTED |
| 5756 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5757 | #endif |
| 5758 | #ifdef _PC_FILESIZEBITS |
| 5759 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5760 | #endif |
| 5761 | #ifdef _PC_LAST |
| 5762 | {"PC_LAST", _PC_LAST}, |
| 5763 | #endif |
| 5764 | #ifdef _PC_LINK_MAX |
| 5765 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5766 | #endif |
| 5767 | #ifdef _PC_MAX_CANON |
| 5768 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5769 | #endif |
| 5770 | #ifdef _PC_MAX_INPUT |
| 5771 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5772 | #endif |
| 5773 | #ifdef _PC_NAME_MAX |
| 5774 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5775 | #endif |
| 5776 | #ifdef _PC_NO_TRUNC |
| 5777 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5778 | #endif |
| 5779 | #ifdef _PC_PATH_MAX |
| 5780 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5781 | #endif |
| 5782 | #ifdef _PC_PIPE_BUF |
| 5783 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5784 | #endif |
| 5785 | #ifdef _PC_PRIO_IO |
| 5786 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5787 | #endif |
| 5788 | #ifdef _PC_SOCK_MAXBUF |
| 5789 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5790 | #endif |
| 5791 | #ifdef _PC_SYNC_IO |
| 5792 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5793 | #endif |
| 5794 | #ifdef _PC_VDISABLE |
| 5795 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5796 | #endif |
| 5797 | }; |
| 5798 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5799 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5800 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5801 | { |
| 5802 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5803 | sizeof(posix_constants_pathconf) |
| 5804 | / sizeof(struct constdef)); |
| 5805 | } |
| 5806 | #endif |
| 5807 | |
| 5808 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5809 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5810 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5811 | 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] | 5812 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5813 | |
| 5814 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5815 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5816 | { |
| 5817 | PyObject *result = NULL; |
| 5818 | int name, fd; |
| 5819 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5820 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5821 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5822 | long limit; |
| 5823 | |
| 5824 | errno = 0; |
| 5825 | limit = fpathconf(fd, name); |
| 5826 | if (limit == -1 && errno != 0) |
| 5827 | posix_error(); |
| 5828 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5829 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5830 | } |
| 5831 | return result; |
| 5832 | } |
| 5833 | #endif |
| 5834 | |
| 5835 | |
| 5836 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5837 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5838 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5839 | 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] | 5840 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5841 | |
| 5842 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5843 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5844 | { |
| 5845 | PyObject *result = NULL; |
| 5846 | int name; |
| 5847 | char *path; |
| 5848 | |
| 5849 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5850 | conv_path_confname, &name)) { |
| 5851 | long limit; |
| 5852 | |
| 5853 | errno = 0; |
| 5854 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5855 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5856 | if (errno == EINVAL) |
| 5857 | /* could be a path or name problem */ |
| 5858 | posix_error(); |
| 5859 | else |
| 5860 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5861 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5862 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5863 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5864 | } |
| 5865 | return result; |
| 5866 | } |
| 5867 | #endif |
| 5868 | |
| 5869 | #ifdef HAVE_CONFSTR |
| 5870 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5871 | #ifdef _CS_ARCHITECTURE |
| 5872 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5873 | #endif |
| 5874 | #ifdef _CS_HOSTNAME |
| 5875 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5876 | #endif |
| 5877 | #ifdef _CS_HW_PROVIDER |
| 5878 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5879 | #endif |
| 5880 | #ifdef _CS_HW_SERIAL |
| 5881 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5882 | #endif |
| 5883 | #ifdef _CS_INITTAB_NAME |
| 5884 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5885 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5886 | #ifdef _CS_LFS64_CFLAGS |
| 5887 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5888 | #endif |
| 5889 | #ifdef _CS_LFS64_LDFLAGS |
| 5890 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5891 | #endif |
| 5892 | #ifdef _CS_LFS64_LIBS |
| 5893 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5894 | #endif |
| 5895 | #ifdef _CS_LFS64_LINTFLAGS |
| 5896 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5897 | #endif |
| 5898 | #ifdef _CS_LFS_CFLAGS |
| 5899 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5900 | #endif |
| 5901 | #ifdef _CS_LFS_LDFLAGS |
| 5902 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5903 | #endif |
| 5904 | #ifdef _CS_LFS_LIBS |
| 5905 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5906 | #endif |
| 5907 | #ifdef _CS_LFS_LINTFLAGS |
| 5908 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5909 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5910 | #ifdef _CS_MACHINE |
| 5911 | {"CS_MACHINE", _CS_MACHINE}, |
| 5912 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5913 | #ifdef _CS_PATH |
| 5914 | {"CS_PATH", _CS_PATH}, |
| 5915 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5916 | #ifdef _CS_RELEASE |
| 5917 | {"CS_RELEASE", _CS_RELEASE}, |
| 5918 | #endif |
| 5919 | #ifdef _CS_SRPC_DOMAIN |
| 5920 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5921 | #endif |
| 5922 | #ifdef _CS_SYSNAME |
| 5923 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5924 | #endif |
| 5925 | #ifdef _CS_VERSION |
| 5926 | {"CS_VERSION", _CS_VERSION}, |
| 5927 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5928 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5929 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5930 | #endif |
| 5931 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5932 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5933 | #endif |
| 5934 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5935 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5936 | #endif |
| 5937 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5938 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5939 | #endif |
| 5940 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5941 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5942 | #endif |
| 5943 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5944 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5945 | #endif |
| 5946 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5947 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5948 | #endif |
| 5949 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5950 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5951 | #endif |
| 5952 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5953 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5954 | #endif |
| 5955 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5956 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5957 | #endif |
| 5958 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5959 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5960 | #endif |
| 5961 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5962 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5963 | #endif |
| 5964 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5965 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5966 | #endif |
| 5967 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5968 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5969 | #endif |
| 5970 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5971 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5972 | #endif |
| 5973 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5974 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5975 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5976 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5977 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5978 | #endif |
| 5979 | #ifdef _MIPS_CS_BASE |
| 5980 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5981 | #endif |
| 5982 | #ifdef _MIPS_CS_HOSTID |
| 5983 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5984 | #endif |
| 5985 | #ifdef _MIPS_CS_HW_NAME |
| 5986 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5987 | #endif |
| 5988 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5989 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5990 | #endif |
| 5991 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5992 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5993 | #endif |
| 5994 | #ifdef _MIPS_CS_OSREL_MIN |
| 5995 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5996 | #endif |
| 5997 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5998 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5999 | #endif |
| 6000 | #ifdef _MIPS_CS_OS_NAME |
| 6001 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6002 | #endif |
| 6003 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6004 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6005 | #endif |
| 6006 | #ifdef _MIPS_CS_PROCESSORS |
| 6007 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6008 | #endif |
| 6009 | #ifdef _MIPS_CS_SERIAL |
| 6010 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6011 | #endif |
| 6012 | #ifdef _MIPS_CS_VENDOR |
| 6013 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6014 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6015 | }; |
| 6016 | |
| 6017 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6018 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6019 | { |
| 6020 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6021 | sizeof(posix_constants_confstr) |
| 6022 | / sizeof(struct constdef)); |
| 6023 | } |
| 6024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6025 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6026 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6027 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6028 | |
| 6029 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6030 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6031 | { |
| 6032 | PyObject *result = NULL; |
| 6033 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6034 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6035 | |
| 6036 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6037 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6038 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6039 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6040 | len = confstr(name, buffer, sizeof(buffer)); |
| 6041 | if (len == 0) { |
| 6042 | if (errno) { |
| 6043 | posix_error(); |
| 6044 | } |
| 6045 | else { |
| 6046 | result = Py_None; |
| 6047 | Py_INCREF(Py_None); |
| 6048 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6049 | } |
| 6050 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6051 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6052 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6053 | if (result != NULL) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 6054 | confstr(name, _PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6055 | } |
| 6056 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6057 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6058 | } |
| 6059 | } |
| 6060 | return result; |
| 6061 | } |
| 6062 | #endif |
| 6063 | |
| 6064 | |
| 6065 | #ifdef HAVE_SYSCONF |
| 6066 | static struct constdef posix_constants_sysconf[] = { |
| 6067 | #ifdef _SC_2_CHAR_TERM |
| 6068 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6069 | #endif |
| 6070 | #ifdef _SC_2_C_BIND |
| 6071 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6072 | #endif |
| 6073 | #ifdef _SC_2_C_DEV |
| 6074 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6075 | #endif |
| 6076 | #ifdef _SC_2_C_VERSION |
| 6077 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6078 | #endif |
| 6079 | #ifdef _SC_2_FORT_DEV |
| 6080 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6081 | #endif |
| 6082 | #ifdef _SC_2_FORT_RUN |
| 6083 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6084 | #endif |
| 6085 | #ifdef _SC_2_LOCALEDEF |
| 6086 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6087 | #endif |
| 6088 | #ifdef _SC_2_SW_DEV |
| 6089 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6090 | #endif |
| 6091 | #ifdef _SC_2_UPE |
| 6092 | {"SC_2_UPE", _SC_2_UPE}, |
| 6093 | #endif |
| 6094 | #ifdef _SC_2_VERSION |
| 6095 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6096 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6097 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6098 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6099 | #endif |
| 6100 | #ifdef _SC_ACL |
| 6101 | {"SC_ACL", _SC_ACL}, |
| 6102 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6103 | #ifdef _SC_AIO_LISTIO_MAX |
| 6104 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6105 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6106 | #ifdef _SC_AIO_MAX |
| 6107 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6108 | #endif |
| 6109 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6110 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6111 | #endif |
| 6112 | #ifdef _SC_ARG_MAX |
| 6113 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6114 | #endif |
| 6115 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6116 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6117 | #endif |
| 6118 | #ifdef _SC_ATEXIT_MAX |
| 6119 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6120 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6121 | #ifdef _SC_AUDIT |
| 6122 | {"SC_AUDIT", _SC_AUDIT}, |
| 6123 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6124 | #ifdef _SC_AVPHYS_PAGES |
| 6125 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6126 | #endif |
| 6127 | #ifdef _SC_BC_BASE_MAX |
| 6128 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6129 | #endif |
| 6130 | #ifdef _SC_BC_DIM_MAX |
| 6131 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6132 | #endif |
| 6133 | #ifdef _SC_BC_SCALE_MAX |
| 6134 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6135 | #endif |
| 6136 | #ifdef _SC_BC_STRING_MAX |
| 6137 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6138 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6139 | #ifdef _SC_CAP |
| 6140 | {"SC_CAP", _SC_CAP}, |
| 6141 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6142 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6143 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6144 | #endif |
| 6145 | #ifdef _SC_CHAR_BIT |
| 6146 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6147 | #endif |
| 6148 | #ifdef _SC_CHAR_MAX |
| 6149 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6150 | #endif |
| 6151 | #ifdef _SC_CHAR_MIN |
| 6152 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6153 | #endif |
| 6154 | #ifdef _SC_CHILD_MAX |
| 6155 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6156 | #endif |
| 6157 | #ifdef _SC_CLK_TCK |
| 6158 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6159 | #endif |
| 6160 | #ifdef _SC_COHER_BLKSZ |
| 6161 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6162 | #endif |
| 6163 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6164 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6165 | #endif |
| 6166 | #ifdef _SC_DCACHE_ASSOC |
| 6167 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6168 | #endif |
| 6169 | #ifdef _SC_DCACHE_BLKSZ |
| 6170 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6171 | #endif |
| 6172 | #ifdef _SC_DCACHE_LINESZ |
| 6173 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6174 | #endif |
| 6175 | #ifdef _SC_DCACHE_SZ |
| 6176 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6177 | #endif |
| 6178 | #ifdef _SC_DCACHE_TBLKSZ |
| 6179 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6180 | #endif |
| 6181 | #ifdef _SC_DELAYTIMER_MAX |
| 6182 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6183 | #endif |
| 6184 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6185 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6186 | #endif |
| 6187 | #ifdef _SC_EXPR_NEST_MAX |
| 6188 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6189 | #endif |
| 6190 | #ifdef _SC_FSYNC |
| 6191 | {"SC_FSYNC", _SC_FSYNC}, |
| 6192 | #endif |
| 6193 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6194 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6195 | #endif |
| 6196 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6197 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6198 | #endif |
| 6199 | #ifdef _SC_ICACHE_ASSOC |
| 6200 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6201 | #endif |
| 6202 | #ifdef _SC_ICACHE_BLKSZ |
| 6203 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6204 | #endif |
| 6205 | #ifdef _SC_ICACHE_LINESZ |
| 6206 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6207 | #endif |
| 6208 | #ifdef _SC_ICACHE_SZ |
| 6209 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6210 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6211 | #ifdef _SC_INF |
| 6212 | {"SC_INF", _SC_INF}, |
| 6213 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6214 | #ifdef _SC_INT_MAX |
| 6215 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6216 | #endif |
| 6217 | #ifdef _SC_INT_MIN |
| 6218 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6219 | #endif |
| 6220 | #ifdef _SC_IOV_MAX |
| 6221 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6222 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6223 | #ifdef _SC_IP_SECOPTS |
| 6224 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6225 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6226 | #ifdef _SC_JOB_CONTROL |
| 6227 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6228 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6229 | #ifdef _SC_KERN_POINTERS |
| 6230 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6231 | #endif |
| 6232 | #ifdef _SC_KERN_SIM |
| 6233 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6234 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6235 | #ifdef _SC_LINE_MAX |
| 6236 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6237 | #endif |
| 6238 | #ifdef _SC_LOGIN_NAME_MAX |
| 6239 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6240 | #endif |
| 6241 | #ifdef _SC_LOGNAME_MAX |
| 6242 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6243 | #endif |
| 6244 | #ifdef _SC_LONG_BIT |
| 6245 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6246 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6247 | #ifdef _SC_MAC |
| 6248 | {"SC_MAC", _SC_MAC}, |
| 6249 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6250 | #ifdef _SC_MAPPED_FILES |
| 6251 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6252 | #endif |
| 6253 | #ifdef _SC_MAXPID |
| 6254 | {"SC_MAXPID", _SC_MAXPID}, |
| 6255 | #endif |
| 6256 | #ifdef _SC_MB_LEN_MAX |
| 6257 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6258 | #endif |
| 6259 | #ifdef _SC_MEMLOCK |
| 6260 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6261 | #endif |
| 6262 | #ifdef _SC_MEMLOCK_RANGE |
| 6263 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6264 | #endif |
| 6265 | #ifdef _SC_MEMORY_PROTECTION |
| 6266 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6267 | #endif |
| 6268 | #ifdef _SC_MESSAGE_PASSING |
| 6269 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6270 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6271 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6272 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6273 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6274 | #ifdef _SC_MQ_OPEN_MAX |
| 6275 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6276 | #endif |
| 6277 | #ifdef _SC_MQ_PRIO_MAX |
| 6278 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6279 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6280 | #ifdef _SC_NACLS_MAX |
| 6281 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6282 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6283 | #ifdef _SC_NGROUPS_MAX |
| 6284 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6285 | #endif |
| 6286 | #ifdef _SC_NL_ARGMAX |
| 6287 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6288 | #endif |
| 6289 | #ifdef _SC_NL_LANGMAX |
| 6290 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6291 | #endif |
| 6292 | #ifdef _SC_NL_MSGMAX |
| 6293 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6294 | #endif |
| 6295 | #ifdef _SC_NL_NMAX |
| 6296 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6297 | #endif |
| 6298 | #ifdef _SC_NL_SETMAX |
| 6299 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6300 | #endif |
| 6301 | #ifdef _SC_NL_TEXTMAX |
| 6302 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6303 | #endif |
| 6304 | #ifdef _SC_NPROCESSORS_CONF |
| 6305 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6306 | #endif |
| 6307 | #ifdef _SC_NPROCESSORS_ONLN |
| 6308 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6309 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6310 | #ifdef _SC_NPROC_CONF |
| 6311 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6312 | #endif |
| 6313 | #ifdef _SC_NPROC_ONLN |
| 6314 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6315 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6316 | #ifdef _SC_NZERO |
| 6317 | {"SC_NZERO", _SC_NZERO}, |
| 6318 | #endif |
| 6319 | #ifdef _SC_OPEN_MAX |
| 6320 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6321 | #endif |
| 6322 | #ifdef _SC_PAGESIZE |
| 6323 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6324 | #endif |
| 6325 | #ifdef _SC_PAGE_SIZE |
| 6326 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6327 | #endif |
| 6328 | #ifdef _SC_PASS_MAX |
| 6329 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6330 | #endif |
| 6331 | #ifdef _SC_PHYS_PAGES |
| 6332 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6333 | #endif |
| 6334 | #ifdef _SC_PII |
| 6335 | {"SC_PII", _SC_PII}, |
| 6336 | #endif |
| 6337 | #ifdef _SC_PII_INTERNET |
| 6338 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6339 | #endif |
| 6340 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6341 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6342 | #endif |
| 6343 | #ifdef _SC_PII_INTERNET_STREAM |
| 6344 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6345 | #endif |
| 6346 | #ifdef _SC_PII_OSI |
| 6347 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6348 | #endif |
| 6349 | #ifdef _SC_PII_OSI_CLTS |
| 6350 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6351 | #endif |
| 6352 | #ifdef _SC_PII_OSI_COTS |
| 6353 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6354 | #endif |
| 6355 | #ifdef _SC_PII_OSI_M |
| 6356 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6357 | #endif |
| 6358 | #ifdef _SC_PII_SOCKET |
| 6359 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6360 | #endif |
| 6361 | #ifdef _SC_PII_XTI |
| 6362 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6363 | #endif |
| 6364 | #ifdef _SC_POLL |
| 6365 | {"SC_POLL", _SC_POLL}, |
| 6366 | #endif |
| 6367 | #ifdef _SC_PRIORITIZED_IO |
| 6368 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6369 | #endif |
| 6370 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6371 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6372 | #endif |
| 6373 | #ifdef _SC_REALTIME_SIGNALS |
| 6374 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6375 | #endif |
| 6376 | #ifdef _SC_RE_DUP_MAX |
| 6377 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6378 | #endif |
| 6379 | #ifdef _SC_RTSIG_MAX |
| 6380 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6381 | #endif |
| 6382 | #ifdef _SC_SAVED_IDS |
| 6383 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6384 | #endif |
| 6385 | #ifdef _SC_SCHAR_MAX |
| 6386 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6387 | #endif |
| 6388 | #ifdef _SC_SCHAR_MIN |
| 6389 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6390 | #endif |
| 6391 | #ifdef _SC_SELECT |
| 6392 | {"SC_SELECT", _SC_SELECT}, |
| 6393 | #endif |
| 6394 | #ifdef _SC_SEMAPHORES |
| 6395 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6396 | #endif |
| 6397 | #ifdef _SC_SEM_NSEMS_MAX |
| 6398 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6399 | #endif |
| 6400 | #ifdef _SC_SEM_VALUE_MAX |
| 6401 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6402 | #endif |
| 6403 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6404 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6405 | #endif |
| 6406 | #ifdef _SC_SHRT_MAX |
| 6407 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6408 | #endif |
| 6409 | #ifdef _SC_SHRT_MIN |
| 6410 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6411 | #endif |
| 6412 | #ifdef _SC_SIGQUEUE_MAX |
| 6413 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6414 | #endif |
| 6415 | #ifdef _SC_SIGRT_MAX |
| 6416 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6417 | #endif |
| 6418 | #ifdef _SC_SIGRT_MIN |
| 6419 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6420 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6421 | #ifdef _SC_SOFTPOWER |
| 6422 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6423 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6424 | #ifdef _SC_SPLIT_CACHE |
| 6425 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6426 | #endif |
| 6427 | #ifdef _SC_SSIZE_MAX |
| 6428 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6429 | #endif |
| 6430 | #ifdef _SC_STACK_PROT |
| 6431 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6432 | #endif |
| 6433 | #ifdef _SC_STREAM_MAX |
| 6434 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6435 | #endif |
| 6436 | #ifdef _SC_SYNCHRONIZED_IO |
| 6437 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6438 | #endif |
| 6439 | #ifdef _SC_THREADS |
| 6440 | {"SC_THREADS", _SC_THREADS}, |
| 6441 | #endif |
| 6442 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6443 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6444 | #endif |
| 6445 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6446 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6447 | #endif |
| 6448 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6449 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6450 | #endif |
| 6451 | #ifdef _SC_THREAD_KEYS_MAX |
| 6452 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6453 | #endif |
| 6454 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6455 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6456 | #endif |
| 6457 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6458 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6459 | #endif |
| 6460 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6461 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6462 | #endif |
| 6463 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6464 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6465 | #endif |
| 6466 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6467 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6468 | #endif |
| 6469 | #ifdef _SC_THREAD_STACK_MIN |
| 6470 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6471 | #endif |
| 6472 | #ifdef _SC_THREAD_THREADS_MAX |
| 6473 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6474 | #endif |
| 6475 | #ifdef _SC_TIMERS |
| 6476 | {"SC_TIMERS", _SC_TIMERS}, |
| 6477 | #endif |
| 6478 | #ifdef _SC_TIMER_MAX |
| 6479 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6480 | #endif |
| 6481 | #ifdef _SC_TTY_NAME_MAX |
| 6482 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6483 | #endif |
| 6484 | #ifdef _SC_TZNAME_MAX |
| 6485 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6486 | #endif |
| 6487 | #ifdef _SC_T_IOV_MAX |
| 6488 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6489 | #endif |
| 6490 | #ifdef _SC_UCHAR_MAX |
| 6491 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6492 | #endif |
| 6493 | #ifdef _SC_UINT_MAX |
| 6494 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6495 | #endif |
| 6496 | #ifdef _SC_UIO_MAXIOV |
| 6497 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6498 | #endif |
| 6499 | #ifdef _SC_ULONG_MAX |
| 6500 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6501 | #endif |
| 6502 | #ifdef _SC_USHRT_MAX |
| 6503 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6504 | #endif |
| 6505 | #ifdef _SC_VERSION |
| 6506 | {"SC_VERSION", _SC_VERSION}, |
| 6507 | #endif |
| 6508 | #ifdef _SC_WORD_BIT |
| 6509 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6510 | #endif |
| 6511 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6512 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6513 | #endif |
| 6514 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6515 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6516 | #endif |
| 6517 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6518 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6519 | #endif |
| 6520 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6521 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6522 | #endif |
| 6523 | #ifdef _SC_XOPEN_CRYPT |
| 6524 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6525 | #endif |
| 6526 | #ifdef _SC_XOPEN_ENH_I18N |
| 6527 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6528 | #endif |
| 6529 | #ifdef _SC_XOPEN_LEGACY |
| 6530 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6531 | #endif |
| 6532 | #ifdef _SC_XOPEN_REALTIME |
| 6533 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6534 | #endif |
| 6535 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6536 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6537 | #endif |
| 6538 | #ifdef _SC_XOPEN_SHM |
| 6539 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6540 | #endif |
| 6541 | #ifdef _SC_XOPEN_UNIX |
| 6542 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6543 | #endif |
| 6544 | #ifdef _SC_XOPEN_VERSION |
| 6545 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6546 | #endif |
| 6547 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6548 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6549 | #endif |
| 6550 | #ifdef _SC_XOPEN_XPG2 |
| 6551 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6552 | #endif |
| 6553 | #ifdef _SC_XOPEN_XPG3 |
| 6554 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6555 | #endif |
| 6556 | #ifdef _SC_XOPEN_XPG4 |
| 6557 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6558 | #endif |
| 6559 | }; |
| 6560 | |
| 6561 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6562 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6563 | { |
| 6564 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6565 | sizeof(posix_constants_sysconf) |
| 6566 | / sizeof(struct constdef)); |
| 6567 | } |
| 6568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6569 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6570 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6571 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6572 | |
| 6573 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6574 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6575 | { |
| 6576 | PyObject *result = NULL; |
| 6577 | int name; |
| 6578 | |
| 6579 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6580 | int value; |
| 6581 | |
| 6582 | errno = 0; |
| 6583 | value = sysconf(name); |
| 6584 | if (value == -1 && errno != 0) |
| 6585 | posix_error(); |
| 6586 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6587 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6588 | } |
| 6589 | return result; |
| 6590 | } |
| 6591 | #endif |
| 6592 | |
| 6593 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6594 | /* This code is used to ensure that the tables of configuration value names |
| 6595 | * are in sorted order as required by conv_confname(), and also to build the |
| 6596 | * the exported dictionaries that are used to publish information about the |
| 6597 | * names available on the host platform. |
| 6598 | * |
| 6599 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6600 | * when used, even for platforms we're not able to test on. It also makes |
| 6601 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6602 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6603 | |
| 6604 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6605 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6606 | { |
| 6607 | const struct constdef *c1 = |
| 6608 | (const struct constdef *) v1; |
| 6609 | const struct constdef *c2 = |
| 6610 | (const struct constdef *) v2; |
| 6611 | |
| 6612 | return strcmp(c1->name, c2->name); |
| 6613 | } |
| 6614 | |
| 6615 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6616 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6617 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6618 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6619 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6620 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6621 | |
| 6622 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6623 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6624 | if (d == NULL) |
| 6625 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6626 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6627 | for (i=0; i < tablesize; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6628 | PyObject *o = PyLong_FromLong(table[i].value); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6629 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6630 | Py_XDECREF(o); |
| 6631 | Py_DECREF(d); |
| 6632 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6633 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6634 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6635 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6636 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6637 | } |
| 6638 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6639 | /* Return -1 on failure, 0 on success. */ |
| 6640 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6641 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6642 | { |
| 6643 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6644 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6645 | sizeof(posix_constants_pathconf) |
| 6646 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6647 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6648 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6649 | #endif |
| 6650 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6651 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6652 | sizeof(posix_constants_confstr) |
| 6653 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6654 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6655 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6656 | #endif |
| 6657 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6658 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6659 | sizeof(posix_constants_sysconf) |
| 6660 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6661 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6662 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6663 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6664 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6665 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6666 | |
| 6667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6668 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6669 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6670 | 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] | 6671 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6672 | |
| 6673 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6674 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6675 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6676 | abort(); |
| 6677 | /*NOTREACHED*/ |
| 6678 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6679 | return NULL; |
| 6680 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6681 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6682 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6683 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6684 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6685 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6686 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6687 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6688 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6689 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6690 | application (if any) its extension is associated.\n\ |
| 6691 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6692 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6693 | \n\ |
| 6694 | startfile returns as soon as the associated application is launched.\n\ |
| 6695 | There is no option to wait for the application to close, and no way\n\ |
| 6696 | to retrieve the application's exit status.\n\ |
| 6697 | \n\ |
| 6698 | The filepath is relative to the current directory. If you want to use\n\ |
| 6699 | 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] | 6700 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6701 | |
| 6702 | static PyObject * |
| 6703 | win32_startfile(PyObject *self, PyObject *args) |
| 6704 | { |
| 6705 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6706 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6707 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6708 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6709 | if (unicode_file_names()) { |
| 6710 | PyObject *unipath, *woperation = NULL; |
| 6711 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6712 | &unipath, &operation)) { |
| 6713 | PyErr_Clear(); |
| 6714 | goto normal; |
| 6715 | } |
| 6716 | |
| 6717 | |
| 6718 | if (operation) { |
| 6719 | woperation = PyUnicode_DecodeASCII(operation, |
| 6720 | strlen(operation), NULL); |
| 6721 | if (!woperation) { |
| 6722 | PyErr_Clear(); |
| 6723 | operation = NULL; |
| 6724 | goto normal; |
| 6725 | } |
| 6726 | } |
| 6727 | |
| 6728 | Py_BEGIN_ALLOW_THREADS |
| 6729 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6730 | PyUnicode_AS_UNICODE(unipath), |
| 6731 | NULL, NULL, SW_SHOWNORMAL); |
| 6732 | Py_END_ALLOW_THREADS |
| 6733 | |
| 6734 | Py_XDECREF(woperation); |
| 6735 | if (rc <= (HINSTANCE)32) { |
| 6736 | PyObject *errval = win32_error_unicode("startfile", |
| 6737 | PyUnicode_AS_UNICODE(unipath)); |
| 6738 | return errval; |
| 6739 | } |
| 6740 | Py_INCREF(Py_None); |
| 6741 | return Py_None; |
| 6742 | } |
| 6743 | #endif |
| 6744 | |
| 6745 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6746 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 6747 | Py_FileSystemDefaultEncoding, &filepath, |
| 6748 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6749 | return NULL; |
| 6750 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6751 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6752 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6753 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6754 | if (rc <= (HINSTANCE)32) { |
| 6755 | PyObject *errval = win32_error("startfile", filepath); |
| 6756 | PyMem_Free(filepath); |
| 6757 | return errval; |
| 6758 | } |
| 6759 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6760 | Py_INCREF(Py_None); |
| 6761 | return Py_None; |
| 6762 | } |
| 6763 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6764 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6765 | #ifdef HAVE_GETLOADAVG |
| 6766 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6767 | "getloadavg() -> (float, float, float)\n\n\ |
| 6768 | Return the number of processes in the system run queue averaged over\n\ |
| 6769 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6770 | was unobtainable"); |
| 6771 | |
| 6772 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6773 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6774 | { |
| 6775 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6776 | if (getloadavg(loadavg, 3)!=3) { |
| 6777 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6778 | return NULL; |
| 6779 | } else |
| 6780 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6781 | } |
| 6782 | #endif |
| 6783 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6784 | #ifdef MS_WINDOWS |
| 6785 | |
| 6786 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6787 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6788 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6789 | |
| 6790 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6791 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6792 | DWORD dwFlags ); |
| 6793 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6794 | BYTE *pbBuffer ); |
| 6795 | |
| 6796 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6797 | /* This handle is never explicitly released. Instead, the operating |
| 6798 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6799 | static HCRYPTPROV hCryptProv = 0; |
| 6800 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6801 | static PyObject* |
| 6802 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6803 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6804 | int howMany; |
| 6805 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6806 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6807 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6808 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6809 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6810 | if (howMany < 0) |
| 6811 | return PyErr_Format(PyExc_ValueError, |
| 6812 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6813 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6814 | if (hCryptProv == 0) { |
| 6815 | HINSTANCE hAdvAPI32 = NULL; |
| 6816 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6817 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6818 | /* Obtain handle to the DLL containing CryptoAPI |
| 6819 | This should not fail */ |
| 6820 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6821 | if(hAdvAPI32 == NULL) |
| 6822 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6823 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6824 | /* Obtain pointers to the CryptoAPI functions |
| 6825 | This will fail on some early versions of Win95 */ |
| 6826 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6827 | hAdvAPI32, |
| 6828 | "CryptAcquireContextA"); |
| 6829 | if (pCryptAcquireContext == NULL) |
| 6830 | return PyErr_Format(PyExc_NotImplementedError, |
| 6831 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6832 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6833 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6834 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6835 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6836 | return PyErr_Format(PyExc_NotImplementedError, |
| 6837 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6838 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6839 | /* Acquire context */ |
| 6840 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6841 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6842 | return win32_error("CryptAcquireContext", NULL); |
| 6843 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6844 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6845 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6846 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6847 | if (result != NULL) { |
| 6848 | /* Get random data */ |
Amaury Forgeot d'Arc | a05ada3 | 2008-07-21 21:13:14 +0000 | [diff] [blame] | 6849 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6850 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6851 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6852 | Py_DECREF(result); |
| 6853 | return win32_error("CryptGenRandom", NULL); |
| 6854 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6855 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6856 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6857 | } |
| 6858 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6859 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6860 | PyDoc_STRVAR(device_encoding__doc__, |
| 6861 | "device_encoding(fd) -> str\n\n\ |
| 6862 | Return a string describing the encoding of the device\n\ |
| 6863 | if the output is a terminal; else return None."); |
| 6864 | |
| 6865 | static PyObject * |
| 6866 | device_encoding(PyObject *self, PyObject *args) |
| 6867 | { |
| 6868 | int fd; |
| 6869 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6870 | return NULL; |
Kristján Valur Jónsson | 649170b | 2009-03-24 14:15:49 +0000 | [diff] [blame] | 6871 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6872 | Py_INCREF(Py_None); |
| 6873 | return Py_None; |
| 6874 | } |
| 6875 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6876 | if (fd == 0) { |
| 6877 | char buf[100]; |
| 6878 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6879 | return PyUnicode_FromString(buf); |
| 6880 | } |
| 6881 | if (fd == 1 || fd == 2) { |
| 6882 | char buf[100]; |
| 6883 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6884 | return PyUnicode_FromString(buf); |
| 6885 | } |
| 6886 | #elif defined(CODESET) |
| 6887 | { |
| 6888 | char *codeset = nl_langinfo(CODESET); |
Mark Dickinson | da2706b | 2008-12-11 18:03:03 +0000 | [diff] [blame] | 6889 | if (codeset != NULL && codeset[0] != 0) |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6890 | return PyUnicode_FromString(codeset); |
| 6891 | } |
| 6892 | #endif |
| 6893 | Py_INCREF(Py_None); |
| 6894 | return Py_None; |
| 6895 | } |
| 6896 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6897 | #ifdef __VMS |
| 6898 | /* Use openssl random routine */ |
| 6899 | #include <openssl/rand.h> |
| 6900 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6901 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6902 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6903 | |
| 6904 | static PyObject* |
| 6905 | vms_urandom(PyObject *self, PyObject *args) |
| 6906 | { |
| 6907 | int howMany; |
| 6908 | PyObject* result; |
| 6909 | |
| 6910 | /* Read arguments */ |
| 6911 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6912 | return NULL; |
| 6913 | if (howMany < 0) |
| 6914 | return PyErr_Format(PyExc_ValueError, |
| 6915 | "negative argument not allowed"); |
| 6916 | |
| 6917 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6918 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6919 | if (result != NULL) { |
| 6920 | /* Get random data */ |
| 6921 | if (RAND_pseudo_bytes((unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6922 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6923 | howMany) < 0) { |
| 6924 | Py_DECREF(result); |
| 6925 | return PyErr_Format(PyExc_ValueError, |
| 6926 | "RAND_pseudo_bytes"); |
| 6927 | } |
| 6928 | } |
| 6929 | return result; |
| 6930 | } |
| 6931 | #endif |
| 6932 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6933 | static PyMethodDef posix_methods[] = { |
| 6934 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6935 | #ifdef HAVE_TTYNAME |
| 6936 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6937 | #endif |
| 6938 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6939 | #ifdef HAVE_CHFLAGS |
| 6940 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 6941 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6942 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 6943 | #ifdef HAVE_FCHMOD |
| 6944 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
| 6945 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6946 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6947 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6948 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 6949 | #ifdef HAVE_LCHMOD |
| 6950 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
| 6951 | #endif /* HAVE_LCHMOD */ |
| 6952 | #ifdef HAVE_FCHOWN |
| 6953 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
| 6954 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6955 | #ifdef HAVE_LCHFLAGS |
| 6956 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 6957 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6958 | #ifdef HAVE_LCHOWN |
| 6959 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6960 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6961 | #ifdef HAVE_CHROOT |
| 6962 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6963 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6964 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6965 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6966 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6967 | #ifdef HAVE_GETCWD |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 6968 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 6969 | METH_NOARGS, posix_getcwd__doc__}, |
| 6970 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 6971 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6972 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6973 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6974 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6975 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6976 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6977 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6978 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6979 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6980 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6981 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6982 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6983 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6984 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6985 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6986 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6987 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6988 | {"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] | 6989 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6990 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6991 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6992 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6993 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6994 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6995 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6996 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6997 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6998 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6999 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7000 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7001 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7002 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7003 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7004 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7005 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7006 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7007 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7008 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7009 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7010 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7011 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7012 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7013 | #if defined(PYOS_OS2) |
| 7014 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7015 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7016 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7017 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7018 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7019 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7020 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7021 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7022 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7023 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7024 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7025 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7026 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7027 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7028 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7029 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7030 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7031 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7032 | #endif /* HAVE_GETEGID */ |
| 7033 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7034 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7035 | #endif /* HAVE_GETEUID */ |
| 7036 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7037 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7038 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7039 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7040 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7041 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7042 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7043 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7044 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7045 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7046 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7047 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7048 | #endif /* HAVE_GETPPID */ |
| 7049 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7050 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7051 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7052 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7053 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7054 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7055 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7056 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7057 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7058 | #ifdef HAVE_KILLPG |
| 7059 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7060 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7061 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7062 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7063 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7064 | #ifdef MS_WINDOWS |
| 7065 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 7066 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7067 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7068 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7069 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7070 | #ifdef HAVE_SETEUID |
| 7071 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7072 | #endif /* HAVE_SETEUID */ |
| 7073 | #ifdef HAVE_SETEGID |
| 7074 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7075 | #endif /* HAVE_SETEGID */ |
| 7076 | #ifdef HAVE_SETREUID |
| 7077 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7078 | #endif /* HAVE_SETREUID */ |
| 7079 | #ifdef HAVE_SETREGID |
| 7080 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7081 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7082 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7083 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7084 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7085 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 7086 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7087 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7088 | #ifdef HAVE_GETPGID |
| 7089 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7090 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7091 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7092 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7093 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7094 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7095 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7096 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7097 | #ifdef HAVE_WAIT3 |
| 7098 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7099 | #endif /* HAVE_WAIT3 */ |
| 7100 | #ifdef HAVE_WAIT4 |
| 7101 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7102 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7103 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7104 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7105 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7106 | #ifdef HAVE_GETSID |
| 7107 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7108 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7109 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7110 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7111 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7112 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7113 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7114 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7115 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7116 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7117 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7118 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7119 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7120 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7121 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7122 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7123 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7124 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7125 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7126 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7127 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7128 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7129 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7130 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7131 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7132 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7133 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7134 | #endif |
| 7135 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7136 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7137 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7138 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7139 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7140 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7141 | #ifdef HAVE_DEVICE_MACROS |
| 7142 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7143 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7144 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7145 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7146 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7147 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7148 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7149 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7150 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7151 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7152 | #ifdef HAVE_UNSETENV |
| 7153 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7154 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7155 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7156 | #ifdef HAVE_FCHDIR |
| 7157 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7158 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7159 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7160 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7161 | #endif |
| 7162 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7163 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7164 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7165 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7166 | #ifdef WCOREDUMP |
| 7167 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7168 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7169 | #ifdef WIFCONTINUED |
| 7170 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7171 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7172 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7173 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7174 | #endif /* WIFSTOPPED */ |
| 7175 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7176 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7177 | #endif /* WIFSIGNALED */ |
| 7178 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7179 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7180 | #endif /* WIFEXITED */ |
| 7181 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7182 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7183 | #endif /* WEXITSTATUS */ |
| 7184 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7185 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7186 | #endif /* WTERMSIG */ |
| 7187 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7188 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7189 | #endif /* WSTOPSIG */ |
| 7190 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7191 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7192 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7193 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7194 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7195 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7196 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7197 | #ifdef HAVE_CONFSTR |
| 7198 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7199 | #endif |
| 7200 | #ifdef HAVE_SYSCONF |
| 7201 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7202 | #endif |
| 7203 | #ifdef HAVE_FPATHCONF |
| 7204 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7205 | #endif |
| 7206 | #ifdef HAVE_PATHCONF |
| 7207 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7208 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7209 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7210 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7211 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7212 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7213 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7214 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7215 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7216 | #ifdef MS_WINDOWS |
| 7217 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7218 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7219 | #ifdef __VMS |
| 7220 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 7221 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7222 | {NULL, NULL} /* Sentinel */ |
| 7223 | }; |
| 7224 | |
| 7225 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7226 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7227 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7228 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7229 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7230 | } |
| 7231 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7232 | #if defined(PYOS_OS2) |
| 7233 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7234 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7235 | { |
| 7236 | APIRET rc; |
| 7237 | ULONG values[QSV_MAX+1]; |
| 7238 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7239 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7240 | |
| 7241 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7242 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7243 | Py_END_ALLOW_THREADS |
| 7244 | |
| 7245 | if (rc != NO_ERROR) { |
| 7246 | os2_error(rc); |
| 7247 | return -1; |
| 7248 | } |
| 7249 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7250 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7251 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7252 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7253 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7254 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7255 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7256 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7257 | |
| 7258 | switch (values[QSV_VERSION_MINOR]) { |
| 7259 | case 0: ver = "2.00"; break; |
| 7260 | case 10: ver = "2.10"; break; |
| 7261 | case 11: ver = "2.11"; break; |
| 7262 | case 30: ver = "3.00"; break; |
| 7263 | case 40: ver = "4.00"; break; |
| 7264 | case 50: ver = "5.00"; break; |
| 7265 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7266 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7267 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7268 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7269 | ver = &tmp[0]; |
| 7270 | } |
| 7271 | |
| 7272 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7273 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7274 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7275 | |
| 7276 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7277 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7278 | tmp[1] = ':'; |
| 7279 | tmp[2] = '\0'; |
| 7280 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7281 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7282 | } |
| 7283 | #endif |
| 7284 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7285 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7286 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7287 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7288 | #ifdef F_OK |
| 7289 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7290 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7291 | #ifdef R_OK |
| 7292 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7293 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7294 | #ifdef W_OK |
| 7295 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7296 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7297 | #ifdef X_OK |
| 7298 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7299 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7300 | #ifdef NGROUPS_MAX |
| 7301 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7302 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7303 | #ifdef TMP_MAX |
| 7304 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7305 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7306 | #ifdef WCONTINUED |
| 7307 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7308 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7309 | #ifdef WNOHANG |
| 7310 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7311 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7312 | #ifdef WUNTRACED |
| 7313 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7314 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7315 | #ifdef O_RDONLY |
| 7316 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7317 | #endif |
| 7318 | #ifdef O_WRONLY |
| 7319 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7320 | #endif |
| 7321 | #ifdef O_RDWR |
| 7322 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7323 | #endif |
| 7324 | #ifdef O_NDELAY |
| 7325 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7326 | #endif |
| 7327 | #ifdef O_NONBLOCK |
| 7328 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7329 | #endif |
| 7330 | #ifdef O_APPEND |
| 7331 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7332 | #endif |
| 7333 | #ifdef O_DSYNC |
| 7334 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7335 | #endif |
| 7336 | #ifdef O_RSYNC |
| 7337 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7338 | #endif |
| 7339 | #ifdef O_SYNC |
| 7340 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7341 | #endif |
| 7342 | #ifdef O_NOCTTY |
| 7343 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7344 | #endif |
| 7345 | #ifdef O_CREAT |
| 7346 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7347 | #endif |
| 7348 | #ifdef O_EXCL |
| 7349 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7350 | #endif |
| 7351 | #ifdef O_TRUNC |
| 7352 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7353 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7354 | #ifdef O_BINARY |
| 7355 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7356 | #endif |
| 7357 | #ifdef O_TEXT |
| 7358 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7359 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7360 | #ifdef O_LARGEFILE |
| 7361 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7362 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7363 | #ifdef O_SHLOCK |
| 7364 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7365 | #endif |
| 7366 | #ifdef O_EXLOCK |
| 7367 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7368 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7369 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7370 | /* MS Windows */ |
| 7371 | #ifdef O_NOINHERIT |
| 7372 | /* Don't inherit in child processes. */ |
| 7373 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7374 | #endif |
| 7375 | #ifdef _O_SHORT_LIVED |
| 7376 | /* Optimize for short life (keep in memory). */ |
| 7377 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7378 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7379 | #endif |
| 7380 | #ifdef O_TEMPORARY |
| 7381 | /* Automatically delete when last handle is closed. */ |
| 7382 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7383 | #endif |
| 7384 | #ifdef O_RANDOM |
| 7385 | /* Optimize for random access. */ |
| 7386 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7387 | #endif |
| 7388 | #ifdef O_SEQUENTIAL |
| 7389 | /* Optimize for sequential access. */ |
| 7390 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7391 | #endif |
| 7392 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7393 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7394 | #ifdef O_ASYNC |
| 7395 | /* Send a SIGIO signal whenever input or output |
| 7396 | becomes available on file descriptor */ |
| 7397 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
| 7398 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7399 | #ifdef O_DIRECT |
| 7400 | /* Direct disk access. */ |
| 7401 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7402 | #endif |
| 7403 | #ifdef O_DIRECTORY |
| 7404 | /* Must be a directory. */ |
| 7405 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7406 | #endif |
| 7407 | #ifdef O_NOFOLLOW |
| 7408 | /* Do not follow links. */ |
| 7409 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7410 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7411 | #ifdef O_NOATIME |
| 7412 | /* Do not update the access time. */ |
| 7413 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
| 7414 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7415 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7416 | /* These come from sysexits.h */ |
| 7417 | #ifdef EX_OK |
| 7418 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7419 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7420 | #ifdef EX_USAGE |
| 7421 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7422 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7423 | #ifdef EX_DATAERR |
| 7424 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7425 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7426 | #ifdef EX_NOINPUT |
| 7427 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7428 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7429 | #ifdef EX_NOUSER |
| 7430 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7431 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7432 | #ifdef EX_NOHOST |
| 7433 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7434 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7435 | #ifdef EX_UNAVAILABLE |
| 7436 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7437 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7438 | #ifdef EX_SOFTWARE |
| 7439 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7440 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7441 | #ifdef EX_OSERR |
| 7442 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7443 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7444 | #ifdef EX_OSFILE |
| 7445 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7446 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7447 | #ifdef EX_CANTCREAT |
| 7448 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7449 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7450 | #ifdef EX_IOERR |
| 7451 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7452 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7453 | #ifdef EX_TEMPFAIL |
| 7454 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7455 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7456 | #ifdef EX_PROTOCOL |
| 7457 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7458 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7459 | #ifdef EX_NOPERM |
| 7460 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7461 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7462 | #ifdef EX_CONFIG |
| 7463 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7464 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7465 | #ifdef EX_NOTFOUND |
| 7466 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7467 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7468 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7469 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7470 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7471 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7472 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7473 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7474 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7475 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7476 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7477 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7478 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7479 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7480 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7481 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7482 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7483 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7484 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7485 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7486 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7487 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7488 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7489 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7490 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7491 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7492 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7493 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7494 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7495 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7496 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7497 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7498 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7499 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7500 | #if defined(PYOS_OS2) |
| 7501 | if (insertvalues(d)) return -1; |
| 7502 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7503 | return 0; |
| 7504 | } |
| 7505 | |
| 7506 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7507 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7508 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7509 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7510 | |
| 7511 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7512 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7513 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7514 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7515 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7516 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7517 | #define MODNAME "posix" |
| 7518 | #endif |
| 7519 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7520 | static struct PyModuleDef posixmodule = { |
| 7521 | PyModuleDef_HEAD_INIT, |
| 7522 | MODNAME, |
| 7523 | posix__doc__, |
| 7524 | -1, |
| 7525 | posix_methods, |
| 7526 | NULL, |
| 7527 | NULL, |
| 7528 | NULL, |
| 7529 | NULL |
| 7530 | }; |
| 7531 | |
| 7532 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7533 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7534 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7535 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7536 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7537 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7538 | m = PyModule_Create(&posixmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7539 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7540 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7541 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7542 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7543 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7544 | Py_XINCREF(v); |
| 7545 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7546 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7547 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7548 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7549 | if (all_ins(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7550 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7551 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7552 | if (setup_confname_tables(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7553 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7554 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7555 | Py_INCREF(PyExc_OSError); |
| 7556 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7557 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7558 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7559 | if (posix_putenv_garbage == NULL) |
| 7560 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7561 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7562 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7563 | if (!initialized) { |
| 7564 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7565 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7566 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7567 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7568 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7569 | structseq_new = StatResultType.tp_new; |
| 7570 | StatResultType.tp_new = statresult_new; |
| 7571 | |
| 7572 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7573 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7574 | #ifdef NEED_TICKS_PER_SECOND |
| 7575 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
| 7576 | ticks_per_second = sysconf(_SC_CLK_TCK); |
| 7577 | # elif defined(HZ) |
| 7578 | ticks_per_second = HZ; |
| 7579 | # else |
| 7580 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
| 7581 | # endif |
| 7582 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7583 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7584 | Py_INCREF((PyObject*) &StatResultType); |
| 7585 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7586 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7587 | PyModule_AddObject(m, "statvfs_result", |
| 7588 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7589 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7590 | |
| 7591 | #ifdef __APPLE__ |
| 7592 | /* |
| 7593 | * Step 2 of weak-linking support on Mac OS X. |
| 7594 | * |
| 7595 | * The code below removes functions that are not available on the |
| 7596 | * currently active platform. |
| 7597 | * |
| 7598 | * This block allow one to use a python binary that was build on |
| 7599 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7600 | * OSX 10.4. |
| 7601 | */ |
| 7602 | #ifdef HAVE_FSTATVFS |
| 7603 | if (fstatvfs == NULL) { |
| 7604 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7605 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7606 | } |
| 7607 | } |
| 7608 | #endif /* HAVE_FSTATVFS */ |
| 7609 | |
| 7610 | #ifdef HAVE_STATVFS |
| 7611 | if (statvfs == NULL) { |
| 7612 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7613 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7614 | } |
| 7615 | } |
| 7616 | #endif /* HAVE_STATVFS */ |
| 7617 | |
| 7618 | # ifdef HAVE_LCHOWN |
| 7619 | if (lchown == NULL) { |
| 7620 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7621 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7622 | } |
| 7623 | } |
| 7624 | #endif /* HAVE_LCHOWN */ |
| 7625 | |
| 7626 | |
| 7627 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7628 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7629 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7630 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7631 | |
| 7632 | #ifdef __cplusplus |
| 7633 | } |
| 7634 | #endif |