Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
| 18 | * Step 1 of support for weak-linking a number of symbols existing on |
| 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
| 31 | #include "structseq.h" |
| 32 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #if defined(PYOS_OS2) |
| 49 | #define INCL_DOS |
| 50 | #define INCL_DOSERRORS |
| 51 | #define INCL_DOSPROCESS |
| 52 | #define INCL_NOPMAPI |
| 53 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 54 | #if defined(PYCC_GCC) |
| 55 | #include <ctype.h> |
| 56 | #include <io.h> |
| 57 | #include <stdio.h> |
| 58 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 59 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 60 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | #endif /* HAVE_SYS_TYPES_H */ |
| 66 | |
| 67 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYS_WAIT_H |
| 72 | #include <sys/wait.h> /* For WNOHANG */ |
| 73 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 76 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | #ifdef HAVE_FCNTL_H |
| 80 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 81 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | #ifdef HAVE_GRP_H |
| 84 | #include <grp.h> |
| 85 | #endif |
| 86 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 87 | #ifdef HAVE_SYSEXITS_H |
| 88 | #include <sysexits.h> |
| 89 | #endif /* HAVE_SYSEXITS_H */ |
| 90 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYS_LOADAVG_H |
| 92 | #include <sys/loadavg.h> |
| 93 | #endif |
| 94 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 95 | #ifdef HAVE_LANGINFO_H |
| 96 | #include <langinfo.h> |
| 97 | #endif |
| 98 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> |
| 103 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
| 106 | #define HAVE_OPENDIR 1 |
| 107 | #define HAVE_SYSTEM 1 |
| 108 | #if defined(__OS2__) |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> |
| 113 | #else |
| 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR 1 |
| 118 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 119 | #define HAVE_SYSTEM 1 |
| 120 | #define HAVE_WAIT 1 |
| 121 | #else |
| 122 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 123 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 124 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #define HAVE_EXECV 1 |
| 126 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 128 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 129 | #define HAVE_FSYNC 1 |
| 130 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 131 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 132 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 133 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #else /* all other compilers */ |
| 135 | /* Unix functions that the configure script doesn't check for */ |
| 136 | #define HAVE_EXECV 1 |
| 137 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 138 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 139 | #define HAVE_FORK1 1 |
| 140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #define HAVE_GETCWD 1 |
| 142 | #define HAVE_GETEGID 1 |
| 143 | #define HAVE_GETEUID 1 |
| 144 | #define HAVE_GETGID 1 |
| 145 | #define HAVE_GETPPID 1 |
| 146 | #define HAVE_GETUID 1 |
| 147 | #define HAVE_KILL 1 |
| 148 | #define HAVE_OPENDIR 1 |
| 149 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
| 151 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 152 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 153 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #endif /* _MSC_VER */ |
| 155 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 156 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 157 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 161 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 162 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 163 | (default) */ |
| 164 | extern char *ctermid_r(char *); |
| 165 | #endif |
| 166 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 167 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 170 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 171 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #endif |
| 177 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int chdir(char *); |
| 179 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 180 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int chdir(const char *); |
| 182 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 184 | #ifdef __BORLANDC__ |
| 185 | extern int chmod(const char *, int); |
| 186 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 188 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 189 | /*#ifdef HAVE_FCHMOD |
| 190 | extern int fchmod(int, mode_t); |
| 191 | #endif*/ |
| 192 | /*#ifdef HAVE_LCHMOD |
| 193 | extern int lchmod(const char *, mode_t); |
| 194 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int chown(const char *, uid_t, gid_t); |
| 196 | extern char *getcwd(char *, int); |
| 197 | extern char *strerror(int); |
| 198 | extern int link(const char *, const char *); |
| 199 | extern int rename(const char *, const char *); |
| 200 | extern int stat(const char *, struct stat *); |
| 201 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 206 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 207 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #ifdef HAVE_UTIME_H |
| 213 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 216 | #ifdef HAVE_SYS_UTIME_H |
| 217 | #include <sys/utime.h> |
| 218 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 219 | #endif /* HAVE_SYS_UTIME_H */ |
| 220 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #ifdef HAVE_SYS_TIMES_H |
| 222 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 223 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | |
| 225 | #ifdef HAVE_SYS_PARAM_H |
| 226 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 227 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | |
| 229 | #ifdef HAVE_SYS_UTSNAME_H |
| 230 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 231 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 236 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 237 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #include <direct.h> |
| 239 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 240 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 243 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 244 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 249 | #endif |
| 250 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 255 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 256 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 258 | #endif |
| 259 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 261 | #endif |
| 262 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 264 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 265 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 266 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 268 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 269 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 271 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 273 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 275 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 276 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 277 | #define MAXPATHLEN PATH_MAX |
| 278 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 279 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 280 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 281 | #endif /* MAXPATHLEN */ |
| 282 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 283 | #ifdef UNION_WAIT |
| 284 | /* Emulate some macros on systems that have a union instead of macros */ |
| 285 | |
| 286 | #ifndef WIFEXITED |
| 287 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 288 | #endif |
| 289 | |
| 290 | #ifndef WEXITSTATUS |
| 291 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 292 | #endif |
| 293 | |
| 294 | #ifndef WTERMSIG |
| 295 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 296 | #endif |
| 297 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 298 | #define WAIT_TYPE union wait |
| 299 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 300 | |
| 301 | #else /* !UNION_WAIT */ |
| 302 | #define WAIT_TYPE int |
| 303 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 304 | #endif /* UNION_WAIT */ |
| 305 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 306 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 307 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 308 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 309 | #define USE_CTERMID_R |
| 310 | #endif |
| 311 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 312 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 313 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 314 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 315 | # define STAT win32_stat |
| 316 | # define FSTAT win32_fstat |
| 317 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 318 | #else |
| 319 | # define STAT stat |
| 320 | # define FSTAT fstat |
| 321 | # define STRUCT_STAT struct stat |
| 322 | #endif |
| 323 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 324 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 325 | #include <sys/mkdev.h> |
| 326 | #else |
| 327 | #if defined(MAJOR_IN_SYSMACROS) |
| 328 | #include <sys/sysmacros.h> |
| 329 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 330 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 331 | #include <sys/mkdev.h> |
| 332 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 333 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 334 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 335 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 336 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 337 | * valid and throw an assertion if it isn't. |
| 338 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 339 | * an assertion can be useful, but it does contradict the POSIX standard |
| 340 | * which for write(2) states: |
| 341 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 342 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 343 | * writing." |
| 344 | * Furthermore, python allows the user to enter any old integer |
| 345 | * as a fd and should merely raise a python exception on error. |
| 346 | * The Microsoft CRT doesn't provide an official way to check for the |
| 347 | * validity of a file descriptor, but we can emulate its internal behaviour |
| 348 | * by using the exported __pinfo data member and knowledge of the |
| 349 | * internal structures involved. |
| 350 | * The structures below must be updated for each version of visual studio |
| 351 | * according to the file internal.h in the CRT source, until MS comes |
| 352 | * up with a less hacky way to do this. |
| 353 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 354 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 355 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 356 | /* The actual size of the structure is determined at runtime. |
| 357 | * Only the first items must be present. |
| 358 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 359 | typedef struct { |
| 360 | intptr_t osfhnd; |
| 361 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 362 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 363 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 364 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 365 | #define IOINFO_L2E 5 |
| 366 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 367 | #define IOINFO_ARRAYS 64 |
| 368 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 369 | #define FOPEN 0x01 |
| 370 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 371 | |
| 372 | /* This function emulates what the windows CRT does to validate file handles */ |
| 373 | int |
| 374 | _PyVerify_fd(int fd) |
| 375 | { |
| 376 | const int i1 = fd >> IOINFO_L2E; |
| 377 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 378 | |
| 379 | static int sizeof_ioinfo = 0; |
| 380 | |
| 381 | /* Determine the actual size of the ioinfo structure, |
| 382 | * as used by the CRT loaded in memory |
| 383 | */ |
| 384 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 385 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 386 | } |
| 387 | if (sizeof_ioinfo == 0) { |
| 388 | /* This should not happen... */ |
| 389 | goto fail; |
| 390 | } |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 391 | |
| 392 | /* See that it isn't a special CLEAR fileno */ |
| 393 | if (fd != _NO_CONSOLE_FILENO) { |
| 394 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 395 | * we check pointer validity and other info |
| 396 | */ |
| 397 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 398 | /* finally, check that the file is open */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 399 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 400 | if (info->osfile & FOPEN) { |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 401 | return 1; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 402 | } |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 405 | fail: |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 406 | errno = EBADF; |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 411 | static int |
| 412 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 413 | { |
| 414 | if (!_PyVerify_fd(fd1)) |
| 415 | return 0; |
| 416 | if (fd2 == _NO_CONSOLE_FILENO) |
| 417 | return 0; |
| 418 | if ((unsigned)fd2 < _NHANDLE_) |
| 419 | return 1; |
| 420 | else |
| 421 | return 0; |
| 422 | } |
| 423 | #else |
| 424 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 425 | #define _PyVerify_fd_dup2(A, B) (1) |
| 426 | #endif |
| 427 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 428 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 429 | #ifdef WITH_NEXT_FRAMEWORK |
| 430 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 431 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 432 | */ |
| 433 | #include <crt_externs.h> |
| 434 | static char **environ; |
| 435 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 436 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 437 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 438 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 439 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 440 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 442 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 443 | #ifdef MS_WINDOWS |
| 444 | wchar_t **e; |
| 445 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 446 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 447 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 448 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 449 | if (d == NULL) |
| 450 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 451 | #ifdef WITH_NEXT_FRAMEWORK |
| 452 | if (environ == NULL) |
| 453 | environ = *_NSGetEnviron(); |
| 454 | #endif |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 455 | #ifdef MS_WINDOWS |
| 456 | /* _wenviron must be initialized in this way if the program is started |
| 457 | through main() instead of wmain(). */ |
| 458 | _wgetenv(L""); |
| 459 | if (_wenviron == NULL) |
| 460 | return d; |
| 461 | /* This part ignores errors */ |
| 462 | for (e = _wenviron; *e != NULL; e++) { |
| 463 | PyObject *k; |
| 464 | PyObject *v; |
| 465 | wchar_t *p = wcschr(*e, L'='); |
| 466 | if (p == NULL) |
| 467 | continue; |
| 468 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 469 | if (k == NULL) { |
| 470 | PyErr_Clear(); |
| 471 | continue; |
| 472 | } |
| 473 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 474 | if (v == NULL) { |
| 475 | PyErr_Clear(); |
| 476 | Py_DECREF(k); |
| 477 | continue; |
| 478 | } |
| 479 | if (PyDict_GetItem(d, k) == NULL) { |
| 480 | if (PyDict_SetItem(d, k, v) != 0) |
| 481 | PyErr_Clear(); |
| 482 | } |
| 483 | Py_DECREF(k); |
| 484 | Py_DECREF(v); |
| 485 | } |
| 486 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 487 | if (environ == NULL) |
| 488 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 489 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 490 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 491 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 492 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 493 | char *p = strchr(*e, '='); |
| 494 | if (p == NULL) |
| 495 | continue; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 496 | k = PyUnicode_Decode(*e, (int)(p-*e), |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 497 | Py_FileSystemDefaultEncoding, "surrogateescape"); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 498 | if (k == NULL) { |
| 499 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 500 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 501 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 502 | v = PyUnicode_Decode(p+1, strlen(p+1), |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 503 | Py_FileSystemDefaultEncoding, "surrogateescape"); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 504 | if (v == NULL) { |
| 505 | PyErr_Clear(); |
| 506 | Py_DECREF(k); |
| 507 | continue; |
| 508 | } |
| 509 | if (PyDict_GetItem(d, k) == NULL) { |
| 510 | if (PyDict_SetItem(d, k, v) != 0) |
| 511 | PyErr_Clear(); |
| 512 | } |
| 513 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 514 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 515 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 516 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 517 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 518 | { |
| 519 | APIRET rc; |
| 520 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 521 | |
| 522 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 523 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 524 | PyObject *v = PyBytes_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 525 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 526 | Py_DECREF(v); |
| 527 | } |
| 528 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 529 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 530 | PyObject *v = PyBytes_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 531 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 532 | Py_DECREF(v); |
| 533 | } |
| 534 | } |
| 535 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 536 | return d; |
| 537 | } |
| 538 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 539 | /* Convert a bytes object to a char*. Optionally lock the buffer if it is a |
| 540 | bytes array. */ |
| 541 | |
| 542 | static char* |
| 543 | bytes2str(PyObject* o, int lock) |
| 544 | { |
| 545 | if(PyBytes_Check(o)) |
| 546 | return PyBytes_AsString(o); |
| 547 | else if(PyByteArray_Check(o)) { |
| 548 | if (lock && PyObject_GetBuffer(o, NULL, 0) < 0) |
| 549 | /* On a bytearray, this should not fail. */ |
| 550 | PyErr_BadInternalCall(); |
| 551 | return PyByteArray_AsString(o); |
| 552 | } else { |
| 553 | /* The FS converter should have verified that this |
| 554 | is either bytes or bytearray. */ |
| 555 | Py_FatalError("bad object passed to bytes2str"); |
| 556 | /* not reached. */ |
| 557 | return ""; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* Release the lock, decref the object. */ |
| 562 | static void |
| 563 | release_bytes(PyObject* o) |
| 564 | { |
| 565 | if (PyByteArray_Check(o)) |
| 566 | o->ob_type->tp_as_buffer->bf_releasebuffer(NULL, 0); |
| 567 | Py_DECREF(o); |
| 568 | } |
| 569 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 571 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 572 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 573 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 574 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 575 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 576 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 577 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 578 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 579 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 580 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 581 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 584 | #ifdef Py_WIN_WIDE_FILENAMES |
| 585 | static PyObject * |
| 586 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 587 | { |
| 588 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 589 | } |
| 590 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 591 | |
| 592 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 593 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 594 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 595 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 596 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
| 597 | bytes2str(name, 0)); |
| 598 | release_bytes(name); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 599 | return rc; |
| 600 | } |
| 601 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 602 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 603 | static PyObject * |
| 604 | win32_error(char* function, char* filename) |
| 605 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 606 | /* XXX We should pass the function name along in the future. |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 607 | (winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 608 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 609 | Windows error object, which is non-trivial. |
| 610 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 611 | errno = GetLastError(); |
| 612 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 613 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 614 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 615 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 616 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 617 | |
| 618 | #ifdef Py_WIN_WIDE_FILENAMES |
| 619 | static PyObject * |
| 620 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 621 | { |
| 622 | /* XXX - see win32_error for comments on 'function' */ |
| 623 | errno = GetLastError(); |
| 624 | if (filename) |
| 625 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 626 | else |
| 627 | return PyErr_SetFromWindowsErr(errno); |
| 628 | } |
| 629 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 630 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 631 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 632 | { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 633 | if (PyUnicode_CheckExact(*param)) |
| 634 | Py_INCREF(*param); |
| 635 | else if (PyUnicode_Check(*param)) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 636 | /* For a Unicode subtype that's not a Unicode object, |
| 637 | return a true Unicode object with the same data. */ |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 638 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 639 | PyUnicode_GET_SIZE(*param)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 640 | else |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 641 | *param = PyUnicode_FromEncodedObject(*param, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 642 | Py_FileSystemDefaultEncoding, |
| 643 | "strict"); |
| 644 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 648 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 649 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 650 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 651 | #if defined(PYOS_OS2) |
| 652 | /********************************************************************** |
| 653 | * Helper Function to Trim and Format OS/2 Messages |
| 654 | **********************************************************************/ |
| 655 | static void |
| 656 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 657 | { |
| 658 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 659 | |
| 660 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 661 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 662 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 663 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 664 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 665 | } |
| 666 | |
| 667 | /* Add Optional Reason Text */ |
| 668 | if (reason) { |
| 669 | strcat(msgbuf, " : "); |
| 670 | strcat(msgbuf, reason); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /********************************************************************** |
| 675 | * Decode an OS/2 Operating System Error Code |
| 676 | * |
| 677 | * A convenience function to lookup an OS/2 error code and return a |
| 678 | * text message we can use to raise a Python exception. |
| 679 | * |
| 680 | * Notes: |
| 681 | * The messages for errors returned from the OS/2 kernel reside in |
| 682 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 683 | * |
| 684 | **********************************************************************/ |
| 685 | static char * |
| 686 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 687 | { |
| 688 | APIRET rc; |
| 689 | ULONG msglen; |
| 690 | |
| 691 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 692 | Py_BEGIN_ALLOW_THREADS |
| 693 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 694 | errorcode, "oso001.msg", &msglen); |
| 695 | Py_END_ALLOW_THREADS |
| 696 | |
| 697 | if (rc == NO_ERROR) |
| 698 | os2_formatmsg(msgbuf, msglen, reason); |
| 699 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 700 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 701 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 702 | |
| 703 | return msgbuf; |
| 704 | } |
| 705 | |
| 706 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 707 | errors are not in a global variable e.g. 'errno' nor are |
| 708 | they congruent with posix error numbers. */ |
| 709 | |
| 710 | static PyObject * os2_error(int code) |
| 711 | { |
| 712 | char text[1024]; |
| 713 | PyObject *v; |
| 714 | |
| 715 | os2_strerror(text, sizeof(text), code, ""); |
| 716 | |
| 717 | v = Py_BuildValue("(is)", code, text); |
| 718 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 719 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 720 | Py_DECREF(v); |
| 721 | } |
| 722 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 723 | } |
| 724 | |
| 725 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 726 | |
| 727 | /* POSIX generic methods */ |
| 728 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 729 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 730 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 731 | { |
| 732 | int fd; |
| 733 | int res; |
| 734 | fd = PyObject_AsFileDescriptor(fdobj); |
| 735 | if (fd < 0) |
| 736 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 737 | if (!_PyVerify_fd(fd)) |
| 738 | return posix_error(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 739 | Py_BEGIN_ALLOW_THREADS |
| 740 | res = (*func)(fd); |
| 741 | Py_END_ALLOW_THREADS |
| 742 | if (res < 0) |
| 743 | return posix_error(); |
| 744 | Py_INCREF(Py_None); |
| 745 | return Py_None; |
| 746 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 747 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 748 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 749 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 750 | unicode_file_names(void) |
| 751 | { |
| 752 | static int canusewide = -1; |
| 753 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 754 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 755 | the Windows NT family. */ |
| 756 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 757 | } |
| 758 | return canusewide; |
| 759 | } |
| 760 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 761 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 762 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 763 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 764 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 765 | PyObject *opath1 = NULL; |
| 766 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 767 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 768 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 769 | PyUnicode_FSConverter, &opath1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 770 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 771 | path1 = bytes2str(opath1, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 772 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 773 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 774 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 775 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 776 | return posix_error_with_allocated_filename(opath1); |
| 777 | release_bytes(opath1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 778 | Py_INCREF(Py_None); |
| 779 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 782 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 783 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 784 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 785 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 786 | { |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame^] | 787 | PyObject *opath1 = NULL, *opath2 = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 788 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 789 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 790 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 791 | PyUnicode_FSConverter, &opath1, |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame^] | 792 | PyUnicode_FSConverter, &opath2)) { |
| 793 | Py_XDECREF(opath1); |
| 794 | Py_XDECREF(opath2); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 795 | return NULL; |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame^] | 796 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 797 | path1 = bytes2str(opath1, 1); |
| 798 | path2 = bytes2str(opath2, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 799 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 800 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 801 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 802 | release_bytes(opath1); |
| 803 | release_bytes(opath2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 804 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 805 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 806 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 807 | Py_INCREF(Py_None); |
| 808 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 811 | #ifdef Py_WIN_WIDE_FILENAMES |
| 812 | static PyObject* |
| 813 | win32_1str(PyObject* args, char* func, |
| 814 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 815 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 816 | { |
| 817 | PyObject *uni; |
| 818 | char *ansi; |
| 819 | BOOL result; |
| 820 | if (unicode_file_names()) { |
| 821 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 822 | PyErr_Clear(); |
| 823 | else { |
| 824 | Py_BEGIN_ALLOW_THREADS |
| 825 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 826 | Py_END_ALLOW_THREADS |
| 827 | if (!result) |
| 828 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 829 | Py_INCREF(Py_None); |
| 830 | return Py_None; |
| 831 | } |
| 832 | } |
| 833 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 834 | return NULL; |
| 835 | Py_BEGIN_ALLOW_THREADS |
| 836 | result = funcA(ansi); |
| 837 | Py_END_ALLOW_THREADS |
| 838 | if (!result) |
| 839 | return win32_error(func, ansi); |
| 840 | Py_INCREF(Py_None); |
| 841 | return Py_None; |
| 842 | |
| 843 | } |
| 844 | |
| 845 | /* This is a reimplementation of the C library's chdir function, |
| 846 | but one that produces Win32 errors instead of DOS error codes. |
| 847 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 848 | it also needs to set "magic" environment variables indicating |
| 849 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 850 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 851 | win32_chdir(LPCSTR path) |
| 852 | { |
| 853 | char new_path[MAX_PATH+1]; |
| 854 | int result; |
| 855 | char env[4] = "=x:"; |
| 856 | |
| 857 | if(!SetCurrentDirectoryA(path)) |
| 858 | return FALSE; |
| 859 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 860 | if (!result) |
| 861 | return FALSE; |
| 862 | /* In the ANSI API, there should not be any paths longer |
| 863 | than MAX_PATH. */ |
| 864 | assert(result <= MAX_PATH+1); |
| 865 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 866 | strncmp(new_path, "//", 2) == 0) |
| 867 | /* UNC path, nothing to do. */ |
| 868 | return TRUE; |
| 869 | env[1] = new_path[0]; |
| 870 | return SetEnvironmentVariableA(env, new_path); |
| 871 | } |
| 872 | |
| 873 | /* The Unicode version differs from the ANSI version |
| 874 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 875 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 876 | win32_wchdir(LPCWSTR path) |
| 877 | { |
| 878 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 879 | int result; |
| 880 | wchar_t env[4] = L"=x:"; |
| 881 | |
| 882 | if(!SetCurrentDirectoryW(path)) |
| 883 | return FALSE; |
| 884 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 885 | if (!result) |
| 886 | return FALSE; |
| 887 | if (result > MAX_PATH+1) { |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 888 | new_path = malloc(result * sizeof(wchar_t)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 889 | if (!new_path) { |
| 890 | SetLastError(ERROR_OUTOFMEMORY); |
| 891 | return FALSE; |
| 892 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 893 | result = GetCurrentDirectoryW(result, new_path); |
| 894 | if (!result) { |
| 895 | free(new_path); |
| 896 | return FALSE; |
| 897 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 898 | } |
| 899 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 900 | wcsncmp(new_path, L"//", 2) == 0) |
| 901 | /* UNC path, nothing to do. */ |
| 902 | return TRUE; |
| 903 | env[1] = new_path[0]; |
| 904 | result = SetEnvironmentVariableW(env, new_path); |
| 905 | if (new_path != _new_path) |
| 906 | free(new_path); |
| 907 | return result; |
| 908 | } |
| 909 | #endif |
| 910 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 911 | #ifdef MS_WINDOWS |
| 912 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 913 | - time stamps are restricted to second resolution |
| 914 | - file modification times suffer from forth-and-back conversions between |
| 915 | UTC and local time |
| 916 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 917 | */ |
| 918 | #define HAVE_STAT_NSEC 1 |
| 919 | |
| 920 | struct win32_stat{ |
| 921 | int st_dev; |
| 922 | __int64 st_ino; |
| 923 | unsigned short st_mode; |
| 924 | int st_nlink; |
| 925 | int st_uid; |
| 926 | int st_gid; |
| 927 | int st_rdev; |
| 928 | __int64 st_size; |
| 929 | int st_atime; |
| 930 | int st_atime_nsec; |
| 931 | int st_mtime; |
| 932 | int st_mtime_nsec; |
| 933 | int st_ctime; |
| 934 | int st_ctime_nsec; |
| 935 | }; |
| 936 | |
| 937 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 938 | |
| 939 | static void |
| 940 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 941 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 942 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 943 | /* Cannot simply cast and dereference in_ptr, |
| 944 | since it might not be aligned properly */ |
| 945 | __int64 in; |
| 946 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 947 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 948 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 949 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 950 | } |
| 951 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 952 | static void |
| 953 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 954 | { |
| 955 | /* XXX endianness */ |
| 956 | __int64 out; |
| 957 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 958 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 959 | memcpy(out_ptr, &out, sizeof(out)); |
| 960 | } |
| 961 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 962 | /* Below, we *know* that ugo+r is 0444 */ |
| 963 | #if _S_IREAD != 0400 |
| 964 | #error Unsupported C library |
| 965 | #endif |
| 966 | static int |
| 967 | attributes_to_mode(DWORD attr) |
| 968 | { |
| 969 | int m = 0; |
| 970 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 971 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 972 | else |
| 973 | m |= _S_IFREG; |
| 974 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 975 | m |= 0444; |
| 976 | else |
| 977 | m |= 0666; |
| 978 | return m; |
| 979 | } |
| 980 | |
| 981 | static int |
| 982 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 983 | { |
| 984 | memset(result, 0, sizeof(*result)); |
| 985 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 986 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 987 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 988 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 989 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 994 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 995 | static int checked = 0; |
| 996 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 997 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 998 | static void |
| 999 | check_gfax() |
| 1000 | { |
| 1001 | HINSTANCE hKernel32; |
| 1002 | if (checked) |
| 1003 | return; |
| 1004 | checked = 1; |
| 1005 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1006 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 1007 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 1008 | } |
| 1009 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1010 | static BOOL |
| 1011 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1012 | { |
| 1013 | HANDLE hFindFile; |
| 1014 | WIN32_FIND_DATAA FileData; |
| 1015 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1016 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1017 | return FALSE; |
| 1018 | FindClose(hFindFile); |
| 1019 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1020 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1021 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1022 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1023 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1024 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1025 | return TRUE; |
| 1026 | } |
| 1027 | |
| 1028 | static BOOL |
| 1029 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1030 | { |
| 1031 | HANDLE hFindFile; |
| 1032 | WIN32_FIND_DATAW FileData; |
| 1033 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1034 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1035 | return FALSE; |
| 1036 | FindClose(hFindFile); |
| 1037 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1038 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1039 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1040 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1041 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1042 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1043 | return TRUE; |
| 1044 | } |
| 1045 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1046 | static BOOL WINAPI |
| 1047 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 1048 | GET_FILEEX_INFO_LEVELS level, |
| 1049 | LPVOID pv) |
| 1050 | { |
| 1051 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1052 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1053 | /* First try to use the system's implementation, if that is |
| 1054 | available and either succeeds to gives an error other than |
| 1055 | that it isn't implemented. */ |
| 1056 | check_gfax(); |
| 1057 | if (gfaxa) { |
| 1058 | result = gfaxa(pszFile, level, pv); |
| 1059 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1060 | return result; |
| 1061 | } |
| 1062 | /* It's either not present, or not implemented. |
| 1063 | Emulate using FindFirstFile. */ |
| 1064 | if (level != GetFileExInfoStandard) { |
| 1065 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1066 | return FALSE; |
| 1067 | } |
| 1068 | /* Use GetFileAttributes to validate that the file name |
| 1069 | does not contain wildcards (which FindFirstFile would |
| 1070 | accept). */ |
| 1071 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 1072 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1073 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static BOOL WINAPI |
| 1077 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 1078 | GET_FILEEX_INFO_LEVELS level, |
| 1079 | LPVOID pv) |
| 1080 | { |
| 1081 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1082 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1083 | /* First try to use the system's implementation, if that is |
| 1084 | available and either succeeds to gives an error other than |
| 1085 | that it isn't implemented. */ |
| 1086 | check_gfax(); |
| 1087 | if (gfaxa) { |
| 1088 | result = gfaxw(pszFile, level, pv); |
| 1089 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1090 | return result; |
| 1091 | } |
| 1092 | /* It's either not present, or not implemented. |
| 1093 | Emulate using FindFirstFile. */ |
| 1094 | if (level != GetFileExInfoStandard) { |
| 1095 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1096 | return FALSE; |
| 1097 | } |
| 1098 | /* Use GetFileAttributes to validate that the file name |
| 1099 | does not contain wildcards (which FindFirstFile would |
| 1100 | accept). */ |
| 1101 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 1102 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1103 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1106 | static int |
| 1107 | win32_stat(const char* path, struct win32_stat *result) |
| 1108 | { |
| 1109 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1110 | int code; |
| 1111 | char *dot; |
| 1112 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1113 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1114 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1115 | /* Protocol violation: we explicitly clear errno, instead of |
| 1116 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1117 | errno = 0; |
| 1118 | return -1; |
| 1119 | } else { |
| 1120 | /* Could not get attributes on open file. Fall back to |
| 1121 | reading the directory. */ |
| 1122 | if (!attributes_from_dir(path, &info)) { |
| 1123 | /* Very strange. This should not fail now */ |
| 1124 | errno = 0; |
| 1125 | return -1; |
| 1126 | } |
| 1127 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1128 | } |
| 1129 | code = attribute_data_to_stat(&info, result); |
| 1130 | if (code != 0) |
| 1131 | return code; |
| 1132 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1133 | dot = strrchr(path, '.'); |
| 1134 | if (dot) { |
| 1135 | if (stricmp(dot, ".bat") == 0 || |
| 1136 | stricmp(dot, ".cmd") == 0 || |
| 1137 | stricmp(dot, ".exe") == 0 || |
| 1138 | stricmp(dot, ".com") == 0) |
| 1139 | result->st_mode |= 0111; |
| 1140 | } |
| 1141 | return code; |
| 1142 | } |
| 1143 | |
| 1144 | static int |
| 1145 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1146 | { |
| 1147 | int code; |
| 1148 | const wchar_t *dot; |
| 1149 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1150 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1151 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1152 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1153 | /* Protocol violation: we explicitly clear errno, instead of |
| 1154 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1155 | errno = 0; |
| 1156 | return -1; |
| 1157 | } else { |
| 1158 | /* Could not get attributes on open file. Fall back to |
| 1159 | reading the directory. */ |
| 1160 | if (!attributes_from_dir_w(path, &info)) { |
| 1161 | /* Very strange. This should not fail now */ |
| 1162 | errno = 0; |
| 1163 | return -1; |
| 1164 | } |
| 1165 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1166 | } |
| 1167 | code = attribute_data_to_stat(&info, result); |
| 1168 | if (code < 0) |
| 1169 | return code; |
| 1170 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1171 | dot = wcsrchr(path, '.'); |
| 1172 | if (dot) { |
| 1173 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1174 | _wcsicmp(dot, L".cmd") == 0 || |
| 1175 | _wcsicmp(dot, L".exe") == 0 || |
| 1176 | _wcsicmp(dot, L".com") == 0) |
| 1177 | result->st_mode |= 0111; |
| 1178 | } |
| 1179 | return code; |
| 1180 | } |
| 1181 | |
| 1182 | static int |
| 1183 | win32_fstat(int file_number, struct win32_stat *result) |
| 1184 | { |
| 1185 | BY_HANDLE_FILE_INFORMATION info; |
| 1186 | HANDLE h; |
| 1187 | int type; |
| 1188 | |
| 1189 | h = (HANDLE)_get_osfhandle(file_number); |
| 1190 | |
| 1191 | /* Protocol violation: we explicitly clear errno, instead of |
| 1192 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1193 | errno = 0; |
| 1194 | |
| 1195 | if (h == INVALID_HANDLE_VALUE) { |
| 1196 | /* This is really a C library error (invalid file handle). |
| 1197 | We set the Win32 error to the closes one matching. */ |
| 1198 | SetLastError(ERROR_INVALID_HANDLE); |
| 1199 | return -1; |
| 1200 | } |
| 1201 | memset(result, 0, sizeof(*result)); |
| 1202 | |
| 1203 | type = GetFileType(h); |
| 1204 | if (type == FILE_TYPE_UNKNOWN) { |
| 1205 | DWORD error = GetLastError(); |
| 1206 | if (error != 0) { |
| 1207 | return -1; |
| 1208 | } |
| 1209 | /* else: valid but unknown file */ |
| 1210 | } |
| 1211 | |
| 1212 | if (type != FILE_TYPE_DISK) { |
| 1213 | if (type == FILE_TYPE_CHAR) |
| 1214 | result->st_mode = _S_IFCHR; |
| 1215 | else if (type == FILE_TYPE_PIPE) |
| 1216 | result->st_mode = _S_IFIFO; |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | if (!GetFileInformationByHandle(h, &info)) { |
| 1221 | return -1; |
| 1222 | } |
| 1223 | |
| 1224 | /* similar to stat() */ |
| 1225 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1226 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1227 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1228 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1229 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1230 | /* specific to fstat() */ |
| 1231 | result->st_nlink = info.nNumberOfLinks; |
| 1232 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | #endif /* MS_WINDOWS */ |
| 1237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1238 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1239 | "stat_result: Result from stat or lstat.\n\n\ |
| 1240 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1241 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1242 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1243 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1244 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1245 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1246 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1247 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1248 | |
| 1249 | static PyStructSequence_Field stat_result_fields[] = { |
| 1250 | {"st_mode", "protection bits"}, |
| 1251 | {"st_ino", "inode"}, |
| 1252 | {"st_dev", "device"}, |
| 1253 | {"st_nlink", "number of hard links"}, |
| 1254 | {"st_uid", "user ID of owner"}, |
| 1255 | {"st_gid", "group ID of owner"}, |
| 1256 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1257 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1258 | {NULL, "integer time of last access"}, |
| 1259 | {NULL, "integer time of last modification"}, |
| 1260 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1261 | {"st_atime", "time of last access"}, |
| 1262 | {"st_mtime", "time of last modification"}, |
| 1263 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1264 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1265 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1266 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1267 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1268 | {"st_blocks", "number of blocks allocated"}, |
| 1269 | #endif |
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 | {"st_rdev", "device type (if inode device)"}, |
| 1272 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1273 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1274 | {"st_flags", "user defined flags for file"}, |
| 1275 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1276 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1277 | {"st_gen", "generation number"}, |
| 1278 | #endif |
| 1279 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1280 | {"st_birthtime", "time of creation"}, |
| 1281 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1282 | {0} |
| 1283 | }; |
| 1284 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1285 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1286 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1287 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1288 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1289 | #endif |
| 1290 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1291 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1292 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1293 | #else |
| 1294 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1295 | #endif |
| 1296 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1297 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1298 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1299 | #else |
| 1300 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1301 | #endif |
| 1302 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1303 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1304 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1305 | #else |
| 1306 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1307 | #endif |
| 1308 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1309 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1310 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1311 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1312 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1313 | #endif |
| 1314 | |
| 1315 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1316 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1317 | #else |
| 1318 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1319 | #endif |
| 1320 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1321 | static PyStructSequence_Desc stat_result_desc = { |
| 1322 | "stat_result", /* name */ |
| 1323 | stat_result__doc__, /* doc */ |
| 1324 | stat_result_fields, |
| 1325 | 10 |
| 1326 | }; |
| 1327 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1328 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1329 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1330 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1331 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1332 | 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] | 1333 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1334 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1335 | |
| 1336 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1337 | {"f_bsize", }, |
| 1338 | {"f_frsize", }, |
| 1339 | {"f_blocks", }, |
| 1340 | {"f_bfree", }, |
| 1341 | {"f_bavail", }, |
| 1342 | {"f_files", }, |
| 1343 | {"f_ffree", }, |
| 1344 | {"f_favail", }, |
| 1345 | {"f_flag", }, |
| 1346 | {"f_namemax",}, |
| 1347 | {0} |
| 1348 | }; |
| 1349 | |
| 1350 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1351 | "statvfs_result", /* name */ |
| 1352 | statvfs_result__doc__, /* doc */ |
| 1353 | statvfs_result_fields, |
| 1354 | 10 |
| 1355 | }; |
| 1356 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1357 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1358 | static PyTypeObject StatResultType; |
| 1359 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1360 | static newfunc structseq_new; |
| 1361 | |
| 1362 | static PyObject * |
| 1363 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1364 | { |
| 1365 | PyStructSequence *result; |
| 1366 | int i; |
| 1367 | |
| 1368 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1369 | if (!result) |
| 1370 | return NULL; |
| 1371 | /* If we have been initialized from a tuple, |
| 1372 | st_?time might be set to None. Initialize it |
| 1373 | from the int slots. */ |
| 1374 | for (i = 7; i <= 9; i++) { |
| 1375 | if (result->ob_item[i+3] == Py_None) { |
| 1376 | Py_DECREF(Py_None); |
| 1377 | Py_INCREF(result->ob_item[i]); |
| 1378 | result->ob_item[i+3] = result->ob_item[i]; |
| 1379 | } |
| 1380 | } |
| 1381 | return (PyObject*)result; |
| 1382 | } |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1387 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1388 | |
| 1389 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1390 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1391 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1392 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1393 | future calls return ints. \n\ |
| 1394 | If newval is omitted, return the current setting.\n"); |
| 1395 | |
| 1396 | static PyObject* |
| 1397 | stat_float_times(PyObject* self, PyObject *args) |
| 1398 | { |
| 1399 | int newval = -1; |
| 1400 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1401 | return NULL; |
| 1402 | if (newval == -1) |
| 1403 | /* Return old value */ |
| 1404 | return PyBool_FromLong(_stat_float_times); |
| 1405 | _stat_float_times = newval; |
| 1406 | Py_INCREF(Py_None); |
| 1407 | return Py_None; |
| 1408 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1409 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1410 | static void |
| 1411 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1412 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1413 | PyObject *fval,*ival; |
| 1414 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1415 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1416 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1417 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1418 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1419 | if (!ival) |
| 1420 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1421 | if (_stat_float_times) { |
| 1422 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1423 | } else { |
| 1424 | fval = ival; |
| 1425 | Py_INCREF(fval); |
| 1426 | } |
| 1427 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1428 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1431 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1432 | (used by posix_stat() and posix_fstat()) */ |
| 1433 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1434 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1435 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1436 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1437 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1438 | if (v == NULL) |
| 1439 | return NULL; |
| 1440 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1441 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1442 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1443 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1444 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1445 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1446 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1447 | #endif |
| 1448 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1449 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1450 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1451 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1452 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1453 | #endif |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1454 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1455 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1456 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1457 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1458 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1459 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1460 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1461 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1462 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1463 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1464 | #if defined(HAVE_STAT_TV_NSEC) |
| 1465 | ansec = st->st_atim.tv_nsec; |
| 1466 | mnsec = st->st_mtim.tv_nsec; |
| 1467 | cnsec = st->st_ctim.tv_nsec; |
| 1468 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1469 | ansec = st->st_atimespec.tv_nsec; |
| 1470 | mnsec = st->st_mtimespec.tv_nsec; |
| 1471 | cnsec = st->st_ctimespec.tv_nsec; |
| 1472 | #elif defined(HAVE_STAT_NSEC) |
| 1473 | ansec = st->st_atime_nsec; |
| 1474 | mnsec = st->st_mtime_nsec; |
| 1475 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1476 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1477 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1478 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1479 | fill_time(v, 7, st->st_atime, ansec); |
| 1480 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1481 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1482 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1483 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1484 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1485 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1486 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1487 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1488 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1489 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1490 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1491 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1492 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1493 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1494 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1495 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1496 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1497 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1498 | #endif |
| 1499 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1500 | { |
| 1501 | PyObject *val; |
| 1502 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1503 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1504 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1505 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1506 | #else |
| 1507 | bnsec = 0; |
| 1508 | #endif |
| 1509 | if (_stat_float_times) { |
| 1510 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1511 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1512 | val = PyLong_FromLong((long)bsec); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1513 | } |
| 1514 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1515 | val); |
| 1516 | } |
| 1517 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1518 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1519 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1520 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1521 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1522 | |
| 1523 | if (PyErr_Occurred()) { |
| 1524 | Py_DECREF(v); |
| 1525 | return NULL; |
| 1526 | } |
| 1527 | |
| 1528 | return v; |
| 1529 | } |
| 1530 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1531 | #ifdef MS_WINDOWS |
| 1532 | |
| 1533 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1534 | where / can be used in place of \ and the trailing slash is optional. |
| 1535 | Both SERVER and SHARE must have at least one character. |
| 1536 | */ |
| 1537 | |
| 1538 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1539 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1540 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1541 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1542 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1543 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1544 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1545 | IsUNCRootA(char *path, int pathlen) |
| 1546 | { |
| 1547 | #define ISSLASH ISSLASHA |
| 1548 | |
| 1549 | int i, share; |
| 1550 | |
| 1551 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1552 | /* minimum UNCRoot is \\x\y */ |
| 1553 | return FALSE; |
| 1554 | for (i = 2; i < pathlen ; i++) |
| 1555 | if (ISSLASH(path[i])) break; |
| 1556 | if (i == 2 || i == pathlen) |
| 1557 | /* do not allow \\\SHARE or \\SERVER */ |
| 1558 | return FALSE; |
| 1559 | share = i+1; |
| 1560 | for (i = share; i < pathlen; i++) |
| 1561 | if (ISSLASH(path[i])) break; |
| 1562 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1563 | |
| 1564 | #undef ISSLASH |
| 1565 | } |
| 1566 | |
| 1567 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1568 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1569 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1570 | { |
| 1571 | #define ISSLASH ISSLASHW |
| 1572 | |
| 1573 | int i, share; |
| 1574 | |
| 1575 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1576 | /* minimum UNCRoot is \\x\y */ |
| 1577 | return FALSE; |
| 1578 | for (i = 2; i < pathlen ; i++) |
| 1579 | if (ISSLASH(path[i])) break; |
| 1580 | if (i == 2 || i == pathlen) |
| 1581 | /* do not allow \\\SHARE or \\SERVER */ |
| 1582 | return FALSE; |
| 1583 | share = i+1; |
| 1584 | for (i = share; i < pathlen; i++) |
| 1585 | if (ISSLASH(path[i])) break; |
| 1586 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1587 | |
| 1588 | #undef ISSLASH |
| 1589 | } |
| 1590 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1591 | #endif /* MS_WINDOWS */ |
| 1592 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1593 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1594 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1595 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1596 | #ifdef __VMS |
| 1597 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1598 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1599 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1600 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1601 | char *wformat, |
| 1602 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1603 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1604 | STRUCT_STAT st; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1605 | PyObject *opath; |
| 1606 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1607 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1608 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1609 | |
| 1610 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1611 | /* If on wide-character-capable OS see if argument |
| 1612 | is Unicode and if so use wide API. */ |
| 1613 | if (unicode_file_names()) { |
| 1614 | PyUnicodeObject *po; |
| 1615 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1616 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1617 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1618 | Py_BEGIN_ALLOW_THREADS |
| 1619 | /* PyUnicode_AS_UNICODE result OK without |
| 1620 | thread lock as it is a simple dereference. */ |
| 1621 | res = wstatfunc(wpath, &st); |
| 1622 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1623 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1624 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1625 | return win32_error_unicode("stat", wpath); |
| 1626 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1627 | } |
| 1628 | /* Drop the argument parsing error as narrow strings |
| 1629 | are also valid. */ |
| 1630 | PyErr_Clear(); |
| 1631 | } |
| 1632 | #endif |
| 1633 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1634 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1635 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1636 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1637 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1638 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1639 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1640 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1641 | |
| 1642 | if (res != 0) { |
| 1643 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1644 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1645 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1646 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1647 | #endif |
| 1648 | } |
| 1649 | else |
| 1650 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1651 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1652 | release_bytes(opath); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1653 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1656 | /* POSIX methods */ |
| 1657 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1658 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1659 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1660 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1661 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1662 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1663 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1664 | 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] | 1665 | |
| 1666 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1667 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1668 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1669 | PyObject *opath; |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1670 | char *path; |
| 1671 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1672 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1673 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1674 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1675 | if (unicode_file_names()) { |
| 1676 | PyUnicodeObject *po; |
| 1677 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1678 | Py_BEGIN_ALLOW_THREADS |
| 1679 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1680 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1681 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1682 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1683 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1684 | } |
| 1685 | /* Drop the argument parsing error as narrow strings |
| 1686 | are also valid. */ |
| 1687 | PyErr_Clear(); |
| 1688 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1689 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1690 | PyUnicode_FSConverter, &opath, &mode)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1691 | return 0; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1692 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1693 | Py_BEGIN_ALLOW_THREADS |
| 1694 | attr = GetFileAttributesA(path); |
| 1695 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1696 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1697 | finish: |
| 1698 | if (attr == 0xFFFFFFFF) |
| 1699 | /* File does not exist, or cannot read attributes */ |
| 1700 | return PyBool_FromLong(0); |
| 1701 | /* Access is possible if either write access wasn't requested, or |
Guido van Rossum | b00324f | 2007-12-04 01:13:14 +0000 | [diff] [blame] | 1702 | the file isn't read-only, or if it's a directory, as there are |
| 1703 | no read-only directories on Windows. */ |
| 1704 | return PyBool_FromLong(!(mode & 2) |
| 1705 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1706 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1707 | #else |
| 1708 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1709 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1710 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1711 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1712 | path = bytes2str(opath, 1); |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1713 | Py_BEGIN_ALLOW_THREADS |
| 1714 | res = access(path, mode); |
| 1715 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1716 | release_bytes(opath); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1717 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1718 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1721 | #ifndef F_OK |
| 1722 | #define F_OK 0 |
| 1723 | #endif |
| 1724 | #ifndef R_OK |
| 1725 | #define R_OK 4 |
| 1726 | #endif |
| 1727 | #ifndef W_OK |
| 1728 | #define W_OK 2 |
| 1729 | #endif |
| 1730 | #ifndef X_OK |
| 1731 | #define X_OK 1 |
| 1732 | #endif |
| 1733 | |
| 1734 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1735 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1736 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1737 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1738 | |
| 1739 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1740 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1741 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1742 | int id; |
| 1743 | char *ret; |
| 1744 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1745 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1746 | return NULL; |
| 1747 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1748 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1749 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1750 | if (id == 0) { |
| 1751 | ret = ttyname(); |
| 1752 | } |
| 1753 | else { |
| 1754 | ret = NULL; |
| 1755 | } |
| 1756 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1757 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1758 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1759 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1760 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1761 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1762 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1763 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1764 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1765 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1766 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1767 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1768 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1769 | |
| 1770 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1771 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1772 | { |
| 1773 | char *ret; |
| 1774 | char buffer[L_ctermid]; |
| 1775 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1776 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1777 | ret = ctermid_r(buffer); |
| 1778 | #else |
| 1779 | ret = ctermid(buffer); |
| 1780 | #endif |
| 1781 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1782 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1783 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1784 | } |
| 1785 | #endif |
| 1786 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1787 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1788 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1789 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1790 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1791 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1792 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1793 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1794 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 1795 | 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] | 1796 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1797 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1798 | #elif defined(__VMS) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1799 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1800 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1801 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1802 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1805 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1806 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1807 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1808 | 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] | 1809 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1810 | |
| 1811 | static PyObject * |
| 1812 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1813 | { |
| 1814 | return posix_fildes(fdobj, fchdir); |
| 1815 | } |
| 1816 | #endif /* HAVE_FCHDIR */ |
| 1817 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1819 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1820 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1821 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1822 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1823 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1824 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1825 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1826 | PyObject *opath = NULL; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1827 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1828 | int i; |
| 1829 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1830 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1831 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1832 | if (unicode_file_names()) { |
| 1833 | PyUnicodeObject *po; |
| 1834 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1835 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1836 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1837 | if (attr != 0xFFFFFFFF) { |
| 1838 | if (i & _S_IWRITE) |
| 1839 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1840 | else |
| 1841 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1842 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1843 | } |
| 1844 | else |
| 1845 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1846 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1847 | if (!res) |
| 1848 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1849 | PyUnicode_AS_UNICODE(po)); |
| 1850 | Py_INCREF(Py_None); |
| 1851 | return Py_None; |
| 1852 | } |
| 1853 | /* Drop the argument parsing error as narrow strings |
| 1854 | are also valid. */ |
| 1855 | PyErr_Clear(); |
| 1856 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1857 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1858 | &opath, &i)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1859 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1860 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1861 | Py_BEGIN_ALLOW_THREADS |
| 1862 | attr = GetFileAttributesA(path); |
| 1863 | if (attr != 0xFFFFFFFF) { |
| 1864 | if (i & _S_IWRITE) |
| 1865 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1866 | else |
| 1867 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1868 | res = SetFileAttributesA(path, attr); |
| 1869 | } |
| 1870 | else |
| 1871 | res = 0; |
| 1872 | Py_END_ALLOW_THREADS |
| 1873 | if (!res) { |
| 1874 | win32_error("chmod", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1875 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1876 | return NULL; |
| 1877 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1878 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1879 | Py_INCREF(Py_None); |
| 1880 | return Py_None; |
| 1881 | #else /* Py_WIN_WIDE_FILENAMES */ |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1882 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1883 | &opath, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1884 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1885 | path = bytes2str(opath, 1); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1886 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1887 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1888 | Py_END_ALLOW_THREADS |
| 1889 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1890 | return posix_error_with_allocated_filename(opath); |
| 1891 | release_bytes(opath); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1892 | Py_INCREF(Py_None); |
| 1893 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1894 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1897 | #ifdef HAVE_FCHMOD |
| 1898 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1899 | "fchmod(fd, mode)\n\n\ |
| 1900 | Change the access permissions of the file given by file\n\ |
| 1901 | descriptor fd."); |
| 1902 | |
| 1903 | static PyObject * |
| 1904 | posix_fchmod(PyObject *self, PyObject *args) |
| 1905 | { |
| 1906 | int fd, mode, res; |
| 1907 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1908 | return NULL; |
| 1909 | Py_BEGIN_ALLOW_THREADS |
| 1910 | res = fchmod(fd, mode); |
| 1911 | Py_END_ALLOW_THREADS |
| 1912 | if (res < 0) |
| 1913 | return posix_error(); |
| 1914 | Py_RETURN_NONE; |
| 1915 | } |
| 1916 | #endif /* HAVE_FCHMOD */ |
| 1917 | |
| 1918 | #ifdef HAVE_LCHMOD |
| 1919 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1920 | "lchmod(path, mode)\n\n\ |
| 1921 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1922 | affects the link itself rather than the target."); |
| 1923 | |
| 1924 | static PyObject * |
| 1925 | posix_lchmod(PyObject *self, PyObject *args) |
| 1926 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1927 | PyObject *opath; |
| 1928 | char *path; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1929 | int i; |
| 1930 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1931 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 1932 | &opath, &i)) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1933 | return NULL; |
Eric Smith | 86a05ec | 2009-05-05 13:07:30 +0000 | [diff] [blame] | 1934 | path = bytes2str(opath, 1); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1935 | Py_BEGIN_ALLOW_THREADS |
| 1936 | res = lchmod(path, i); |
| 1937 | Py_END_ALLOW_THREADS |
| 1938 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1939 | return posix_error_with_allocated_filename(opath); |
| 1940 | release_bytes(opath); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1941 | Py_RETURN_NONE; |
| 1942 | } |
| 1943 | #endif /* HAVE_LCHMOD */ |
| 1944 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1945 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1946 | #ifdef HAVE_CHFLAGS |
| 1947 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1948 | "chflags(path, flags)\n\n\ |
| 1949 | Set file flags."); |
| 1950 | |
| 1951 | static PyObject * |
| 1952 | posix_chflags(PyObject *self, PyObject *args) |
| 1953 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1954 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1955 | char *path; |
| 1956 | unsigned long flags; |
| 1957 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1958 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 1959 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1960 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1961 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1962 | Py_BEGIN_ALLOW_THREADS |
| 1963 | res = chflags(path, flags); |
| 1964 | Py_END_ALLOW_THREADS |
| 1965 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1966 | return posix_error_with_allocated_filename(opath); |
| 1967 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1968 | Py_INCREF(Py_None); |
| 1969 | return Py_None; |
| 1970 | } |
| 1971 | #endif /* HAVE_CHFLAGS */ |
| 1972 | |
| 1973 | #ifdef HAVE_LCHFLAGS |
| 1974 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1975 | "lchflags(path, flags)\n\n\ |
| 1976 | Set file flags.\n\ |
| 1977 | This function will not follow symbolic links."); |
| 1978 | |
| 1979 | static PyObject * |
| 1980 | posix_lchflags(PyObject *self, PyObject *args) |
| 1981 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1982 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1983 | char *path; |
| 1984 | unsigned long flags; |
| 1985 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1986 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Martin v. Löwis | 4adbc34 | 2009-05-05 17:17:55 +0000 | [diff] [blame] | 1987 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1988 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1989 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1990 | Py_BEGIN_ALLOW_THREADS |
| 1991 | res = lchflags(path, flags); |
| 1992 | Py_END_ALLOW_THREADS |
| 1993 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1994 | return posix_error_with_allocated_filename(opath); |
| 1995 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1996 | Py_INCREF(Py_None); |
| 1997 | return Py_None; |
| 1998 | } |
| 1999 | #endif /* HAVE_LCHFLAGS */ |
| 2000 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2001 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2002 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2003 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2004 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2005 | |
| 2006 | static PyObject * |
| 2007 | posix_chroot(PyObject *self, PyObject *args) |
| 2008 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2009 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2010 | } |
| 2011 | #endif |
| 2012 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2013 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2014 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2015 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2016 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2017 | |
| 2018 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2019 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2020 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2021 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2022 | } |
| 2023 | #endif /* HAVE_FSYNC */ |
| 2024 | |
| 2025 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2026 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2027 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2028 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2029 | #endif |
| 2030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2031 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2032 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2033 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2034 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2035 | |
| 2036 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2037 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2038 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2039 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2040 | } |
| 2041 | #endif /* HAVE_FDATASYNC */ |
| 2042 | |
| 2043 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2044 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2045 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2046 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2047 | 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] | 2048 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2049 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2050 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2051 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2052 | PyObject *opath; |
| 2053 | char *path; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2054 | long uid, gid; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2055 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2056 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2057 | PyUnicode_FSConverter, &opath, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2058 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2059 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2060 | path = bytes2str(opath, 1); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2061 | Py_BEGIN_ALLOW_THREADS |
| 2062 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2063 | Py_END_ALLOW_THREADS |
| 2064 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2065 | return posix_error_with_allocated_filename(opath); |
| 2066 | release_bytes(opath); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2067 | Py_INCREF(Py_None); |
| 2068 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2069 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2070 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2071 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2072 | #ifdef HAVE_FCHOWN |
| 2073 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2074 | "fchown(fd, uid, gid)\n\n\ |
| 2075 | Change the owner and group id of the file given by file descriptor\n\ |
| 2076 | fd to the numeric uid and gid."); |
| 2077 | |
| 2078 | static PyObject * |
| 2079 | posix_fchown(PyObject *self, PyObject *args) |
| 2080 | { |
| 2081 | int fd, uid, gid; |
| 2082 | int res; |
| 2083 | if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid)) |
| 2084 | return NULL; |
| 2085 | Py_BEGIN_ALLOW_THREADS |
| 2086 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2087 | Py_END_ALLOW_THREADS |
| 2088 | if (res < 0) |
| 2089 | return posix_error(); |
| 2090 | Py_RETURN_NONE; |
| 2091 | } |
| 2092 | #endif /* HAVE_FCHOWN */ |
| 2093 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2094 | #ifdef HAVE_LCHOWN |
| 2095 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2096 | "lchown(path, uid, gid)\n\n\ |
| 2097 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2098 | This function will not follow symbolic links."); |
| 2099 | |
| 2100 | static PyObject * |
| 2101 | posix_lchown(PyObject *self, PyObject *args) |
| 2102 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2103 | PyObject *opath; |
| 2104 | char *path; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2105 | int uid, gid; |
| 2106 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2107 | if (!PyArg_ParseTuple(args, "O&ii:lchown", |
| 2108 | PyUnicode_FSConverter, &opath, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2109 | &uid, &gid)) |
| 2110 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2111 | path = bytes2str(opath, 1); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2112 | Py_BEGIN_ALLOW_THREADS |
| 2113 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2114 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2115 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2116 | return posix_error_with_allocated_filename(opath); |
| 2117 | release_bytes(opath); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2118 | Py_INCREF(Py_None); |
| 2119 | return Py_None; |
| 2120 | } |
| 2121 | #endif /* HAVE_LCHOWN */ |
| 2122 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2123 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2124 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2125 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2126 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2127 | { |
| 2128 | char buf[1026]; |
| 2129 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2130 | |
| 2131 | #ifdef Py_WIN_WIDE_FILENAMES |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2132 | if (!use_bytes && unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2133 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2134 | wchar_t *wbuf2 = wbuf; |
| 2135 | PyObject *resobj; |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2136 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2137 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2138 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2139 | /* If the buffer is large enough, len does not include the |
| 2140 | terminating \0. If the buffer is too small, len includes |
| 2141 | the space needed for the terminator. */ |
| 2142 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2143 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2144 | if (wbuf2) |
| 2145 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2146 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2147 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2148 | if (!wbuf2) { |
| 2149 | PyErr_NoMemory(); |
| 2150 | return NULL; |
| 2151 | } |
| 2152 | if (!len) { |
| 2153 | if (wbuf2 != wbuf) free(wbuf2); |
| 2154 | return win32_error("getcwdu", NULL); |
| 2155 | } |
| 2156 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2157 | if (wbuf2 != wbuf) free(wbuf2); |
| 2158 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2159 | } |
| 2160 | #endif |
| 2161 | |
| 2162 | Py_BEGIN_ALLOW_THREADS |
| 2163 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2164 | res = _getcwd2(buf, sizeof buf); |
| 2165 | #else |
| 2166 | res = getcwd(buf, sizeof buf); |
| 2167 | #endif |
| 2168 | Py_END_ALLOW_THREADS |
| 2169 | if (res == NULL) |
| 2170 | return posix_error(); |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2171 | if (use_bytes) |
| 2172 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2173 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape"); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2174 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2175 | |
| 2176 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2177 | "getcwd() -> path\n\n\ |
| 2178 | Return a unicode string representing the current working directory."); |
| 2179 | |
| 2180 | static PyObject * |
| 2181 | posix_getcwd_unicode(PyObject *self) |
| 2182 | { |
| 2183 | return posix_getcwd(0); |
| 2184 | } |
| 2185 | |
| 2186 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2187 | "getcwdb() -> path\n\n\ |
| 2188 | Return a bytes string representing the current working directory."); |
| 2189 | |
| 2190 | static PyObject * |
| 2191 | posix_getcwd_bytes(PyObject *self) |
| 2192 | { |
| 2193 | return posix_getcwd(1); |
| 2194 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2195 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2196 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2197 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2198 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2199 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2200 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2201 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2202 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2203 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2204 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2205 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2206 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2207 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2208 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2211 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2212 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2213 | Return a list containing the names of the entries in the directory.\n\ |
| 2214 | \n\ |
| 2215 | path: path of directory to list\n\ |
| 2216 | \n\ |
| 2217 | 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] | 2218 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2219 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2220 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2221 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2222 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2223 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2224 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2225 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2226 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2227 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2228 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2229 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2230 | WIN32_FIND_DATA FileData; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2231 | PyObject *opath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2232 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2233 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2234 | 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] | 2235 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2236 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2237 | /* If on wide-character-capable OS see if argument |
| 2238 | is Unicode and if so use wide API. */ |
| 2239 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2240 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2241 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2242 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2243 | Py_UNICODE *wnamebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2244 | /* Overallocate for \\*.*\0 */ |
| 2245 | len = PyUnicode_GET_SIZE(po); |
| 2246 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2247 | if (!wnamebuf) { |
| 2248 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2249 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2250 | } |
| 2251 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2252 | if (len > 0) { |
| 2253 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2254 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2255 | wnamebuf[len++] = L'\\'; |
| 2256 | wcscpy(wnamebuf + len, L"*.*"); |
| 2257 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2258 | if ((d = PyList_New(0)) == NULL) { |
| 2259 | free(wnamebuf); |
| 2260 | return NULL; |
| 2261 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2262 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2263 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2264 | int error = GetLastError(); |
| 2265 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2266 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2267 | return d; |
| 2268 | } |
| 2269 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2270 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2271 | free(wnamebuf); |
| 2272 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2273 | } |
| 2274 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2275 | /* Skip over . and .. */ |
| 2276 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2277 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2278 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2279 | if (v == NULL) { |
| 2280 | Py_DECREF(d); |
| 2281 | d = NULL; |
| 2282 | break; |
| 2283 | } |
| 2284 | if (PyList_Append(d, v) != 0) { |
| 2285 | Py_DECREF(v); |
| 2286 | Py_DECREF(d); |
| 2287 | d = NULL; |
| 2288 | break; |
| 2289 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2290 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2291 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2292 | Py_BEGIN_ALLOW_THREADS |
| 2293 | result = FindNextFileW(hFindFile, &wFileData); |
| 2294 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2295 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2296 | it got to the end of the directory. */ |
| 2297 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2298 | Py_DECREF(d); |
| 2299 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2300 | FindClose(hFindFile); |
| 2301 | free(wnamebuf); |
| 2302 | return NULL; |
| 2303 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2304 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2305 | |
| 2306 | if (FindClose(hFindFile) == FALSE) { |
| 2307 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2308 | win32_error_unicode("FindClose", wnamebuf); |
| 2309 | free(wnamebuf); |
| 2310 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2311 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2312 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2313 | return d; |
| 2314 | } |
| 2315 | /* Drop the argument parsing error as narrow strings |
| 2316 | are also valid. */ |
| 2317 | PyErr_Clear(); |
| 2318 | } |
| 2319 | #endif |
| 2320 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2321 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2322 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2323 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2324 | if (PyObject_Size(opath)+1 > MAX_PATH) { |
| 2325 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2326 | Py_DECREF(opath); |
| 2327 | return NULL; |
| 2328 | } |
| 2329 | strcpy(namebuf, bytes2str(opath, 0)); |
| 2330 | len = PyObject_Size(opath); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2331 | if (len > 0) { |
| 2332 | char ch = namebuf[len-1]; |
| 2333 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2334 | namebuf[len++] = '/'; |
Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2335 | strcpy(namebuf + len, "*.*"); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2336 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2337 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2338 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2339 | return NULL; |
| 2340 | |
| 2341 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2342 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2343 | int error = GetLastError(); |
| 2344 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2345 | return d; |
| 2346 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2347 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2348 | } |
| 2349 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2350 | /* Skip over . and .. */ |
| 2351 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2352 | strcmp(FileData.cFileName, "..") != 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2353 | v = PyBytes_FromString(FileData.cFileName); |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2354 | if (v == NULL) { |
| 2355 | Py_DECREF(d); |
| 2356 | d = NULL; |
| 2357 | break; |
| 2358 | } |
| 2359 | if (PyList_Append(d, v) != 0) { |
| 2360 | Py_DECREF(v); |
| 2361 | Py_DECREF(d); |
| 2362 | d = NULL; |
| 2363 | break; |
| 2364 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2365 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2366 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2367 | Py_BEGIN_ALLOW_THREADS |
| 2368 | result = FindNextFile(hFindFile, &FileData); |
| 2369 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2370 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2371 | it got to the end of the directory. */ |
| 2372 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2373 | Py_DECREF(d); |
| 2374 | win32_error("FindNextFile", namebuf); |
| 2375 | FindClose(hFindFile); |
| 2376 | return NULL; |
| 2377 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2378 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2379 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2380 | if (FindClose(hFindFile) == FALSE) { |
| 2381 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2382 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2383 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2384 | |
| 2385 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2386 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2387 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2388 | |
| 2389 | #ifndef MAX_PATH |
| 2390 | #define MAX_PATH CCHMAXPATH |
| 2391 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2392 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2393 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2394 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2395 | PyObject *d, *v; |
| 2396 | char namebuf[MAX_PATH+5]; |
| 2397 | HDIR hdir = 1; |
| 2398 | ULONG srchcnt = 1; |
| 2399 | FILEFINDBUF3 ep; |
| 2400 | APIRET rc; |
| 2401 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2402 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2403 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2404 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2405 | name = bytes2str(oname); |
| 2406 | len = PyObject_Size(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2407 | if (len >= MAX_PATH) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2408 | release_bytes(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2409 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2410 | return NULL; |
| 2411 | } |
| 2412 | strcpy(namebuf, name); |
| 2413 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2414 | if (*pt == ALTSEP) |
| 2415 | *pt = SEP; |
| 2416 | if (namebuf[len-1] != SEP) |
| 2417 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2418 | strcpy(namebuf + len, "*.*"); |
| 2419 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2420 | if ((d = PyList_New(0)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2421 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2422 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2423 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2424 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2425 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2426 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2427 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2428 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2429 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2430 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2431 | |
| 2432 | if (rc != NO_ERROR) { |
| 2433 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2434 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2437 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2438 | do { |
| 2439 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2440 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2441 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2442 | |
| 2443 | strcpy(namebuf, ep.achName); |
| 2444 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2445 | /* Leave Case of Name Alone -- In Native Form */ |
| 2446 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2447 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2448 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2449 | if (v == NULL) { |
| 2450 | Py_DECREF(d); |
| 2451 | d = NULL; |
| 2452 | break; |
| 2453 | } |
| 2454 | if (PyList_Append(d, v) != 0) { |
| 2455 | Py_DECREF(v); |
| 2456 | Py_DECREF(d); |
| 2457 | d = NULL; |
| 2458 | break; |
| 2459 | } |
| 2460 | Py_DECREF(v); |
| 2461 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2462 | } |
| 2463 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2464 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2465 | return d; |
| 2466 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2467 | PyObject *oname; |
| 2468 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2469 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2470 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2471 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2472 | int arg_is_unicode = 1; |
| 2473 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2474 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2475 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2476 | arg_is_unicode = 0; |
| 2477 | PyErr_Clear(); |
| 2478 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2479 | if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2480 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2481 | name = bytes2str(oname, 1); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2482 | if ((dirp = opendir(name)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2483 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2484 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2485 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2486 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2487 | release_bytes(oname); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2488 | return NULL; |
| 2489 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2490 | for (;;) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2491 | errno = 0; |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2492 | Py_BEGIN_ALLOW_THREADS |
| 2493 | ep = readdir(dirp); |
| 2494 | Py_END_ALLOW_THREADS |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2495 | if (ep == NULL) { |
| 2496 | if (errno == 0) { |
| 2497 | break; |
| 2498 | } else { |
| 2499 | closedir(dirp); |
| 2500 | Py_DECREF(d); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2501 | return posix_error_with_allocated_filename(oname); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2502 | } |
| 2503 | } |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2504 | if (ep->d_name[0] == '.' && |
| 2505 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2506 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2507 | continue; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2508 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2509 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2510 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2511 | d = NULL; |
| 2512 | break; |
| 2513 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2514 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2515 | PyObject *w; |
| 2516 | |
| 2517 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2518 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2519 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2520 | Py_DECREF(v); |
| 2521 | if (w != NULL) |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2522 | v = w; |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2523 | else { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2524 | /* Encoding failed to decode ASCII bytes. |
| 2525 | Raise exception. */ |
| 2526 | Py_DECREF(d); |
| 2527 | d = NULL; |
| 2528 | break; |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2529 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2530 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2531 | if (PyList_Append(d, v) != 0) { |
| 2532 | Py_DECREF(v); |
| 2533 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2534 | d = NULL; |
| 2535 | break; |
| 2536 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2537 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2538 | } |
| 2539 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2540 | release_bytes(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2541 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2542 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2543 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2544 | #endif /* which OS */ |
| 2545 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2546 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2547 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2548 | /* A helper function for abspath on win32 */ |
| 2549 | static PyObject * |
| 2550 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2551 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2552 | PyObject *opath; |
| 2553 | char *path; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2554 | char outbuf[MAX_PATH*2]; |
| 2555 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2556 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2557 | if (unicode_file_names()) { |
| 2558 | PyUnicodeObject *po; |
| 2559 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 2560 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2561 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2562 | Py_UNICODE *wtemp; |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 2563 | DWORD result; |
| 2564 | PyObject *v; |
| 2565 | result = GetFullPathNameW(wpath, |
| 2566 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2567 | woutbuf, &wtemp); |
| 2568 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2569 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2570 | if (!woutbufp) |
| 2571 | return PyErr_NoMemory(); |
| 2572 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2573 | } |
| 2574 | if (result) |
| 2575 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2576 | else |
| 2577 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2578 | if (woutbufp != woutbuf) |
| 2579 | free(woutbufp); |
| 2580 | return v; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2581 | } |
| 2582 | /* Drop the argument parsing error as narrow strings |
| 2583 | are also valid. */ |
| 2584 | PyErr_Clear(); |
| 2585 | } |
| 2586 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2587 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2588 | PyUnicode_FSConverter, &opath)) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2589 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2590 | path = bytes2str(opath, 1); |
| 2591 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2592 | outbuf, &temp)) { |
| 2593 | win32_error("GetFullPathName", path); |
| 2594 | release_bytes(opath); |
| 2595 | return NULL; |
| 2596 | } |
| 2597 | release_bytes(opath); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2598 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2599 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2600 | Py_FileSystemDefaultEncoding, NULL); |
| 2601 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2602 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2603 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2604 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2605 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2606 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2607 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2608 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2609 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2610 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2611 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2612 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2613 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2614 | PyObject *opath; |
| 2615 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2616 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2617 | |
| 2618 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2619 | if (unicode_file_names()) { |
| 2620 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2621 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2622 | Py_BEGIN_ALLOW_THREADS |
| 2623 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2624 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2625 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2626 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2627 | if (!res) |
| 2628 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2629 | Py_INCREF(Py_None); |
| 2630 | return Py_None; |
| 2631 | } |
| 2632 | /* Drop the argument parsing error as narrow strings |
| 2633 | are also valid. */ |
| 2634 | PyErr_Clear(); |
| 2635 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2636 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2637 | PyUnicode_FSConverter, &opath, &mode)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2638 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2639 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2640 | Py_BEGIN_ALLOW_THREADS |
| 2641 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2642 | it is a simple dereference. */ |
| 2643 | res = CreateDirectoryA(path, NULL); |
| 2644 | Py_END_ALLOW_THREADS |
| 2645 | if (!res) { |
| 2646 | win32_error("mkdir", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2647 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2648 | return NULL; |
| 2649 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2650 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2651 | Py_INCREF(Py_None); |
| 2652 | return Py_None; |
| 2653 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2654 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2655 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2656 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2657 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2658 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2659 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2660 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2661 | res = mkdir(path); |
| 2662 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2663 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2664 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2665 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2666 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2667 | return posix_error_with_allocated_filename(opath); |
| 2668 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2669 | Py_INCREF(Py_None); |
| 2670 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2671 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2674 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2675 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2676 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2677 | #include <sys/resource.h> |
| 2678 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2679 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2680 | |
| 2681 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2682 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2683 | "nice(inc) -> new_priority\n\n\ |
| 2684 | 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] | 2685 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2686 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2687 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2688 | { |
| 2689 | int increment, value; |
| 2690 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2691 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2692 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2693 | |
| 2694 | /* There are two flavours of 'nice': one that returns the new |
| 2695 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2696 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2697 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2698 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2699 | If we are of the nice family that returns the new priority, we |
| 2700 | need to clear errno before the call, and check if errno is filled |
| 2701 | before calling posix_error() on a returnvalue of -1, because the |
| 2702 | -1 may be the actual new priority! */ |
| 2703 | |
| 2704 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2705 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2706 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2707 | if (value == 0) |
| 2708 | value = getpriority(PRIO_PROCESS, 0); |
| 2709 | #endif |
| 2710 | if (value == -1 && errno != 0) |
| 2711 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2712 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2713 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2714 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2715 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2717 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2718 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2719 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2721 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2722 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2723 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2724 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2725 | PyObject *o1, *o2; |
| 2726 | char *p1, *p2; |
| 2727 | BOOL result; |
| 2728 | if (unicode_file_names()) { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2729 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 2730 | goto error; |
| 2731 | if (!convert_to_unicode(&o1)) |
| 2732 | goto error; |
| 2733 | if (!convert_to_unicode(&o2)) { |
| 2734 | Py_DECREF(o1); |
| 2735 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2736 | } |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2737 | Py_BEGIN_ALLOW_THREADS |
| 2738 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2739 | PyUnicode_AsUnicode(o2)); |
| 2740 | Py_END_ALLOW_THREADS |
| 2741 | Py_DECREF(o1); |
| 2742 | Py_DECREF(o2); |
| 2743 | if (!result) |
| 2744 | return win32_error("rename", NULL); |
| 2745 | Py_INCREF(Py_None); |
| 2746 | return Py_None; |
| 2747 | error: |
| 2748 | PyErr_Clear(); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2749 | } |
| 2750 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2751 | return NULL; |
| 2752 | Py_BEGIN_ALLOW_THREADS |
| 2753 | result = MoveFileA(p1, p2); |
| 2754 | Py_END_ALLOW_THREADS |
| 2755 | if (!result) |
| 2756 | return win32_error("rename", NULL); |
| 2757 | Py_INCREF(Py_None); |
| 2758 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2759 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2760 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2761 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2765 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2766 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2767 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2768 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2769 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2770 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2771 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2772 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2773 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2774 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2775 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2776 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2780 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2781 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2782 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2783 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2784 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2785 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2786 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2787 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2788 | return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2789 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2790 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2791 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2794 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2795 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2796 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2797 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2798 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2799 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2800 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2801 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2802 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2803 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2804 | #ifdef MS_WINDOWS |
| 2805 | wchar_t *command; |
| 2806 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2807 | return NULL; |
| 2808 | #else |
| 2809 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2810 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2811 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2812 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2813 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2814 | #ifdef MS_WINDOWS |
| 2815 | sts = _wsystem(command); |
| 2816 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2817 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2818 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2819 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2820 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2821 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2822 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2823 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2824 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2825 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2826 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2827 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2828 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2829 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2830 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2831 | { |
| 2832 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2833 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2834 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2835 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2836 | if (i < 0) |
| 2837 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2838 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2841 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2842 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2843 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2844 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2845 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2846 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2847 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2848 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2849 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2850 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2851 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2852 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2853 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2854 | return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2855 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2856 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2857 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2860 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2861 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2862 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2863 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2864 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2865 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2866 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2867 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2868 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2869 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2870 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2871 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2872 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2873 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2874 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2875 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2876 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2877 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2878 | u.sysname, |
| 2879 | u.nodename, |
| 2880 | u.release, |
| 2881 | u.version, |
| 2882 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2883 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2884 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2885 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2886 | static int |
| 2887 | extract_time(PyObject *t, long* sec, long* usec) |
| 2888 | { |
| 2889 | long intval; |
| 2890 | if (PyFloat_Check(t)) { |
| 2891 | double tval = PyFloat_AsDouble(t); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2892 | 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] | 2893 | if (!intobj) |
| 2894 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2895 | intval = PyLong_AsLong(intobj); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2896 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2897 | if (intval == -1 && PyErr_Occurred()) |
| 2898 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2899 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2900 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2901 | if (*usec < 0) |
| 2902 | /* If rounding gave us a negative number, |
| 2903 | truncate. */ |
| 2904 | *usec = 0; |
| 2905 | return 0; |
| 2906 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2907 | intval = PyLong_AsLong(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2908 | if (intval == -1 && PyErr_Occurred()) |
| 2909 | return -1; |
| 2910 | *sec = intval; |
| 2911 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2912 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2913 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2915 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2916 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2917 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2918 | 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] | 2919 | 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] | 2920 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2921 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2922 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2923 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2924 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2925 | PyObject *arg; |
| 2926 | PyUnicodeObject *obwpath; |
| 2927 | wchar_t *wpath = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2928 | PyObject *oapath; |
| 2929 | char *apath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2930 | HANDLE hFile; |
| 2931 | long atimesec, mtimesec, ausec, musec; |
| 2932 | FILETIME atime, mtime; |
| 2933 | PyObject *result = NULL; |
| 2934 | |
| 2935 | if (unicode_file_names()) { |
| 2936 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2937 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2938 | Py_BEGIN_ALLOW_THREADS |
| 2939 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2940 | NULL, OPEN_EXISTING, |
| 2941 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2942 | Py_END_ALLOW_THREADS |
| 2943 | if (hFile == INVALID_HANDLE_VALUE) |
| 2944 | return win32_error_unicode("utime", wpath); |
| 2945 | } else |
| 2946 | /* Drop the argument parsing error as narrow strings |
| 2947 | are also valid. */ |
| 2948 | PyErr_Clear(); |
| 2949 | } |
| 2950 | if (!wpath) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2951 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 2952 | PyUnicode_FSConverter, &oapath, &arg)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2953 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2954 | apath = bytes2str(oapath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2955 | Py_BEGIN_ALLOW_THREADS |
| 2956 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2957 | NULL, OPEN_EXISTING, |
| 2958 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2959 | Py_END_ALLOW_THREADS |
| 2960 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2961 | win32_error("utime", apath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2962 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2963 | return NULL; |
| 2964 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2965 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | if (arg == Py_None) { |
| 2969 | SYSTEMTIME now; |
| 2970 | GetSystemTime(&now); |
| 2971 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2972 | !SystemTimeToFileTime(&now, &atime)) { |
| 2973 | win32_error("utime", NULL); |
| 2974 | goto done; |
| 2975 | } |
| 2976 | } |
| 2977 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2978 | PyErr_SetString(PyExc_TypeError, |
| 2979 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2980 | goto done; |
| 2981 | } |
| 2982 | else { |
| 2983 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2984 | &atimesec, &ausec) == -1) |
| 2985 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2986 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2987 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2988 | &mtimesec, &musec) == -1) |
| 2989 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2990 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2991 | } |
| 2992 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2993 | /* Avoid putting the file name into the error here, |
| 2994 | as that may confuse the user into believing that |
| 2995 | something is wrong with the file, when it also |
| 2996 | could be the time stamp that gives a problem. */ |
| 2997 | win32_error("utime", NULL); |
| 2998 | } |
| 2999 | Py_INCREF(Py_None); |
| 3000 | result = Py_None; |
| 3001 | done: |
| 3002 | CloseHandle(hFile); |
| 3003 | return result; |
| 3004 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 3005 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3006 | PyObject *opath; |
| 3007 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3008 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3009 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3010 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3011 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3012 | #if defined(HAVE_UTIMES) |
| 3013 | struct timeval buf[2]; |
| 3014 | #define ATIME buf[0].tv_sec |
| 3015 | #define MTIME buf[1].tv_sec |
| 3016 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3017 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3018 | struct utimbuf buf; |
| 3019 | #define ATIME buf.actime |
| 3020 | #define MTIME buf.modtime |
| 3021 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3022 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3023 | time_t buf[2]; |
| 3024 | #define ATIME buf[0] |
| 3025 | #define MTIME buf[1] |
| 3026 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3027 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3028 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3029 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3030 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3031 | PyUnicode_FSConverter, &opath, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3032 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3033 | path = bytes2str(opath, 1); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3034 | if (arg == Py_None) { |
| 3035 | /* optional time values not given */ |
| 3036 | Py_BEGIN_ALLOW_THREADS |
| 3037 | res = utime(path, NULL); |
| 3038 | Py_END_ALLOW_THREADS |
| 3039 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3040 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3041 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3042 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3043 | release_bytes(opath); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3044 | return NULL; |
| 3045 | } |
| 3046 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3047 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3048 | &atime, &ausec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3049 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3050 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3051 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3052 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3053 | &mtime, &musec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3054 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3055 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3056 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3057 | ATIME = atime; |
| 3058 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3059 | #ifdef HAVE_UTIMES |
| 3060 | buf[0].tv_usec = ausec; |
| 3061 | buf[1].tv_usec = musec; |
| 3062 | Py_BEGIN_ALLOW_THREADS |
| 3063 | res = utimes(path, buf); |
| 3064 | Py_END_ALLOW_THREADS |
| 3065 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3066 | Py_BEGIN_ALLOW_THREADS |
| 3067 | res = utime(path, UTIME_ARG); |
| 3068 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3069 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3070 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 3071 | if (res < 0) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3072 | return posix_error_with_allocated_filename(opath); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 3073 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3074 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3075 | Py_INCREF(Py_None); |
| 3076 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3077 | #undef UTIME_ARG |
| 3078 | #undef ATIME |
| 3079 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3080 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3083 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3084 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3086 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3087 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3088 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3089 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3090 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3091 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3092 | { |
| 3093 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3094 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3095 | return NULL; |
| 3096 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 3097 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3100 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3101 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3102 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3103 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3104 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3105 | for (i = 0; i < count; i++) |
| 3106 | PyMem_Free(array[i]); |
| 3107 | PyMem_DEL(array); |
| 3108 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3109 | |
| 3110 | int fsconvert_strdup(PyObject *o, char**out) |
| 3111 | { |
| 3112 | PyObject *bytes; |
| 3113 | Py_ssize_t size; |
| 3114 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3115 | return 0; |
| 3116 | size = PyObject_Size(bytes); |
| 3117 | *out = PyMem_Malloc(size+1); |
| 3118 | if (!*out) |
| 3119 | return 0; |
| 3120 | /* Don't lock bytes, as we hold the GIL */ |
| 3121 | memcpy(*out, bytes2str(bytes, 0), size+1); |
| 3122 | Py_DECREF(bytes); |
| 3123 | return 1; |
| 3124 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3125 | #endif |
| 3126 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3127 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3128 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3129 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3130 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3131 | Execute an executable path with arguments, replacing current process.\n\ |
| 3132 | \n\ |
| 3133 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3134 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3135 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3136 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3137 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3138 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3139 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3140 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3141 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3142 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3143 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3144 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3145 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 3146 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3147 | argv is a list or tuple of strings. */ |
| 3148 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3149 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3150 | PyUnicode_FSConverter, |
| 3151 | &opath, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3152 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3153 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3154 | if (PyList_Check(argv)) { |
| 3155 | argc = PyList_Size(argv); |
| 3156 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3157 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3158 | else if (PyTuple_Check(argv)) { |
| 3159 | argc = PyTuple_Size(argv); |
| 3160 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3161 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3162 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3163 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3164 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3165 | return NULL; |
| 3166 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3167 | if (argc < 1) { |
| 3168 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3169 | release_bytes(opath); |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3170 | return NULL; |
| 3171 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3172 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3173 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3174 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3175 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3176 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3177 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3178 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3179 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3180 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3181 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3182 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3183 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3184 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3185 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3186 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3187 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3188 | } |
| 3189 | argvlist[argc] = NULL; |
| 3190 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3191 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3192 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3193 | /* If we get here it's definitely an error */ |
| 3194 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3195 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3196 | release_bytes(opath); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3197 | return posix_error(); |
| 3198 | } |
| 3199 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3201 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3202 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3203 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3204 | \n\ |
| 3205 | path: path of executable file\n\ |
| 3206 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3207 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3208 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3209 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3210 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3211 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3212 | PyObject *opath; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3213 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3214 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3215 | char **argvlist; |
| 3216 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3217 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3218 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3219 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3220 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3221 | |
| 3222 | /* execve has three arguments: (path, argv, env), where |
| 3223 | argv is a list or tuple of strings and env is a dictionary |
| 3224 | like posix.environ. */ |
| 3225 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3226 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3227 | PyUnicode_FSConverter, |
| 3228 | &opath, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3229 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3230 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3231 | if (PyList_Check(argv)) { |
| 3232 | argc = PyList_Size(argv); |
| 3233 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3234 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3235 | else if (PyTuple_Check(argv)) { |
| 3236 | argc = PyTuple_Size(argv); |
| 3237 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3238 | } |
| 3239 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3240 | PyErr_SetString(PyExc_TypeError, |
| 3241 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3242 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3243 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3244 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3245 | PyErr_SetString(PyExc_TypeError, |
| 3246 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3247 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3250 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3251 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3252 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3253 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3254 | } |
| 3255 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3256 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3257 | &argvlist[i])) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3258 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3259 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3260 | goto fail_1; |
| 3261 | } |
| 3262 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3263 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3264 | argvlist[argc] = NULL; |
| 3265 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3266 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3267 | if (i < 0) |
| 3268 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3269 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3270 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3271 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3272 | goto fail_1; |
| 3273 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3274 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3275 | keys = PyMapping_Keys(env); |
| 3276 | vals = PyMapping_Values(env); |
| 3277 | if (!keys || !vals) |
| 3278 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3279 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3280 | PyErr_SetString(PyExc_TypeError, |
| 3281 | "execve(): env.keys() or env.values() is not a list"); |
| 3282 | goto fail_2; |
| 3283 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3284 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3285 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3286 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3287 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3288 | |
| 3289 | key = PyList_GetItem(keys, pos); |
| 3290 | val = PyList_GetItem(vals, pos); |
| 3291 | if (!key || !val) |
| 3292 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3293 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3294 | if (!PyArg_Parse( |
| 3295 | key, |
| 3296 | "s;execve() arg 3 contains a non-string key", |
| 3297 | &k) || |
| 3298 | !PyArg_Parse( |
| 3299 | val, |
| 3300 | "s;execve() arg 3 contains a non-string value", |
| 3301 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3302 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3303 | goto fail_2; |
| 3304 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3305 | |
| 3306 | #if defined(PYOS_OS2) |
| 3307 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3308 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3309 | #endif |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3310 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3311 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3312 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3313 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3314 | goto fail_2; |
| 3315 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3316 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3317 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3318 | #if defined(PYOS_OS2) |
| 3319 | } |
| 3320 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3321 | } |
| 3322 | envlist[envc] = 0; |
| 3323 | |
| 3324 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3325 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3326 | /* If we get here it's definitely an error */ |
| 3327 | |
| 3328 | (void) posix_error(); |
| 3329 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3330 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3331 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3332 | PyMem_DEL(envlist[envc]); |
| 3333 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3334 | fail_1: |
| 3335 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3336 | Py_XDECREF(vals); |
| 3337 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3338 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3339 | release_bytes(opath); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3340 | return NULL; |
| 3341 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3342 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3343 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3344 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3345 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3346 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3347 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3348 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3349 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3350 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3351 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3352 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3353 | |
| 3354 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3355 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3356 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3357 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3358 | char *path; |
| 3359 | PyObject *argv; |
| 3360 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3361 | int mode, i; |
| 3362 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3363 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3364 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3365 | |
| 3366 | /* spawnv has three arguments: (mode, path, argv), where |
| 3367 | argv is a list or tuple of strings. */ |
| 3368 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3369 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 3370 | PyUnicode_FSConverter, |
| 3371 | &opath, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3372 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3373 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3374 | if (PyList_Check(argv)) { |
| 3375 | argc = PyList_Size(argv); |
| 3376 | getitem = PyList_GetItem; |
| 3377 | } |
| 3378 | else if (PyTuple_Check(argv)) { |
| 3379 | argc = PyTuple_Size(argv); |
| 3380 | getitem = PyTuple_GetItem; |
| 3381 | } |
| 3382 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3383 | PyErr_SetString(PyExc_TypeError, |
| 3384 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3385 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3386 | return NULL; |
| 3387 | } |
| 3388 | |
| 3389 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3390 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3391 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3392 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3393 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3394 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3395 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3396 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3397 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3398 | PyErr_SetString( |
| 3399 | PyExc_TypeError, |
| 3400 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3401 | release_bytes(opath); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3402 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3403 | } |
| 3404 | } |
| 3405 | argvlist[argc] = NULL; |
| 3406 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3407 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3408 | Py_BEGIN_ALLOW_THREADS |
| 3409 | spawnval = spawnv(mode, path, argvlist); |
| 3410 | Py_END_ALLOW_THREADS |
| 3411 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3412 | if (mode == _OLD_P_OVERLAY) |
| 3413 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3414 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3415 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3416 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3417 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3418 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3419 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3420 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3421 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3422 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3423 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3424 | return posix_error(); |
| 3425 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3426 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3427 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3428 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3429 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3430 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3434 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3435 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3436 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3437 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3438 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3439 | path: path of executable file\n\ |
| 3440 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3441 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3442 | |
| 3443 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3444 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3445 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3446 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3447 | char *path; |
| 3448 | PyObject *argv, *env; |
| 3449 | char **argvlist; |
| 3450 | char **envlist; |
| 3451 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3452 | int mode, pos, envc; |
| 3453 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3454 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3455 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3456 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3457 | |
| 3458 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3459 | argv is a list or tuple of strings and env is a dictionary |
| 3460 | like posix.environ. */ |
| 3461 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3462 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 3463 | PyUnicode_FSConverter, |
| 3464 | &opath, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3465 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3466 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3467 | if (PyList_Check(argv)) { |
| 3468 | argc = PyList_Size(argv); |
| 3469 | getitem = PyList_GetItem; |
| 3470 | } |
| 3471 | else if (PyTuple_Check(argv)) { |
| 3472 | argc = PyTuple_Size(argv); |
| 3473 | getitem = PyTuple_GetItem; |
| 3474 | } |
| 3475 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3476 | PyErr_SetString(PyExc_TypeError, |
| 3477 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3478 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3479 | } |
| 3480 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3481 | PyErr_SetString(PyExc_TypeError, |
| 3482 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3483 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | argvlist = PyMem_NEW(char *, argc+1); |
| 3487 | if (argvlist == NULL) { |
| 3488 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3489 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3490 | } |
| 3491 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3492 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3493 | &argvlist[i])) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3494 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3495 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3496 | goto fail_1; |
| 3497 | } |
| 3498 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3499 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3500 | argvlist[argc] = NULL; |
| 3501 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3502 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3503 | if (i < 0) |
| 3504 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3505 | envlist = PyMem_NEW(char *, i + 1); |
| 3506 | if (envlist == NULL) { |
| 3507 | PyErr_NoMemory(); |
| 3508 | goto fail_1; |
| 3509 | } |
| 3510 | envc = 0; |
| 3511 | keys = PyMapping_Keys(env); |
| 3512 | vals = PyMapping_Values(env); |
| 3513 | if (!keys || !vals) |
| 3514 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3515 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3516 | PyErr_SetString(PyExc_TypeError, |
| 3517 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3518 | goto fail_2; |
| 3519 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3520 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3521 | for (pos = 0; pos < i; pos++) { |
| 3522 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3523 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3524 | |
| 3525 | key = PyList_GetItem(keys, pos); |
| 3526 | val = PyList_GetItem(vals, pos); |
| 3527 | if (!key || !val) |
| 3528 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3529 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3530 | if (!PyArg_Parse( |
| 3531 | key, |
| 3532 | "s;spawnve() arg 3 contains a non-string key", |
| 3533 | &k) || |
| 3534 | !PyArg_Parse( |
| 3535 | val, |
| 3536 | "s;spawnve() arg 3 contains a non-string value", |
| 3537 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3538 | { |
| 3539 | goto fail_2; |
| 3540 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3541 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3542 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3543 | if (p == NULL) { |
| 3544 | PyErr_NoMemory(); |
| 3545 | goto fail_2; |
| 3546 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3547 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3548 | envlist[envc++] = p; |
| 3549 | } |
| 3550 | envlist[envc] = 0; |
| 3551 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3552 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3553 | Py_BEGIN_ALLOW_THREADS |
| 3554 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3555 | Py_END_ALLOW_THREADS |
| 3556 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3557 | if (mode == _OLD_P_OVERLAY) |
| 3558 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3559 | |
| 3560 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3561 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3562 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3563 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3564 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3565 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3566 | (void) posix_error(); |
| 3567 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3568 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3569 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3570 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3571 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3572 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3573 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3574 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3575 | while (--envc >= 0) |
| 3576 | PyMem_DEL(envlist[envc]); |
| 3577 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3578 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3579 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3580 | Py_XDECREF(vals); |
| 3581 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3582 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3583 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3584 | return res; |
| 3585 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3586 | |
| 3587 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3588 | #if defined(PYOS_OS2) |
| 3589 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3590 | "spawnvp(mode, file, args)\n\n\ |
| 3591 | Execute the program 'file' in a new process, using the environment\n\ |
| 3592 | search path to find the file.\n\ |
| 3593 | \n\ |
| 3594 | mode: mode of process creation\n\ |
| 3595 | file: executable file name\n\ |
| 3596 | args: tuple or list of strings"); |
| 3597 | |
| 3598 | static PyObject * |
| 3599 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3600 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3601 | PyObject *opath; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3602 | char *path; |
| 3603 | PyObject *argv; |
| 3604 | char **argvlist; |
| 3605 | int mode, i, argc; |
| 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 | |
| 3609 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3610 | argv is a list or tuple of strings. */ |
| 3611 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3612 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 3613 | PyUnicode_FSConverter, |
| 3614 | &opath, &argv)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3615 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3616 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3617 | if (PyList_Check(argv)) { |
| 3618 | argc = PyList_Size(argv); |
| 3619 | getitem = PyList_GetItem; |
| 3620 | } |
| 3621 | else if (PyTuple_Check(argv)) { |
| 3622 | argc = PyTuple_Size(argv); |
| 3623 | getitem = PyTuple_GetItem; |
| 3624 | } |
| 3625 | else { |
| 3626 | PyErr_SetString(PyExc_TypeError, |
| 3627 | "spawnvp() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3628 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3629 | return NULL; |
| 3630 | } |
| 3631 | |
| 3632 | argvlist = PyMem_NEW(char *, argc+1); |
| 3633 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3634 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3635 | return PyErr_NoMemory(); |
| 3636 | } |
| 3637 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3638 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3639 | &argvlist[i])) { |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3640 | free_string_array(argvlist, i); |
| 3641 | PyErr_SetString( |
| 3642 | PyExc_TypeError, |
| 3643 | "spawnvp() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3644 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3645 | return NULL; |
| 3646 | } |
| 3647 | } |
| 3648 | argvlist[argc] = NULL; |
| 3649 | |
| 3650 | Py_BEGIN_ALLOW_THREADS |
| 3651 | #if defined(PYCC_GCC) |
| 3652 | spawnval = spawnvp(mode, path, argvlist); |
| 3653 | #else |
| 3654 | spawnval = _spawnvp(mode, path, argvlist); |
| 3655 | #endif |
| 3656 | Py_END_ALLOW_THREADS |
| 3657 | |
| 3658 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3659 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3660 | |
| 3661 | if (spawnval == -1) |
| 3662 | return posix_error(); |
| 3663 | else |
| 3664 | return Py_BuildValue("l", (long) spawnval); |
| 3665 | } |
| 3666 | |
| 3667 | |
| 3668 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3669 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3670 | Execute the program 'file' in a new process, using the environment\n\ |
| 3671 | search path to find the file.\n\ |
| 3672 | \n\ |
| 3673 | mode: mode of process creation\n\ |
| 3674 | file: executable file name\n\ |
| 3675 | args: tuple or list of arguments\n\ |
| 3676 | env: dictionary of strings mapping to strings"); |
| 3677 | |
| 3678 | static PyObject * |
| 3679 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3680 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3681 | PyObject *opath |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3682 | char *path; |
| 3683 | PyObject *argv, *env; |
| 3684 | char **argvlist; |
| 3685 | char **envlist; |
| 3686 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3687 | int mode, i, pos, argc, envc; |
| 3688 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3689 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3690 | int lastarg = 0; |
| 3691 | |
| 3692 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3693 | argv is a list or tuple of strings and env is a dictionary |
| 3694 | like posix.environ. */ |
| 3695 | |
| 3696 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3697 | PyUnicode_FSConverter, |
| 3698 | &opath, &argv, &env)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3699 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3700 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3701 | if (PyList_Check(argv)) { |
| 3702 | argc = PyList_Size(argv); |
| 3703 | getitem = PyList_GetItem; |
| 3704 | } |
| 3705 | else if (PyTuple_Check(argv)) { |
| 3706 | argc = PyTuple_Size(argv); |
| 3707 | getitem = PyTuple_GetItem; |
| 3708 | } |
| 3709 | else { |
| 3710 | PyErr_SetString(PyExc_TypeError, |
| 3711 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3712 | goto fail_0; |
| 3713 | } |
| 3714 | if (!PyMapping_Check(env)) { |
| 3715 | PyErr_SetString(PyExc_TypeError, |
| 3716 | "spawnvpe() arg 3 must be a mapping object"); |
| 3717 | goto fail_0; |
| 3718 | } |
| 3719 | |
| 3720 | argvlist = PyMem_NEW(char *, argc+1); |
| 3721 | if (argvlist == NULL) { |
| 3722 | PyErr_NoMemory(); |
| 3723 | goto fail_0; |
| 3724 | } |
| 3725 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3726 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3727 | &argvlist[i])) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3728 | { |
| 3729 | lastarg = i; |
| 3730 | goto fail_1; |
| 3731 | } |
| 3732 | } |
| 3733 | lastarg = argc; |
| 3734 | argvlist[argc] = NULL; |
| 3735 | |
| 3736 | i = PyMapping_Size(env); |
| 3737 | if (i < 0) |
| 3738 | goto fail_1; |
| 3739 | envlist = PyMem_NEW(char *, i + 1); |
| 3740 | if (envlist == NULL) { |
| 3741 | PyErr_NoMemory(); |
| 3742 | goto fail_1; |
| 3743 | } |
| 3744 | envc = 0; |
| 3745 | keys = PyMapping_Keys(env); |
| 3746 | vals = PyMapping_Values(env); |
| 3747 | if (!keys || !vals) |
| 3748 | goto fail_2; |
| 3749 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3750 | PyErr_SetString(PyExc_TypeError, |
| 3751 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3752 | goto fail_2; |
| 3753 | } |
| 3754 | |
| 3755 | for (pos = 0; pos < i; pos++) { |
| 3756 | char *p, *k, *v; |
| 3757 | size_t len; |
| 3758 | |
| 3759 | key = PyList_GetItem(keys, pos); |
| 3760 | val = PyList_GetItem(vals, pos); |
| 3761 | if (!key || !val) |
| 3762 | goto fail_2; |
| 3763 | |
| 3764 | if (!PyArg_Parse( |
| 3765 | key, |
| 3766 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3767 | &k) || |
| 3768 | !PyArg_Parse( |
| 3769 | val, |
| 3770 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3771 | &v)) |
| 3772 | { |
| 3773 | goto fail_2; |
| 3774 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3775 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3776 | p = PyMem_NEW(char, len); |
| 3777 | if (p == NULL) { |
| 3778 | PyErr_NoMemory(); |
| 3779 | goto fail_2; |
| 3780 | } |
| 3781 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3782 | envlist[envc++] = p; |
| 3783 | } |
| 3784 | envlist[envc] = 0; |
| 3785 | |
| 3786 | Py_BEGIN_ALLOW_THREADS |
| 3787 | #if defined(PYCC_GCC) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3788 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3789 | #else |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3790 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3791 | #endif |
| 3792 | Py_END_ALLOW_THREADS |
| 3793 | |
| 3794 | if (spawnval == -1) |
| 3795 | (void) posix_error(); |
| 3796 | else |
| 3797 | res = Py_BuildValue("l", (long) spawnval); |
| 3798 | |
| 3799 | fail_2: |
| 3800 | while (--envc >= 0) |
| 3801 | PyMem_DEL(envlist[envc]); |
| 3802 | PyMem_DEL(envlist); |
| 3803 | fail_1: |
| 3804 | free_string_array(argvlist, lastarg); |
| 3805 | Py_XDECREF(vals); |
| 3806 | Py_XDECREF(keys); |
| 3807 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3808 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3809 | return res; |
| 3810 | } |
| 3811 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3812 | #endif /* HAVE_SPAWNV */ |
| 3813 | |
| 3814 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3815 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3816 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3817 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3818 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3819 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3820 | 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] | 3821 | |
| 3822 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3823 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3824 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3825 | pid_t pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3826 | if (pid == -1) |
| 3827 | return posix_error(); |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 3828 | if (pid == 0) |
| 3829 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3830 | return PyLong_FromLong(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3831 | } |
| 3832 | #endif |
| 3833 | |
| 3834 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3835 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3836 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3837 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3838 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3839 | 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] | 3840 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3841 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3842 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3843 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3844 | pid_t pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3845 | if (pid == -1) |
| 3846 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3847 | if (pid == 0) |
| 3848 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3849 | return PyLong_FromLong(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3850 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3851 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3852 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3853 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3854 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3855 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3856 | #define DEV_PTY_FILE "/dev/ptc" |
| 3857 | #define HAVE_DEV_PTMX |
| 3858 | #else |
| 3859 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3860 | #endif |
| 3861 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3862 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3863 | #ifdef HAVE_PTY_H |
| 3864 | #include <pty.h> |
| 3865 | #else |
| 3866 | #ifdef HAVE_LIBUTIL_H |
| 3867 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3868 | #endif /* HAVE_LIBUTIL_H */ |
| 3869 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3870 | #ifdef HAVE_STROPTS_H |
| 3871 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3872 | #endif |
| 3873 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3874 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3875 | #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] | 3876 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3877 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3878 | 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] | 3879 | |
| 3880 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3881 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3882 | { |
| 3883 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3884 | #ifndef HAVE_OPENPTY |
| 3885 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3886 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3887 | #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] | 3888 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3889 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3890 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3891 | #endif |
| 3892 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3893 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3894 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3895 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3896 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3897 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3898 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3899 | if (slave_name == NULL) |
| 3900 | return posix_error(); |
| 3901 | |
| 3902 | slave_fd = open(slave_name, O_RDWR); |
| 3903 | if (slave_fd < 0) |
| 3904 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3905 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3906 | 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] | 3907 | if (master_fd < 0) |
| 3908 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3909 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3910 | /* change permission of slave */ |
| 3911 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3912 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3913 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3914 | } |
| 3915 | /* unlock slave */ |
| 3916 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3917 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3918 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3919 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3920 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3921 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3922 | if (slave_name == NULL) |
| 3923 | return posix_error(); |
| 3924 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3925 | if (slave_fd < 0) |
| 3926 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3927 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3928 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3929 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3930 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3931 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3932 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3933 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3934 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3935 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3936 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3937 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3938 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3939 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3940 | |
| 3941 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3942 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3943 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3944 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3945 | 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] | 3946 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3947 | |
| 3948 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3949 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3950 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3951 | int master_fd = -1; |
| 3952 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3953 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3954 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3955 | if (pid == -1) |
| 3956 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3957 | if (pid == 0) |
| 3958 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3959 | return Py_BuildValue("(li)", pid, master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3960 | } |
| 3961 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3962 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3963 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3964 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3965 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3966 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3967 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3968 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3969 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3970 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3971 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3972 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3973 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3974 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3975 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3976 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3977 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3978 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3979 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3980 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3981 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3982 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3983 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3984 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3985 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3986 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3987 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3988 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3989 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3990 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3991 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3992 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3993 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3994 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3995 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3996 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3997 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3998 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3999 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4000 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4001 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4002 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4003 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4004 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4005 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4006 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4007 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4008 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4009 | return PyLong_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4012 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4013 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4014 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4015 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4016 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4017 | |
| 4018 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4019 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4020 | { |
| 4021 | PyObject *result = NULL; |
| 4022 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4023 | #ifdef NGROUPS_MAX |
| 4024 | #define MAX_GROUPS NGROUPS_MAX |
| 4025 | #else |
| 4026 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 4027 | #define MAX_GROUPS 64 |
| 4028 | #endif |
| 4029 | gid_t grouplist[MAX_GROUPS]; |
| 4030 | int n; |
| 4031 | |
| 4032 | n = getgroups(MAX_GROUPS, grouplist); |
| 4033 | if (n < 0) |
| 4034 | posix_error(); |
| 4035 | else { |
| 4036 | result = PyList_New(n); |
| 4037 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4038 | int i; |
| 4039 | for (i = 0; i < n; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4040 | PyObject *o = PyLong_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4041 | if (o == NULL) { |
| 4042 | Py_DECREF(result); |
| 4043 | result = NULL; |
| 4044 | break; |
| 4045 | } |
| 4046 | PyList_SET_ITEM(result, i, o); |
| 4047 | } |
| 4048 | } |
| 4049 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4050 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4051 | return result; |
| 4052 | } |
| 4053 | #endif |
| 4054 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4055 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4056 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4057 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4058 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4059 | |
| 4060 | static PyObject * |
| 4061 | posix_getpgid(PyObject *self, PyObject *args) |
| 4062 | { |
| 4063 | int pid, pgid; |
| 4064 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 4065 | return NULL; |
| 4066 | pgid = getpgid(pid); |
| 4067 | if (pgid < 0) |
| 4068 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4069 | return PyLong_FromLong((long)pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4070 | } |
| 4071 | #endif /* HAVE_GETPGID */ |
| 4072 | |
| 4073 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4074 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4075 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4076 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4077 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4078 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4079 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4080 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4081 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4082 | #ifdef GETPGRP_HAVE_ARG |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4083 | return PyLong_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4084 | #else /* GETPGRP_HAVE_ARG */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4085 | return PyLong_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4086 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4087 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4088 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4089 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4090 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4091 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4092 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4093 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4094 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4095 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4096 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4097 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4098 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4099 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4100 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4101 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4102 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4103 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4104 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4105 | Py_INCREF(Py_None); |
| 4106 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4109 | #endif /* HAVE_SETPGRP */ |
| 4110 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4111 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4112 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4113 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4114 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4115 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4116 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4117 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4118 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4119 | return PyLong_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4120 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4121 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4122 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4123 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4124 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4125 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4126 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4127 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4128 | |
| 4129 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4130 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4131 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4132 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4133 | char *name; |
| 4134 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4135 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4136 | errno = 0; |
| 4137 | name = getlogin(); |
| 4138 | if (name == NULL) { |
| 4139 | if (errno) |
| 4140 | posix_error(); |
| 4141 | else |
| 4142 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 4143 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4144 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4145 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 4146 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4147 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4148 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4149 | return result; |
| 4150 | } |
| 4151 | #endif |
| 4152 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4153 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4154 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4155 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4156 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4157 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4158 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4159 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4160 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4161 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4162 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4163 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4164 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4165 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4166 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4167 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4168 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4169 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4170 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4171 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4172 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4173 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4174 | pid_t pid; |
| 4175 | int sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4176 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4177 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4178 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4179 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4180 | APIRET rc; |
| 4181 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4182 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4183 | |
| 4184 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4185 | APIRET rc; |
| 4186 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4187 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4188 | |
| 4189 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4190 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4191 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4192 | if (kill(pid, sig) == -1) |
| 4193 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4194 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4195 | Py_INCREF(Py_None); |
| 4196 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4197 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4198 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4199 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4200 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4201 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4202 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4203 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4204 | |
| 4205 | static PyObject * |
| 4206 | posix_killpg(PyObject *self, PyObject *args) |
| 4207 | { |
| 4208 | int pgid, sig; |
| 4209 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 4210 | return NULL; |
| 4211 | if (killpg(pgid, sig) == -1) |
| 4212 | return posix_error(); |
| 4213 | Py_INCREF(Py_None); |
| 4214 | return Py_None; |
| 4215 | } |
| 4216 | #endif |
| 4217 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4218 | #ifdef HAVE_PLOCK |
| 4219 | |
| 4220 | #ifdef HAVE_SYS_LOCK_H |
| 4221 | #include <sys/lock.h> |
| 4222 | #endif |
| 4223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4224 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4225 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4226 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4227 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4229 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4230 | { |
| 4231 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4232 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4233 | return NULL; |
| 4234 | if (plock(op) == -1) |
| 4235 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4236 | Py_INCREF(Py_None); |
| 4237 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4238 | } |
| 4239 | #endif |
| 4240 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4241 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4242 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4243 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4244 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4245 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4246 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4247 | Set the current process's user id."); |
| 4248 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4249 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4250 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4251 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4252 | long uid_arg; |
| 4253 | uid_t uid; |
| 4254 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4255 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4256 | uid = uid_arg; |
| 4257 | if (uid != uid_arg) { |
| 4258 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4259 | return NULL; |
| 4260 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4261 | if (setuid(uid) < 0) |
| 4262 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4263 | Py_INCREF(Py_None); |
| 4264 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4265 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4266 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4267 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4268 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4269 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4270 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4271 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4272 | Set the current process's effective user id."); |
| 4273 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4274 | static PyObject * |
| 4275 | posix_seteuid (PyObject *self, PyObject *args) |
| 4276 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4277 | long euid_arg; |
| 4278 | uid_t euid; |
| 4279 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4280 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4281 | euid = euid_arg; |
| 4282 | if (euid != euid_arg) { |
| 4283 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4284 | return NULL; |
| 4285 | } |
| 4286 | if (seteuid(euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4287 | return posix_error(); |
| 4288 | } else { |
| 4289 | Py_INCREF(Py_None); |
| 4290 | return Py_None; |
| 4291 | } |
| 4292 | } |
| 4293 | #endif /* HAVE_SETEUID */ |
| 4294 | |
| 4295 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4296 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4297 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4298 | Set the current process's effective group id."); |
| 4299 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4300 | static PyObject * |
| 4301 | posix_setegid (PyObject *self, PyObject *args) |
| 4302 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4303 | long egid_arg; |
| 4304 | gid_t egid; |
| 4305 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4306 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4307 | egid = egid_arg; |
| 4308 | if (egid != egid_arg) { |
| 4309 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4310 | return NULL; |
| 4311 | } |
| 4312 | if (setegid(egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4313 | return posix_error(); |
| 4314 | } else { |
| 4315 | Py_INCREF(Py_None); |
| 4316 | return Py_None; |
| 4317 | } |
| 4318 | } |
| 4319 | #endif /* HAVE_SETEGID */ |
| 4320 | |
| 4321 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4322 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4323 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4324 | Set the current process's real and effective user ids."); |
| 4325 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4326 | static PyObject * |
| 4327 | posix_setreuid (PyObject *self, PyObject *args) |
| 4328 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4329 | long ruid_arg, euid_arg; |
| 4330 | uid_t ruid, euid; |
| 4331 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4332 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4333 | ruid = ruid_arg; |
| 4334 | euid = euid_arg; |
| 4335 | if (euid != euid_arg || ruid != ruid_arg) { |
| 4336 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4337 | return NULL; |
| 4338 | } |
| 4339 | if (setreuid(ruid, euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4340 | return posix_error(); |
| 4341 | } else { |
| 4342 | Py_INCREF(Py_None); |
| 4343 | return Py_None; |
| 4344 | } |
| 4345 | } |
| 4346 | #endif /* HAVE_SETREUID */ |
| 4347 | |
| 4348 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4349 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4350 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4351 | Set the current process's real and effective group ids."); |
| 4352 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4353 | static PyObject * |
| 4354 | posix_setregid (PyObject *self, PyObject *args) |
| 4355 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4356 | long rgid_arg, egid_arg; |
| 4357 | gid_t rgid, egid; |
| 4358 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4359 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4360 | rgid = rgid_arg; |
| 4361 | egid = egid_arg; |
| 4362 | if (egid != egid_arg || rgid != rgid_arg) { |
| 4363 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4364 | return NULL; |
| 4365 | } |
| 4366 | if (setregid(rgid, egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4367 | return posix_error(); |
| 4368 | } else { |
| 4369 | Py_INCREF(Py_None); |
| 4370 | return Py_None; |
| 4371 | } |
| 4372 | } |
| 4373 | #endif /* HAVE_SETREGID */ |
| 4374 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4375 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4376 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4377 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4378 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4379 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4380 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4381 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4382 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4383 | long gid_arg; |
| 4384 | gid_t gid; |
| 4385 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4386 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4387 | gid = gid_arg; |
| 4388 | if (gid != gid_arg) { |
| 4389 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4390 | return NULL; |
| 4391 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4392 | if (setgid(gid) < 0) |
| 4393 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4394 | Py_INCREF(Py_None); |
| 4395 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4396 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4397 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4398 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4399 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4400 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4401 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4402 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4403 | |
| 4404 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4405 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4406 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4407 | int i, len; |
| 4408 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4409 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4410 | if (!PySequence_Check(groups)) { |
| 4411 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4412 | return NULL; |
| 4413 | } |
| 4414 | len = PySequence_Size(groups); |
| 4415 | if (len > MAX_GROUPS) { |
| 4416 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4417 | return NULL; |
| 4418 | } |
| 4419 | for(i = 0; i < len; i++) { |
| 4420 | PyObject *elem; |
| 4421 | elem = PySequence_GetItem(groups, i); |
| 4422 | if (!elem) |
| 4423 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4424 | if (!PyLong_Check(elem)) { |
| 4425 | PyErr_SetString(PyExc_TypeError, |
| 4426 | "groups must be integers"); |
| 4427 | Py_DECREF(elem); |
| 4428 | return NULL; |
| 4429 | } else { |
| 4430 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4431 | if (PyErr_Occurred()) { |
| 4432 | PyErr_SetString(PyExc_TypeError, |
| 4433 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4434 | Py_DECREF(elem); |
| 4435 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4436 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4437 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4438 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4439 | if (grouplist[i] != x) { |
| 4440 | PyErr_SetString(PyExc_TypeError, |
| 4441 | "group id too big"); |
| 4442 | Py_DECREF(elem); |
| 4443 | return NULL; |
| 4444 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4445 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4446 | Py_DECREF(elem); |
| 4447 | } |
| 4448 | |
| 4449 | if (setgroups(len, grouplist) < 0) |
| 4450 | return posix_error(); |
| 4451 | Py_INCREF(Py_None); |
| 4452 | return Py_None; |
| 4453 | } |
| 4454 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4455 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4456 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4457 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4458 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4459 | { |
| 4460 | PyObject *result; |
| 4461 | static PyObject *struct_rusage; |
| 4462 | |
| 4463 | if (pid == -1) |
| 4464 | return posix_error(); |
| 4465 | |
| 4466 | if (struct_rusage == NULL) { |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4467 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4468 | if (m == NULL) |
| 4469 | return NULL; |
| 4470 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4471 | Py_DECREF(m); |
| 4472 | if (struct_rusage == NULL) |
| 4473 | return NULL; |
| 4474 | } |
| 4475 | |
| 4476 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4477 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4478 | if (!result) |
| 4479 | return NULL; |
| 4480 | |
| 4481 | #ifndef doubletime |
| 4482 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4483 | #endif |
| 4484 | |
| 4485 | PyStructSequence_SET_ITEM(result, 0, |
| 4486 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4487 | PyStructSequence_SET_ITEM(result, 1, |
| 4488 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4489 | #define SET_INT(result, index, value)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4490 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4491 | SET_INT(result, 2, ru->ru_maxrss); |
| 4492 | SET_INT(result, 3, ru->ru_ixrss); |
| 4493 | SET_INT(result, 4, ru->ru_idrss); |
| 4494 | SET_INT(result, 5, ru->ru_isrss); |
| 4495 | SET_INT(result, 6, ru->ru_minflt); |
| 4496 | SET_INT(result, 7, ru->ru_majflt); |
| 4497 | SET_INT(result, 8, ru->ru_nswap); |
| 4498 | SET_INT(result, 9, ru->ru_inblock); |
| 4499 | SET_INT(result, 10, ru->ru_oublock); |
| 4500 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4501 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4502 | SET_INT(result, 13, ru->ru_nsignals); |
| 4503 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4504 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4505 | #undef SET_INT |
| 4506 | |
| 4507 | if (PyErr_Occurred()) { |
| 4508 | Py_DECREF(result); |
| 4509 | return NULL; |
| 4510 | } |
| 4511 | |
| 4512 | return Py_BuildValue("iiN", pid, status, result); |
| 4513 | } |
| 4514 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4515 | |
| 4516 | #ifdef HAVE_WAIT3 |
| 4517 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4518 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4519 | Wait for completion of a child process."); |
| 4520 | |
| 4521 | static PyObject * |
| 4522 | posix_wait3(PyObject *self, PyObject *args) |
| 4523 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4524 | pid_t pid; |
| 4525 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4526 | struct rusage ru; |
| 4527 | WAIT_TYPE status; |
| 4528 | WAIT_STATUS_INT(status) = 0; |
| 4529 | |
| 4530 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4531 | return NULL; |
| 4532 | |
| 4533 | Py_BEGIN_ALLOW_THREADS |
| 4534 | pid = wait3(&status, options, &ru); |
| 4535 | Py_END_ALLOW_THREADS |
| 4536 | |
| 4537 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4538 | } |
| 4539 | #endif /* HAVE_WAIT3 */ |
| 4540 | |
| 4541 | #ifdef HAVE_WAIT4 |
| 4542 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4543 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4544 | Wait for completion of a given child process."); |
| 4545 | |
| 4546 | static PyObject * |
| 4547 | posix_wait4(PyObject *self, PyObject *args) |
| 4548 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4549 | pid_t pid; |
| 4550 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4551 | struct rusage ru; |
| 4552 | WAIT_TYPE status; |
| 4553 | WAIT_STATUS_INT(status) = 0; |
| 4554 | |
| 4555 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4556 | return NULL; |
| 4557 | |
| 4558 | Py_BEGIN_ALLOW_THREADS |
| 4559 | pid = wait4(pid, &status, options, &ru); |
| 4560 | Py_END_ALLOW_THREADS |
| 4561 | |
| 4562 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4563 | } |
| 4564 | #endif /* HAVE_WAIT4 */ |
| 4565 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4566 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4567 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4568 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4569 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4570 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4571 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4572 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4573 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4574 | pid_t pid; |
| 4575 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4576 | WAIT_TYPE status; |
| 4577 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4578 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4579 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4580 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4581 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4582 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4583 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4584 | if (pid == -1) |
| 4585 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4586 | |
| 4587 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4590 | #elif defined(HAVE_CWAIT) |
| 4591 | |
| 4592 | /* 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] | 4593 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4594 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4595 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4596 | |
| 4597 | static PyObject * |
| 4598 | posix_waitpid(PyObject *self, PyObject *args) |
| 4599 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4600 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4601 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4602 | |
| 4603 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4604 | return NULL; |
| 4605 | Py_BEGIN_ALLOW_THREADS |
| 4606 | pid = _cwait(&status, pid, options); |
| 4607 | Py_END_ALLOW_THREADS |
| 4608 | if (pid == -1) |
| 4609 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4610 | |
| 4611 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4612 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4613 | } |
| 4614 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4615 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4616 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4617 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4618 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4619 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4620 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4621 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4622 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4623 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4624 | pid_t pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4625 | WAIT_TYPE status; |
| 4626 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4627 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4628 | Py_BEGIN_ALLOW_THREADS |
| 4629 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4630 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4631 | if (pid == -1) |
| 4632 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4633 | |
| 4634 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4635 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4636 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4637 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4638 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4639 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4640 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4641 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4642 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4643 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4644 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4645 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4646 | #ifdef HAVE_LSTAT |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4647 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4648 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4649 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4650 | return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4651 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4652 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4653 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4654 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4657 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4658 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4659 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4660 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4661 | 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] | 4662 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4663 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4664 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4665 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4666 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4667 | char buf[MAXPATHLEN]; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4668 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4669 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4670 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4671 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4672 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4673 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 4674 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4675 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4676 | path = bytes2str(opath, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4677 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4678 | if (v == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4679 | release_bytes(opath); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4680 | return NULL; |
| 4681 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4682 | |
| 4683 | if (PyUnicode_Check(v)) { |
| 4684 | arg_is_unicode = 1; |
| 4685 | } |
| 4686 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4687 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4688 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4689 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4690 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4691 | if (n < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4692 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4693 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4694 | release_bytes(opath); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4695 | v = PyBytes_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4696 | if (arg_is_unicode) { |
| 4697 | PyObject *w; |
| 4698 | |
| 4699 | w = PyUnicode_FromEncodedObject(v, |
| 4700 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 4701 | "surrogateescape"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4702 | if (w != NULL) { |
| 4703 | Py_DECREF(v); |
| 4704 | v = w; |
| 4705 | } |
| 4706 | else { |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4707 | v = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4708 | } |
| 4709 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4710 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4711 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4712 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4713 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4714 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4715 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4716 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4717 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4718 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4719 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4720 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4721 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4722 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4723 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4724 | } |
| 4725 | #endif /* HAVE_SYMLINK */ |
| 4726 | |
| 4727 | |
| 4728 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4729 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4730 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4731 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4732 | { |
| 4733 | ULONG value = 0; |
| 4734 | |
| 4735 | Py_BEGIN_ALLOW_THREADS |
| 4736 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4737 | Py_END_ALLOW_THREADS |
| 4738 | |
| 4739 | return value; |
| 4740 | } |
| 4741 | |
| 4742 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4743 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4744 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4745 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4746 | return Py_BuildValue("ddddd", |
| 4747 | (double)0 /* t.tms_utime / HZ */, |
| 4748 | (double)0 /* t.tms_stime / HZ */, |
| 4749 | (double)0 /* t.tms_cutime / HZ */, |
| 4750 | (double)0 /* t.tms_cstime / HZ */, |
| 4751 | (double)system_uptime() / 1000); |
| 4752 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4753 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4754 | #define NEED_TICKS_PER_SECOND |
| 4755 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4756 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4757 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4758 | { |
| 4759 | struct tms t; |
| 4760 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4761 | errno = 0; |
| 4762 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4763 | if (c == (clock_t) -1) |
| 4764 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4765 | return Py_BuildValue("ddddd", |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4766 | (double)t.tms_utime / ticks_per_second, |
| 4767 | (double)t.tms_stime / ticks_per_second, |
| 4768 | (double)t.tms_cutime / ticks_per_second, |
| 4769 | (double)t.tms_cstime / ticks_per_second, |
| 4770 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4771 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4772 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4773 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4774 | |
| 4775 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4776 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4777 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4778 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4779 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4780 | { |
| 4781 | FILETIME create, exit, kernel, user; |
| 4782 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4783 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4784 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4785 | /* The fields of a FILETIME structure are the hi and lo part |
| 4786 | of a 64-bit value expressed in 100 nanosecond units. |
| 4787 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4788 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4789 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4790 | return Py_BuildValue( |
| 4791 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4792 | (double)(user.dwHighDateTime*429.4967296 + |
| 4793 | user.dwLowDateTime*1e-7), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 4794 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4795 | kernel.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4796 | (double)0, |
| 4797 | (double)0, |
| 4798 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4799 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4800 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4801 | |
| 4802 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4803 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4804 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4805 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4806 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4807 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4808 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4809 | #ifdef HAVE_GETSID |
| 4810 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4811 | "getsid(pid) -> sid\n\n\ |
| 4812 | Call the system call getsid()."); |
| 4813 | |
| 4814 | static PyObject * |
| 4815 | posix_getsid(PyObject *self, PyObject *args) |
| 4816 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4817 | pid_t pid; |
| 4818 | int sid; |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4819 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4820 | return NULL; |
| 4821 | sid = getsid(pid); |
| 4822 | if (sid < 0) |
| 4823 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4824 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4825 | } |
| 4826 | #endif /* HAVE_GETSID */ |
| 4827 | |
| 4828 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4829 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4830 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4831 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4832 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4833 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4834 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4835 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4836 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4837 | if (setsid() < 0) |
| 4838 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4839 | Py_INCREF(Py_None); |
| 4840 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4841 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4842 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4843 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4844 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4845 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4846 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4847 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4848 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4849 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4850 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4851 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4852 | pid_t pid; |
| 4853 | int pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4854 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4855 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4856 | if (setpgid(pid, pgrp) < 0) |
| 4857 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4858 | Py_INCREF(Py_None); |
| 4859 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4860 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4861 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4862 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4863 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4864 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4865 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4866 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4867 | 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] | 4868 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4869 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4870 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4871 | { |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4872 | int fd; |
| 4873 | pid_t pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4874 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4875 | return NULL; |
| 4876 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4877 | if (pgid < 0) |
| 4878 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4879 | return PyLong_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4880 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4881 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4882 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4883 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4884 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4885 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4886 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4887 | 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] | 4888 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4889 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4890 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4891 | { |
| 4892 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4893 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4894 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4895 | if (tcsetpgrp(fd, pgid) < 0) |
| 4896 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4897 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4898 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4899 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4900 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4901 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4902 | /* Functions acting on file descriptors */ |
| 4903 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4904 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4905 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4906 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4907 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4908 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4909 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4910 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4911 | PyObject *ofile; |
| 4912 | char *file; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4913 | int flag; |
| 4914 | int mode = 0777; |
| 4915 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4916 | |
| 4917 | #ifdef MS_WINDOWS |
| 4918 | if (unicode_file_names()) { |
| 4919 | PyUnicodeObject *po; |
| 4920 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4921 | Py_BEGIN_ALLOW_THREADS |
| 4922 | /* PyUnicode_AS_UNICODE OK without thread |
| 4923 | lock as it is a simple dereference. */ |
| 4924 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4925 | Py_END_ALLOW_THREADS |
| 4926 | if (fd < 0) |
| 4927 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4928 | return PyLong_FromLong((long)fd); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4929 | } |
| 4930 | /* Drop the argument parsing error as narrow strings |
| 4931 | are also valid. */ |
| 4932 | PyErr_Clear(); |
| 4933 | } |
| 4934 | #endif |
| 4935 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4936 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 4937 | PyUnicode_FSConverter, &ofile, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4938 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4939 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4940 | file = bytes2str(ofile, 1); |
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 | fd = open(file, flag, mode); |
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 (fd < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4945 | return posix_error_with_allocated_filename(ofile); |
| 4946 | release_bytes(ofile); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4947 | return PyLong_FromLong((long)fd); |
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_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4952 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4953 | Close a file descriptor (for low level IO)."); |
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_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4957 | { |
| 4958 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4959 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4960 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4961 | if (!_PyVerify_fd(fd)) |
| 4962 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4963 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4964 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4965 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4966 | if (res < 0) |
| 4967 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4968 | Py_INCREF(Py_None); |
| 4969 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4972 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4973 | PyDoc_STRVAR(posix_closerange__doc__, |
| 4974 | "closerange(fd_low, fd_high)\n\n\ |
| 4975 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 4976 | |
| 4977 | static PyObject * |
| 4978 | posix_closerange(PyObject *self, PyObject *args) |
| 4979 | { |
| 4980 | int fd_from, fd_to, i; |
| 4981 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 4982 | return NULL; |
| 4983 | Py_BEGIN_ALLOW_THREADS |
| 4984 | for (i = fd_from; i < fd_to; i++) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4985 | if (_PyVerify_fd(i)) |
| 4986 | close(i); |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4987 | Py_END_ALLOW_THREADS |
| 4988 | Py_RETURN_NONE; |
| 4989 | } |
| 4990 | |
| 4991 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4992 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4993 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4994 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4995 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4996 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4997 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4998 | { |
| 4999 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5000 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5001 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5002 | if (!_PyVerify_fd(fd)) |
| 5003 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5004 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5005 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5006 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5007 | if (fd < 0) |
| 5008 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5009 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5012 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5013 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5014 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5015 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5016 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5017 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5018 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5019 | { |
| 5020 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5021 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5022 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5023 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 5024 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5025 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5026 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5027 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5028 | if (res < 0) |
| 5029 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5030 | Py_INCREF(Py_None); |
| 5031 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5032 | } |
| 5033 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5034 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5035 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5036 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5037 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5038 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5039 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5040 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5041 | { |
| 5042 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5043 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5044 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5045 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5046 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5047 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5048 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5049 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5050 | return NULL; |
| 5051 | #ifdef SEEK_SET |
| 5052 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5053 | switch (how) { |
| 5054 | case 0: how = SEEK_SET; break; |
| 5055 | case 1: how = SEEK_CUR; break; |
| 5056 | case 2: how = SEEK_END; break; |
| 5057 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5058 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5059 | |
| 5060 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5061 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5062 | #else |
| 5063 | pos = PyLong_Check(posobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5064 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5065 | #endif |
| 5066 | if (PyErr_Occurred()) |
| 5067 | return NULL; |
| 5068 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5069 | if (!_PyVerify_fd(fd)) |
| 5070 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5071 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5072 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5073 | res = _lseeki64(fd, pos, how); |
| 5074 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5075 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5076 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5077 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5078 | if (res < 0) |
| 5079 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5080 | |
| 5081 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5082 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5083 | #else |
| 5084 | return PyLong_FromLongLong(res); |
| 5085 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5088 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5089 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5090 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5091 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5092 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5093 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5094 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5095 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 5096 | int fd, size; |
| 5097 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5098 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5099 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5100 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5101 | if (size < 0) { |
| 5102 | errno = EINVAL; |
| 5103 | return posix_error(); |
| 5104 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5105 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5106 | if (buffer == NULL) |
| 5107 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5108 | if (!_PyVerify_fd(fd)) |
| 5109 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5110 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5111 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5112 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5113 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5114 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5115 | return posix_error(); |
| 5116 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5117 | if (n != size) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5118 | _PyBytes_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5119 | return buffer; |
| 5120 | } |
| 5121 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5123 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5124 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5125 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5128 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5129 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5130 | Py_buffer pbuf; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5131 | int fd; |
| 5132 | Py_ssize_t size; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5133 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 5134 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5135 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5136 | if (!_PyVerify_fd(fd)) |
| 5137 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5138 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5139 | size = write(fd, pbuf.buf, (size_t)pbuf.len); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5140 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5141 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5142 | if (size < 0) |
| 5143 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5144 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5147 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5148 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5149 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5150 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5151 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5152 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5153 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5154 | { |
| 5155 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5156 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5157 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5158 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5159 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5160 | #ifdef __VMS |
| 5161 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5162 | fsync(fd); |
| 5163 | #endif |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5164 | if (!_PyVerify_fd(fd)) |
| 5165 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5166 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5167 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5168 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5169 | if (res != 0) { |
| 5170 | #ifdef MS_WINDOWS |
| 5171 | return win32_error("fstat", NULL); |
| 5172 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5173 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5174 | #endif |
| 5175 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5176 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5177 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5180 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5181 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5182 | 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] | 5183 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5184 | |
| 5185 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5186 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5187 | { |
| 5188 | int fd; |
| 5189 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5190 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5191 | if (!_PyVerify_fd(fd)) |
| 5192 | return PyBool_FromLong(0); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5193 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5194 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5195 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5196 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5197 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5198 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5199 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5200 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5201 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5202 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5203 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5204 | #if defined(PYOS_OS2) |
| 5205 | HFILE read, write; |
| 5206 | APIRET rc; |
| 5207 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5208 | Py_BEGIN_ALLOW_THREADS |
| 5209 | rc = DosCreatePipe( &read, &write, 4096); |
| 5210 | Py_END_ALLOW_THREADS |
| 5211 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5212 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5213 | |
| 5214 | return Py_BuildValue("(ii)", read, write); |
| 5215 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5216 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5217 | int fds[2]; |
| 5218 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5219 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5220 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5221 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5222 | if (res != 0) |
| 5223 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5224 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5225 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5226 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5227 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5228 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5229 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5230 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5231 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5232 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5233 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5234 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5235 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5236 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5237 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5238 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5239 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5240 | #endif /* HAVE_PIPE */ |
| 5241 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5242 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5243 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5244 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5245 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5246 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5247 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5248 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5249 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5250 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5251 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5252 | int mode = 0666; |
| 5253 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5254 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5255 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5256 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5257 | res = mkfifo(filename, mode); |
| 5258 | Py_END_ALLOW_THREADS |
| 5259 | if (res < 0) |
| 5260 | return posix_error(); |
| 5261 | Py_INCREF(Py_None); |
| 5262 | return Py_None; |
| 5263 | } |
| 5264 | #endif |
| 5265 | |
| 5266 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5267 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5268 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5269 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5270 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5271 | named filename. mode specifies both the permissions to use and the\n\ |
| 5272 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5273 | 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] | 5274 | device defines the newly created device special file (probably using\n\ |
| 5275 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5276 | |
| 5277 | |
| 5278 | static PyObject * |
| 5279 | posix_mknod(PyObject *self, PyObject *args) |
| 5280 | { |
| 5281 | char *filename; |
| 5282 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5283 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5284 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5285 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5286 | return NULL; |
| 5287 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5288 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5289 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5290 | if (res < 0) |
| 5291 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5292 | Py_INCREF(Py_None); |
| 5293 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5294 | } |
| 5295 | #endif |
| 5296 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5297 | #ifdef HAVE_DEVICE_MACROS |
| 5298 | PyDoc_STRVAR(posix_major__doc__, |
| 5299 | "major(device) -> major number\n\ |
| 5300 | Extracts a device major number from a raw device number."); |
| 5301 | |
| 5302 | static PyObject * |
| 5303 | posix_major(PyObject *self, PyObject *args) |
| 5304 | { |
| 5305 | int device; |
| 5306 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5307 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5308 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | PyDoc_STRVAR(posix_minor__doc__, |
| 5312 | "minor(device) -> minor number\n\ |
| 5313 | Extracts a device minor number from a raw device number."); |
| 5314 | |
| 5315 | static PyObject * |
| 5316 | posix_minor(PyObject *self, PyObject *args) |
| 5317 | { |
| 5318 | int device; |
| 5319 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5320 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5321 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5325 | "makedev(major, minor) -> device number\n\ |
| 5326 | Composes a raw device number from the major and minor device numbers."); |
| 5327 | |
| 5328 | static PyObject * |
| 5329 | posix_makedev(PyObject *self, PyObject *args) |
| 5330 | { |
| 5331 | int major, minor; |
| 5332 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5333 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5334 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5335 | } |
| 5336 | #endif /* device macros */ |
| 5337 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5338 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5339 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5340 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5341 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5342 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5343 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5344 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5345 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5346 | { |
| 5347 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5348 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5349 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5350 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5351 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5352 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5353 | return NULL; |
| 5354 | |
| 5355 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5356 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5357 | #else |
| 5358 | length = PyLong_Check(lenobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5359 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5360 | #endif |
| 5361 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5362 | return NULL; |
| 5363 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5364 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5365 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5366 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 9053d75 | 2009-01-19 17:53:36 +0000 | [diff] [blame] | 5367 | if (res < 0) |
| 5368 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5369 | Py_INCREF(Py_None); |
| 5370 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5371 | } |
| 5372 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5373 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5374 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5375 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5376 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5377 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5378 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5379 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5380 | * get re-set with another call for the same key. */ |
| 5381 | static PyObject *posix_putenv_garbage; |
| 5382 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5383 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5384 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5385 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5386 | #ifdef MS_WINDOWS |
| 5387 | wchar_t *s1, *s2; |
| 5388 | wchar_t *newenv; |
| 5389 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5390 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5391 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5392 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5393 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5394 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5395 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5396 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5397 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5398 | if (!PyArg_ParseTuple(args, |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5399 | "uu:putenv", |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5400 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5401 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5402 | #else |
| 5403 | if (!PyArg_ParseTuple(args, |
| 5404 | "O&O&:putenv", |
| 5405 | PyUnicode_FSConverter, &os1, |
| 5406 | PyUnicode_FSConverter, &os2)) |
| 5407 | return NULL; |
| 5408 | s1 = bytes2str(os1, 1); |
| 5409 | s2 = bytes2str(os2, 1); |
| 5410 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5411 | |
| 5412 | #if defined(PYOS_OS2) |
| 5413 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5414 | APIRET rc; |
| 5415 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5416 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5417 | if (rc != NO_ERROR) |
| 5418 | return os2_error(rc); |
| 5419 | |
| 5420 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5421 | APIRET rc; |
| 5422 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5423 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5424 | if (rc != NO_ERROR) |
| 5425 | return os2_error(rc); |
| 5426 | } else { |
| 5427 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5428 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5429 | /* len includes space for a trailing \0; the size arg to |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5430 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5431 | #ifdef MS_WINDOWS |
| 5432 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5433 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5434 | #else |
| 5435 | len = strlen(s1) + strlen(s2) + 2; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5436 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5437 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5438 | if (newstr == NULL) |
| 5439 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5440 | #ifdef MS_WINDOWS |
| 5441 | newenv = PyUnicode_AsUnicode(newstr); |
| 5442 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5443 | if (_wputenv(newenv)) { |
| 5444 | Py_DECREF(newstr); |
| 5445 | posix_error(); |
| 5446 | return NULL; |
| 5447 | } |
| 5448 | #else |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5449 | newenv = PyBytes_AS_STRING(newstr); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5450 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5451 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5452 | Py_DECREF(newstr); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5453 | release_bytes(os1); |
| 5454 | release_bytes(os2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5455 | posix_error(); |
| 5456 | return NULL; |
| 5457 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5458 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5459 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5460 | * this will cause previous value to be collected. This has to |
| 5461 | * happen after the real putenv() call because the old value |
| 5462 | * was still accessible until then. */ |
| 5463 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5464 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5465 | /* really not much we can do; just leak */ |
| 5466 | PyErr_Clear(); |
| 5467 | } |
| 5468 | else { |
| 5469 | Py_DECREF(newstr); |
| 5470 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5471 | |
| 5472 | #if defined(PYOS_OS2) |
| 5473 | } |
| 5474 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5475 | #ifndef MS_WINDOWS |
| 5476 | release_bytes(os1); |
| 5477 | release_bytes(os2); |
| 5478 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5479 | Py_INCREF(Py_None); |
| 5480 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5481 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5482 | #endif /* putenv */ |
| 5483 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5484 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5485 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5486 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5487 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5488 | |
| 5489 | static PyObject * |
| 5490 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5491 | { |
| 5492 | char *s1; |
| 5493 | |
| 5494 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5495 | return NULL; |
| 5496 | |
| 5497 | unsetenv(s1); |
| 5498 | |
| 5499 | /* Remove the key from posix_putenv_garbage; |
| 5500 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5501 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5502 | * old value was still accessible until then. |
| 5503 | */ |
| 5504 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5505 | PyTuple_GET_ITEM(args, 0))) { |
| 5506 | /* really not much we can do; just leak */ |
| 5507 | PyErr_Clear(); |
| 5508 | } |
| 5509 | |
| 5510 | Py_INCREF(Py_None); |
| 5511 | return Py_None; |
| 5512 | } |
| 5513 | #endif /* unsetenv */ |
| 5514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5515 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5516 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5517 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5518 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5519 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5520 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5521 | { |
| 5522 | int code; |
| 5523 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5524 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5525 | return NULL; |
| 5526 | message = strerror(code); |
| 5527 | if (message == NULL) { |
| 5528 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5529 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5530 | return NULL; |
| 5531 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5532 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5533 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5534 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5535 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5536 | #ifdef HAVE_SYS_WAIT_H |
| 5537 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5538 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5539 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5540 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5541 | 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] | 5542 | |
| 5543 | static PyObject * |
| 5544 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5545 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5546 | WAIT_TYPE status; |
| 5547 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5548 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5549 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5550 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5551 | |
| 5552 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5553 | } |
| 5554 | #endif /* WCOREDUMP */ |
| 5555 | |
| 5556 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5557 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5558 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5559 | 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] | 5560 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5561 | |
| 5562 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5563 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5564 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5565 | WAIT_TYPE status; |
| 5566 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5567 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5568 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5569 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5570 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5571 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5572 | } |
| 5573 | #endif /* WIFCONTINUED */ |
| 5574 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5575 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5576 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5577 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5578 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5579 | |
| 5580 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5581 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5582 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5583 | WAIT_TYPE status; |
| 5584 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5585 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5586 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5587 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5588 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5589 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5590 | } |
| 5591 | #endif /* WIFSTOPPED */ |
| 5592 | |
| 5593 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5594 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5595 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5596 | 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] | 5597 | |
| 5598 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5599 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5600 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5601 | WAIT_TYPE status; |
| 5602 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5603 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5604 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5605 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5606 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5607 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5608 | } |
| 5609 | #endif /* WIFSIGNALED */ |
| 5610 | |
| 5611 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5612 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5613 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5614 | 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] | 5615 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5616 | |
| 5617 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5618 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5619 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5620 | WAIT_TYPE status; |
| 5621 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5622 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5623 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5624 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5625 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5626 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5627 | } |
| 5628 | #endif /* WIFEXITED */ |
| 5629 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5630 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5631 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5632 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5633 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5634 | |
| 5635 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5636 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5637 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5638 | WAIT_TYPE status; |
| 5639 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5640 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5641 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5642 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5643 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5644 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5645 | } |
| 5646 | #endif /* WEXITSTATUS */ |
| 5647 | |
| 5648 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5649 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5650 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5651 | 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] | 5652 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5653 | |
| 5654 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5655 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5656 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5657 | WAIT_TYPE status; |
| 5658 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5659 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5660 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5661 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5662 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5663 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5664 | } |
| 5665 | #endif /* WTERMSIG */ |
| 5666 | |
| 5667 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5668 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5669 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5670 | Return the signal that stopped the process that provided\n\ |
| 5671 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5672 | |
| 5673 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5674 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5675 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5676 | WAIT_TYPE status; |
| 5677 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5678 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5679 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5680 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5681 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5682 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5683 | } |
| 5684 | #endif /* WSTOPSIG */ |
| 5685 | |
| 5686 | #endif /* HAVE_SYS_WAIT_H */ |
| 5687 | |
| 5688 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5689 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5690 | #ifdef _SCO_DS |
| 5691 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5692 | needed definitions in sys/statvfs.h */ |
| 5693 | #define _SVID3 |
| 5694 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5695 | #include <sys/statvfs.h> |
| 5696 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5697 | static PyObject* |
| 5698 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5699 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5700 | if (v == NULL) |
| 5701 | return NULL; |
| 5702 | |
| 5703 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5704 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5705 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5706 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5707 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5708 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5709 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5710 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5711 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 5712 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5713 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5714 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5715 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5716 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5717 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5718 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5719 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5720 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5721 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5722 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5723 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5724 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5725 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5726 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5727 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5728 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5729 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5730 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5731 | #endif |
| 5732 | |
| 5733 | return v; |
| 5734 | } |
| 5735 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5736 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5737 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5738 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5739 | |
| 5740 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5741 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5742 | { |
| 5743 | int fd, res; |
| 5744 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5745 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5746 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5747 | return NULL; |
| 5748 | Py_BEGIN_ALLOW_THREADS |
| 5749 | res = fstatvfs(fd, &st); |
| 5750 | Py_END_ALLOW_THREADS |
| 5751 | if (res != 0) |
| 5752 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5753 | |
| 5754 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5755 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5756 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5757 | |
| 5758 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5759 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5760 | #include <sys/statvfs.h> |
| 5761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5762 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5763 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5764 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5765 | |
| 5766 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5767 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5768 | { |
| 5769 | char *path; |
| 5770 | int res; |
| 5771 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5772 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5773 | return NULL; |
| 5774 | Py_BEGIN_ALLOW_THREADS |
| 5775 | res = statvfs(path, &st); |
| 5776 | Py_END_ALLOW_THREADS |
| 5777 | if (res != 0) |
| 5778 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5779 | |
| 5780 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5781 | } |
| 5782 | #endif /* HAVE_STATVFS */ |
| 5783 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5784 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5785 | * It maps strings representing configuration variable names to |
| 5786 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5787 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5788 | * rarely-used constants. There are three separate tables that use |
| 5789 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5790 | * |
| 5791 | * This code is always included, even if none of the interfaces that |
| 5792 | * need it are included. The #if hackery needed to avoid it would be |
| 5793 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5794 | */ |
| 5795 | struct constdef { |
| 5796 | char *name; |
| 5797 | long value; |
| 5798 | }; |
| 5799 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5800 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5801 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5802 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5803 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5804 | if (PyLong_Check(arg)) { |
| 5805 | *valuep = PyLong_AS_LONG(arg); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5806 | return 1; |
| 5807 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5808 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5809 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5810 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5811 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5812 | size_t hi = tablesize; |
| 5813 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5814 | const char *confname; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5815 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5816 | PyErr_SetString(PyExc_TypeError, |
| 5817 | "configuration names must be strings or integers"); |
| 5818 | return 0; |
| 5819 | } |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 5820 | confname = _PyUnicode_AsString(arg); |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5821 | if (confname == NULL) |
| 5822 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5823 | while (lo < hi) { |
| 5824 | mid = (lo + hi) / 2; |
| 5825 | cmp = strcmp(confname, table[mid].name); |
| 5826 | if (cmp < 0) |
| 5827 | hi = mid; |
| 5828 | else if (cmp > 0) |
| 5829 | lo = mid + 1; |
| 5830 | else { |
| 5831 | *valuep = table[mid].value; |
| 5832 | return 1; |
| 5833 | } |
| 5834 | } |
| 5835 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5836 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5837 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | |
| 5841 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5842 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5843 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5844 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5845 | #endif |
| 5846 | #ifdef _PC_ABI_ASYNC_IO |
| 5847 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5848 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5849 | #ifdef _PC_ASYNC_IO |
| 5850 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5851 | #endif |
| 5852 | #ifdef _PC_CHOWN_RESTRICTED |
| 5853 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5854 | #endif |
| 5855 | #ifdef _PC_FILESIZEBITS |
| 5856 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5857 | #endif |
| 5858 | #ifdef _PC_LAST |
| 5859 | {"PC_LAST", _PC_LAST}, |
| 5860 | #endif |
| 5861 | #ifdef _PC_LINK_MAX |
| 5862 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5863 | #endif |
| 5864 | #ifdef _PC_MAX_CANON |
| 5865 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5866 | #endif |
| 5867 | #ifdef _PC_MAX_INPUT |
| 5868 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5869 | #endif |
| 5870 | #ifdef _PC_NAME_MAX |
| 5871 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5872 | #endif |
| 5873 | #ifdef _PC_NO_TRUNC |
| 5874 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5875 | #endif |
| 5876 | #ifdef _PC_PATH_MAX |
| 5877 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5878 | #endif |
| 5879 | #ifdef _PC_PIPE_BUF |
| 5880 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5881 | #endif |
| 5882 | #ifdef _PC_PRIO_IO |
| 5883 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5884 | #endif |
| 5885 | #ifdef _PC_SOCK_MAXBUF |
| 5886 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5887 | #endif |
| 5888 | #ifdef _PC_SYNC_IO |
| 5889 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5890 | #endif |
| 5891 | #ifdef _PC_VDISABLE |
| 5892 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5893 | #endif |
| 5894 | }; |
| 5895 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5896 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5897 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5898 | { |
| 5899 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5900 | sizeof(posix_constants_pathconf) |
| 5901 | / sizeof(struct constdef)); |
| 5902 | } |
| 5903 | #endif |
| 5904 | |
| 5905 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5906 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5907 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5908 | 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] | 5909 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5910 | |
| 5911 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5912 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5913 | { |
| 5914 | PyObject *result = NULL; |
| 5915 | int name, fd; |
| 5916 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5917 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5918 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5919 | long limit; |
| 5920 | |
| 5921 | errno = 0; |
| 5922 | limit = fpathconf(fd, name); |
| 5923 | if (limit == -1 && errno != 0) |
| 5924 | posix_error(); |
| 5925 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5926 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5927 | } |
| 5928 | return result; |
| 5929 | } |
| 5930 | #endif |
| 5931 | |
| 5932 | |
| 5933 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5934 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5935 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5936 | 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] | 5937 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5938 | |
| 5939 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5940 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5941 | { |
| 5942 | PyObject *result = NULL; |
| 5943 | int name; |
| 5944 | char *path; |
| 5945 | |
| 5946 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5947 | conv_path_confname, &name)) { |
| 5948 | long limit; |
| 5949 | |
| 5950 | errno = 0; |
| 5951 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5952 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5953 | if (errno == EINVAL) |
| 5954 | /* could be a path or name problem */ |
| 5955 | posix_error(); |
| 5956 | else |
| 5957 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5958 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5959 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5960 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5961 | } |
| 5962 | return result; |
| 5963 | } |
| 5964 | #endif |
| 5965 | |
| 5966 | #ifdef HAVE_CONFSTR |
| 5967 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5968 | #ifdef _CS_ARCHITECTURE |
| 5969 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5970 | #endif |
| 5971 | #ifdef _CS_HOSTNAME |
| 5972 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5973 | #endif |
| 5974 | #ifdef _CS_HW_PROVIDER |
| 5975 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5976 | #endif |
| 5977 | #ifdef _CS_HW_SERIAL |
| 5978 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5979 | #endif |
| 5980 | #ifdef _CS_INITTAB_NAME |
| 5981 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5982 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5983 | #ifdef _CS_LFS64_CFLAGS |
| 5984 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5985 | #endif |
| 5986 | #ifdef _CS_LFS64_LDFLAGS |
| 5987 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5988 | #endif |
| 5989 | #ifdef _CS_LFS64_LIBS |
| 5990 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5991 | #endif |
| 5992 | #ifdef _CS_LFS64_LINTFLAGS |
| 5993 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5994 | #endif |
| 5995 | #ifdef _CS_LFS_CFLAGS |
| 5996 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5997 | #endif |
| 5998 | #ifdef _CS_LFS_LDFLAGS |
| 5999 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6000 | #endif |
| 6001 | #ifdef _CS_LFS_LIBS |
| 6002 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6003 | #endif |
| 6004 | #ifdef _CS_LFS_LINTFLAGS |
| 6005 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6006 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6007 | #ifdef _CS_MACHINE |
| 6008 | {"CS_MACHINE", _CS_MACHINE}, |
| 6009 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6010 | #ifdef _CS_PATH |
| 6011 | {"CS_PATH", _CS_PATH}, |
| 6012 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6013 | #ifdef _CS_RELEASE |
| 6014 | {"CS_RELEASE", _CS_RELEASE}, |
| 6015 | #endif |
| 6016 | #ifdef _CS_SRPC_DOMAIN |
| 6017 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6018 | #endif |
| 6019 | #ifdef _CS_SYSNAME |
| 6020 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6021 | #endif |
| 6022 | #ifdef _CS_VERSION |
| 6023 | {"CS_VERSION", _CS_VERSION}, |
| 6024 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6025 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6026 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6027 | #endif |
| 6028 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6029 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6030 | #endif |
| 6031 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6032 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6033 | #endif |
| 6034 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6035 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6036 | #endif |
| 6037 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6038 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6039 | #endif |
| 6040 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6041 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6042 | #endif |
| 6043 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6044 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6045 | #endif |
| 6046 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6047 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6048 | #endif |
| 6049 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6050 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6051 | #endif |
| 6052 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6053 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6054 | #endif |
| 6055 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6056 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6057 | #endif |
| 6058 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6059 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6060 | #endif |
| 6061 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6062 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6063 | #endif |
| 6064 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6065 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6066 | #endif |
| 6067 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6068 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6069 | #endif |
| 6070 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6071 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6072 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6073 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6074 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6075 | #endif |
| 6076 | #ifdef _MIPS_CS_BASE |
| 6077 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6078 | #endif |
| 6079 | #ifdef _MIPS_CS_HOSTID |
| 6080 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6081 | #endif |
| 6082 | #ifdef _MIPS_CS_HW_NAME |
| 6083 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6084 | #endif |
| 6085 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6086 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6087 | #endif |
| 6088 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6089 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6090 | #endif |
| 6091 | #ifdef _MIPS_CS_OSREL_MIN |
| 6092 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6093 | #endif |
| 6094 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6095 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6096 | #endif |
| 6097 | #ifdef _MIPS_CS_OS_NAME |
| 6098 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6099 | #endif |
| 6100 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6101 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6102 | #endif |
| 6103 | #ifdef _MIPS_CS_PROCESSORS |
| 6104 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6105 | #endif |
| 6106 | #ifdef _MIPS_CS_SERIAL |
| 6107 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6108 | #endif |
| 6109 | #ifdef _MIPS_CS_VENDOR |
| 6110 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6111 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6112 | }; |
| 6113 | |
| 6114 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6115 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6116 | { |
| 6117 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6118 | sizeof(posix_constants_confstr) |
| 6119 | / sizeof(struct constdef)); |
| 6120 | } |
| 6121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6122 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6123 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6124 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6125 | |
| 6126 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6127 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6128 | { |
| 6129 | PyObject *result = NULL; |
| 6130 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6131 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6132 | |
| 6133 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6134 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6135 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6136 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6137 | len = confstr(name, buffer, sizeof(buffer)); |
| 6138 | if (len == 0) { |
| 6139 | if (errno) { |
| 6140 | posix_error(); |
| 6141 | } |
| 6142 | else { |
| 6143 | result = Py_None; |
| 6144 | Py_INCREF(Py_None); |
| 6145 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6146 | } |
| 6147 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6148 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6149 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6150 | if (result != NULL) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 6151 | confstr(name, _PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6152 | } |
| 6153 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6154 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6155 | } |
| 6156 | } |
| 6157 | return result; |
| 6158 | } |
| 6159 | #endif |
| 6160 | |
| 6161 | |
| 6162 | #ifdef HAVE_SYSCONF |
| 6163 | static struct constdef posix_constants_sysconf[] = { |
| 6164 | #ifdef _SC_2_CHAR_TERM |
| 6165 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6166 | #endif |
| 6167 | #ifdef _SC_2_C_BIND |
| 6168 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6169 | #endif |
| 6170 | #ifdef _SC_2_C_DEV |
| 6171 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6172 | #endif |
| 6173 | #ifdef _SC_2_C_VERSION |
| 6174 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6175 | #endif |
| 6176 | #ifdef _SC_2_FORT_DEV |
| 6177 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6178 | #endif |
| 6179 | #ifdef _SC_2_FORT_RUN |
| 6180 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6181 | #endif |
| 6182 | #ifdef _SC_2_LOCALEDEF |
| 6183 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6184 | #endif |
| 6185 | #ifdef _SC_2_SW_DEV |
| 6186 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6187 | #endif |
| 6188 | #ifdef _SC_2_UPE |
| 6189 | {"SC_2_UPE", _SC_2_UPE}, |
| 6190 | #endif |
| 6191 | #ifdef _SC_2_VERSION |
| 6192 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6193 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6194 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6195 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6196 | #endif |
| 6197 | #ifdef _SC_ACL |
| 6198 | {"SC_ACL", _SC_ACL}, |
| 6199 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6200 | #ifdef _SC_AIO_LISTIO_MAX |
| 6201 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6202 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6203 | #ifdef _SC_AIO_MAX |
| 6204 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6205 | #endif |
| 6206 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6207 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6208 | #endif |
| 6209 | #ifdef _SC_ARG_MAX |
| 6210 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6211 | #endif |
| 6212 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6213 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6214 | #endif |
| 6215 | #ifdef _SC_ATEXIT_MAX |
| 6216 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6217 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6218 | #ifdef _SC_AUDIT |
| 6219 | {"SC_AUDIT", _SC_AUDIT}, |
| 6220 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6221 | #ifdef _SC_AVPHYS_PAGES |
| 6222 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6223 | #endif |
| 6224 | #ifdef _SC_BC_BASE_MAX |
| 6225 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6226 | #endif |
| 6227 | #ifdef _SC_BC_DIM_MAX |
| 6228 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6229 | #endif |
| 6230 | #ifdef _SC_BC_SCALE_MAX |
| 6231 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6232 | #endif |
| 6233 | #ifdef _SC_BC_STRING_MAX |
| 6234 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6235 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6236 | #ifdef _SC_CAP |
| 6237 | {"SC_CAP", _SC_CAP}, |
| 6238 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6239 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6240 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6241 | #endif |
| 6242 | #ifdef _SC_CHAR_BIT |
| 6243 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6244 | #endif |
| 6245 | #ifdef _SC_CHAR_MAX |
| 6246 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6247 | #endif |
| 6248 | #ifdef _SC_CHAR_MIN |
| 6249 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6250 | #endif |
| 6251 | #ifdef _SC_CHILD_MAX |
| 6252 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6253 | #endif |
| 6254 | #ifdef _SC_CLK_TCK |
| 6255 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6256 | #endif |
| 6257 | #ifdef _SC_COHER_BLKSZ |
| 6258 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6259 | #endif |
| 6260 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6261 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6262 | #endif |
| 6263 | #ifdef _SC_DCACHE_ASSOC |
| 6264 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6265 | #endif |
| 6266 | #ifdef _SC_DCACHE_BLKSZ |
| 6267 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6268 | #endif |
| 6269 | #ifdef _SC_DCACHE_LINESZ |
| 6270 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6271 | #endif |
| 6272 | #ifdef _SC_DCACHE_SZ |
| 6273 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6274 | #endif |
| 6275 | #ifdef _SC_DCACHE_TBLKSZ |
| 6276 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6277 | #endif |
| 6278 | #ifdef _SC_DELAYTIMER_MAX |
| 6279 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6280 | #endif |
| 6281 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6282 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6283 | #endif |
| 6284 | #ifdef _SC_EXPR_NEST_MAX |
| 6285 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6286 | #endif |
| 6287 | #ifdef _SC_FSYNC |
| 6288 | {"SC_FSYNC", _SC_FSYNC}, |
| 6289 | #endif |
| 6290 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6291 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6292 | #endif |
| 6293 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6294 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6295 | #endif |
| 6296 | #ifdef _SC_ICACHE_ASSOC |
| 6297 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6298 | #endif |
| 6299 | #ifdef _SC_ICACHE_BLKSZ |
| 6300 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6301 | #endif |
| 6302 | #ifdef _SC_ICACHE_LINESZ |
| 6303 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6304 | #endif |
| 6305 | #ifdef _SC_ICACHE_SZ |
| 6306 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6307 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6308 | #ifdef _SC_INF |
| 6309 | {"SC_INF", _SC_INF}, |
| 6310 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6311 | #ifdef _SC_INT_MAX |
| 6312 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6313 | #endif |
| 6314 | #ifdef _SC_INT_MIN |
| 6315 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6316 | #endif |
| 6317 | #ifdef _SC_IOV_MAX |
| 6318 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6319 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6320 | #ifdef _SC_IP_SECOPTS |
| 6321 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6322 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6323 | #ifdef _SC_JOB_CONTROL |
| 6324 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6325 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6326 | #ifdef _SC_KERN_POINTERS |
| 6327 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6328 | #endif |
| 6329 | #ifdef _SC_KERN_SIM |
| 6330 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6331 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6332 | #ifdef _SC_LINE_MAX |
| 6333 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6334 | #endif |
| 6335 | #ifdef _SC_LOGIN_NAME_MAX |
| 6336 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6337 | #endif |
| 6338 | #ifdef _SC_LOGNAME_MAX |
| 6339 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6340 | #endif |
| 6341 | #ifdef _SC_LONG_BIT |
| 6342 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6343 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6344 | #ifdef _SC_MAC |
| 6345 | {"SC_MAC", _SC_MAC}, |
| 6346 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6347 | #ifdef _SC_MAPPED_FILES |
| 6348 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6349 | #endif |
| 6350 | #ifdef _SC_MAXPID |
| 6351 | {"SC_MAXPID", _SC_MAXPID}, |
| 6352 | #endif |
| 6353 | #ifdef _SC_MB_LEN_MAX |
| 6354 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6355 | #endif |
| 6356 | #ifdef _SC_MEMLOCK |
| 6357 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6358 | #endif |
| 6359 | #ifdef _SC_MEMLOCK_RANGE |
| 6360 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6361 | #endif |
| 6362 | #ifdef _SC_MEMORY_PROTECTION |
| 6363 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6364 | #endif |
| 6365 | #ifdef _SC_MESSAGE_PASSING |
| 6366 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6367 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6368 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6369 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6370 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6371 | #ifdef _SC_MQ_OPEN_MAX |
| 6372 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6373 | #endif |
| 6374 | #ifdef _SC_MQ_PRIO_MAX |
| 6375 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6376 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6377 | #ifdef _SC_NACLS_MAX |
| 6378 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6379 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6380 | #ifdef _SC_NGROUPS_MAX |
| 6381 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6382 | #endif |
| 6383 | #ifdef _SC_NL_ARGMAX |
| 6384 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6385 | #endif |
| 6386 | #ifdef _SC_NL_LANGMAX |
| 6387 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6388 | #endif |
| 6389 | #ifdef _SC_NL_MSGMAX |
| 6390 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6391 | #endif |
| 6392 | #ifdef _SC_NL_NMAX |
| 6393 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6394 | #endif |
| 6395 | #ifdef _SC_NL_SETMAX |
| 6396 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6397 | #endif |
| 6398 | #ifdef _SC_NL_TEXTMAX |
| 6399 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6400 | #endif |
| 6401 | #ifdef _SC_NPROCESSORS_CONF |
| 6402 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6403 | #endif |
| 6404 | #ifdef _SC_NPROCESSORS_ONLN |
| 6405 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6406 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6407 | #ifdef _SC_NPROC_CONF |
| 6408 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6409 | #endif |
| 6410 | #ifdef _SC_NPROC_ONLN |
| 6411 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6412 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6413 | #ifdef _SC_NZERO |
| 6414 | {"SC_NZERO", _SC_NZERO}, |
| 6415 | #endif |
| 6416 | #ifdef _SC_OPEN_MAX |
| 6417 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6418 | #endif |
| 6419 | #ifdef _SC_PAGESIZE |
| 6420 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6421 | #endif |
| 6422 | #ifdef _SC_PAGE_SIZE |
| 6423 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6424 | #endif |
| 6425 | #ifdef _SC_PASS_MAX |
| 6426 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6427 | #endif |
| 6428 | #ifdef _SC_PHYS_PAGES |
| 6429 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6430 | #endif |
| 6431 | #ifdef _SC_PII |
| 6432 | {"SC_PII", _SC_PII}, |
| 6433 | #endif |
| 6434 | #ifdef _SC_PII_INTERNET |
| 6435 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6436 | #endif |
| 6437 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6438 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6439 | #endif |
| 6440 | #ifdef _SC_PII_INTERNET_STREAM |
| 6441 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6442 | #endif |
| 6443 | #ifdef _SC_PII_OSI |
| 6444 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6445 | #endif |
| 6446 | #ifdef _SC_PII_OSI_CLTS |
| 6447 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6448 | #endif |
| 6449 | #ifdef _SC_PII_OSI_COTS |
| 6450 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6451 | #endif |
| 6452 | #ifdef _SC_PII_OSI_M |
| 6453 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6454 | #endif |
| 6455 | #ifdef _SC_PII_SOCKET |
| 6456 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6457 | #endif |
| 6458 | #ifdef _SC_PII_XTI |
| 6459 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6460 | #endif |
| 6461 | #ifdef _SC_POLL |
| 6462 | {"SC_POLL", _SC_POLL}, |
| 6463 | #endif |
| 6464 | #ifdef _SC_PRIORITIZED_IO |
| 6465 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6466 | #endif |
| 6467 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6468 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6469 | #endif |
| 6470 | #ifdef _SC_REALTIME_SIGNALS |
| 6471 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6472 | #endif |
| 6473 | #ifdef _SC_RE_DUP_MAX |
| 6474 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6475 | #endif |
| 6476 | #ifdef _SC_RTSIG_MAX |
| 6477 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6478 | #endif |
| 6479 | #ifdef _SC_SAVED_IDS |
| 6480 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6481 | #endif |
| 6482 | #ifdef _SC_SCHAR_MAX |
| 6483 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6484 | #endif |
| 6485 | #ifdef _SC_SCHAR_MIN |
| 6486 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6487 | #endif |
| 6488 | #ifdef _SC_SELECT |
| 6489 | {"SC_SELECT", _SC_SELECT}, |
| 6490 | #endif |
| 6491 | #ifdef _SC_SEMAPHORES |
| 6492 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6493 | #endif |
| 6494 | #ifdef _SC_SEM_NSEMS_MAX |
| 6495 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6496 | #endif |
| 6497 | #ifdef _SC_SEM_VALUE_MAX |
| 6498 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6499 | #endif |
| 6500 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6501 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6502 | #endif |
| 6503 | #ifdef _SC_SHRT_MAX |
| 6504 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6505 | #endif |
| 6506 | #ifdef _SC_SHRT_MIN |
| 6507 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6508 | #endif |
| 6509 | #ifdef _SC_SIGQUEUE_MAX |
| 6510 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6511 | #endif |
| 6512 | #ifdef _SC_SIGRT_MAX |
| 6513 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6514 | #endif |
| 6515 | #ifdef _SC_SIGRT_MIN |
| 6516 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6517 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6518 | #ifdef _SC_SOFTPOWER |
| 6519 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6520 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6521 | #ifdef _SC_SPLIT_CACHE |
| 6522 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6523 | #endif |
| 6524 | #ifdef _SC_SSIZE_MAX |
| 6525 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6526 | #endif |
| 6527 | #ifdef _SC_STACK_PROT |
| 6528 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6529 | #endif |
| 6530 | #ifdef _SC_STREAM_MAX |
| 6531 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6532 | #endif |
| 6533 | #ifdef _SC_SYNCHRONIZED_IO |
| 6534 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6535 | #endif |
| 6536 | #ifdef _SC_THREADS |
| 6537 | {"SC_THREADS", _SC_THREADS}, |
| 6538 | #endif |
| 6539 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6540 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6541 | #endif |
| 6542 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6543 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6544 | #endif |
| 6545 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6546 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6547 | #endif |
| 6548 | #ifdef _SC_THREAD_KEYS_MAX |
| 6549 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6550 | #endif |
| 6551 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6552 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6553 | #endif |
| 6554 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6555 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6556 | #endif |
| 6557 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6558 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6559 | #endif |
| 6560 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6561 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6562 | #endif |
| 6563 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6564 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6565 | #endif |
| 6566 | #ifdef _SC_THREAD_STACK_MIN |
| 6567 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6568 | #endif |
| 6569 | #ifdef _SC_THREAD_THREADS_MAX |
| 6570 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6571 | #endif |
| 6572 | #ifdef _SC_TIMERS |
| 6573 | {"SC_TIMERS", _SC_TIMERS}, |
| 6574 | #endif |
| 6575 | #ifdef _SC_TIMER_MAX |
| 6576 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6577 | #endif |
| 6578 | #ifdef _SC_TTY_NAME_MAX |
| 6579 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6580 | #endif |
| 6581 | #ifdef _SC_TZNAME_MAX |
| 6582 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6583 | #endif |
| 6584 | #ifdef _SC_T_IOV_MAX |
| 6585 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6586 | #endif |
| 6587 | #ifdef _SC_UCHAR_MAX |
| 6588 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6589 | #endif |
| 6590 | #ifdef _SC_UINT_MAX |
| 6591 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6592 | #endif |
| 6593 | #ifdef _SC_UIO_MAXIOV |
| 6594 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6595 | #endif |
| 6596 | #ifdef _SC_ULONG_MAX |
| 6597 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6598 | #endif |
| 6599 | #ifdef _SC_USHRT_MAX |
| 6600 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6601 | #endif |
| 6602 | #ifdef _SC_VERSION |
| 6603 | {"SC_VERSION", _SC_VERSION}, |
| 6604 | #endif |
| 6605 | #ifdef _SC_WORD_BIT |
| 6606 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6607 | #endif |
| 6608 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6609 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6610 | #endif |
| 6611 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6612 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6613 | #endif |
| 6614 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6615 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6616 | #endif |
| 6617 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6618 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6619 | #endif |
| 6620 | #ifdef _SC_XOPEN_CRYPT |
| 6621 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6622 | #endif |
| 6623 | #ifdef _SC_XOPEN_ENH_I18N |
| 6624 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6625 | #endif |
| 6626 | #ifdef _SC_XOPEN_LEGACY |
| 6627 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6628 | #endif |
| 6629 | #ifdef _SC_XOPEN_REALTIME |
| 6630 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6631 | #endif |
| 6632 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6633 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6634 | #endif |
| 6635 | #ifdef _SC_XOPEN_SHM |
| 6636 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6637 | #endif |
| 6638 | #ifdef _SC_XOPEN_UNIX |
| 6639 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6640 | #endif |
| 6641 | #ifdef _SC_XOPEN_VERSION |
| 6642 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6643 | #endif |
| 6644 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6645 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6646 | #endif |
| 6647 | #ifdef _SC_XOPEN_XPG2 |
| 6648 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6649 | #endif |
| 6650 | #ifdef _SC_XOPEN_XPG3 |
| 6651 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6652 | #endif |
| 6653 | #ifdef _SC_XOPEN_XPG4 |
| 6654 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6655 | #endif |
| 6656 | }; |
| 6657 | |
| 6658 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6659 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6660 | { |
| 6661 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6662 | sizeof(posix_constants_sysconf) |
| 6663 | / sizeof(struct constdef)); |
| 6664 | } |
| 6665 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6666 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6667 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6668 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6669 | |
| 6670 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6671 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6672 | { |
| 6673 | PyObject *result = NULL; |
| 6674 | int name; |
| 6675 | |
| 6676 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6677 | int value; |
| 6678 | |
| 6679 | errno = 0; |
| 6680 | value = sysconf(name); |
| 6681 | if (value == -1 && errno != 0) |
| 6682 | posix_error(); |
| 6683 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6684 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6685 | } |
| 6686 | return result; |
| 6687 | } |
| 6688 | #endif |
| 6689 | |
| 6690 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6691 | /* This code is used to ensure that the tables of configuration value names |
| 6692 | * are in sorted order as required by conv_confname(), and also to build the |
| 6693 | * the exported dictionaries that are used to publish information about the |
| 6694 | * names available on the host platform. |
| 6695 | * |
| 6696 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6697 | * when used, even for platforms we're not able to test on. It also makes |
| 6698 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6699 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6700 | |
| 6701 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6702 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6703 | { |
| 6704 | const struct constdef *c1 = |
| 6705 | (const struct constdef *) v1; |
| 6706 | const struct constdef *c2 = |
| 6707 | (const struct constdef *) v2; |
| 6708 | |
| 6709 | return strcmp(c1->name, c2->name); |
| 6710 | } |
| 6711 | |
| 6712 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6713 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6714 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6715 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6716 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6717 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6718 | |
| 6719 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6720 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6721 | if (d == NULL) |
| 6722 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6723 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6724 | for (i=0; i < tablesize; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6725 | PyObject *o = PyLong_FromLong(table[i].value); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6726 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6727 | Py_XDECREF(o); |
| 6728 | Py_DECREF(d); |
| 6729 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6730 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6731 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6732 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6733 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6734 | } |
| 6735 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6736 | /* Return -1 on failure, 0 on success. */ |
| 6737 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6738 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6739 | { |
| 6740 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6741 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6742 | sizeof(posix_constants_pathconf) |
| 6743 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6744 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6745 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6746 | #endif |
| 6747 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6748 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6749 | sizeof(posix_constants_confstr) |
| 6750 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6751 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6752 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6753 | #endif |
| 6754 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6755 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6756 | sizeof(posix_constants_sysconf) |
| 6757 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6758 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6759 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6760 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6761 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6762 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6763 | |
| 6764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6765 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6766 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6767 | 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] | 6768 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6769 | |
| 6770 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6771 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6772 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6773 | abort(); |
| 6774 | /*NOTREACHED*/ |
| 6775 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6776 | return NULL; |
| 6777 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6778 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6779 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6780 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6781 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6782 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6783 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6784 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6785 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6786 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6787 | application (if any) its extension is associated.\n\ |
| 6788 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6789 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6790 | \n\ |
| 6791 | startfile returns as soon as the associated application is launched.\n\ |
| 6792 | There is no option to wait for the application to close, and no way\n\ |
| 6793 | to retrieve the application's exit status.\n\ |
| 6794 | \n\ |
| 6795 | The filepath is relative to the current directory. If you want to use\n\ |
| 6796 | 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] | 6797 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6798 | |
| 6799 | static PyObject * |
| 6800 | win32_startfile(PyObject *self, PyObject *args) |
| 6801 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6802 | PyObject *ofilepath; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6803 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6804 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6805 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6806 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6807 | if (unicode_file_names()) { |
| 6808 | PyObject *unipath, *woperation = NULL; |
| 6809 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6810 | &unipath, &operation)) { |
| 6811 | PyErr_Clear(); |
| 6812 | goto normal; |
| 6813 | } |
| 6814 | |
| 6815 | |
| 6816 | if (operation) { |
| 6817 | woperation = PyUnicode_DecodeASCII(operation, |
| 6818 | strlen(operation), NULL); |
| 6819 | if (!woperation) { |
| 6820 | PyErr_Clear(); |
| 6821 | operation = NULL; |
| 6822 | goto normal; |
| 6823 | } |
| 6824 | } |
| 6825 | |
| 6826 | Py_BEGIN_ALLOW_THREADS |
| 6827 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6828 | PyUnicode_AS_UNICODE(unipath), |
| 6829 | NULL, NULL, SW_SHOWNORMAL); |
| 6830 | Py_END_ALLOW_THREADS |
| 6831 | |
| 6832 | Py_XDECREF(woperation); |
| 6833 | if (rc <= (HINSTANCE)32) { |
| 6834 | PyObject *errval = win32_error_unicode("startfile", |
| 6835 | PyUnicode_AS_UNICODE(unipath)); |
| 6836 | return errval; |
| 6837 | } |
| 6838 | Py_INCREF(Py_None); |
| 6839 | return Py_None; |
| 6840 | } |
| 6841 | #endif |
| 6842 | |
| 6843 | normal: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6844 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 6845 | PyUnicode_FSConverter, &ofilepath, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6846 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6847 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6848 | filepath = bytes2str(ofilepath, 1); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6849 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6850 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6851 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6852 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6853 | if (rc <= (HINSTANCE)32) { |
| 6854 | PyObject *errval = win32_error("startfile", filepath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6855 | release_bytes(ofilepath); |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6856 | return errval; |
| 6857 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6858 | release_bytes(ofilepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6859 | Py_INCREF(Py_None); |
| 6860 | return Py_None; |
| 6861 | } |
| 6862 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6863 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6864 | #ifdef HAVE_GETLOADAVG |
| 6865 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6866 | "getloadavg() -> (float, float, float)\n\n\ |
| 6867 | Return the number of processes in the system run queue averaged over\n\ |
| 6868 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6869 | was unobtainable"); |
| 6870 | |
| 6871 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6872 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6873 | { |
| 6874 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6875 | if (getloadavg(loadavg, 3)!=3) { |
| 6876 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6877 | return NULL; |
| 6878 | } else |
| 6879 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6880 | } |
| 6881 | #endif |
| 6882 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6883 | #ifdef MS_WINDOWS |
| 6884 | |
| 6885 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6886 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6887 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6888 | |
| 6889 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6890 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6891 | DWORD dwFlags ); |
| 6892 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6893 | BYTE *pbBuffer ); |
| 6894 | |
| 6895 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6896 | /* This handle is never explicitly released. Instead, the operating |
| 6897 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6898 | static HCRYPTPROV hCryptProv = 0; |
| 6899 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6900 | static PyObject* |
| 6901 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6902 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6903 | int howMany; |
| 6904 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6905 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6906 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6907 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6908 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6909 | if (howMany < 0) |
| 6910 | return PyErr_Format(PyExc_ValueError, |
| 6911 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6912 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6913 | if (hCryptProv == 0) { |
| 6914 | HINSTANCE hAdvAPI32 = NULL; |
| 6915 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6916 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6917 | /* Obtain handle to the DLL containing CryptoAPI |
| 6918 | This should not fail */ |
| 6919 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6920 | if(hAdvAPI32 == NULL) |
| 6921 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6922 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6923 | /* Obtain pointers to the CryptoAPI functions |
| 6924 | This will fail on some early versions of Win95 */ |
| 6925 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6926 | hAdvAPI32, |
| 6927 | "CryptAcquireContextA"); |
| 6928 | if (pCryptAcquireContext == NULL) |
| 6929 | return PyErr_Format(PyExc_NotImplementedError, |
| 6930 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6931 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6932 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6933 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6934 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6935 | return PyErr_Format(PyExc_NotImplementedError, |
| 6936 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6937 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6938 | /* Acquire context */ |
| 6939 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6940 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6941 | return win32_error("CryptAcquireContext", NULL); |
| 6942 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6943 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6944 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6945 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6946 | if (result != NULL) { |
| 6947 | /* Get random data */ |
Amaury Forgeot d'Arc | a05ada3 | 2008-07-21 21:13:14 +0000 | [diff] [blame] | 6948 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6949 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6950 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6951 | Py_DECREF(result); |
| 6952 | return win32_error("CryptGenRandom", NULL); |
| 6953 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6954 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6955 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6956 | } |
| 6957 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6958 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6959 | PyDoc_STRVAR(device_encoding__doc__, |
| 6960 | "device_encoding(fd) -> str\n\n\ |
| 6961 | Return a string describing the encoding of the device\n\ |
| 6962 | if the output is a terminal; else return None."); |
| 6963 | |
| 6964 | static PyObject * |
| 6965 | device_encoding(PyObject *self, PyObject *args) |
| 6966 | { |
| 6967 | int fd; |
| 6968 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6969 | return NULL; |
Kristján Valur Jónsson | 649170b | 2009-03-24 14:15:49 +0000 | [diff] [blame] | 6970 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6971 | Py_INCREF(Py_None); |
| 6972 | return Py_None; |
| 6973 | } |
| 6974 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6975 | if (fd == 0) { |
| 6976 | char buf[100]; |
| 6977 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6978 | return PyUnicode_FromString(buf); |
| 6979 | } |
| 6980 | if (fd == 1 || fd == 2) { |
| 6981 | char buf[100]; |
| 6982 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6983 | return PyUnicode_FromString(buf); |
| 6984 | } |
| 6985 | #elif defined(CODESET) |
| 6986 | { |
| 6987 | char *codeset = nl_langinfo(CODESET); |
Mark Dickinson | da2706b | 2008-12-11 18:03:03 +0000 | [diff] [blame] | 6988 | if (codeset != NULL && codeset[0] != 0) |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6989 | return PyUnicode_FromString(codeset); |
| 6990 | } |
| 6991 | #endif |
| 6992 | Py_INCREF(Py_None); |
| 6993 | return Py_None; |
| 6994 | } |
| 6995 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6996 | #ifdef __VMS |
| 6997 | /* Use openssl random routine */ |
| 6998 | #include <openssl/rand.h> |
| 6999 | PyDoc_STRVAR(vms_urandom__doc__, |
| 7000 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 7001 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7002 | |
| 7003 | static PyObject* |
| 7004 | vms_urandom(PyObject *self, PyObject *args) |
| 7005 | { |
| 7006 | int howMany; |
| 7007 | PyObject* result; |
| 7008 | |
| 7009 | /* Read arguments */ |
| 7010 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7011 | return NULL; |
| 7012 | if (howMany < 0) |
| 7013 | return PyErr_Format(PyExc_ValueError, |
| 7014 | "negative argument not allowed"); |
| 7015 | |
| 7016 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7017 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7018 | if (result != NULL) { |
| 7019 | /* Get random data */ |
| 7020 | if (RAND_pseudo_bytes((unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7021 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7022 | howMany) < 0) { |
| 7023 | Py_DECREF(result); |
| 7024 | return PyErr_Format(PyExc_ValueError, |
| 7025 | "RAND_pseudo_bytes"); |
| 7026 | } |
| 7027 | } |
| 7028 | return result; |
| 7029 | } |
| 7030 | #endif |
| 7031 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7032 | static PyMethodDef posix_methods[] = { |
| 7033 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7034 | #ifdef HAVE_TTYNAME |
| 7035 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7036 | #endif |
| 7037 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7038 | #ifdef HAVE_CHFLAGS |
| 7039 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 7040 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7041 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7042 | #ifdef HAVE_FCHMOD |
| 7043 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
| 7044 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7045 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7046 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7047 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7048 | #ifdef HAVE_LCHMOD |
| 7049 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
| 7050 | #endif /* HAVE_LCHMOD */ |
| 7051 | #ifdef HAVE_FCHOWN |
| 7052 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
| 7053 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7054 | #ifdef HAVE_LCHFLAGS |
| 7055 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 7056 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7057 | #ifdef HAVE_LCHOWN |
| 7058 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7059 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7060 | #ifdef HAVE_CHROOT |
| 7061 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7062 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7063 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7064 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7065 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7066 | #ifdef HAVE_GETCWD |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 7067 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 7068 | METH_NOARGS, posix_getcwd__doc__}, |
| 7069 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 7070 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7071 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7072 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7073 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7074 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7075 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7076 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7077 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7078 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7079 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7080 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7081 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7082 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7083 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7084 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7085 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7086 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7087 | {"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] | 7088 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7089 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7090 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7091 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7092 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7093 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7094 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7095 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7096 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7097 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7098 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7099 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7100 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7101 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7102 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7103 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7104 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7105 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7106 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7107 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7108 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7109 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7110 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7111 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7112 | #if defined(PYOS_OS2) |
| 7113 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7114 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7115 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7116 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7117 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7118 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7119 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7120 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7121 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7122 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7123 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7124 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7125 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7126 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7127 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7128 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7129 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7130 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7131 | #endif /* HAVE_GETEGID */ |
| 7132 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7133 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7134 | #endif /* HAVE_GETEUID */ |
| 7135 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7136 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7137 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7138 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7139 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7140 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7141 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7142 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7143 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7144 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7145 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7146 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7147 | #endif /* HAVE_GETPPID */ |
| 7148 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7149 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7150 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7151 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7152 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7153 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7154 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7155 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7156 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7157 | #ifdef HAVE_KILLPG |
| 7158 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7159 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7160 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7161 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7162 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7163 | #ifdef MS_WINDOWS |
| 7164 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 7165 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7166 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7167 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7168 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7169 | #ifdef HAVE_SETEUID |
| 7170 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7171 | #endif /* HAVE_SETEUID */ |
| 7172 | #ifdef HAVE_SETEGID |
| 7173 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7174 | #endif /* HAVE_SETEGID */ |
| 7175 | #ifdef HAVE_SETREUID |
| 7176 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7177 | #endif /* HAVE_SETREUID */ |
| 7178 | #ifdef HAVE_SETREGID |
| 7179 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7180 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7181 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7182 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7183 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7184 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 7185 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7186 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7187 | #ifdef HAVE_GETPGID |
| 7188 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7189 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7190 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7191 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7192 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7193 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7194 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7195 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7196 | #ifdef HAVE_WAIT3 |
| 7197 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7198 | #endif /* HAVE_WAIT3 */ |
| 7199 | #ifdef HAVE_WAIT4 |
| 7200 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7201 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7202 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7203 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7204 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7205 | #ifdef HAVE_GETSID |
| 7206 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7207 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7208 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7209 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7210 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7211 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7212 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7213 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7214 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7215 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7216 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7217 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7218 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7219 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7220 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7221 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7222 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7223 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7224 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7225 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7226 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7227 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7228 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7229 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7230 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7231 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7232 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7233 | #endif |
| 7234 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7235 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7236 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7237 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7238 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7239 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7240 | #ifdef HAVE_DEVICE_MACROS |
| 7241 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7242 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7243 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7244 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7245 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7246 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7247 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7248 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7249 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7250 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7251 | #ifdef HAVE_UNSETENV |
| 7252 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7253 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7254 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7255 | #ifdef HAVE_FCHDIR |
| 7256 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7257 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7258 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7259 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7260 | #endif |
| 7261 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7262 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7263 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7264 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7265 | #ifdef WCOREDUMP |
| 7266 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7267 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7268 | #ifdef WIFCONTINUED |
| 7269 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7270 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7271 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7272 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7273 | #endif /* WIFSTOPPED */ |
| 7274 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7275 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7276 | #endif /* WIFSIGNALED */ |
| 7277 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7278 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7279 | #endif /* WIFEXITED */ |
| 7280 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7281 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7282 | #endif /* WEXITSTATUS */ |
| 7283 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7284 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7285 | #endif /* WTERMSIG */ |
| 7286 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7287 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7288 | #endif /* WSTOPSIG */ |
| 7289 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7290 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7291 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7292 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7293 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7294 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7295 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7296 | #ifdef HAVE_CONFSTR |
| 7297 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7298 | #endif |
| 7299 | #ifdef HAVE_SYSCONF |
| 7300 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7301 | #endif |
| 7302 | #ifdef HAVE_FPATHCONF |
| 7303 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7304 | #endif |
| 7305 | #ifdef HAVE_PATHCONF |
| 7306 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7307 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7308 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7309 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7310 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7311 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7312 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7313 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7314 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7315 | #ifdef MS_WINDOWS |
| 7316 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7317 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7318 | #ifdef __VMS |
| 7319 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 7320 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7321 | {NULL, NULL} /* Sentinel */ |
| 7322 | }; |
| 7323 | |
| 7324 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7325 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7326 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7327 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7328 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7329 | } |
| 7330 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7331 | #if defined(PYOS_OS2) |
| 7332 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7333 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7334 | { |
| 7335 | APIRET rc; |
| 7336 | ULONG values[QSV_MAX+1]; |
| 7337 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7338 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7339 | |
| 7340 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7341 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7342 | Py_END_ALLOW_THREADS |
| 7343 | |
| 7344 | if (rc != NO_ERROR) { |
| 7345 | os2_error(rc); |
| 7346 | return -1; |
| 7347 | } |
| 7348 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7349 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7350 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7351 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7352 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7353 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7354 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7355 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7356 | |
| 7357 | switch (values[QSV_VERSION_MINOR]) { |
| 7358 | case 0: ver = "2.00"; break; |
| 7359 | case 10: ver = "2.10"; break; |
| 7360 | case 11: ver = "2.11"; break; |
| 7361 | case 30: ver = "3.00"; break; |
| 7362 | case 40: ver = "4.00"; break; |
| 7363 | case 50: ver = "5.00"; break; |
| 7364 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7365 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7366 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7367 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7368 | ver = &tmp[0]; |
| 7369 | } |
| 7370 | |
| 7371 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7372 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7373 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7374 | |
| 7375 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7376 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7377 | tmp[1] = ':'; |
| 7378 | tmp[2] = '\0'; |
| 7379 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7380 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7381 | } |
| 7382 | #endif |
| 7383 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7384 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7385 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7386 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7387 | #ifdef F_OK |
| 7388 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7389 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7390 | #ifdef R_OK |
| 7391 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7392 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7393 | #ifdef W_OK |
| 7394 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7395 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7396 | #ifdef X_OK |
| 7397 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7398 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7399 | #ifdef NGROUPS_MAX |
| 7400 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7401 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7402 | #ifdef TMP_MAX |
| 7403 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7404 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7405 | #ifdef WCONTINUED |
| 7406 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7407 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7408 | #ifdef WNOHANG |
| 7409 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7410 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7411 | #ifdef WUNTRACED |
| 7412 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7413 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7414 | #ifdef O_RDONLY |
| 7415 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7416 | #endif |
| 7417 | #ifdef O_WRONLY |
| 7418 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7419 | #endif |
| 7420 | #ifdef O_RDWR |
| 7421 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7422 | #endif |
| 7423 | #ifdef O_NDELAY |
| 7424 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7425 | #endif |
| 7426 | #ifdef O_NONBLOCK |
| 7427 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7428 | #endif |
| 7429 | #ifdef O_APPEND |
| 7430 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7431 | #endif |
| 7432 | #ifdef O_DSYNC |
| 7433 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7434 | #endif |
| 7435 | #ifdef O_RSYNC |
| 7436 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7437 | #endif |
| 7438 | #ifdef O_SYNC |
| 7439 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7440 | #endif |
| 7441 | #ifdef O_NOCTTY |
| 7442 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7443 | #endif |
| 7444 | #ifdef O_CREAT |
| 7445 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7446 | #endif |
| 7447 | #ifdef O_EXCL |
| 7448 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7449 | #endif |
| 7450 | #ifdef O_TRUNC |
| 7451 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7452 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7453 | #ifdef O_BINARY |
| 7454 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7455 | #endif |
| 7456 | #ifdef O_TEXT |
| 7457 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7458 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7459 | #ifdef O_LARGEFILE |
| 7460 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7461 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7462 | #ifdef O_SHLOCK |
| 7463 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7464 | #endif |
| 7465 | #ifdef O_EXLOCK |
| 7466 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7467 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7468 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7469 | /* MS Windows */ |
| 7470 | #ifdef O_NOINHERIT |
| 7471 | /* Don't inherit in child processes. */ |
| 7472 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7473 | #endif |
| 7474 | #ifdef _O_SHORT_LIVED |
| 7475 | /* Optimize for short life (keep in memory). */ |
| 7476 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7477 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7478 | #endif |
| 7479 | #ifdef O_TEMPORARY |
| 7480 | /* Automatically delete when last handle is closed. */ |
| 7481 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7482 | #endif |
| 7483 | #ifdef O_RANDOM |
| 7484 | /* Optimize for random access. */ |
| 7485 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7486 | #endif |
| 7487 | #ifdef O_SEQUENTIAL |
| 7488 | /* Optimize for sequential access. */ |
| 7489 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7490 | #endif |
| 7491 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7492 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7493 | #ifdef O_ASYNC |
| 7494 | /* Send a SIGIO signal whenever input or output |
| 7495 | becomes available on file descriptor */ |
| 7496 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
| 7497 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7498 | #ifdef O_DIRECT |
| 7499 | /* Direct disk access. */ |
| 7500 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7501 | #endif |
| 7502 | #ifdef O_DIRECTORY |
| 7503 | /* Must be a directory. */ |
| 7504 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7505 | #endif |
| 7506 | #ifdef O_NOFOLLOW |
| 7507 | /* Do not follow links. */ |
| 7508 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7509 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7510 | #ifdef O_NOATIME |
| 7511 | /* Do not update the access time. */ |
| 7512 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
| 7513 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7514 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7515 | /* These come from sysexits.h */ |
| 7516 | #ifdef EX_OK |
| 7517 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7518 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7519 | #ifdef EX_USAGE |
| 7520 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7521 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7522 | #ifdef EX_DATAERR |
| 7523 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7524 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7525 | #ifdef EX_NOINPUT |
| 7526 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7527 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7528 | #ifdef EX_NOUSER |
| 7529 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7530 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7531 | #ifdef EX_NOHOST |
| 7532 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7533 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7534 | #ifdef EX_UNAVAILABLE |
| 7535 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7536 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7537 | #ifdef EX_SOFTWARE |
| 7538 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7539 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7540 | #ifdef EX_OSERR |
| 7541 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7542 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7543 | #ifdef EX_OSFILE |
| 7544 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7545 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7546 | #ifdef EX_CANTCREAT |
| 7547 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7548 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7549 | #ifdef EX_IOERR |
| 7550 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7551 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7552 | #ifdef EX_TEMPFAIL |
| 7553 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7554 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7555 | #ifdef EX_PROTOCOL |
| 7556 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7557 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7558 | #ifdef EX_NOPERM |
| 7559 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7560 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7561 | #ifdef EX_CONFIG |
| 7562 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7563 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7564 | #ifdef EX_NOTFOUND |
| 7565 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7566 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7567 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7568 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7569 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7570 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7571 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7572 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7573 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7574 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7575 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7576 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7577 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7578 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7579 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7580 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7581 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7582 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7583 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7584 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7585 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7586 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7587 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7588 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7589 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7590 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7591 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7592 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7593 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7594 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7595 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7596 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7597 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7598 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7599 | #if defined(PYOS_OS2) |
| 7600 | if (insertvalues(d)) return -1; |
| 7601 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7602 | return 0; |
| 7603 | } |
| 7604 | |
| 7605 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7606 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7607 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7608 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7609 | |
| 7610 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7611 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7612 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7613 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7614 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7615 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7616 | #define MODNAME "posix" |
| 7617 | #endif |
| 7618 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7619 | static struct PyModuleDef posixmodule = { |
| 7620 | PyModuleDef_HEAD_INIT, |
| 7621 | MODNAME, |
| 7622 | posix__doc__, |
| 7623 | -1, |
| 7624 | posix_methods, |
| 7625 | NULL, |
| 7626 | NULL, |
| 7627 | NULL, |
| 7628 | NULL |
| 7629 | }; |
| 7630 | |
| 7631 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7632 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7633 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7634 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7635 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7636 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7637 | m = PyModule_Create(&posixmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7638 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7639 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7640 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7641 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7642 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7643 | Py_XINCREF(v); |
| 7644 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7645 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7646 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7647 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7648 | if (all_ins(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7649 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7650 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7651 | if (setup_confname_tables(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7652 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7653 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7654 | Py_INCREF(PyExc_OSError); |
| 7655 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7656 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7657 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7658 | if (posix_putenv_garbage == NULL) |
| 7659 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7660 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7661 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7662 | if (!initialized) { |
| 7663 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7664 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7665 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7666 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7667 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7668 | structseq_new = StatResultType.tp_new; |
| 7669 | StatResultType.tp_new = statresult_new; |
| 7670 | |
| 7671 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7672 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7673 | #ifdef NEED_TICKS_PER_SECOND |
| 7674 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
| 7675 | ticks_per_second = sysconf(_SC_CLK_TCK); |
| 7676 | # elif defined(HZ) |
| 7677 | ticks_per_second = HZ; |
| 7678 | # else |
| 7679 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
| 7680 | # endif |
| 7681 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7682 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7683 | Py_INCREF((PyObject*) &StatResultType); |
| 7684 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7685 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7686 | PyModule_AddObject(m, "statvfs_result", |
| 7687 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7688 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7689 | |
| 7690 | #ifdef __APPLE__ |
| 7691 | /* |
| 7692 | * Step 2 of weak-linking support on Mac OS X. |
| 7693 | * |
| 7694 | * The code below removes functions that are not available on the |
| 7695 | * currently active platform. |
| 7696 | * |
| 7697 | * This block allow one to use a python binary that was build on |
| 7698 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7699 | * OSX 10.4. |
| 7700 | */ |
| 7701 | #ifdef HAVE_FSTATVFS |
| 7702 | if (fstatvfs == NULL) { |
| 7703 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7704 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7705 | } |
| 7706 | } |
| 7707 | #endif /* HAVE_FSTATVFS */ |
| 7708 | |
| 7709 | #ifdef HAVE_STATVFS |
| 7710 | if (statvfs == NULL) { |
| 7711 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7712 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7713 | } |
| 7714 | } |
| 7715 | #endif /* HAVE_STATVFS */ |
| 7716 | |
| 7717 | # ifdef HAVE_LCHOWN |
| 7718 | if (lchown == NULL) { |
| 7719 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7720 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7721 | } |
| 7722 | } |
| 7723 | #endif /* HAVE_LCHOWN */ |
| 7724 | |
| 7725 | |
| 7726 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7727 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7728 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7729 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7730 | |
| 7731 | #ifdef __cplusplus |
| 7732 | } |
| 7733 | #endif |