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 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 787 | PyObject *opath1, *opath2; |
| 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, |
| 792 | PyUnicode_FSConverter, &opath2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 793 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 794 | path1 = bytes2str(opath1, 1); |
| 795 | path2 = bytes2str(opath2, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 796 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 797 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 798 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 799 | release_bytes(opath1); |
| 800 | release_bytes(opath2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 801 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 802 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 803 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 804 | Py_INCREF(Py_None); |
| 805 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 808 | #ifdef Py_WIN_WIDE_FILENAMES |
| 809 | static PyObject* |
| 810 | win32_1str(PyObject* args, char* func, |
| 811 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 812 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 813 | { |
| 814 | PyObject *uni; |
| 815 | char *ansi; |
| 816 | BOOL result; |
| 817 | if (unicode_file_names()) { |
| 818 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 819 | PyErr_Clear(); |
| 820 | else { |
| 821 | Py_BEGIN_ALLOW_THREADS |
| 822 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 823 | Py_END_ALLOW_THREADS |
| 824 | if (!result) |
| 825 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 826 | Py_INCREF(Py_None); |
| 827 | return Py_None; |
| 828 | } |
| 829 | } |
| 830 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 831 | return NULL; |
| 832 | Py_BEGIN_ALLOW_THREADS |
| 833 | result = funcA(ansi); |
| 834 | Py_END_ALLOW_THREADS |
| 835 | if (!result) |
| 836 | return win32_error(func, ansi); |
| 837 | Py_INCREF(Py_None); |
| 838 | return Py_None; |
| 839 | |
| 840 | } |
| 841 | |
| 842 | /* This is a reimplementation of the C library's chdir function, |
| 843 | but one that produces Win32 errors instead of DOS error codes. |
| 844 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 845 | it also needs to set "magic" environment variables indicating |
| 846 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 847 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 848 | win32_chdir(LPCSTR path) |
| 849 | { |
| 850 | char new_path[MAX_PATH+1]; |
| 851 | int result; |
| 852 | char env[4] = "=x:"; |
| 853 | |
| 854 | if(!SetCurrentDirectoryA(path)) |
| 855 | return FALSE; |
| 856 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 857 | if (!result) |
| 858 | return FALSE; |
| 859 | /* In the ANSI API, there should not be any paths longer |
| 860 | than MAX_PATH. */ |
| 861 | assert(result <= MAX_PATH+1); |
| 862 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 863 | strncmp(new_path, "//", 2) == 0) |
| 864 | /* UNC path, nothing to do. */ |
| 865 | return TRUE; |
| 866 | env[1] = new_path[0]; |
| 867 | return SetEnvironmentVariableA(env, new_path); |
| 868 | } |
| 869 | |
| 870 | /* The Unicode version differs from the ANSI version |
| 871 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 872 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 873 | win32_wchdir(LPCWSTR path) |
| 874 | { |
| 875 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 876 | int result; |
| 877 | wchar_t env[4] = L"=x:"; |
| 878 | |
| 879 | if(!SetCurrentDirectoryW(path)) |
| 880 | return FALSE; |
| 881 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 882 | if (!result) |
| 883 | return FALSE; |
| 884 | if (result > MAX_PATH+1) { |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 885 | new_path = malloc(result * sizeof(wchar_t)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 886 | if (!new_path) { |
| 887 | SetLastError(ERROR_OUTOFMEMORY); |
| 888 | return FALSE; |
| 889 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 890 | result = GetCurrentDirectoryW(result, new_path); |
| 891 | if (!result) { |
| 892 | free(new_path); |
| 893 | return FALSE; |
| 894 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 895 | } |
| 896 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 897 | wcsncmp(new_path, L"//", 2) == 0) |
| 898 | /* UNC path, nothing to do. */ |
| 899 | return TRUE; |
| 900 | env[1] = new_path[0]; |
| 901 | result = SetEnvironmentVariableW(env, new_path); |
| 902 | if (new_path != _new_path) |
| 903 | free(new_path); |
| 904 | return result; |
| 905 | } |
| 906 | #endif |
| 907 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 908 | #ifdef MS_WINDOWS |
| 909 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 910 | - time stamps are restricted to second resolution |
| 911 | - file modification times suffer from forth-and-back conversions between |
| 912 | UTC and local time |
| 913 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 914 | */ |
| 915 | #define HAVE_STAT_NSEC 1 |
| 916 | |
| 917 | struct win32_stat{ |
| 918 | int st_dev; |
| 919 | __int64 st_ino; |
| 920 | unsigned short st_mode; |
| 921 | int st_nlink; |
| 922 | int st_uid; |
| 923 | int st_gid; |
| 924 | int st_rdev; |
| 925 | __int64 st_size; |
| 926 | int st_atime; |
| 927 | int st_atime_nsec; |
| 928 | int st_mtime; |
| 929 | int st_mtime_nsec; |
| 930 | int st_ctime; |
| 931 | int st_ctime_nsec; |
| 932 | }; |
| 933 | |
| 934 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 935 | |
| 936 | static void |
| 937 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 938 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 939 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 940 | /* Cannot simply cast and dereference in_ptr, |
| 941 | since it might not be aligned properly */ |
| 942 | __int64 in; |
| 943 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 944 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 945 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 946 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 947 | } |
| 948 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 949 | static void |
| 950 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 951 | { |
| 952 | /* XXX endianness */ |
| 953 | __int64 out; |
| 954 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 955 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 956 | memcpy(out_ptr, &out, sizeof(out)); |
| 957 | } |
| 958 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 959 | /* Below, we *know* that ugo+r is 0444 */ |
| 960 | #if _S_IREAD != 0400 |
| 961 | #error Unsupported C library |
| 962 | #endif |
| 963 | static int |
| 964 | attributes_to_mode(DWORD attr) |
| 965 | { |
| 966 | int m = 0; |
| 967 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 968 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 969 | else |
| 970 | m |= _S_IFREG; |
| 971 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 972 | m |= 0444; |
| 973 | else |
| 974 | m |= 0666; |
| 975 | return m; |
| 976 | } |
| 977 | |
| 978 | static int |
| 979 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 980 | { |
| 981 | memset(result, 0, sizeof(*result)); |
| 982 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 983 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 984 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 985 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 986 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 991 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 992 | static int checked = 0; |
| 993 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 994 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 995 | static void |
| 996 | check_gfax() |
| 997 | { |
| 998 | HINSTANCE hKernel32; |
| 999 | if (checked) |
| 1000 | return; |
| 1001 | checked = 1; |
| 1002 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1003 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 1004 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 1005 | } |
| 1006 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1007 | static BOOL |
| 1008 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1009 | { |
| 1010 | HANDLE hFindFile; |
| 1011 | WIN32_FIND_DATAA FileData; |
| 1012 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1013 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1014 | return FALSE; |
| 1015 | FindClose(hFindFile); |
| 1016 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1017 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1018 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1019 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1020 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1021 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1022 | return TRUE; |
| 1023 | } |
| 1024 | |
| 1025 | static BOOL |
| 1026 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1027 | { |
| 1028 | HANDLE hFindFile; |
| 1029 | WIN32_FIND_DATAW FileData; |
| 1030 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1031 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1032 | return FALSE; |
| 1033 | FindClose(hFindFile); |
| 1034 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1035 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1036 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1037 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1038 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1039 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1040 | return TRUE; |
| 1041 | } |
| 1042 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1043 | static BOOL WINAPI |
| 1044 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 1045 | GET_FILEEX_INFO_LEVELS level, |
| 1046 | LPVOID pv) |
| 1047 | { |
| 1048 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1049 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1050 | /* First try to use the system's implementation, if that is |
| 1051 | available and either succeeds to gives an error other than |
| 1052 | that it isn't implemented. */ |
| 1053 | check_gfax(); |
| 1054 | if (gfaxa) { |
| 1055 | result = gfaxa(pszFile, level, pv); |
| 1056 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1057 | return result; |
| 1058 | } |
| 1059 | /* It's either not present, or not implemented. |
| 1060 | Emulate using FindFirstFile. */ |
| 1061 | if (level != GetFileExInfoStandard) { |
| 1062 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1063 | return FALSE; |
| 1064 | } |
| 1065 | /* Use GetFileAttributes to validate that the file name |
| 1066 | does not contain wildcards (which FindFirstFile would |
| 1067 | accept). */ |
| 1068 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 1069 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1070 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | static BOOL WINAPI |
| 1074 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 1075 | GET_FILEEX_INFO_LEVELS level, |
| 1076 | LPVOID pv) |
| 1077 | { |
| 1078 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1079 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 1080 | /* First try to use the system's implementation, if that is |
| 1081 | available and either succeeds to gives an error other than |
| 1082 | that it isn't implemented. */ |
| 1083 | check_gfax(); |
| 1084 | if (gfaxa) { |
| 1085 | result = gfaxw(pszFile, level, pv); |
| 1086 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1087 | return result; |
| 1088 | } |
| 1089 | /* It's either not present, or not implemented. |
| 1090 | Emulate using FindFirstFile. */ |
| 1091 | if (level != GetFileExInfoStandard) { |
| 1092 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1093 | return FALSE; |
| 1094 | } |
| 1095 | /* Use GetFileAttributes to validate that the file name |
| 1096 | does not contain wildcards (which FindFirstFile would |
| 1097 | accept). */ |
| 1098 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 1099 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1100 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1103 | static int |
| 1104 | win32_stat(const char* path, struct win32_stat *result) |
| 1105 | { |
| 1106 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1107 | int code; |
| 1108 | char *dot; |
| 1109 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1110 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1111 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1112 | /* Protocol violation: we explicitly clear errno, instead of |
| 1113 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1114 | errno = 0; |
| 1115 | return -1; |
| 1116 | } else { |
| 1117 | /* Could not get attributes on open file. Fall back to |
| 1118 | reading the directory. */ |
| 1119 | if (!attributes_from_dir(path, &info)) { |
| 1120 | /* Very strange. This should not fail now */ |
| 1121 | errno = 0; |
| 1122 | return -1; |
| 1123 | } |
| 1124 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1125 | } |
| 1126 | code = attribute_data_to_stat(&info, result); |
| 1127 | if (code != 0) |
| 1128 | return code; |
| 1129 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1130 | dot = strrchr(path, '.'); |
| 1131 | if (dot) { |
| 1132 | if (stricmp(dot, ".bat") == 0 || |
| 1133 | stricmp(dot, ".cmd") == 0 || |
| 1134 | stricmp(dot, ".exe") == 0 || |
| 1135 | stricmp(dot, ".com") == 0) |
| 1136 | result->st_mode |= 0111; |
| 1137 | } |
| 1138 | return code; |
| 1139 | } |
| 1140 | |
| 1141 | static int |
| 1142 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1143 | { |
| 1144 | int code; |
| 1145 | const wchar_t *dot; |
| 1146 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1147 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1148 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1149 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1150 | /* Protocol violation: we explicitly clear errno, instead of |
| 1151 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1152 | errno = 0; |
| 1153 | return -1; |
| 1154 | } else { |
| 1155 | /* Could not get attributes on open file. Fall back to |
| 1156 | reading the directory. */ |
| 1157 | if (!attributes_from_dir_w(path, &info)) { |
| 1158 | /* Very strange. This should not fail now */ |
| 1159 | errno = 0; |
| 1160 | return -1; |
| 1161 | } |
| 1162 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1163 | } |
| 1164 | code = attribute_data_to_stat(&info, result); |
| 1165 | if (code < 0) |
| 1166 | return code; |
| 1167 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1168 | dot = wcsrchr(path, '.'); |
| 1169 | if (dot) { |
| 1170 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1171 | _wcsicmp(dot, L".cmd") == 0 || |
| 1172 | _wcsicmp(dot, L".exe") == 0 || |
| 1173 | _wcsicmp(dot, L".com") == 0) |
| 1174 | result->st_mode |= 0111; |
| 1175 | } |
| 1176 | return code; |
| 1177 | } |
| 1178 | |
| 1179 | static int |
| 1180 | win32_fstat(int file_number, struct win32_stat *result) |
| 1181 | { |
| 1182 | BY_HANDLE_FILE_INFORMATION info; |
| 1183 | HANDLE h; |
| 1184 | int type; |
| 1185 | |
| 1186 | h = (HANDLE)_get_osfhandle(file_number); |
| 1187 | |
| 1188 | /* Protocol violation: we explicitly clear errno, instead of |
| 1189 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1190 | errno = 0; |
| 1191 | |
| 1192 | if (h == INVALID_HANDLE_VALUE) { |
| 1193 | /* This is really a C library error (invalid file handle). |
| 1194 | We set the Win32 error to the closes one matching. */ |
| 1195 | SetLastError(ERROR_INVALID_HANDLE); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | memset(result, 0, sizeof(*result)); |
| 1199 | |
| 1200 | type = GetFileType(h); |
| 1201 | if (type == FILE_TYPE_UNKNOWN) { |
| 1202 | DWORD error = GetLastError(); |
| 1203 | if (error != 0) { |
| 1204 | return -1; |
| 1205 | } |
| 1206 | /* else: valid but unknown file */ |
| 1207 | } |
| 1208 | |
| 1209 | if (type != FILE_TYPE_DISK) { |
| 1210 | if (type == FILE_TYPE_CHAR) |
| 1211 | result->st_mode = _S_IFCHR; |
| 1212 | else if (type == FILE_TYPE_PIPE) |
| 1213 | result->st_mode = _S_IFIFO; |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | if (!GetFileInformationByHandle(h, &info)) { |
| 1218 | return -1; |
| 1219 | } |
| 1220 | |
| 1221 | /* similar to stat() */ |
| 1222 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1223 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1224 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1225 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1226 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1227 | /* specific to fstat() */ |
| 1228 | result->st_nlink = info.nNumberOfLinks; |
| 1229 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | #endif /* MS_WINDOWS */ |
| 1234 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1235 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1236 | "stat_result: Result from stat or lstat.\n\n\ |
| 1237 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1238 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1239 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1240 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1241 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1242 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1243 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1244 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1245 | |
| 1246 | static PyStructSequence_Field stat_result_fields[] = { |
| 1247 | {"st_mode", "protection bits"}, |
| 1248 | {"st_ino", "inode"}, |
| 1249 | {"st_dev", "device"}, |
| 1250 | {"st_nlink", "number of hard links"}, |
| 1251 | {"st_uid", "user ID of owner"}, |
| 1252 | {"st_gid", "group ID of owner"}, |
| 1253 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1254 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1255 | {NULL, "integer time of last access"}, |
| 1256 | {NULL, "integer time of last modification"}, |
| 1257 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1258 | {"st_atime", "time of last access"}, |
| 1259 | {"st_mtime", "time of last modification"}, |
| 1260 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1261 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1262 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1263 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1264 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1265 | {"st_blocks", "number of blocks allocated"}, |
| 1266 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1267 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1268 | {"st_rdev", "device type (if inode device)"}, |
| 1269 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1270 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1271 | {"st_flags", "user defined flags for file"}, |
| 1272 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1273 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1274 | {"st_gen", "generation number"}, |
| 1275 | #endif |
| 1276 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1277 | {"st_birthtime", "time of creation"}, |
| 1278 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1279 | {0} |
| 1280 | }; |
| 1281 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1282 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1283 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1284 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1285 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1286 | #endif |
| 1287 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1288 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1289 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1290 | #else |
| 1291 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1292 | #endif |
| 1293 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1294 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1295 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1296 | #else |
| 1297 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1298 | #endif |
| 1299 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1300 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1301 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1302 | #else |
| 1303 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1304 | #endif |
| 1305 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1306 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1307 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1308 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1309 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1310 | #endif |
| 1311 | |
| 1312 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1313 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1314 | #else |
| 1315 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1316 | #endif |
| 1317 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1318 | static PyStructSequence_Desc stat_result_desc = { |
| 1319 | "stat_result", /* name */ |
| 1320 | stat_result__doc__, /* doc */ |
| 1321 | stat_result_fields, |
| 1322 | 10 |
| 1323 | }; |
| 1324 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1325 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1326 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1327 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1328 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1329 | 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] | 1330 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1331 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1332 | |
| 1333 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1334 | {"f_bsize", }, |
| 1335 | {"f_frsize", }, |
| 1336 | {"f_blocks", }, |
| 1337 | {"f_bfree", }, |
| 1338 | {"f_bavail", }, |
| 1339 | {"f_files", }, |
| 1340 | {"f_ffree", }, |
| 1341 | {"f_favail", }, |
| 1342 | {"f_flag", }, |
| 1343 | {"f_namemax",}, |
| 1344 | {0} |
| 1345 | }; |
| 1346 | |
| 1347 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1348 | "statvfs_result", /* name */ |
| 1349 | statvfs_result__doc__, /* doc */ |
| 1350 | statvfs_result_fields, |
| 1351 | 10 |
| 1352 | }; |
| 1353 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1354 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1355 | static PyTypeObject StatResultType; |
| 1356 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1357 | static newfunc structseq_new; |
| 1358 | |
| 1359 | static PyObject * |
| 1360 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1361 | { |
| 1362 | PyStructSequence *result; |
| 1363 | int i; |
| 1364 | |
| 1365 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1366 | if (!result) |
| 1367 | return NULL; |
| 1368 | /* If we have been initialized from a tuple, |
| 1369 | st_?time might be set to None. Initialize it |
| 1370 | from the int slots. */ |
| 1371 | for (i = 7; i <= 9; i++) { |
| 1372 | if (result->ob_item[i+3] == Py_None) { |
| 1373 | Py_DECREF(Py_None); |
| 1374 | Py_INCREF(result->ob_item[i]); |
| 1375 | result->ob_item[i+3] = result->ob_item[i]; |
| 1376 | } |
| 1377 | } |
| 1378 | return (PyObject*)result; |
| 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1384 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1385 | |
| 1386 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1387 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1388 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1389 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1390 | future calls return ints. \n\ |
| 1391 | If newval is omitted, return the current setting.\n"); |
| 1392 | |
| 1393 | static PyObject* |
| 1394 | stat_float_times(PyObject* self, PyObject *args) |
| 1395 | { |
| 1396 | int newval = -1; |
| 1397 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1398 | return NULL; |
| 1399 | if (newval == -1) |
| 1400 | /* Return old value */ |
| 1401 | return PyBool_FromLong(_stat_float_times); |
| 1402 | _stat_float_times = newval; |
| 1403 | Py_INCREF(Py_None); |
| 1404 | return Py_None; |
| 1405 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1406 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1407 | static void |
| 1408 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1409 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1410 | PyObject *fval,*ival; |
| 1411 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1412 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1413 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1414 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1415 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1416 | if (!ival) |
| 1417 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1418 | if (_stat_float_times) { |
| 1419 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1420 | } else { |
| 1421 | fval = ival; |
| 1422 | Py_INCREF(fval); |
| 1423 | } |
| 1424 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1425 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1428 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1429 | (used by posix_stat() and posix_fstat()) */ |
| 1430 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1431 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1432 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1433 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1434 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1435 | if (v == NULL) |
| 1436 | return NULL; |
| 1437 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1438 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1439 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1440 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1441 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1442 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1443 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1444 | #endif |
| 1445 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1446 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1447 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1448 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1449 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1450 | #endif |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1451 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1452 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1453 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1454 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1455 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1456 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1457 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1458 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1459 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1460 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1461 | #if defined(HAVE_STAT_TV_NSEC) |
| 1462 | ansec = st->st_atim.tv_nsec; |
| 1463 | mnsec = st->st_mtim.tv_nsec; |
| 1464 | cnsec = st->st_ctim.tv_nsec; |
| 1465 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1466 | ansec = st->st_atimespec.tv_nsec; |
| 1467 | mnsec = st->st_mtimespec.tv_nsec; |
| 1468 | cnsec = st->st_ctimespec.tv_nsec; |
| 1469 | #elif defined(HAVE_STAT_NSEC) |
| 1470 | ansec = st->st_atime_nsec; |
| 1471 | mnsec = st->st_mtime_nsec; |
| 1472 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1473 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1474 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1475 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1476 | fill_time(v, 7, st->st_atime, ansec); |
| 1477 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1478 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1479 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1480 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1481 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1482 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1483 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1484 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1485 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1486 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1487 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1488 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1489 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1490 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1491 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1492 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1493 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1494 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1495 | #endif |
| 1496 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1497 | { |
| 1498 | PyObject *val; |
| 1499 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1500 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1501 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1502 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1503 | #else |
| 1504 | bnsec = 0; |
| 1505 | #endif |
| 1506 | if (_stat_float_times) { |
| 1507 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1508 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1509 | val = PyLong_FromLong((long)bsec); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1510 | } |
| 1511 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1512 | val); |
| 1513 | } |
| 1514 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1515 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1516 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1517 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1518 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1519 | |
| 1520 | if (PyErr_Occurred()) { |
| 1521 | Py_DECREF(v); |
| 1522 | return NULL; |
| 1523 | } |
| 1524 | |
| 1525 | return v; |
| 1526 | } |
| 1527 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1528 | #ifdef MS_WINDOWS |
| 1529 | |
| 1530 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1531 | where / can be used in place of \ and the trailing slash is optional. |
| 1532 | Both SERVER and SHARE must have at least one character. |
| 1533 | */ |
| 1534 | |
| 1535 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1536 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1537 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1538 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1539 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1540 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1541 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1542 | IsUNCRootA(char *path, int pathlen) |
| 1543 | { |
| 1544 | #define ISSLASH ISSLASHA |
| 1545 | |
| 1546 | int i, share; |
| 1547 | |
| 1548 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1549 | /* minimum UNCRoot is \\x\y */ |
| 1550 | return FALSE; |
| 1551 | for (i = 2; i < pathlen ; i++) |
| 1552 | if (ISSLASH(path[i])) break; |
| 1553 | if (i == 2 || i == pathlen) |
| 1554 | /* do not allow \\\SHARE or \\SERVER */ |
| 1555 | return FALSE; |
| 1556 | share = i+1; |
| 1557 | for (i = share; i < pathlen; i++) |
| 1558 | if (ISSLASH(path[i])) break; |
| 1559 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1560 | |
| 1561 | #undef ISSLASH |
| 1562 | } |
| 1563 | |
| 1564 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1565 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1566 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1567 | { |
| 1568 | #define ISSLASH ISSLASHW |
| 1569 | |
| 1570 | int i, share; |
| 1571 | |
| 1572 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1573 | /* minimum UNCRoot is \\x\y */ |
| 1574 | return FALSE; |
| 1575 | for (i = 2; i < pathlen ; i++) |
| 1576 | if (ISSLASH(path[i])) break; |
| 1577 | if (i == 2 || i == pathlen) |
| 1578 | /* do not allow \\\SHARE or \\SERVER */ |
| 1579 | return FALSE; |
| 1580 | share = i+1; |
| 1581 | for (i = share; i < pathlen; i++) |
| 1582 | if (ISSLASH(path[i])) break; |
| 1583 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1584 | |
| 1585 | #undef ISSLASH |
| 1586 | } |
| 1587 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1588 | #endif /* MS_WINDOWS */ |
| 1589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1590 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1591 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1592 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1593 | #ifdef __VMS |
| 1594 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1595 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1596 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1597 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1598 | char *wformat, |
| 1599 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1600 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1601 | STRUCT_STAT st; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1602 | PyObject *opath; |
| 1603 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1604 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1605 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1606 | |
| 1607 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1608 | /* If on wide-character-capable OS see if argument |
| 1609 | is Unicode and if so use wide API. */ |
| 1610 | if (unicode_file_names()) { |
| 1611 | PyUnicodeObject *po; |
| 1612 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1613 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1614 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1615 | Py_BEGIN_ALLOW_THREADS |
| 1616 | /* PyUnicode_AS_UNICODE result OK without |
| 1617 | thread lock as it is a simple dereference. */ |
| 1618 | res = wstatfunc(wpath, &st); |
| 1619 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1620 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1621 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1622 | return win32_error_unicode("stat", wpath); |
| 1623 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1624 | } |
| 1625 | /* Drop the argument parsing error as narrow strings |
| 1626 | are also valid. */ |
| 1627 | PyErr_Clear(); |
| 1628 | } |
| 1629 | #endif |
| 1630 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1631 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1632 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1633 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1634 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1635 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1636 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1637 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1638 | |
| 1639 | if (res != 0) { |
| 1640 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1641 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1642 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1643 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1644 | #endif |
| 1645 | } |
| 1646 | else |
| 1647 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1648 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1649 | release_bytes(opath); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1650 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1653 | /* POSIX methods */ |
| 1654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1655 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1656 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1657 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1658 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1659 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1660 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1661 | 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] | 1662 | |
| 1663 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1664 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1665 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1666 | PyObject *opath; |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1667 | char *path; |
| 1668 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1669 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1670 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1671 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1672 | if (unicode_file_names()) { |
| 1673 | PyUnicodeObject *po; |
| 1674 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1675 | Py_BEGIN_ALLOW_THREADS |
| 1676 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1677 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1678 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1679 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1680 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1681 | } |
| 1682 | /* Drop the argument parsing error as narrow strings |
| 1683 | are also valid. */ |
| 1684 | PyErr_Clear(); |
| 1685 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1686 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1687 | PyUnicode_FSConverter, &opath, &mode)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1688 | return 0; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1689 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1690 | Py_BEGIN_ALLOW_THREADS |
| 1691 | attr = GetFileAttributesA(path); |
| 1692 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1693 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1694 | finish: |
| 1695 | if (attr == 0xFFFFFFFF) |
| 1696 | /* File does not exist, or cannot read attributes */ |
| 1697 | return PyBool_FromLong(0); |
| 1698 | /* Access is possible if either write access wasn't requested, or |
Guido van Rossum | b00324f | 2007-12-04 01:13:14 +0000 | [diff] [blame] | 1699 | the file isn't read-only, or if it's a directory, as there are |
| 1700 | no read-only directories on Windows. */ |
| 1701 | return PyBool_FromLong(!(mode & 2) |
| 1702 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1703 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1704 | #else |
| 1705 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1706 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1707 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1708 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1709 | path = bytes2str(opath, 1); |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1710 | Py_BEGIN_ALLOW_THREADS |
| 1711 | res = access(path, mode); |
| 1712 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1713 | release_bytes(opath); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1714 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1715 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1718 | #ifndef F_OK |
| 1719 | #define F_OK 0 |
| 1720 | #endif |
| 1721 | #ifndef R_OK |
| 1722 | #define R_OK 4 |
| 1723 | #endif |
| 1724 | #ifndef W_OK |
| 1725 | #define W_OK 2 |
| 1726 | #endif |
| 1727 | #ifndef X_OK |
| 1728 | #define X_OK 1 |
| 1729 | #endif |
| 1730 | |
| 1731 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1732 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1733 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1734 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1735 | |
| 1736 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1737 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1738 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1739 | int id; |
| 1740 | char *ret; |
| 1741 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1742 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1743 | return NULL; |
| 1744 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1745 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1746 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1747 | if (id == 0) { |
| 1748 | ret = ttyname(); |
| 1749 | } |
| 1750 | else { |
| 1751 | ret = NULL; |
| 1752 | } |
| 1753 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1754 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1755 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1756 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1757 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1758 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1759 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1760 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1761 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1762 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1763 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1764 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1765 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1766 | |
| 1767 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1768 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1769 | { |
| 1770 | char *ret; |
| 1771 | char buffer[L_ctermid]; |
| 1772 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1773 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1774 | ret = ctermid_r(buffer); |
| 1775 | #else |
| 1776 | ret = ctermid(buffer); |
| 1777 | #endif |
| 1778 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1779 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1780 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1781 | } |
| 1782 | #endif |
| 1783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1784 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1785 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1786 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1787 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1788 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1789 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1790 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1791 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 1792 | 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] | 1793 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1794 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1795 | #elif defined(__VMS) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1796 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1797 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1798 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1799 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1802 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1803 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1804 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1805 | 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] | 1806 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1807 | |
| 1808 | static PyObject * |
| 1809 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1810 | { |
| 1811 | return posix_fildes(fdobj, fchdir); |
| 1812 | } |
| 1813 | #endif /* HAVE_FCHDIR */ |
| 1814 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1815 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1816 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1817 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1818 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1819 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1820 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1821 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1822 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1823 | PyObject *opath = NULL; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1824 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1825 | int i; |
| 1826 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1827 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1828 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1829 | if (unicode_file_names()) { |
| 1830 | PyUnicodeObject *po; |
| 1831 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1832 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1833 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1834 | if (attr != 0xFFFFFFFF) { |
| 1835 | if (i & _S_IWRITE) |
| 1836 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1837 | else |
| 1838 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1839 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1840 | } |
| 1841 | else |
| 1842 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1843 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1844 | if (!res) |
| 1845 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1846 | PyUnicode_AS_UNICODE(po)); |
| 1847 | Py_INCREF(Py_None); |
| 1848 | return Py_None; |
| 1849 | } |
| 1850 | /* Drop the argument parsing error as narrow strings |
| 1851 | are also valid. */ |
| 1852 | PyErr_Clear(); |
| 1853 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1854 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1855 | &opath, &i)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1856 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1857 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1858 | Py_BEGIN_ALLOW_THREADS |
| 1859 | attr = GetFileAttributesA(path); |
| 1860 | if (attr != 0xFFFFFFFF) { |
| 1861 | if (i & _S_IWRITE) |
| 1862 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1863 | else |
| 1864 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1865 | res = SetFileAttributesA(path, attr); |
| 1866 | } |
| 1867 | else |
| 1868 | res = 0; |
| 1869 | Py_END_ALLOW_THREADS |
| 1870 | if (!res) { |
| 1871 | win32_error("chmod", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1872 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1873 | return NULL; |
| 1874 | } |
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 | Py_INCREF(Py_None); |
| 1877 | return Py_None; |
| 1878 | #else /* Py_WIN_WIDE_FILENAMES */ |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1879 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1880 | &opath, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1881 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1882 | path = bytes2str(opath, 1); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1883 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1884 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1885 | Py_END_ALLOW_THREADS |
| 1886 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1887 | return posix_error_with_allocated_filename(opath); |
| 1888 | release_bytes(opath); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1889 | Py_INCREF(Py_None); |
| 1890 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1891 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1894 | #ifdef HAVE_FCHMOD |
| 1895 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1896 | "fchmod(fd, mode)\n\n\ |
| 1897 | Change the access permissions of the file given by file\n\ |
| 1898 | descriptor fd."); |
| 1899 | |
| 1900 | static PyObject * |
| 1901 | posix_fchmod(PyObject *self, PyObject *args) |
| 1902 | { |
| 1903 | int fd, mode, res; |
| 1904 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1905 | return NULL; |
| 1906 | Py_BEGIN_ALLOW_THREADS |
| 1907 | res = fchmod(fd, mode); |
| 1908 | Py_END_ALLOW_THREADS |
| 1909 | if (res < 0) |
| 1910 | return posix_error(); |
| 1911 | Py_RETURN_NONE; |
| 1912 | } |
| 1913 | #endif /* HAVE_FCHMOD */ |
| 1914 | |
| 1915 | #ifdef HAVE_LCHMOD |
| 1916 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1917 | "lchmod(path, mode)\n\n\ |
| 1918 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1919 | affects the link itself rather than the target."); |
| 1920 | |
| 1921 | static PyObject * |
| 1922 | posix_lchmod(PyObject *self, PyObject *args) |
| 1923 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1924 | PyObject *opath; |
| 1925 | char *path; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1926 | int i; |
| 1927 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1928 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 1929 | &opath, &i)) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1930 | return NULL; |
Eric Smith | 86a05ec | 2009-05-05 13:07:30 +0000 | [diff] [blame] | 1931 | path = bytes2str(opath, 1); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1932 | Py_BEGIN_ALLOW_THREADS |
| 1933 | res = lchmod(path, i); |
| 1934 | Py_END_ALLOW_THREADS |
| 1935 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1936 | return posix_error_with_allocated_filename(opath); |
| 1937 | release_bytes(opath); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1938 | Py_RETURN_NONE; |
| 1939 | } |
| 1940 | #endif /* HAVE_LCHMOD */ |
| 1941 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1942 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1943 | #ifdef HAVE_CHFLAGS |
| 1944 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1945 | "chflags(path, flags)\n\n\ |
| 1946 | Set file flags."); |
| 1947 | |
| 1948 | static PyObject * |
| 1949 | posix_chflags(PyObject *self, PyObject *args) |
| 1950 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1951 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1952 | char *path; |
| 1953 | unsigned long flags; |
| 1954 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1955 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 1956 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1957 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1958 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1959 | Py_BEGIN_ALLOW_THREADS |
| 1960 | res = chflags(path, flags); |
| 1961 | Py_END_ALLOW_THREADS |
| 1962 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1963 | return posix_error_with_allocated_filename(opath); |
| 1964 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1965 | Py_INCREF(Py_None); |
| 1966 | return Py_None; |
| 1967 | } |
| 1968 | #endif /* HAVE_CHFLAGS */ |
| 1969 | |
| 1970 | #ifdef HAVE_LCHFLAGS |
| 1971 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1972 | "lchflags(path, flags)\n\n\ |
| 1973 | Set file flags.\n\ |
| 1974 | This function will not follow symbolic links."); |
| 1975 | |
| 1976 | static PyObject * |
| 1977 | posix_lchflags(PyObject *self, PyObject *args) |
| 1978 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1979 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1980 | char *path; |
| 1981 | unsigned long flags; |
| 1982 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1983 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Martin v. Löwis | 4adbc34 | 2009-05-05 17:17:55 +0000 | [diff] [blame] | 1984 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1985 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1986 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1987 | Py_BEGIN_ALLOW_THREADS |
| 1988 | res = lchflags(path, flags); |
| 1989 | Py_END_ALLOW_THREADS |
| 1990 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1991 | return posix_error_with_allocated_filename(opath); |
| 1992 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1993 | Py_INCREF(Py_None); |
| 1994 | return Py_None; |
| 1995 | } |
| 1996 | #endif /* HAVE_LCHFLAGS */ |
| 1997 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1998 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1999 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2000 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2001 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2002 | |
| 2003 | static PyObject * |
| 2004 | posix_chroot(PyObject *self, PyObject *args) |
| 2005 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2006 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2007 | } |
| 2008 | #endif |
| 2009 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2010 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2011 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2012 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2013 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2014 | |
| 2015 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2016 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2017 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2018 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2019 | } |
| 2020 | #endif /* HAVE_FSYNC */ |
| 2021 | |
| 2022 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2023 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2024 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2025 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2026 | #endif |
| 2027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2028 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2029 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2030 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2031 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2032 | |
| 2033 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2034 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2035 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2036 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2037 | } |
| 2038 | #endif /* HAVE_FDATASYNC */ |
| 2039 | |
| 2040 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2041 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2042 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2043 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2044 | 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] | 2045 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2046 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2047 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2048 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2049 | PyObject *opath; |
| 2050 | char *path; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 2051 | long uid, gid; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2052 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2053 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2054 | PyUnicode_FSConverter, &opath, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2055 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2056 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2057 | path = bytes2str(opath, 1); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2058 | Py_BEGIN_ALLOW_THREADS |
| 2059 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2060 | Py_END_ALLOW_THREADS |
| 2061 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2062 | return posix_error_with_allocated_filename(opath); |
| 2063 | release_bytes(opath); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 2064 | Py_INCREF(Py_None); |
| 2065 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2066 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2067 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2068 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2069 | #ifdef HAVE_FCHOWN |
| 2070 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2071 | "fchown(fd, uid, gid)\n\n\ |
| 2072 | Change the owner and group id of the file given by file descriptor\n\ |
| 2073 | fd to the numeric uid and gid."); |
| 2074 | |
| 2075 | static PyObject * |
| 2076 | posix_fchown(PyObject *self, PyObject *args) |
| 2077 | { |
| 2078 | int fd, uid, gid; |
| 2079 | int res; |
| 2080 | if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid)) |
| 2081 | return NULL; |
| 2082 | Py_BEGIN_ALLOW_THREADS |
| 2083 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2084 | Py_END_ALLOW_THREADS |
| 2085 | if (res < 0) |
| 2086 | return posix_error(); |
| 2087 | Py_RETURN_NONE; |
| 2088 | } |
| 2089 | #endif /* HAVE_FCHOWN */ |
| 2090 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2091 | #ifdef HAVE_LCHOWN |
| 2092 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2093 | "lchown(path, uid, gid)\n\n\ |
| 2094 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2095 | This function will not follow symbolic links."); |
| 2096 | |
| 2097 | static PyObject * |
| 2098 | posix_lchown(PyObject *self, PyObject *args) |
| 2099 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2100 | PyObject *opath; |
| 2101 | char *path; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2102 | int uid, gid; |
| 2103 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2104 | if (!PyArg_ParseTuple(args, "O&ii:lchown", |
| 2105 | PyUnicode_FSConverter, &opath, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2106 | &uid, &gid)) |
| 2107 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2108 | path = bytes2str(opath, 1); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2109 | Py_BEGIN_ALLOW_THREADS |
| 2110 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2111 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2112 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2113 | return posix_error_with_allocated_filename(opath); |
| 2114 | release_bytes(opath); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2115 | Py_INCREF(Py_None); |
| 2116 | return Py_None; |
| 2117 | } |
| 2118 | #endif /* HAVE_LCHOWN */ |
| 2119 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2120 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2121 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2122 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2123 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2124 | { |
| 2125 | char buf[1026]; |
| 2126 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2127 | |
| 2128 | #ifdef Py_WIN_WIDE_FILENAMES |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2129 | if (!use_bytes && unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2130 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2131 | wchar_t *wbuf2 = wbuf; |
| 2132 | PyObject *resobj; |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2133 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2134 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2135 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2136 | /* If the buffer is large enough, len does not include the |
| 2137 | terminating \0. If the buffer is too small, len includes |
| 2138 | the space needed for the terminator. */ |
| 2139 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2140 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2141 | if (wbuf2) |
| 2142 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2143 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2144 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2145 | if (!wbuf2) { |
| 2146 | PyErr_NoMemory(); |
| 2147 | return NULL; |
| 2148 | } |
| 2149 | if (!len) { |
| 2150 | if (wbuf2 != wbuf) free(wbuf2); |
| 2151 | return win32_error("getcwdu", NULL); |
| 2152 | } |
| 2153 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2154 | if (wbuf2 != wbuf) free(wbuf2); |
| 2155 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2156 | } |
| 2157 | #endif |
| 2158 | |
| 2159 | Py_BEGIN_ALLOW_THREADS |
| 2160 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2161 | res = _getcwd2(buf, sizeof buf); |
| 2162 | #else |
| 2163 | res = getcwd(buf, sizeof buf); |
| 2164 | #endif |
| 2165 | Py_END_ALLOW_THREADS |
| 2166 | if (res == NULL) |
| 2167 | return posix_error(); |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2168 | if (use_bytes) |
| 2169 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame^] | 2170 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape"); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2171 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2172 | |
| 2173 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2174 | "getcwd() -> path\n\n\ |
| 2175 | Return a unicode string representing the current working directory."); |
| 2176 | |
| 2177 | static PyObject * |
| 2178 | posix_getcwd_unicode(PyObject *self) |
| 2179 | { |
| 2180 | return posix_getcwd(0); |
| 2181 | } |
| 2182 | |
| 2183 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2184 | "getcwdb() -> path\n\n\ |
| 2185 | Return a bytes string representing the current working directory."); |
| 2186 | |
| 2187 | static PyObject * |
| 2188 | posix_getcwd_bytes(PyObject *self) |
| 2189 | { |
| 2190 | return posix_getcwd(1); |
| 2191 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2192 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2193 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2194 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2195 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2196 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2197 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2198 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2199 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2200 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2201 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2202 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2203 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2204 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2205 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2208 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2209 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2210 | Return a list containing the names of the entries in the directory.\n\ |
| 2211 | \n\ |
| 2212 | path: path of directory to list\n\ |
| 2213 | \n\ |
| 2214 | 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] | 2215 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2216 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2217 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2218 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2219 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2220 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2221 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2222 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2223 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2224 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2225 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2226 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2227 | WIN32_FIND_DATA FileData; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2228 | PyObject *opath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2229 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2230 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2231 | 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] | 2232 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2233 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2234 | /* If on wide-character-capable OS see if argument |
| 2235 | is Unicode and if so use wide API. */ |
| 2236 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2237 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2238 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2239 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2240 | Py_UNICODE *wnamebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2241 | /* Overallocate for \\*.*\0 */ |
| 2242 | len = PyUnicode_GET_SIZE(po); |
| 2243 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2244 | if (!wnamebuf) { |
| 2245 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2246 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2247 | } |
| 2248 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2249 | if (len > 0) { |
| 2250 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2251 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2252 | wnamebuf[len++] = L'\\'; |
| 2253 | wcscpy(wnamebuf + len, L"*.*"); |
| 2254 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2255 | if ((d = PyList_New(0)) == NULL) { |
| 2256 | free(wnamebuf); |
| 2257 | return NULL; |
| 2258 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2259 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2260 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2261 | int error = GetLastError(); |
| 2262 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2263 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2264 | return d; |
| 2265 | } |
| 2266 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2267 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2268 | free(wnamebuf); |
| 2269 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2270 | } |
| 2271 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2272 | /* Skip over . and .. */ |
| 2273 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2274 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2275 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2276 | if (v == NULL) { |
| 2277 | Py_DECREF(d); |
| 2278 | d = NULL; |
| 2279 | break; |
| 2280 | } |
| 2281 | if (PyList_Append(d, v) != 0) { |
| 2282 | Py_DECREF(v); |
| 2283 | Py_DECREF(d); |
| 2284 | d = NULL; |
| 2285 | break; |
| 2286 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2287 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2288 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2289 | Py_BEGIN_ALLOW_THREADS |
| 2290 | result = FindNextFileW(hFindFile, &wFileData); |
| 2291 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2292 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2293 | it got to the end of the directory. */ |
| 2294 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2295 | Py_DECREF(d); |
| 2296 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2297 | FindClose(hFindFile); |
| 2298 | free(wnamebuf); |
| 2299 | return NULL; |
| 2300 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2301 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2302 | |
| 2303 | if (FindClose(hFindFile) == FALSE) { |
| 2304 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2305 | win32_error_unicode("FindClose", wnamebuf); |
| 2306 | free(wnamebuf); |
| 2307 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2308 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2309 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2310 | return d; |
| 2311 | } |
| 2312 | /* Drop the argument parsing error as narrow strings |
| 2313 | are also valid. */ |
| 2314 | PyErr_Clear(); |
| 2315 | } |
| 2316 | #endif |
| 2317 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2318 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2319 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2320 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2321 | if (PyObject_Size(opath)+1 > MAX_PATH) { |
| 2322 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2323 | Py_DECREF(opath); |
| 2324 | return NULL; |
| 2325 | } |
| 2326 | strcpy(namebuf, bytes2str(opath, 0)); |
| 2327 | len = PyObject_Size(opath); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2328 | if (len > 0) { |
| 2329 | char ch = namebuf[len-1]; |
| 2330 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2331 | namebuf[len++] = '/'; |
Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2332 | strcpy(namebuf + len, "*.*"); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2333 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2334 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2335 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2336 | return NULL; |
| 2337 | |
| 2338 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2339 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2340 | int error = GetLastError(); |
| 2341 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2342 | return d; |
| 2343 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2344 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2345 | } |
| 2346 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2347 | /* Skip over . and .. */ |
| 2348 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2349 | strcmp(FileData.cFileName, "..") != 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2350 | v = PyBytes_FromString(FileData.cFileName); |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2351 | if (v == NULL) { |
| 2352 | Py_DECREF(d); |
| 2353 | d = NULL; |
| 2354 | break; |
| 2355 | } |
| 2356 | if (PyList_Append(d, v) != 0) { |
| 2357 | Py_DECREF(v); |
| 2358 | Py_DECREF(d); |
| 2359 | d = NULL; |
| 2360 | break; |
| 2361 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2362 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2363 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2364 | Py_BEGIN_ALLOW_THREADS |
| 2365 | result = FindNextFile(hFindFile, &FileData); |
| 2366 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2367 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2368 | it got to the end of the directory. */ |
| 2369 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2370 | Py_DECREF(d); |
| 2371 | win32_error("FindNextFile", namebuf); |
| 2372 | FindClose(hFindFile); |
| 2373 | return NULL; |
| 2374 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2375 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2376 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2377 | if (FindClose(hFindFile) == FALSE) { |
| 2378 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2379 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2380 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2381 | |
| 2382 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2383 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2384 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2385 | |
| 2386 | #ifndef MAX_PATH |
| 2387 | #define MAX_PATH CCHMAXPATH |
| 2388 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2389 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2390 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2391 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2392 | PyObject *d, *v; |
| 2393 | char namebuf[MAX_PATH+5]; |
| 2394 | HDIR hdir = 1; |
| 2395 | ULONG srchcnt = 1; |
| 2396 | FILEFINDBUF3 ep; |
| 2397 | APIRET rc; |
| 2398 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2399 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2400 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2401 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2402 | name = bytes2str(oname); |
| 2403 | len = PyObject_Size(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2404 | if (len >= MAX_PATH) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2405 | release_bytes(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2406 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2407 | return NULL; |
| 2408 | } |
| 2409 | strcpy(namebuf, name); |
| 2410 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2411 | if (*pt == ALTSEP) |
| 2412 | *pt = SEP; |
| 2413 | if (namebuf[len-1] != SEP) |
| 2414 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2415 | strcpy(namebuf + len, "*.*"); |
| 2416 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2417 | if ((d = PyList_New(0)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2418 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2419 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2420 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2421 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2422 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2423 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2424 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2425 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2426 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2427 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2428 | |
| 2429 | if (rc != NO_ERROR) { |
| 2430 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2431 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2434 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2435 | do { |
| 2436 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2437 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2438 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2439 | |
| 2440 | strcpy(namebuf, ep.achName); |
| 2441 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2442 | /* Leave Case of Name Alone -- In Native Form */ |
| 2443 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2444 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2445 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2446 | if (v == NULL) { |
| 2447 | Py_DECREF(d); |
| 2448 | d = NULL; |
| 2449 | break; |
| 2450 | } |
| 2451 | if (PyList_Append(d, v) != 0) { |
| 2452 | Py_DECREF(v); |
| 2453 | Py_DECREF(d); |
| 2454 | d = NULL; |
| 2455 | break; |
| 2456 | } |
| 2457 | Py_DECREF(v); |
| 2458 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2459 | } |
| 2460 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2461 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2462 | return d; |
| 2463 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2464 | PyObject *oname; |
| 2465 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2466 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2467 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2468 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2469 | int arg_is_unicode = 1; |
| 2470 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2471 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2472 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2473 | arg_is_unicode = 0; |
| 2474 | PyErr_Clear(); |
| 2475 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2476 | if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2477 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2478 | name = bytes2str(oname, 1); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2479 | if ((dirp = opendir(name)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2480 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2481 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2482 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2483 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2484 | release_bytes(oname); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2485 | return NULL; |
| 2486 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2487 | for (;;) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2488 | errno = 0; |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2489 | Py_BEGIN_ALLOW_THREADS |
| 2490 | ep = readdir(dirp); |
| 2491 | Py_END_ALLOW_THREADS |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2492 | if (ep == NULL) { |
| 2493 | if (errno == 0) { |
| 2494 | break; |
| 2495 | } else { |
| 2496 | closedir(dirp); |
| 2497 | Py_DECREF(d); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2498 | return posix_error_with_allocated_filename(oname); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2499 | } |
| 2500 | } |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2501 | if (ep->d_name[0] == '.' && |
| 2502 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2503 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2504 | continue; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2505 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2506 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2507 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2508 | d = NULL; |
| 2509 | break; |
| 2510 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2511 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2512 | PyObject *w; |
| 2513 | |
| 2514 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2515 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame^] | 2516 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2517 | Py_DECREF(v); |
| 2518 | if (w != NULL) |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2519 | v = w; |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2520 | else { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2521 | /* Encoding failed to decode ASCII bytes. |
| 2522 | Raise exception. */ |
| 2523 | Py_DECREF(d); |
| 2524 | d = NULL; |
| 2525 | break; |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2526 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2527 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2528 | if (PyList_Append(d, v) != 0) { |
| 2529 | Py_DECREF(v); |
| 2530 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2531 | d = NULL; |
| 2532 | break; |
| 2533 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2534 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2535 | } |
| 2536 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2537 | release_bytes(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2538 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2539 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2540 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2541 | #endif /* which OS */ |
| 2542 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2543 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2544 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2545 | /* A helper function for abspath on win32 */ |
| 2546 | static PyObject * |
| 2547 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2548 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2549 | PyObject *opath; |
| 2550 | char *path; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2551 | char outbuf[MAX_PATH*2]; |
| 2552 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2553 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2554 | if (unicode_file_names()) { |
| 2555 | PyUnicodeObject *po; |
| 2556 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 2557 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2558 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2559 | Py_UNICODE *wtemp; |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 2560 | DWORD result; |
| 2561 | PyObject *v; |
| 2562 | result = GetFullPathNameW(wpath, |
| 2563 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2564 | woutbuf, &wtemp); |
| 2565 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2566 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2567 | if (!woutbufp) |
| 2568 | return PyErr_NoMemory(); |
| 2569 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2570 | } |
| 2571 | if (result) |
| 2572 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2573 | else |
| 2574 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2575 | if (woutbufp != woutbuf) |
| 2576 | free(woutbufp); |
| 2577 | return v; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2578 | } |
| 2579 | /* Drop the argument parsing error as narrow strings |
| 2580 | are also valid. */ |
| 2581 | PyErr_Clear(); |
| 2582 | } |
| 2583 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2584 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2585 | PyUnicode_FSConverter, &opath)) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2586 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2587 | path = bytes2str(opath, 1); |
| 2588 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2589 | outbuf, &temp)) { |
| 2590 | win32_error("GetFullPathName", path); |
| 2591 | release_bytes(opath); |
| 2592 | return NULL; |
| 2593 | } |
| 2594 | release_bytes(opath); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2595 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2596 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2597 | Py_FileSystemDefaultEncoding, NULL); |
| 2598 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2599 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2600 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2601 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2603 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2604 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2605 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2606 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2607 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2608 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2609 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2610 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2611 | PyObject *opath; |
| 2612 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2613 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2614 | |
| 2615 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2616 | if (unicode_file_names()) { |
| 2617 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2618 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2619 | Py_BEGIN_ALLOW_THREADS |
| 2620 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2621 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2622 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2623 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2624 | if (!res) |
| 2625 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2626 | Py_INCREF(Py_None); |
| 2627 | return Py_None; |
| 2628 | } |
| 2629 | /* Drop the argument parsing error as narrow strings |
| 2630 | are also valid. */ |
| 2631 | PyErr_Clear(); |
| 2632 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2633 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2634 | PyUnicode_FSConverter, &opath, &mode)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2635 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2636 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2637 | Py_BEGIN_ALLOW_THREADS |
| 2638 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2639 | it is a simple dereference. */ |
| 2640 | res = CreateDirectoryA(path, NULL); |
| 2641 | Py_END_ALLOW_THREADS |
| 2642 | if (!res) { |
| 2643 | win32_error("mkdir", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2644 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2645 | return NULL; |
| 2646 | } |
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 | Py_INCREF(Py_None); |
| 2649 | return Py_None; |
| 2650 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2651 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2652 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2653 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2654 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2655 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2656 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2657 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2658 | res = mkdir(path); |
| 2659 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2660 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2661 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2662 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2663 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2664 | return posix_error_with_allocated_filename(opath); |
| 2665 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2666 | Py_INCREF(Py_None); |
| 2667 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2668 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2671 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2672 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2673 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2674 | #include <sys/resource.h> |
| 2675 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2676 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2677 | |
| 2678 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2679 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2680 | "nice(inc) -> new_priority\n\n\ |
| 2681 | 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] | 2682 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2684 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2685 | { |
| 2686 | int increment, value; |
| 2687 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2688 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2689 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2690 | |
| 2691 | /* There are two flavours of 'nice': one that returns the new |
| 2692 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2693 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2694 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2695 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2696 | If we are of the nice family that returns the new priority, we |
| 2697 | need to clear errno before the call, and check if errno is filled |
| 2698 | before calling posix_error() on a returnvalue of -1, because the |
| 2699 | -1 may be the actual new priority! */ |
| 2700 | |
| 2701 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2702 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2703 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2704 | if (value == 0) |
| 2705 | value = getpriority(PRIO_PROCESS, 0); |
| 2706 | #endif |
| 2707 | if (value == -1 && errno != 0) |
| 2708 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2709 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2710 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2711 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2712 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2713 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2714 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2715 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2716 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2717 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2718 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2719 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2720 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2721 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2722 | PyObject *o1, *o2; |
| 2723 | char *p1, *p2; |
| 2724 | BOOL result; |
| 2725 | if (unicode_file_names()) { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2726 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 2727 | goto error; |
| 2728 | if (!convert_to_unicode(&o1)) |
| 2729 | goto error; |
| 2730 | if (!convert_to_unicode(&o2)) { |
| 2731 | Py_DECREF(o1); |
| 2732 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2733 | } |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2734 | Py_BEGIN_ALLOW_THREADS |
| 2735 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2736 | PyUnicode_AsUnicode(o2)); |
| 2737 | Py_END_ALLOW_THREADS |
| 2738 | Py_DECREF(o1); |
| 2739 | Py_DECREF(o2); |
| 2740 | if (!result) |
| 2741 | return win32_error("rename", NULL); |
| 2742 | Py_INCREF(Py_None); |
| 2743 | return Py_None; |
| 2744 | error: |
| 2745 | PyErr_Clear(); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2746 | } |
| 2747 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2748 | return NULL; |
| 2749 | Py_BEGIN_ALLOW_THREADS |
| 2750 | result = MoveFileA(p1, p2); |
| 2751 | Py_END_ALLOW_THREADS |
| 2752 | if (!result) |
| 2753 | return win32_error("rename", NULL); |
| 2754 | Py_INCREF(Py_None); |
| 2755 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2756 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2757 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2758 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2762 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2763 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2764 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2765 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2766 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2767 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2768 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2769 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2770 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2771 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2772 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2773 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2777 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2778 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2779 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2780 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2781 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2782 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2783 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2784 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2785 | 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] | 2786 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2787 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2788 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2791 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2792 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2793 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2794 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2795 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2796 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2797 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2798 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2799 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2800 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2801 | #ifdef MS_WINDOWS |
| 2802 | wchar_t *command; |
| 2803 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2804 | return NULL; |
| 2805 | #else |
| 2806 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2807 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2808 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2809 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2810 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2811 | #ifdef MS_WINDOWS |
| 2812 | sts = _wsystem(command); |
| 2813 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2814 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2815 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2816 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2817 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2818 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2819 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2820 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2821 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2822 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2823 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2824 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2825 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2826 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2827 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2828 | { |
| 2829 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2830 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2831 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2832 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2833 | if (i < 0) |
| 2834 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2835 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2839 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2840 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2841 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2843 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2844 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2845 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2846 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2847 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2848 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2849 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2850 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2851 | return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2852 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2853 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2854 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2857 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2858 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2859 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2860 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2861 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2863 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2864 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2865 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2866 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2867 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2868 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2869 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2870 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2871 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2872 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2873 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2874 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2875 | u.sysname, |
| 2876 | u.nodename, |
| 2877 | u.release, |
| 2878 | u.version, |
| 2879 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2880 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2881 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2882 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2883 | static int |
| 2884 | extract_time(PyObject *t, long* sec, long* usec) |
| 2885 | { |
| 2886 | long intval; |
| 2887 | if (PyFloat_Check(t)) { |
| 2888 | double tval = PyFloat_AsDouble(t); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2889 | 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] | 2890 | if (!intobj) |
| 2891 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2892 | intval = PyLong_AsLong(intobj); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2893 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2894 | if (intval == -1 && PyErr_Occurred()) |
| 2895 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2896 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2897 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2898 | if (*usec < 0) |
| 2899 | /* If rounding gave us a negative number, |
| 2900 | truncate. */ |
| 2901 | *usec = 0; |
| 2902 | return 0; |
| 2903 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2904 | intval = PyLong_AsLong(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2905 | if (intval == -1 && PyErr_Occurred()) |
| 2906 | return -1; |
| 2907 | *sec = intval; |
| 2908 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2909 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2910 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2912 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2913 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2914 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2915 | 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] | 2916 | 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] | 2917 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2918 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2919 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2920 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2921 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2922 | PyObject *arg; |
| 2923 | PyUnicodeObject *obwpath; |
| 2924 | wchar_t *wpath = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2925 | PyObject *oapath; |
| 2926 | char *apath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2927 | HANDLE hFile; |
| 2928 | long atimesec, mtimesec, ausec, musec; |
| 2929 | FILETIME atime, mtime; |
| 2930 | PyObject *result = NULL; |
| 2931 | |
| 2932 | if (unicode_file_names()) { |
| 2933 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2934 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2935 | Py_BEGIN_ALLOW_THREADS |
| 2936 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2937 | NULL, OPEN_EXISTING, |
| 2938 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2939 | Py_END_ALLOW_THREADS |
| 2940 | if (hFile == INVALID_HANDLE_VALUE) |
| 2941 | return win32_error_unicode("utime", wpath); |
| 2942 | } else |
| 2943 | /* Drop the argument parsing error as narrow strings |
| 2944 | are also valid. */ |
| 2945 | PyErr_Clear(); |
| 2946 | } |
| 2947 | if (!wpath) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2948 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 2949 | PyUnicode_FSConverter, &oapath, &arg)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2950 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2951 | apath = bytes2str(oapath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2952 | Py_BEGIN_ALLOW_THREADS |
| 2953 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2954 | NULL, OPEN_EXISTING, |
| 2955 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2956 | Py_END_ALLOW_THREADS |
| 2957 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2958 | win32_error("utime", apath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2959 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2960 | return NULL; |
| 2961 | } |
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 | } |
| 2964 | |
| 2965 | if (arg == Py_None) { |
| 2966 | SYSTEMTIME now; |
| 2967 | GetSystemTime(&now); |
| 2968 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2969 | !SystemTimeToFileTime(&now, &atime)) { |
| 2970 | win32_error("utime", NULL); |
| 2971 | goto done; |
| 2972 | } |
| 2973 | } |
| 2974 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2975 | PyErr_SetString(PyExc_TypeError, |
| 2976 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2977 | goto done; |
| 2978 | } |
| 2979 | else { |
| 2980 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2981 | &atimesec, &ausec) == -1) |
| 2982 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2983 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2984 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2985 | &mtimesec, &musec) == -1) |
| 2986 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2987 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2988 | } |
| 2989 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2990 | /* Avoid putting the file name into the error here, |
| 2991 | as that may confuse the user into believing that |
| 2992 | something is wrong with the file, when it also |
| 2993 | could be the time stamp that gives a problem. */ |
| 2994 | win32_error("utime", NULL); |
| 2995 | } |
| 2996 | Py_INCREF(Py_None); |
| 2997 | result = Py_None; |
| 2998 | done: |
| 2999 | CloseHandle(hFile); |
| 3000 | return result; |
| 3001 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 3002 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3003 | PyObject *opath; |
| 3004 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3005 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3006 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3007 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3008 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3009 | #if defined(HAVE_UTIMES) |
| 3010 | struct timeval buf[2]; |
| 3011 | #define ATIME buf[0].tv_sec |
| 3012 | #define MTIME buf[1].tv_sec |
| 3013 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3014 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3015 | struct utimbuf buf; |
| 3016 | #define ATIME buf.actime |
| 3017 | #define MTIME buf.modtime |
| 3018 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3019 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3020 | time_t buf[2]; |
| 3021 | #define ATIME buf[0] |
| 3022 | #define MTIME buf[1] |
| 3023 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3024 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3025 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3026 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3027 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3028 | PyUnicode_FSConverter, &opath, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3029 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3030 | path = bytes2str(opath, 1); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3031 | if (arg == Py_None) { |
| 3032 | /* optional time values not given */ |
| 3033 | Py_BEGIN_ALLOW_THREADS |
| 3034 | res = utime(path, NULL); |
| 3035 | Py_END_ALLOW_THREADS |
| 3036 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3037 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3038 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3039 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3040 | release_bytes(opath); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3041 | return NULL; |
| 3042 | } |
| 3043 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3044 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3045 | &atime, &ausec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3046 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3047 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3048 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3049 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3050 | &mtime, &musec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3051 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3052 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 3053 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3054 | ATIME = atime; |
| 3055 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3056 | #ifdef HAVE_UTIMES |
| 3057 | buf[0].tv_usec = ausec; |
| 3058 | buf[1].tv_usec = musec; |
| 3059 | Py_BEGIN_ALLOW_THREADS |
| 3060 | res = utimes(path, buf); |
| 3061 | Py_END_ALLOW_THREADS |
| 3062 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3063 | Py_BEGIN_ALLOW_THREADS |
| 3064 | res = utime(path, UTIME_ARG); |
| 3065 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3066 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3067 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 3068 | if (res < 0) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3069 | return posix_error_with_allocated_filename(opath); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 3070 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3071 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3072 | Py_INCREF(Py_None); |
| 3073 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3074 | #undef UTIME_ARG |
| 3075 | #undef ATIME |
| 3076 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3077 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3080 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3081 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3082 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3083 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3084 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3085 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3086 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3087 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3088 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3089 | { |
| 3090 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3091 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3092 | return NULL; |
| 3093 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 3094 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3097 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3098 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3099 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3100 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3101 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3102 | for (i = 0; i < count; i++) |
| 3103 | PyMem_Free(array[i]); |
| 3104 | PyMem_DEL(array); |
| 3105 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3106 | |
| 3107 | int fsconvert_strdup(PyObject *o, char**out) |
| 3108 | { |
| 3109 | PyObject *bytes; |
| 3110 | Py_ssize_t size; |
| 3111 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3112 | return 0; |
| 3113 | size = PyObject_Size(bytes); |
| 3114 | *out = PyMem_Malloc(size+1); |
| 3115 | if (!*out) |
| 3116 | return 0; |
| 3117 | /* Don't lock bytes, as we hold the GIL */ |
| 3118 | memcpy(*out, bytes2str(bytes, 0), size+1); |
| 3119 | Py_DECREF(bytes); |
| 3120 | return 1; |
| 3121 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3122 | #endif |
| 3123 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3124 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3125 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3126 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3127 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3128 | Execute an executable path with arguments, replacing current process.\n\ |
| 3129 | \n\ |
| 3130 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3131 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3132 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3133 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3134 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3135 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3136 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3137 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3138 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3139 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3140 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3141 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3142 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 3143 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3144 | argv is a list or tuple of strings. */ |
| 3145 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3146 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3147 | PyUnicode_FSConverter, |
| 3148 | &opath, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3149 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3150 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3151 | if (PyList_Check(argv)) { |
| 3152 | argc = PyList_Size(argv); |
| 3153 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3154 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3155 | else if (PyTuple_Check(argv)) { |
| 3156 | argc = PyTuple_Size(argv); |
| 3157 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3158 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3159 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3160 | 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] | 3161 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3162 | return NULL; |
| 3163 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3164 | if (argc < 1) { |
| 3165 | 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] | 3166 | release_bytes(opath); |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3167 | return NULL; |
| 3168 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3169 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3170 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3171 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3172 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3173 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3174 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3175 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3176 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3177 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3178 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3179 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3180 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3181 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3182 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3183 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3184 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3185 | } |
| 3186 | argvlist[argc] = NULL; |
| 3187 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3188 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3189 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3190 | /* If we get here it's definitely an error */ |
| 3191 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3192 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3193 | release_bytes(opath); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3194 | return posix_error(); |
| 3195 | } |
| 3196 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3198 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3199 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3200 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3201 | \n\ |
| 3202 | path: path of executable file\n\ |
| 3203 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3204 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3205 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3206 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3207 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3208 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3209 | PyObject *opath; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3210 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3211 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3212 | char **argvlist; |
| 3213 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3214 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3215 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3216 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3217 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3218 | |
| 3219 | /* execve has three arguments: (path, argv, env), where |
| 3220 | argv is a list or tuple of strings and env is a dictionary |
| 3221 | like posix.environ. */ |
| 3222 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3223 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3224 | PyUnicode_FSConverter, |
| 3225 | &opath, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3226 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3227 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3228 | if (PyList_Check(argv)) { |
| 3229 | argc = PyList_Size(argv); |
| 3230 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3231 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3232 | else if (PyTuple_Check(argv)) { |
| 3233 | argc = PyTuple_Size(argv); |
| 3234 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3235 | } |
| 3236 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3237 | PyErr_SetString(PyExc_TypeError, |
| 3238 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3239 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3240 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3241 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3242 | PyErr_SetString(PyExc_TypeError, |
| 3243 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3244 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3247 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3248 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3249 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3250 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3251 | } |
| 3252 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3253 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3254 | &argvlist[i])) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3255 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3256 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3257 | goto fail_1; |
| 3258 | } |
| 3259 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3260 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3261 | argvlist[argc] = NULL; |
| 3262 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3263 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3264 | if (i < 0) |
| 3265 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3266 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3267 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3268 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3269 | goto fail_1; |
| 3270 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3271 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3272 | keys = PyMapping_Keys(env); |
| 3273 | vals = PyMapping_Values(env); |
| 3274 | if (!keys || !vals) |
| 3275 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3276 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3277 | PyErr_SetString(PyExc_TypeError, |
| 3278 | "execve(): env.keys() or env.values() is not a list"); |
| 3279 | goto fail_2; |
| 3280 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3281 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3282 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3283 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3284 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3285 | |
| 3286 | key = PyList_GetItem(keys, pos); |
| 3287 | val = PyList_GetItem(vals, pos); |
| 3288 | if (!key || !val) |
| 3289 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3290 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3291 | if (!PyArg_Parse( |
| 3292 | key, |
| 3293 | "s;execve() arg 3 contains a non-string key", |
| 3294 | &k) || |
| 3295 | !PyArg_Parse( |
| 3296 | val, |
| 3297 | "s;execve() arg 3 contains a non-string value", |
| 3298 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3299 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3300 | goto fail_2; |
| 3301 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3302 | |
| 3303 | #if defined(PYOS_OS2) |
| 3304 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3305 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3306 | #endif |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3307 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3308 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3309 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3310 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3311 | goto fail_2; |
| 3312 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3313 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3314 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3315 | #if defined(PYOS_OS2) |
| 3316 | } |
| 3317 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3318 | } |
| 3319 | envlist[envc] = 0; |
| 3320 | |
| 3321 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3322 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3323 | /* If we get here it's definitely an error */ |
| 3324 | |
| 3325 | (void) posix_error(); |
| 3326 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3327 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3328 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3329 | PyMem_DEL(envlist[envc]); |
| 3330 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3331 | fail_1: |
| 3332 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3333 | Py_XDECREF(vals); |
| 3334 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3335 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3336 | release_bytes(opath); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3337 | return NULL; |
| 3338 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3339 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3340 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3341 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3342 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3343 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3344 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3345 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3346 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3347 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3348 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3349 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3350 | |
| 3351 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3352 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3353 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3354 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3355 | char *path; |
| 3356 | PyObject *argv; |
| 3357 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3358 | int mode, i; |
| 3359 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3360 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3361 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3362 | |
| 3363 | /* spawnv has three arguments: (mode, path, argv), where |
| 3364 | argv is a list or tuple of strings. */ |
| 3365 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3366 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 3367 | PyUnicode_FSConverter, |
| 3368 | &opath, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3369 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3370 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3371 | if (PyList_Check(argv)) { |
| 3372 | argc = PyList_Size(argv); |
| 3373 | getitem = PyList_GetItem; |
| 3374 | } |
| 3375 | else if (PyTuple_Check(argv)) { |
| 3376 | argc = PyTuple_Size(argv); |
| 3377 | getitem = PyTuple_GetItem; |
| 3378 | } |
| 3379 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3380 | PyErr_SetString(PyExc_TypeError, |
| 3381 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3382 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3383 | return NULL; |
| 3384 | } |
| 3385 | |
| 3386 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3387 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3388 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3389 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3390 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3391 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3392 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3393 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3394 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3395 | PyErr_SetString( |
| 3396 | PyExc_TypeError, |
| 3397 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3398 | release_bytes(opath); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3399 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3400 | } |
| 3401 | } |
| 3402 | argvlist[argc] = NULL; |
| 3403 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3404 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3405 | Py_BEGIN_ALLOW_THREADS |
| 3406 | spawnval = spawnv(mode, path, argvlist); |
| 3407 | Py_END_ALLOW_THREADS |
| 3408 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3409 | if (mode == _OLD_P_OVERLAY) |
| 3410 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3411 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3412 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3413 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3414 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3415 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3416 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3417 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3418 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3419 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3420 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3421 | return posix_error(); |
| 3422 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3423 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3424 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3425 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3426 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3427 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3431 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3432 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3433 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3434 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3435 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3436 | path: path of executable file\n\ |
| 3437 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3438 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3439 | |
| 3440 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3441 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3442 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3443 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3444 | char *path; |
| 3445 | PyObject *argv, *env; |
| 3446 | char **argvlist; |
| 3447 | char **envlist; |
| 3448 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3449 | int mode, pos, envc; |
| 3450 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3451 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3452 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3453 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3454 | |
| 3455 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3456 | argv is a list or tuple of strings and env is a dictionary |
| 3457 | like posix.environ. */ |
| 3458 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3459 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 3460 | PyUnicode_FSConverter, |
| 3461 | &opath, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3462 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3463 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3464 | if (PyList_Check(argv)) { |
| 3465 | argc = PyList_Size(argv); |
| 3466 | getitem = PyList_GetItem; |
| 3467 | } |
| 3468 | else if (PyTuple_Check(argv)) { |
| 3469 | argc = PyTuple_Size(argv); |
| 3470 | getitem = PyTuple_GetItem; |
| 3471 | } |
| 3472 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3473 | PyErr_SetString(PyExc_TypeError, |
| 3474 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3475 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3476 | } |
| 3477 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3478 | PyErr_SetString(PyExc_TypeError, |
| 3479 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3480 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | argvlist = PyMem_NEW(char *, argc+1); |
| 3484 | if (argvlist == NULL) { |
| 3485 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3486 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3487 | } |
| 3488 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3489 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3490 | &argvlist[i])) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3491 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3492 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3493 | goto fail_1; |
| 3494 | } |
| 3495 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3496 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3497 | argvlist[argc] = NULL; |
| 3498 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3499 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3500 | if (i < 0) |
| 3501 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3502 | envlist = PyMem_NEW(char *, i + 1); |
| 3503 | if (envlist == NULL) { |
| 3504 | PyErr_NoMemory(); |
| 3505 | goto fail_1; |
| 3506 | } |
| 3507 | envc = 0; |
| 3508 | keys = PyMapping_Keys(env); |
| 3509 | vals = PyMapping_Values(env); |
| 3510 | if (!keys || !vals) |
| 3511 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3512 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3513 | PyErr_SetString(PyExc_TypeError, |
| 3514 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3515 | goto fail_2; |
| 3516 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3517 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3518 | for (pos = 0; pos < i; pos++) { |
| 3519 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3520 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3521 | |
| 3522 | key = PyList_GetItem(keys, pos); |
| 3523 | val = PyList_GetItem(vals, pos); |
| 3524 | if (!key || !val) |
| 3525 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3526 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3527 | if (!PyArg_Parse( |
| 3528 | key, |
| 3529 | "s;spawnve() arg 3 contains a non-string key", |
| 3530 | &k) || |
| 3531 | !PyArg_Parse( |
| 3532 | val, |
| 3533 | "s;spawnve() arg 3 contains a non-string value", |
| 3534 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3535 | { |
| 3536 | goto fail_2; |
| 3537 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3538 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3539 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3540 | if (p == NULL) { |
| 3541 | PyErr_NoMemory(); |
| 3542 | goto fail_2; |
| 3543 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3544 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3545 | envlist[envc++] = p; |
| 3546 | } |
| 3547 | envlist[envc] = 0; |
| 3548 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3549 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3550 | Py_BEGIN_ALLOW_THREADS |
| 3551 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3552 | Py_END_ALLOW_THREADS |
| 3553 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3554 | if (mode == _OLD_P_OVERLAY) |
| 3555 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3556 | |
| 3557 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3558 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3559 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3560 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3561 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3562 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3563 | (void) posix_error(); |
| 3564 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3565 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3566 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3567 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3568 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3569 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3570 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3571 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3572 | while (--envc >= 0) |
| 3573 | PyMem_DEL(envlist[envc]); |
| 3574 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3575 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3576 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3577 | Py_XDECREF(vals); |
| 3578 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3579 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3580 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3581 | return res; |
| 3582 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3583 | |
| 3584 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3585 | #if defined(PYOS_OS2) |
| 3586 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3587 | "spawnvp(mode, file, args)\n\n\ |
| 3588 | Execute the program 'file' in a new process, using the environment\n\ |
| 3589 | search path to find the file.\n\ |
| 3590 | \n\ |
| 3591 | mode: mode of process creation\n\ |
| 3592 | file: executable file name\n\ |
| 3593 | args: tuple or list of strings"); |
| 3594 | |
| 3595 | static PyObject * |
| 3596 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3597 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3598 | PyObject *opath; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3599 | char *path; |
| 3600 | PyObject *argv; |
| 3601 | char **argvlist; |
| 3602 | int mode, i, argc; |
| 3603 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3604 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3607 | argv is a list or tuple of strings. */ |
| 3608 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3609 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 3610 | PyUnicode_FSConverter, |
| 3611 | &opath, &argv)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3612 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3613 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3614 | if (PyList_Check(argv)) { |
| 3615 | argc = PyList_Size(argv); |
| 3616 | getitem = PyList_GetItem; |
| 3617 | } |
| 3618 | else if (PyTuple_Check(argv)) { |
| 3619 | argc = PyTuple_Size(argv); |
| 3620 | getitem = PyTuple_GetItem; |
| 3621 | } |
| 3622 | else { |
| 3623 | PyErr_SetString(PyExc_TypeError, |
| 3624 | "spawnvp() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3625 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3626 | return NULL; |
| 3627 | } |
| 3628 | |
| 3629 | argvlist = PyMem_NEW(char *, argc+1); |
| 3630 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3631 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3632 | return PyErr_NoMemory(); |
| 3633 | } |
| 3634 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3635 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3636 | &argvlist[i])) { |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3637 | free_string_array(argvlist, i); |
| 3638 | PyErr_SetString( |
| 3639 | PyExc_TypeError, |
| 3640 | "spawnvp() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3641 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3642 | return NULL; |
| 3643 | } |
| 3644 | } |
| 3645 | argvlist[argc] = NULL; |
| 3646 | |
| 3647 | Py_BEGIN_ALLOW_THREADS |
| 3648 | #if defined(PYCC_GCC) |
| 3649 | spawnval = spawnvp(mode, path, argvlist); |
| 3650 | #else |
| 3651 | spawnval = _spawnvp(mode, path, argvlist); |
| 3652 | #endif |
| 3653 | Py_END_ALLOW_THREADS |
| 3654 | |
| 3655 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3656 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3657 | |
| 3658 | if (spawnval == -1) |
| 3659 | return posix_error(); |
| 3660 | else |
| 3661 | return Py_BuildValue("l", (long) spawnval); |
| 3662 | } |
| 3663 | |
| 3664 | |
| 3665 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3666 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3667 | Execute the program 'file' in a new process, using the environment\n\ |
| 3668 | search path to find the file.\n\ |
| 3669 | \n\ |
| 3670 | mode: mode of process creation\n\ |
| 3671 | file: executable file name\n\ |
| 3672 | args: tuple or list of arguments\n\ |
| 3673 | env: dictionary of strings mapping to strings"); |
| 3674 | |
| 3675 | static PyObject * |
| 3676 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3677 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3678 | PyObject *opath |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3679 | char *path; |
| 3680 | PyObject *argv, *env; |
| 3681 | char **argvlist; |
| 3682 | char **envlist; |
| 3683 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3684 | int mode, i, pos, argc, envc; |
| 3685 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3686 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3687 | int lastarg = 0; |
| 3688 | |
| 3689 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3690 | argv is a list or tuple of strings and env is a dictionary |
| 3691 | like posix.environ. */ |
| 3692 | |
| 3693 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3694 | PyUnicode_FSConverter, |
| 3695 | &opath, &argv, &env)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3696 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3697 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3698 | if (PyList_Check(argv)) { |
| 3699 | argc = PyList_Size(argv); |
| 3700 | getitem = PyList_GetItem; |
| 3701 | } |
| 3702 | else if (PyTuple_Check(argv)) { |
| 3703 | argc = PyTuple_Size(argv); |
| 3704 | getitem = PyTuple_GetItem; |
| 3705 | } |
| 3706 | else { |
| 3707 | PyErr_SetString(PyExc_TypeError, |
| 3708 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3709 | goto fail_0; |
| 3710 | } |
| 3711 | if (!PyMapping_Check(env)) { |
| 3712 | PyErr_SetString(PyExc_TypeError, |
| 3713 | "spawnvpe() arg 3 must be a mapping object"); |
| 3714 | goto fail_0; |
| 3715 | } |
| 3716 | |
| 3717 | argvlist = PyMem_NEW(char *, argc+1); |
| 3718 | if (argvlist == NULL) { |
| 3719 | PyErr_NoMemory(); |
| 3720 | goto fail_0; |
| 3721 | } |
| 3722 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3723 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3724 | &argvlist[i])) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3725 | { |
| 3726 | lastarg = i; |
| 3727 | goto fail_1; |
| 3728 | } |
| 3729 | } |
| 3730 | lastarg = argc; |
| 3731 | argvlist[argc] = NULL; |
| 3732 | |
| 3733 | i = PyMapping_Size(env); |
| 3734 | if (i < 0) |
| 3735 | goto fail_1; |
| 3736 | envlist = PyMem_NEW(char *, i + 1); |
| 3737 | if (envlist == NULL) { |
| 3738 | PyErr_NoMemory(); |
| 3739 | goto fail_1; |
| 3740 | } |
| 3741 | envc = 0; |
| 3742 | keys = PyMapping_Keys(env); |
| 3743 | vals = PyMapping_Values(env); |
| 3744 | if (!keys || !vals) |
| 3745 | goto fail_2; |
| 3746 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3747 | PyErr_SetString(PyExc_TypeError, |
| 3748 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3749 | goto fail_2; |
| 3750 | } |
| 3751 | |
| 3752 | for (pos = 0; pos < i; pos++) { |
| 3753 | char *p, *k, *v; |
| 3754 | size_t len; |
| 3755 | |
| 3756 | key = PyList_GetItem(keys, pos); |
| 3757 | val = PyList_GetItem(vals, pos); |
| 3758 | if (!key || !val) |
| 3759 | goto fail_2; |
| 3760 | |
| 3761 | if (!PyArg_Parse( |
| 3762 | key, |
| 3763 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3764 | &k) || |
| 3765 | !PyArg_Parse( |
| 3766 | val, |
| 3767 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3768 | &v)) |
| 3769 | { |
| 3770 | goto fail_2; |
| 3771 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3772 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3773 | p = PyMem_NEW(char, len); |
| 3774 | if (p == NULL) { |
| 3775 | PyErr_NoMemory(); |
| 3776 | goto fail_2; |
| 3777 | } |
| 3778 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3779 | envlist[envc++] = p; |
| 3780 | } |
| 3781 | envlist[envc] = 0; |
| 3782 | |
| 3783 | Py_BEGIN_ALLOW_THREADS |
| 3784 | #if defined(PYCC_GCC) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3785 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3786 | #else |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3787 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3788 | #endif |
| 3789 | Py_END_ALLOW_THREADS |
| 3790 | |
| 3791 | if (spawnval == -1) |
| 3792 | (void) posix_error(); |
| 3793 | else |
| 3794 | res = Py_BuildValue("l", (long) spawnval); |
| 3795 | |
| 3796 | fail_2: |
| 3797 | while (--envc >= 0) |
| 3798 | PyMem_DEL(envlist[envc]); |
| 3799 | PyMem_DEL(envlist); |
| 3800 | fail_1: |
| 3801 | free_string_array(argvlist, lastarg); |
| 3802 | Py_XDECREF(vals); |
| 3803 | Py_XDECREF(keys); |
| 3804 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3805 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3806 | return res; |
| 3807 | } |
| 3808 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3809 | #endif /* HAVE_SPAWNV */ |
| 3810 | |
| 3811 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3812 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3813 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3814 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3815 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3816 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3817 | 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] | 3818 | |
| 3819 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3820 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3821 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3822 | pid_t pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3823 | if (pid == -1) |
| 3824 | return posix_error(); |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 3825 | if (pid == 0) |
| 3826 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3827 | return PyLong_FromLong(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3828 | } |
| 3829 | #endif |
| 3830 | |
| 3831 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3832 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3833 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3834 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3835 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3836 | 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] | 3837 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3838 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3839 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3840 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3841 | pid_t pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3842 | if (pid == -1) |
| 3843 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3844 | if (pid == 0) |
| 3845 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3846 | return PyLong_FromLong(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3847 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3848 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3849 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3850 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3851 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3852 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3853 | #define DEV_PTY_FILE "/dev/ptc" |
| 3854 | #define HAVE_DEV_PTMX |
| 3855 | #else |
| 3856 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3857 | #endif |
| 3858 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3859 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3860 | #ifdef HAVE_PTY_H |
| 3861 | #include <pty.h> |
| 3862 | #else |
| 3863 | #ifdef HAVE_LIBUTIL_H |
| 3864 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3865 | #endif /* HAVE_LIBUTIL_H */ |
| 3866 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3867 | #ifdef HAVE_STROPTS_H |
| 3868 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3869 | #endif |
| 3870 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3871 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3872 | #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] | 3873 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3874 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3875 | 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] | 3876 | |
| 3877 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3878 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3879 | { |
| 3880 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3881 | #ifndef HAVE_OPENPTY |
| 3882 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3883 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3884 | #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] | 3885 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3886 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3887 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3888 | #endif |
| 3889 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3890 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3891 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3892 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3893 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3894 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3895 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3896 | if (slave_name == NULL) |
| 3897 | return posix_error(); |
| 3898 | |
| 3899 | slave_fd = open(slave_name, O_RDWR); |
| 3900 | if (slave_fd < 0) |
| 3901 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3902 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3903 | 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] | 3904 | if (master_fd < 0) |
| 3905 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3906 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3907 | /* change permission of slave */ |
| 3908 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3909 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3910 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3911 | } |
| 3912 | /* unlock slave */ |
| 3913 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3914 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3915 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3916 | } |
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 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3919 | if (slave_name == NULL) |
| 3920 | return posix_error(); |
| 3921 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3922 | if (slave_fd < 0) |
| 3923 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3924 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3925 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3926 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3927 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3928 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3929 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3930 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3931 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3932 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3933 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3934 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3935 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3936 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3937 | |
| 3938 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3939 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3940 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3941 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3942 | 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] | 3943 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3944 | |
| 3945 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3946 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3947 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3948 | int master_fd = -1; |
| 3949 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3950 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3951 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3952 | if (pid == -1) |
| 3953 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3954 | if (pid == 0) |
| 3955 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3956 | return Py_BuildValue("(li)", pid, master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3957 | } |
| 3958 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3959 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3960 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3961 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3962 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3963 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3964 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3965 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3966 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3967 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3968 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3969 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3970 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3971 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3972 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3973 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3974 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3975 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3976 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3977 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3978 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3979 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3980 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3981 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3982 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3983 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3984 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3985 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3986 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3987 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3988 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3989 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3990 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3991 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3992 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3993 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3994 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3995 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3996 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3997 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3998 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3999 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4000 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4001 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4002 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4003 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4004 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4005 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4006 | return PyLong_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4009 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4010 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4011 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4012 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4013 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4014 | |
| 4015 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4016 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4017 | { |
| 4018 | PyObject *result = NULL; |
| 4019 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4020 | #ifdef NGROUPS_MAX |
| 4021 | #define MAX_GROUPS NGROUPS_MAX |
| 4022 | #else |
| 4023 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 4024 | #define MAX_GROUPS 64 |
| 4025 | #endif |
| 4026 | gid_t grouplist[MAX_GROUPS]; |
| 4027 | int n; |
| 4028 | |
| 4029 | n = getgroups(MAX_GROUPS, grouplist); |
| 4030 | if (n < 0) |
| 4031 | posix_error(); |
| 4032 | else { |
| 4033 | result = PyList_New(n); |
| 4034 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4035 | int i; |
| 4036 | for (i = 0; i < n; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4037 | PyObject *o = PyLong_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4038 | if (o == NULL) { |
| 4039 | Py_DECREF(result); |
| 4040 | result = NULL; |
| 4041 | break; |
| 4042 | } |
| 4043 | PyList_SET_ITEM(result, i, o); |
| 4044 | } |
| 4045 | } |
| 4046 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4047 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4048 | return result; |
| 4049 | } |
| 4050 | #endif |
| 4051 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4052 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4053 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4054 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4055 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4056 | |
| 4057 | static PyObject * |
| 4058 | posix_getpgid(PyObject *self, PyObject *args) |
| 4059 | { |
| 4060 | int pid, pgid; |
| 4061 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 4062 | return NULL; |
| 4063 | pgid = getpgid(pid); |
| 4064 | if (pgid < 0) |
| 4065 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4066 | return PyLong_FromLong((long)pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4067 | } |
| 4068 | #endif /* HAVE_GETPGID */ |
| 4069 | |
| 4070 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4071 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4072 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4073 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4074 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4075 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4076 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4077 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4078 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4079 | #ifdef GETPGRP_HAVE_ARG |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4080 | return PyLong_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4081 | #else /* GETPGRP_HAVE_ARG */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4082 | return PyLong_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4083 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4084 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4085 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4086 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4087 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4088 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4089 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4090 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4091 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4092 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4093 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4094 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4095 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4096 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4097 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4098 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4099 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4100 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4101 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4102 | Py_INCREF(Py_None); |
| 4103 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4106 | #endif /* HAVE_SETPGRP */ |
| 4107 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4108 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4109 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4110 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4111 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4112 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4113 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4114 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4115 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4116 | return PyLong_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4117 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4118 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4119 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4120 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4121 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4122 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4123 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4124 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4125 | |
| 4126 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4127 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4128 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4129 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4130 | char *name; |
| 4131 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4132 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4133 | errno = 0; |
| 4134 | name = getlogin(); |
| 4135 | if (name == NULL) { |
| 4136 | if (errno) |
| 4137 | posix_error(); |
| 4138 | else |
| 4139 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 4140 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4141 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4142 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 4143 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4144 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4145 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4146 | return result; |
| 4147 | } |
| 4148 | #endif |
| 4149 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4150 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4151 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4152 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4153 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4154 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4155 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4156 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4157 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4158 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4159 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4160 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4161 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4162 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4163 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4164 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4165 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4166 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4167 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4168 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4169 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4170 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4171 | pid_t pid; |
| 4172 | int sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4173 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4174 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4175 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4176 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4177 | APIRET rc; |
| 4178 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4179 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4180 | |
| 4181 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4182 | APIRET rc; |
| 4183 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4184 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4185 | |
| 4186 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4187 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4188 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4189 | if (kill(pid, sig) == -1) |
| 4190 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4191 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4192 | Py_INCREF(Py_None); |
| 4193 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4194 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4195 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4196 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4197 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4198 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4199 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4200 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4201 | |
| 4202 | static PyObject * |
| 4203 | posix_killpg(PyObject *self, PyObject *args) |
| 4204 | { |
| 4205 | int pgid, sig; |
| 4206 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 4207 | return NULL; |
| 4208 | if (killpg(pgid, sig) == -1) |
| 4209 | return posix_error(); |
| 4210 | Py_INCREF(Py_None); |
| 4211 | return Py_None; |
| 4212 | } |
| 4213 | #endif |
| 4214 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4215 | #ifdef HAVE_PLOCK |
| 4216 | |
| 4217 | #ifdef HAVE_SYS_LOCK_H |
| 4218 | #include <sys/lock.h> |
| 4219 | #endif |
| 4220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4221 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4222 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4223 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4224 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4225 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4226 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4227 | { |
| 4228 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4229 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4230 | return NULL; |
| 4231 | if (plock(op) == -1) |
| 4232 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4233 | Py_INCREF(Py_None); |
| 4234 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4235 | } |
| 4236 | #endif |
| 4237 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4238 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4239 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4240 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4241 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4242 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4243 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4244 | Set the current process's user id."); |
| 4245 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4246 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4247 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4248 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4249 | long uid_arg; |
| 4250 | uid_t uid; |
| 4251 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4252 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4253 | uid = uid_arg; |
| 4254 | if (uid != uid_arg) { |
| 4255 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4256 | return NULL; |
| 4257 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4258 | if (setuid(uid) < 0) |
| 4259 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4260 | Py_INCREF(Py_None); |
| 4261 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4262 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4263 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4264 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4265 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4266 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4267 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4268 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4269 | Set the current process's effective user id."); |
| 4270 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4271 | static PyObject * |
| 4272 | posix_seteuid (PyObject *self, PyObject *args) |
| 4273 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4274 | long euid_arg; |
| 4275 | uid_t euid; |
| 4276 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4277 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4278 | euid = euid_arg; |
| 4279 | if (euid != euid_arg) { |
| 4280 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4281 | return NULL; |
| 4282 | } |
| 4283 | if (seteuid(euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4284 | return posix_error(); |
| 4285 | } else { |
| 4286 | Py_INCREF(Py_None); |
| 4287 | return Py_None; |
| 4288 | } |
| 4289 | } |
| 4290 | #endif /* HAVE_SETEUID */ |
| 4291 | |
| 4292 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4293 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4294 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4295 | Set the current process's effective group id."); |
| 4296 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4297 | static PyObject * |
| 4298 | posix_setegid (PyObject *self, PyObject *args) |
| 4299 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4300 | long egid_arg; |
| 4301 | gid_t egid; |
| 4302 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4303 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4304 | egid = egid_arg; |
| 4305 | if (egid != egid_arg) { |
| 4306 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4307 | return NULL; |
| 4308 | } |
| 4309 | if (setegid(egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4310 | return posix_error(); |
| 4311 | } else { |
| 4312 | Py_INCREF(Py_None); |
| 4313 | return Py_None; |
| 4314 | } |
| 4315 | } |
| 4316 | #endif /* HAVE_SETEGID */ |
| 4317 | |
| 4318 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4319 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4320 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4321 | Set the current process's real and effective user ids."); |
| 4322 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4323 | static PyObject * |
| 4324 | posix_setreuid (PyObject *self, PyObject *args) |
| 4325 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4326 | long ruid_arg, euid_arg; |
| 4327 | uid_t ruid, euid; |
| 4328 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4329 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4330 | ruid = ruid_arg; |
| 4331 | euid = euid_arg; |
| 4332 | if (euid != euid_arg || ruid != ruid_arg) { |
| 4333 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4334 | return NULL; |
| 4335 | } |
| 4336 | if (setreuid(ruid, euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4337 | return posix_error(); |
| 4338 | } else { |
| 4339 | Py_INCREF(Py_None); |
| 4340 | return Py_None; |
| 4341 | } |
| 4342 | } |
| 4343 | #endif /* HAVE_SETREUID */ |
| 4344 | |
| 4345 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4346 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4347 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4348 | Set the current process's real and effective group ids."); |
| 4349 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4350 | static PyObject * |
| 4351 | posix_setregid (PyObject *self, PyObject *args) |
| 4352 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4353 | long rgid_arg, egid_arg; |
| 4354 | gid_t rgid, egid; |
| 4355 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4356 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4357 | rgid = rgid_arg; |
| 4358 | egid = egid_arg; |
| 4359 | if (egid != egid_arg || rgid != rgid_arg) { |
| 4360 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4361 | return NULL; |
| 4362 | } |
| 4363 | if (setregid(rgid, egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4364 | return posix_error(); |
| 4365 | } else { |
| 4366 | Py_INCREF(Py_None); |
| 4367 | return Py_None; |
| 4368 | } |
| 4369 | } |
| 4370 | #endif /* HAVE_SETREGID */ |
| 4371 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4372 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4373 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4374 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4375 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4377 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4378 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4379 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4380 | long gid_arg; |
| 4381 | gid_t gid; |
| 4382 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4383 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4384 | gid = gid_arg; |
| 4385 | if (gid != gid_arg) { |
| 4386 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4387 | return NULL; |
| 4388 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4389 | if (setgid(gid) < 0) |
| 4390 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4391 | Py_INCREF(Py_None); |
| 4392 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4393 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4394 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4395 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4396 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4397 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4398 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4399 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4400 | |
| 4401 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4402 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4403 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4404 | int i, len; |
| 4405 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4406 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4407 | if (!PySequence_Check(groups)) { |
| 4408 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4409 | return NULL; |
| 4410 | } |
| 4411 | len = PySequence_Size(groups); |
| 4412 | if (len > MAX_GROUPS) { |
| 4413 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4414 | return NULL; |
| 4415 | } |
| 4416 | for(i = 0; i < len; i++) { |
| 4417 | PyObject *elem; |
| 4418 | elem = PySequence_GetItem(groups, i); |
| 4419 | if (!elem) |
| 4420 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4421 | if (!PyLong_Check(elem)) { |
| 4422 | PyErr_SetString(PyExc_TypeError, |
| 4423 | "groups must be integers"); |
| 4424 | Py_DECREF(elem); |
| 4425 | return NULL; |
| 4426 | } else { |
| 4427 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4428 | if (PyErr_Occurred()) { |
| 4429 | PyErr_SetString(PyExc_TypeError, |
| 4430 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4431 | Py_DECREF(elem); |
| 4432 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4433 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4434 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4435 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4436 | if (grouplist[i] != x) { |
| 4437 | PyErr_SetString(PyExc_TypeError, |
| 4438 | "group id too big"); |
| 4439 | Py_DECREF(elem); |
| 4440 | return NULL; |
| 4441 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4442 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4443 | Py_DECREF(elem); |
| 4444 | } |
| 4445 | |
| 4446 | if (setgroups(len, grouplist) < 0) |
| 4447 | return posix_error(); |
| 4448 | Py_INCREF(Py_None); |
| 4449 | return Py_None; |
| 4450 | } |
| 4451 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4452 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4453 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4454 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4455 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4456 | { |
| 4457 | PyObject *result; |
| 4458 | static PyObject *struct_rusage; |
| 4459 | |
| 4460 | if (pid == -1) |
| 4461 | return posix_error(); |
| 4462 | |
| 4463 | if (struct_rusage == NULL) { |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4464 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4465 | if (m == NULL) |
| 4466 | return NULL; |
| 4467 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4468 | Py_DECREF(m); |
| 4469 | if (struct_rusage == NULL) |
| 4470 | return NULL; |
| 4471 | } |
| 4472 | |
| 4473 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4474 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4475 | if (!result) |
| 4476 | return NULL; |
| 4477 | |
| 4478 | #ifndef doubletime |
| 4479 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4480 | #endif |
| 4481 | |
| 4482 | PyStructSequence_SET_ITEM(result, 0, |
| 4483 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4484 | PyStructSequence_SET_ITEM(result, 1, |
| 4485 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4486 | #define SET_INT(result, index, value)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4487 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4488 | SET_INT(result, 2, ru->ru_maxrss); |
| 4489 | SET_INT(result, 3, ru->ru_ixrss); |
| 4490 | SET_INT(result, 4, ru->ru_idrss); |
| 4491 | SET_INT(result, 5, ru->ru_isrss); |
| 4492 | SET_INT(result, 6, ru->ru_minflt); |
| 4493 | SET_INT(result, 7, ru->ru_majflt); |
| 4494 | SET_INT(result, 8, ru->ru_nswap); |
| 4495 | SET_INT(result, 9, ru->ru_inblock); |
| 4496 | SET_INT(result, 10, ru->ru_oublock); |
| 4497 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4498 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4499 | SET_INT(result, 13, ru->ru_nsignals); |
| 4500 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4501 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4502 | #undef SET_INT |
| 4503 | |
| 4504 | if (PyErr_Occurred()) { |
| 4505 | Py_DECREF(result); |
| 4506 | return NULL; |
| 4507 | } |
| 4508 | |
| 4509 | return Py_BuildValue("iiN", pid, status, result); |
| 4510 | } |
| 4511 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4512 | |
| 4513 | #ifdef HAVE_WAIT3 |
| 4514 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4515 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4516 | Wait for completion of a child process."); |
| 4517 | |
| 4518 | static PyObject * |
| 4519 | posix_wait3(PyObject *self, PyObject *args) |
| 4520 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4521 | pid_t pid; |
| 4522 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4523 | struct rusage ru; |
| 4524 | WAIT_TYPE status; |
| 4525 | WAIT_STATUS_INT(status) = 0; |
| 4526 | |
| 4527 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4528 | return NULL; |
| 4529 | |
| 4530 | Py_BEGIN_ALLOW_THREADS |
| 4531 | pid = wait3(&status, options, &ru); |
| 4532 | Py_END_ALLOW_THREADS |
| 4533 | |
| 4534 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4535 | } |
| 4536 | #endif /* HAVE_WAIT3 */ |
| 4537 | |
| 4538 | #ifdef HAVE_WAIT4 |
| 4539 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4540 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4541 | Wait for completion of a given child process."); |
| 4542 | |
| 4543 | static PyObject * |
| 4544 | posix_wait4(PyObject *self, PyObject *args) |
| 4545 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4546 | pid_t pid; |
| 4547 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4548 | struct rusage ru; |
| 4549 | WAIT_TYPE status; |
| 4550 | WAIT_STATUS_INT(status) = 0; |
| 4551 | |
| 4552 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4553 | return NULL; |
| 4554 | |
| 4555 | Py_BEGIN_ALLOW_THREADS |
| 4556 | pid = wait4(pid, &status, options, &ru); |
| 4557 | Py_END_ALLOW_THREADS |
| 4558 | |
| 4559 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4560 | } |
| 4561 | #endif /* HAVE_WAIT4 */ |
| 4562 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4563 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4564 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4565 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4566 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4567 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4568 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4569 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4570 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4571 | pid_t pid; |
| 4572 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4573 | WAIT_TYPE status; |
| 4574 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4575 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4576 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4577 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4578 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4579 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4580 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4581 | if (pid == -1) |
| 4582 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4583 | |
| 4584 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4587 | #elif defined(HAVE_CWAIT) |
| 4588 | |
| 4589 | /* 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] | 4590 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4591 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4592 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4593 | |
| 4594 | static PyObject * |
| 4595 | posix_waitpid(PyObject *self, PyObject *args) |
| 4596 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4597 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4598 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4599 | |
| 4600 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4601 | return NULL; |
| 4602 | Py_BEGIN_ALLOW_THREADS |
| 4603 | pid = _cwait(&status, pid, options); |
| 4604 | Py_END_ALLOW_THREADS |
| 4605 | if (pid == -1) |
| 4606 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4607 | |
| 4608 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4609 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4610 | } |
| 4611 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4612 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4613 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4614 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4615 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4616 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4617 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4618 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4619 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4620 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4621 | pid_t pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4622 | WAIT_TYPE status; |
| 4623 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4624 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4625 | Py_BEGIN_ALLOW_THREADS |
| 4626 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4627 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4628 | if (pid == -1) |
| 4629 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4630 | |
| 4631 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4632 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4633 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4634 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4635 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4636 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4637 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4638 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4639 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4640 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4641 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4642 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4643 | #ifdef HAVE_LSTAT |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4644 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4645 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4646 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4647 | 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] | 4648 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4649 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4650 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4651 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4654 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4655 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4656 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4657 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4658 | 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] | 4659 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4660 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4661 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4662 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4663 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4664 | char buf[MAXPATHLEN]; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4665 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4666 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4667 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4668 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4669 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4670 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 4671 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4672 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4673 | path = bytes2str(opath, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4674 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4675 | if (v == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4676 | release_bytes(opath); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4677 | return NULL; |
| 4678 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4679 | |
| 4680 | if (PyUnicode_Check(v)) { |
| 4681 | arg_is_unicode = 1; |
| 4682 | } |
| 4683 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4684 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4685 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4686 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4687 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4688 | if (n < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4689 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4690 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4691 | release_bytes(opath); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4692 | v = PyBytes_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4693 | if (arg_is_unicode) { |
| 4694 | PyObject *w; |
| 4695 | |
| 4696 | w = PyUnicode_FromEncodedObject(v, |
| 4697 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame^] | 4698 | "surrogateescape"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4699 | if (w != NULL) { |
| 4700 | Py_DECREF(v); |
| 4701 | v = w; |
| 4702 | } |
| 4703 | else { |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4704 | v = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4705 | } |
| 4706 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4707 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4708 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4709 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4710 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4711 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4712 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4713 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4714 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4715 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4716 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4717 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4718 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4719 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4720 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4721 | } |
| 4722 | #endif /* HAVE_SYMLINK */ |
| 4723 | |
| 4724 | |
| 4725 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4726 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4727 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4728 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4729 | { |
| 4730 | ULONG value = 0; |
| 4731 | |
| 4732 | Py_BEGIN_ALLOW_THREADS |
| 4733 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4734 | Py_END_ALLOW_THREADS |
| 4735 | |
| 4736 | return value; |
| 4737 | } |
| 4738 | |
| 4739 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4740 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4741 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4742 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4743 | return Py_BuildValue("ddddd", |
| 4744 | (double)0 /* t.tms_utime / HZ */, |
| 4745 | (double)0 /* t.tms_stime / HZ */, |
| 4746 | (double)0 /* t.tms_cutime / HZ */, |
| 4747 | (double)0 /* t.tms_cstime / HZ */, |
| 4748 | (double)system_uptime() / 1000); |
| 4749 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4750 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4751 | #define NEED_TICKS_PER_SECOND |
| 4752 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4753 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4754 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4755 | { |
| 4756 | struct tms t; |
| 4757 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4758 | errno = 0; |
| 4759 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4760 | if (c == (clock_t) -1) |
| 4761 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4762 | return Py_BuildValue("ddddd", |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4763 | (double)t.tms_utime / ticks_per_second, |
| 4764 | (double)t.tms_stime / ticks_per_second, |
| 4765 | (double)t.tms_cutime / ticks_per_second, |
| 4766 | (double)t.tms_cstime / ticks_per_second, |
| 4767 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4768 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4769 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4770 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4771 | |
| 4772 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4773 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4774 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4775 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4776 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4777 | { |
| 4778 | FILETIME create, exit, kernel, user; |
| 4779 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4780 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4781 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4782 | /* The fields of a FILETIME structure are the hi and lo part |
| 4783 | of a 64-bit value expressed in 100 nanosecond units. |
| 4784 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4785 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4786 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4787 | return Py_BuildValue( |
| 4788 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4789 | (double)(user.dwHighDateTime*429.4967296 + |
| 4790 | user.dwLowDateTime*1e-7), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 4791 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4792 | kernel.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4793 | (double)0, |
| 4794 | (double)0, |
| 4795 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4796 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4797 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4798 | |
| 4799 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4800 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4801 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4802 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4803 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4805 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4806 | #ifdef HAVE_GETSID |
| 4807 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4808 | "getsid(pid) -> sid\n\n\ |
| 4809 | Call the system call getsid()."); |
| 4810 | |
| 4811 | static PyObject * |
| 4812 | posix_getsid(PyObject *self, PyObject *args) |
| 4813 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4814 | pid_t pid; |
| 4815 | int sid; |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4816 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4817 | return NULL; |
| 4818 | sid = getsid(pid); |
| 4819 | if (sid < 0) |
| 4820 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4821 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4822 | } |
| 4823 | #endif /* HAVE_GETSID */ |
| 4824 | |
| 4825 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4826 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4827 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4828 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4829 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4830 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4831 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4832 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4833 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4834 | if (setsid() < 0) |
| 4835 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4836 | Py_INCREF(Py_None); |
| 4837 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4838 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4839 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4840 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4841 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4842 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4843 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4844 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4845 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4846 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4847 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4848 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4849 | pid_t pid; |
| 4850 | int pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4851 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4852 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4853 | if (setpgid(pid, pgrp) < 0) |
| 4854 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4855 | Py_INCREF(Py_None); |
| 4856 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4857 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4858 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4859 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4860 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4861 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4862 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4863 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4864 | 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] | 4865 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4866 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4867 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4868 | { |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4869 | int fd; |
| 4870 | pid_t pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4871 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4872 | return NULL; |
| 4873 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4874 | if (pgid < 0) |
| 4875 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4876 | return PyLong_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4877 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4878 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4879 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4880 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4881 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4882 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4883 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4884 | 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] | 4885 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4886 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4887 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4888 | { |
| 4889 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4890 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4891 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4892 | if (tcsetpgrp(fd, pgid) < 0) |
| 4893 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4894 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4895 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4896 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4897 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4898 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4899 | /* Functions acting on file descriptors */ |
| 4900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4901 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4902 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4903 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4904 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4905 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4906 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4907 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4908 | PyObject *ofile; |
| 4909 | char *file; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4910 | int flag; |
| 4911 | int mode = 0777; |
| 4912 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4913 | |
| 4914 | #ifdef MS_WINDOWS |
| 4915 | if (unicode_file_names()) { |
| 4916 | PyUnicodeObject *po; |
| 4917 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4918 | Py_BEGIN_ALLOW_THREADS |
| 4919 | /* PyUnicode_AS_UNICODE OK without thread |
| 4920 | lock as it is a simple dereference. */ |
| 4921 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4922 | Py_END_ALLOW_THREADS |
| 4923 | if (fd < 0) |
| 4924 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4925 | return PyLong_FromLong((long)fd); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4926 | } |
| 4927 | /* Drop the argument parsing error as narrow strings |
| 4928 | are also valid. */ |
| 4929 | PyErr_Clear(); |
| 4930 | } |
| 4931 | #endif |
| 4932 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4933 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 4934 | PyUnicode_FSConverter, &ofile, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4935 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4936 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4937 | file = bytes2str(ofile, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4938 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4939 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4940 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4941 | if (fd < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4942 | return posix_error_with_allocated_filename(ofile); |
| 4943 | release_bytes(ofile); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4944 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4947 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4948 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4949 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4950 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4951 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4952 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4953 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4954 | { |
| 4955 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4956 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4957 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4958 | if (!_PyVerify_fd(fd)) |
| 4959 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4960 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4961 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4962 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4963 | if (res < 0) |
| 4964 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4965 | Py_INCREF(Py_None); |
| 4966 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4969 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4970 | PyDoc_STRVAR(posix_closerange__doc__, |
| 4971 | "closerange(fd_low, fd_high)\n\n\ |
| 4972 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 4973 | |
| 4974 | static PyObject * |
| 4975 | posix_closerange(PyObject *self, PyObject *args) |
| 4976 | { |
| 4977 | int fd_from, fd_to, i; |
| 4978 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 4979 | return NULL; |
| 4980 | Py_BEGIN_ALLOW_THREADS |
| 4981 | for (i = fd_from; i < fd_to; i++) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4982 | if (_PyVerify_fd(i)) |
| 4983 | close(i); |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4984 | Py_END_ALLOW_THREADS |
| 4985 | Py_RETURN_NONE; |
| 4986 | } |
| 4987 | |
| 4988 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4989 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4990 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4991 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4992 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4993 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4994 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4995 | { |
| 4996 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4997 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4998 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4999 | if (!_PyVerify_fd(fd)) |
| 5000 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5001 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5002 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5003 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5004 | if (fd < 0) |
| 5005 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5006 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5009 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5010 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5011 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5012 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5013 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5014 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5015 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5016 | { |
| 5017 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5018 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5019 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5020 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 5021 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5022 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5023 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5024 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5025 | if (res < 0) |
| 5026 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5027 | Py_INCREF(Py_None); |
| 5028 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5032 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5033 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5034 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5035 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5036 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5037 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5038 | { |
| 5039 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5040 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5041 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5042 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5043 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5044 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5045 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5046 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5047 | return NULL; |
| 5048 | #ifdef SEEK_SET |
| 5049 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5050 | switch (how) { |
| 5051 | case 0: how = SEEK_SET; break; |
| 5052 | case 1: how = SEEK_CUR; break; |
| 5053 | case 2: how = SEEK_END; break; |
| 5054 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5055 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5056 | |
| 5057 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5058 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5059 | #else |
| 5060 | pos = PyLong_Check(posobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5061 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5062 | #endif |
| 5063 | if (PyErr_Occurred()) |
| 5064 | return NULL; |
| 5065 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5066 | if (!_PyVerify_fd(fd)) |
| 5067 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5068 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5069 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5070 | res = _lseeki64(fd, pos, how); |
| 5071 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5072 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5073 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5074 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5075 | if (res < 0) |
| 5076 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5077 | |
| 5078 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5079 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5080 | #else |
| 5081 | return PyLong_FromLongLong(res); |
| 5082 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5086 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5087 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5088 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5089 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5090 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5091 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5092 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 5093 | int fd, size; |
| 5094 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5095 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5096 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5097 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5098 | if (size < 0) { |
| 5099 | errno = EINVAL; |
| 5100 | return posix_error(); |
| 5101 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5102 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5103 | if (buffer == NULL) |
| 5104 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5105 | if (!_PyVerify_fd(fd)) |
| 5106 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5107 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5108 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5109 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5110 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5111 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5112 | return posix_error(); |
| 5113 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5114 | if (n != size) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5115 | _PyBytes_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5116 | return buffer; |
| 5117 | } |
| 5118 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5119 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5120 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5121 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5122 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5123 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5124 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5125 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5126 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5127 | Py_buffer pbuf; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5128 | int fd; |
| 5129 | Py_ssize_t size; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5130 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 5131 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5132 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5133 | if (!_PyVerify_fd(fd)) |
| 5134 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5135 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5136 | size = write(fd, pbuf.buf, (size_t)pbuf.len); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5137 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5138 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5139 | if (size < 0) |
| 5140 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5141 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5144 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5145 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5146 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5147 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5148 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5149 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5150 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5151 | { |
| 5152 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5153 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5154 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5155 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5156 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5157 | #ifdef __VMS |
| 5158 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5159 | fsync(fd); |
| 5160 | #endif |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5161 | if (!_PyVerify_fd(fd)) |
| 5162 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5163 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5164 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5165 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5166 | if (res != 0) { |
| 5167 | #ifdef MS_WINDOWS |
| 5168 | return win32_error("fstat", NULL); |
| 5169 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5170 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5171 | #endif |
| 5172 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5173 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5174 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5177 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5178 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5179 | 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] | 5180 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5181 | |
| 5182 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5183 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5184 | { |
| 5185 | int fd; |
| 5186 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5187 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5188 | if (!_PyVerify_fd(fd)) |
| 5189 | return PyBool_FromLong(0); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5190 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5191 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5192 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5193 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5194 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5195 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5196 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5197 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5198 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5199 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5200 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5201 | #if defined(PYOS_OS2) |
| 5202 | HFILE read, write; |
| 5203 | APIRET rc; |
| 5204 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5205 | Py_BEGIN_ALLOW_THREADS |
| 5206 | rc = DosCreatePipe( &read, &write, 4096); |
| 5207 | Py_END_ALLOW_THREADS |
| 5208 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5209 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5210 | |
| 5211 | return Py_BuildValue("(ii)", read, write); |
| 5212 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5213 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5214 | int fds[2]; |
| 5215 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5216 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5217 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5218 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5219 | if (res != 0) |
| 5220 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5221 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5222 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5223 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5224 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5225 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5226 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5227 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5228 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5229 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5230 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5231 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5232 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5233 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5234 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5235 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5236 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5237 | #endif /* HAVE_PIPE */ |
| 5238 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5239 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5240 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5241 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5242 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5243 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5244 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5245 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5246 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5247 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5248 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5249 | int mode = 0666; |
| 5250 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5251 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5252 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5253 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5254 | res = mkfifo(filename, mode); |
| 5255 | Py_END_ALLOW_THREADS |
| 5256 | if (res < 0) |
| 5257 | return posix_error(); |
| 5258 | Py_INCREF(Py_None); |
| 5259 | return Py_None; |
| 5260 | } |
| 5261 | #endif |
| 5262 | |
| 5263 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5264 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5265 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5266 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5267 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5268 | named filename. mode specifies both the permissions to use and the\n\ |
| 5269 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5270 | 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] | 5271 | device defines the newly created device special file (probably using\n\ |
| 5272 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5273 | |
| 5274 | |
| 5275 | static PyObject * |
| 5276 | posix_mknod(PyObject *self, PyObject *args) |
| 5277 | { |
| 5278 | char *filename; |
| 5279 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5280 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5281 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5282 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5283 | return NULL; |
| 5284 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5285 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5286 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5287 | if (res < 0) |
| 5288 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5289 | Py_INCREF(Py_None); |
| 5290 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5291 | } |
| 5292 | #endif |
| 5293 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5294 | #ifdef HAVE_DEVICE_MACROS |
| 5295 | PyDoc_STRVAR(posix_major__doc__, |
| 5296 | "major(device) -> major number\n\ |
| 5297 | Extracts a device major number from a raw device number."); |
| 5298 | |
| 5299 | static PyObject * |
| 5300 | posix_major(PyObject *self, PyObject *args) |
| 5301 | { |
| 5302 | int device; |
| 5303 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5304 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5305 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | PyDoc_STRVAR(posix_minor__doc__, |
| 5309 | "minor(device) -> minor number\n\ |
| 5310 | Extracts a device minor number from a raw device number."); |
| 5311 | |
| 5312 | static PyObject * |
| 5313 | posix_minor(PyObject *self, PyObject *args) |
| 5314 | { |
| 5315 | int device; |
| 5316 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5317 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5318 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
| 5321 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5322 | "makedev(major, minor) -> device number\n\ |
| 5323 | Composes a raw device number from the major and minor device numbers."); |
| 5324 | |
| 5325 | static PyObject * |
| 5326 | posix_makedev(PyObject *self, PyObject *args) |
| 5327 | { |
| 5328 | int major, minor; |
| 5329 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5330 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5331 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5332 | } |
| 5333 | #endif /* device macros */ |
| 5334 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5335 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5336 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5337 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5338 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5339 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5340 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5341 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5342 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5343 | { |
| 5344 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5345 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5346 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5347 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5348 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5349 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5350 | return NULL; |
| 5351 | |
| 5352 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5353 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5354 | #else |
| 5355 | length = PyLong_Check(lenobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5356 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5357 | #endif |
| 5358 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5359 | return NULL; |
| 5360 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5361 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5362 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5363 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 9053d75 | 2009-01-19 17:53:36 +0000 | [diff] [blame] | 5364 | if (res < 0) |
| 5365 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5366 | Py_INCREF(Py_None); |
| 5367 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5368 | } |
| 5369 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5370 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5371 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5372 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5373 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5374 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5375 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5376 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5377 | * get re-set with another call for the same key. */ |
| 5378 | static PyObject *posix_putenv_garbage; |
| 5379 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5380 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5381 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5382 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5383 | #ifdef MS_WINDOWS |
| 5384 | wchar_t *s1, *s2; |
| 5385 | wchar_t *newenv; |
| 5386 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5387 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5388 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5389 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5390 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5391 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5392 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5393 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5394 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5395 | if (!PyArg_ParseTuple(args, |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5396 | "uu:putenv", |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5397 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5398 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5399 | #else |
| 5400 | if (!PyArg_ParseTuple(args, |
| 5401 | "O&O&:putenv", |
| 5402 | PyUnicode_FSConverter, &os1, |
| 5403 | PyUnicode_FSConverter, &os2)) |
| 5404 | return NULL; |
| 5405 | s1 = bytes2str(os1, 1); |
| 5406 | s2 = bytes2str(os2, 1); |
| 5407 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5408 | |
| 5409 | #if defined(PYOS_OS2) |
| 5410 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5411 | APIRET rc; |
| 5412 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5413 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5414 | if (rc != NO_ERROR) |
| 5415 | return os2_error(rc); |
| 5416 | |
| 5417 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5418 | APIRET rc; |
| 5419 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5420 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5421 | if (rc != NO_ERROR) |
| 5422 | return os2_error(rc); |
| 5423 | } else { |
| 5424 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5425 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5426 | /* len includes space for a trailing \0; the size arg to |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5427 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5428 | #ifdef MS_WINDOWS |
| 5429 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5430 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5431 | #else |
| 5432 | len = strlen(s1) + strlen(s2) + 2; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5433 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5434 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5435 | if (newstr == NULL) |
| 5436 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5437 | #ifdef MS_WINDOWS |
| 5438 | newenv = PyUnicode_AsUnicode(newstr); |
| 5439 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5440 | if (_wputenv(newenv)) { |
| 5441 | Py_DECREF(newstr); |
| 5442 | posix_error(); |
| 5443 | return NULL; |
| 5444 | } |
| 5445 | #else |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5446 | newenv = PyBytes_AS_STRING(newstr); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5447 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5448 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5449 | Py_DECREF(newstr); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5450 | release_bytes(os1); |
| 5451 | release_bytes(os2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5452 | posix_error(); |
| 5453 | return NULL; |
| 5454 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5455 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5456 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5457 | * this will cause previous value to be collected. This has to |
| 5458 | * happen after the real putenv() call because the old value |
| 5459 | * was still accessible until then. */ |
| 5460 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5461 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5462 | /* really not much we can do; just leak */ |
| 5463 | PyErr_Clear(); |
| 5464 | } |
| 5465 | else { |
| 5466 | Py_DECREF(newstr); |
| 5467 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5468 | |
| 5469 | #if defined(PYOS_OS2) |
| 5470 | } |
| 5471 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5472 | #ifndef MS_WINDOWS |
| 5473 | release_bytes(os1); |
| 5474 | release_bytes(os2); |
| 5475 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5476 | Py_INCREF(Py_None); |
| 5477 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5478 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5479 | #endif /* putenv */ |
| 5480 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5481 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5482 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5483 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5484 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5485 | |
| 5486 | static PyObject * |
| 5487 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5488 | { |
| 5489 | char *s1; |
| 5490 | |
| 5491 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5492 | return NULL; |
| 5493 | |
| 5494 | unsetenv(s1); |
| 5495 | |
| 5496 | /* Remove the key from posix_putenv_garbage; |
| 5497 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5498 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5499 | * old value was still accessible until then. |
| 5500 | */ |
| 5501 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5502 | PyTuple_GET_ITEM(args, 0))) { |
| 5503 | /* really not much we can do; just leak */ |
| 5504 | PyErr_Clear(); |
| 5505 | } |
| 5506 | |
| 5507 | Py_INCREF(Py_None); |
| 5508 | return Py_None; |
| 5509 | } |
| 5510 | #endif /* unsetenv */ |
| 5511 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5512 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5513 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5514 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5515 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5516 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5517 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5518 | { |
| 5519 | int code; |
| 5520 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5521 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5522 | return NULL; |
| 5523 | message = strerror(code); |
| 5524 | if (message == NULL) { |
| 5525 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5526 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5527 | return NULL; |
| 5528 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5529 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5530 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5531 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5532 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5533 | #ifdef HAVE_SYS_WAIT_H |
| 5534 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5535 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5536 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5537 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5538 | 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] | 5539 | |
| 5540 | static PyObject * |
| 5541 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5542 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5543 | WAIT_TYPE status; |
| 5544 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5545 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5546 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5547 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5548 | |
| 5549 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5550 | } |
| 5551 | #endif /* WCOREDUMP */ |
| 5552 | |
| 5553 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5554 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5555 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5556 | 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] | 5557 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5558 | |
| 5559 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5560 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5561 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5562 | WAIT_TYPE status; |
| 5563 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5566 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5567 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5568 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5569 | } |
| 5570 | #endif /* WIFCONTINUED */ |
| 5571 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5572 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5573 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5574 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5575 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5576 | |
| 5577 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5578 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5579 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5580 | WAIT_TYPE status; |
| 5581 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5582 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5583 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5584 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5585 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5586 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5587 | } |
| 5588 | #endif /* WIFSTOPPED */ |
| 5589 | |
| 5590 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5591 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5592 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5593 | 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] | 5594 | |
| 5595 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5596 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5597 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5598 | WAIT_TYPE status; |
| 5599 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5600 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5601 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5602 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5603 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5604 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5605 | } |
| 5606 | #endif /* WIFSIGNALED */ |
| 5607 | |
| 5608 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5609 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5610 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5611 | 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] | 5612 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5613 | |
| 5614 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5615 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5616 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5617 | WAIT_TYPE status; |
| 5618 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5619 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5620 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5621 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5622 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5623 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5624 | } |
| 5625 | #endif /* WIFEXITED */ |
| 5626 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5627 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5628 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5629 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5630 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5631 | |
| 5632 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5633 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5634 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5635 | WAIT_TYPE status; |
| 5636 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5637 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5638 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5639 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5640 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5641 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5642 | } |
| 5643 | #endif /* WEXITSTATUS */ |
| 5644 | |
| 5645 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5646 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5647 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5648 | 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] | 5649 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5650 | |
| 5651 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5652 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5653 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5654 | WAIT_TYPE status; |
| 5655 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5656 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5657 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5658 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5659 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5660 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5661 | } |
| 5662 | #endif /* WTERMSIG */ |
| 5663 | |
| 5664 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5665 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5666 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5667 | Return the signal that stopped the process that provided\n\ |
| 5668 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5669 | |
| 5670 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5671 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5672 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5673 | WAIT_TYPE status; |
| 5674 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5675 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5676 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5677 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5678 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5679 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5680 | } |
| 5681 | #endif /* WSTOPSIG */ |
| 5682 | |
| 5683 | #endif /* HAVE_SYS_WAIT_H */ |
| 5684 | |
| 5685 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5686 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5687 | #ifdef _SCO_DS |
| 5688 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5689 | needed definitions in sys/statvfs.h */ |
| 5690 | #define _SVID3 |
| 5691 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5692 | #include <sys/statvfs.h> |
| 5693 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5694 | static PyObject* |
| 5695 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5696 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5697 | if (v == NULL) |
| 5698 | return NULL; |
| 5699 | |
| 5700 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5701 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5702 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5703 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5704 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5705 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5706 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5707 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5708 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 5709 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5710 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5711 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5712 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5713 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5714 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5715 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5716 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5717 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5718 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5719 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5720 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5721 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5722 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5723 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5724 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5725 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5726 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5727 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5728 | #endif |
| 5729 | |
| 5730 | return v; |
| 5731 | } |
| 5732 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5733 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5734 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5735 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5736 | |
| 5737 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5738 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5739 | { |
| 5740 | int fd, res; |
| 5741 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5742 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5743 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5744 | return NULL; |
| 5745 | Py_BEGIN_ALLOW_THREADS |
| 5746 | res = fstatvfs(fd, &st); |
| 5747 | Py_END_ALLOW_THREADS |
| 5748 | if (res != 0) |
| 5749 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5750 | |
| 5751 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5752 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5753 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5754 | |
| 5755 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5756 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5757 | #include <sys/statvfs.h> |
| 5758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5759 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5760 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5761 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5762 | |
| 5763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5764 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5765 | { |
| 5766 | char *path; |
| 5767 | int res; |
| 5768 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5769 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5770 | return NULL; |
| 5771 | Py_BEGIN_ALLOW_THREADS |
| 5772 | res = statvfs(path, &st); |
| 5773 | Py_END_ALLOW_THREADS |
| 5774 | if (res != 0) |
| 5775 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5776 | |
| 5777 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5778 | } |
| 5779 | #endif /* HAVE_STATVFS */ |
| 5780 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5781 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5782 | * It maps strings representing configuration variable names to |
| 5783 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5784 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5785 | * rarely-used constants. There are three separate tables that use |
| 5786 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5787 | * |
| 5788 | * This code is always included, even if none of the interfaces that |
| 5789 | * need it are included. The #if hackery needed to avoid it would be |
| 5790 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5791 | */ |
| 5792 | struct constdef { |
| 5793 | char *name; |
| 5794 | long value; |
| 5795 | }; |
| 5796 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5797 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5798 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5799 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5800 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5801 | if (PyLong_Check(arg)) { |
| 5802 | *valuep = PyLong_AS_LONG(arg); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5803 | return 1; |
| 5804 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5805 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5806 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5807 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5808 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5809 | size_t hi = tablesize; |
| 5810 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5811 | const char *confname; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5812 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5813 | PyErr_SetString(PyExc_TypeError, |
| 5814 | "configuration names must be strings or integers"); |
| 5815 | return 0; |
| 5816 | } |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 5817 | confname = _PyUnicode_AsString(arg); |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5818 | if (confname == NULL) |
| 5819 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5820 | while (lo < hi) { |
| 5821 | mid = (lo + hi) / 2; |
| 5822 | cmp = strcmp(confname, table[mid].name); |
| 5823 | if (cmp < 0) |
| 5824 | hi = mid; |
| 5825 | else if (cmp > 0) |
| 5826 | lo = mid + 1; |
| 5827 | else { |
| 5828 | *valuep = table[mid].value; |
| 5829 | return 1; |
| 5830 | } |
| 5831 | } |
| 5832 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5833 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5834 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5835 | } |
| 5836 | |
| 5837 | |
| 5838 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5839 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5840 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5841 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5842 | #endif |
| 5843 | #ifdef _PC_ABI_ASYNC_IO |
| 5844 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5845 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5846 | #ifdef _PC_ASYNC_IO |
| 5847 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5848 | #endif |
| 5849 | #ifdef _PC_CHOWN_RESTRICTED |
| 5850 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5851 | #endif |
| 5852 | #ifdef _PC_FILESIZEBITS |
| 5853 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5854 | #endif |
| 5855 | #ifdef _PC_LAST |
| 5856 | {"PC_LAST", _PC_LAST}, |
| 5857 | #endif |
| 5858 | #ifdef _PC_LINK_MAX |
| 5859 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5860 | #endif |
| 5861 | #ifdef _PC_MAX_CANON |
| 5862 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5863 | #endif |
| 5864 | #ifdef _PC_MAX_INPUT |
| 5865 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5866 | #endif |
| 5867 | #ifdef _PC_NAME_MAX |
| 5868 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5869 | #endif |
| 5870 | #ifdef _PC_NO_TRUNC |
| 5871 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5872 | #endif |
| 5873 | #ifdef _PC_PATH_MAX |
| 5874 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5875 | #endif |
| 5876 | #ifdef _PC_PIPE_BUF |
| 5877 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5878 | #endif |
| 5879 | #ifdef _PC_PRIO_IO |
| 5880 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5881 | #endif |
| 5882 | #ifdef _PC_SOCK_MAXBUF |
| 5883 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5884 | #endif |
| 5885 | #ifdef _PC_SYNC_IO |
| 5886 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5887 | #endif |
| 5888 | #ifdef _PC_VDISABLE |
| 5889 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5890 | #endif |
| 5891 | }; |
| 5892 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5893 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5894 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5895 | { |
| 5896 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5897 | sizeof(posix_constants_pathconf) |
| 5898 | / sizeof(struct constdef)); |
| 5899 | } |
| 5900 | #endif |
| 5901 | |
| 5902 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5903 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5904 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5905 | 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] | 5906 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5907 | |
| 5908 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5909 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5910 | { |
| 5911 | PyObject *result = NULL; |
| 5912 | int name, fd; |
| 5913 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5914 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5915 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5916 | long limit; |
| 5917 | |
| 5918 | errno = 0; |
| 5919 | limit = fpathconf(fd, name); |
| 5920 | if (limit == -1 && errno != 0) |
| 5921 | posix_error(); |
| 5922 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5923 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5924 | } |
| 5925 | return result; |
| 5926 | } |
| 5927 | #endif |
| 5928 | |
| 5929 | |
| 5930 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5931 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5932 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5933 | 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] | 5934 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5935 | |
| 5936 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5937 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5938 | { |
| 5939 | PyObject *result = NULL; |
| 5940 | int name; |
| 5941 | char *path; |
| 5942 | |
| 5943 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5944 | conv_path_confname, &name)) { |
| 5945 | long limit; |
| 5946 | |
| 5947 | errno = 0; |
| 5948 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5949 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5950 | if (errno == EINVAL) |
| 5951 | /* could be a path or name problem */ |
| 5952 | posix_error(); |
| 5953 | else |
| 5954 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5955 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5956 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5957 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5958 | } |
| 5959 | return result; |
| 5960 | } |
| 5961 | #endif |
| 5962 | |
| 5963 | #ifdef HAVE_CONFSTR |
| 5964 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5965 | #ifdef _CS_ARCHITECTURE |
| 5966 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5967 | #endif |
| 5968 | #ifdef _CS_HOSTNAME |
| 5969 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5970 | #endif |
| 5971 | #ifdef _CS_HW_PROVIDER |
| 5972 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5973 | #endif |
| 5974 | #ifdef _CS_HW_SERIAL |
| 5975 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5976 | #endif |
| 5977 | #ifdef _CS_INITTAB_NAME |
| 5978 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5979 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5980 | #ifdef _CS_LFS64_CFLAGS |
| 5981 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5982 | #endif |
| 5983 | #ifdef _CS_LFS64_LDFLAGS |
| 5984 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5985 | #endif |
| 5986 | #ifdef _CS_LFS64_LIBS |
| 5987 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5988 | #endif |
| 5989 | #ifdef _CS_LFS64_LINTFLAGS |
| 5990 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5991 | #endif |
| 5992 | #ifdef _CS_LFS_CFLAGS |
| 5993 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5994 | #endif |
| 5995 | #ifdef _CS_LFS_LDFLAGS |
| 5996 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5997 | #endif |
| 5998 | #ifdef _CS_LFS_LIBS |
| 5999 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6000 | #endif |
| 6001 | #ifdef _CS_LFS_LINTFLAGS |
| 6002 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6003 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6004 | #ifdef _CS_MACHINE |
| 6005 | {"CS_MACHINE", _CS_MACHINE}, |
| 6006 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6007 | #ifdef _CS_PATH |
| 6008 | {"CS_PATH", _CS_PATH}, |
| 6009 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6010 | #ifdef _CS_RELEASE |
| 6011 | {"CS_RELEASE", _CS_RELEASE}, |
| 6012 | #endif |
| 6013 | #ifdef _CS_SRPC_DOMAIN |
| 6014 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6015 | #endif |
| 6016 | #ifdef _CS_SYSNAME |
| 6017 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6018 | #endif |
| 6019 | #ifdef _CS_VERSION |
| 6020 | {"CS_VERSION", _CS_VERSION}, |
| 6021 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6022 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6023 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6024 | #endif |
| 6025 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6026 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6027 | #endif |
| 6028 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6029 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6030 | #endif |
| 6031 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6032 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6033 | #endif |
| 6034 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6035 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6036 | #endif |
| 6037 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6038 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6039 | #endif |
| 6040 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6041 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6042 | #endif |
| 6043 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6044 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6045 | #endif |
| 6046 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6047 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6048 | #endif |
| 6049 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6050 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6051 | #endif |
| 6052 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6053 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6054 | #endif |
| 6055 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6056 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6057 | #endif |
| 6058 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6059 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6060 | #endif |
| 6061 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6062 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6063 | #endif |
| 6064 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6065 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6066 | #endif |
| 6067 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6068 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6069 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6070 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6071 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6072 | #endif |
| 6073 | #ifdef _MIPS_CS_BASE |
| 6074 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6075 | #endif |
| 6076 | #ifdef _MIPS_CS_HOSTID |
| 6077 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6078 | #endif |
| 6079 | #ifdef _MIPS_CS_HW_NAME |
| 6080 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6081 | #endif |
| 6082 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6083 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6084 | #endif |
| 6085 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6086 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6087 | #endif |
| 6088 | #ifdef _MIPS_CS_OSREL_MIN |
| 6089 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6090 | #endif |
| 6091 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6092 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6093 | #endif |
| 6094 | #ifdef _MIPS_CS_OS_NAME |
| 6095 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6096 | #endif |
| 6097 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6098 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6099 | #endif |
| 6100 | #ifdef _MIPS_CS_PROCESSORS |
| 6101 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6102 | #endif |
| 6103 | #ifdef _MIPS_CS_SERIAL |
| 6104 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6105 | #endif |
| 6106 | #ifdef _MIPS_CS_VENDOR |
| 6107 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6108 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6109 | }; |
| 6110 | |
| 6111 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6112 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6113 | { |
| 6114 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6115 | sizeof(posix_constants_confstr) |
| 6116 | / sizeof(struct constdef)); |
| 6117 | } |
| 6118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6119 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6120 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6121 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6122 | |
| 6123 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6124 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6125 | { |
| 6126 | PyObject *result = NULL; |
| 6127 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6128 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6129 | |
| 6130 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6131 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6132 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6133 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6134 | len = confstr(name, buffer, sizeof(buffer)); |
| 6135 | if (len == 0) { |
| 6136 | if (errno) { |
| 6137 | posix_error(); |
| 6138 | } |
| 6139 | else { |
| 6140 | result = Py_None; |
| 6141 | Py_INCREF(Py_None); |
| 6142 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6143 | } |
| 6144 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6145 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6146 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6147 | if (result != NULL) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 6148 | confstr(name, _PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6149 | } |
| 6150 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6151 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6152 | } |
| 6153 | } |
| 6154 | return result; |
| 6155 | } |
| 6156 | #endif |
| 6157 | |
| 6158 | |
| 6159 | #ifdef HAVE_SYSCONF |
| 6160 | static struct constdef posix_constants_sysconf[] = { |
| 6161 | #ifdef _SC_2_CHAR_TERM |
| 6162 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6163 | #endif |
| 6164 | #ifdef _SC_2_C_BIND |
| 6165 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6166 | #endif |
| 6167 | #ifdef _SC_2_C_DEV |
| 6168 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6169 | #endif |
| 6170 | #ifdef _SC_2_C_VERSION |
| 6171 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6172 | #endif |
| 6173 | #ifdef _SC_2_FORT_DEV |
| 6174 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6175 | #endif |
| 6176 | #ifdef _SC_2_FORT_RUN |
| 6177 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6178 | #endif |
| 6179 | #ifdef _SC_2_LOCALEDEF |
| 6180 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6181 | #endif |
| 6182 | #ifdef _SC_2_SW_DEV |
| 6183 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6184 | #endif |
| 6185 | #ifdef _SC_2_UPE |
| 6186 | {"SC_2_UPE", _SC_2_UPE}, |
| 6187 | #endif |
| 6188 | #ifdef _SC_2_VERSION |
| 6189 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6190 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6191 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6192 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6193 | #endif |
| 6194 | #ifdef _SC_ACL |
| 6195 | {"SC_ACL", _SC_ACL}, |
| 6196 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6197 | #ifdef _SC_AIO_LISTIO_MAX |
| 6198 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6199 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6200 | #ifdef _SC_AIO_MAX |
| 6201 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6202 | #endif |
| 6203 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6204 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6205 | #endif |
| 6206 | #ifdef _SC_ARG_MAX |
| 6207 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6208 | #endif |
| 6209 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6210 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6211 | #endif |
| 6212 | #ifdef _SC_ATEXIT_MAX |
| 6213 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6214 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6215 | #ifdef _SC_AUDIT |
| 6216 | {"SC_AUDIT", _SC_AUDIT}, |
| 6217 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6218 | #ifdef _SC_AVPHYS_PAGES |
| 6219 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6220 | #endif |
| 6221 | #ifdef _SC_BC_BASE_MAX |
| 6222 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6223 | #endif |
| 6224 | #ifdef _SC_BC_DIM_MAX |
| 6225 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6226 | #endif |
| 6227 | #ifdef _SC_BC_SCALE_MAX |
| 6228 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6229 | #endif |
| 6230 | #ifdef _SC_BC_STRING_MAX |
| 6231 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6232 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6233 | #ifdef _SC_CAP |
| 6234 | {"SC_CAP", _SC_CAP}, |
| 6235 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6236 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6237 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6238 | #endif |
| 6239 | #ifdef _SC_CHAR_BIT |
| 6240 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6241 | #endif |
| 6242 | #ifdef _SC_CHAR_MAX |
| 6243 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6244 | #endif |
| 6245 | #ifdef _SC_CHAR_MIN |
| 6246 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6247 | #endif |
| 6248 | #ifdef _SC_CHILD_MAX |
| 6249 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6250 | #endif |
| 6251 | #ifdef _SC_CLK_TCK |
| 6252 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6253 | #endif |
| 6254 | #ifdef _SC_COHER_BLKSZ |
| 6255 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6256 | #endif |
| 6257 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6258 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6259 | #endif |
| 6260 | #ifdef _SC_DCACHE_ASSOC |
| 6261 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6262 | #endif |
| 6263 | #ifdef _SC_DCACHE_BLKSZ |
| 6264 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6265 | #endif |
| 6266 | #ifdef _SC_DCACHE_LINESZ |
| 6267 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6268 | #endif |
| 6269 | #ifdef _SC_DCACHE_SZ |
| 6270 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6271 | #endif |
| 6272 | #ifdef _SC_DCACHE_TBLKSZ |
| 6273 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6274 | #endif |
| 6275 | #ifdef _SC_DELAYTIMER_MAX |
| 6276 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6277 | #endif |
| 6278 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6279 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6280 | #endif |
| 6281 | #ifdef _SC_EXPR_NEST_MAX |
| 6282 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6283 | #endif |
| 6284 | #ifdef _SC_FSYNC |
| 6285 | {"SC_FSYNC", _SC_FSYNC}, |
| 6286 | #endif |
| 6287 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6288 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6289 | #endif |
| 6290 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6291 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6292 | #endif |
| 6293 | #ifdef _SC_ICACHE_ASSOC |
| 6294 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6295 | #endif |
| 6296 | #ifdef _SC_ICACHE_BLKSZ |
| 6297 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6298 | #endif |
| 6299 | #ifdef _SC_ICACHE_LINESZ |
| 6300 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6301 | #endif |
| 6302 | #ifdef _SC_ICACHE_SZ |
| 6303 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6304 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6305 | #ifdef _SC_INF |
| 6306 | {"SC_INF", _SC_INF}, |
| 6307 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6308 | #ifdef _SC_INT_MAX |
| 6309 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6310 | #endif |
| 6311 | #ifdef _SC_INT_MIN |
| 6312 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6313 | #endif |
| 6314 | #ifdef _SC_IOV_MAX |
| 6315 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6316 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6317 | #ifdef _SC_IP_SECOPTS |
| 6318 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6319 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6320 | #ifdef _SC_JOB_CONTROL |
| 6321 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6322 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6323 | #ifdef _SC_KERN_POINTERS |
| 6324 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6325 | #endif |
| 6326 | #ifdef _SC_KERN_SIM |
| 6327 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6328 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6329 | #ifdef _SC_LINE_MAX |
| 6330 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6331 | #endif |
| 6332 | #ifdef _SC_LOGIN_NAME_MAX |
| 6333 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6334 | #endif |
| 6335 | #ifdef _SC_LOGNAME_MAX |
| 6336 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6337 | #endif |
| 6338 | #ifdef _SC_LONG_BIT |
| 6339 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6340 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6341 | #ifdef _SC_MAC |
| 6342 | {"SC_MAC", _SC_MAC}, |
| 6343 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6344 | #ifdef _SC_MAPPED_FILES |
| 6345 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6346 | #endif |
| 6347 | #ifdef _SC_MAXPID |
| 6348 | {"SC_MAXPID", _SC_MAXPID}, |
| 6349 | #endif |
| 6350 | #ifdef _SC_MB_LEN_MAX |
| 6351 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6352 | #endif |
| 6353 | #ifdef _SC_MEMLOCK |
| 6354 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6355 | #endif |
| 6356 | #ifdef _SC_MEMLOCK_RANGE |
| 6357 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6358 | #endif |
| 6359 | #ifdef _SC_MEMORY_PROTECTION |
| 6360 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6361 | #endif |
| 6362 | #ifdef _SC_MESSAGE_PASSING |
| 6363 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6364 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6365 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6366 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6367 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6368 | #ifdef _SC_MQ_OPEN_MAX |
| 6369 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6370 | #endif |
| 6371 | #ifdef _SC_MQ_PRIO_MAX |
| 6372 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6373 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6374 | #ifdef _SC_NACLS_MAX |
| 6375 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6376 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6377 | #ifdef _SC_NGROUPS_MAX |
| 6378 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6379 | #endif |
| 6380 | #ifdef _SC_NL_ARGMAX |
| 6381 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6382 | #endif |
| 6383 | #ifdef _SC_NL_LANGMAX |
| 6384 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6385 | #endif |
| 6386 | #ifdef _SC_NL_MSGMAX |
| 6387 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6388 | #endif |
| 6389 | #ifdef _SC_NL_NMAX |
| 6390 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6391 | #endif |
| 6392 | #ifdef _SC_NL_SETMAX |
| 6393 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6394 | #endif |
| 6395 | #ifdef _SC_NL_TEXTMAX |
| 6396 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6397 | #endif |
| 6398 | #ifdef _SC_NPROCESSORS_CONF |
| 6399 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6400 | #endif |
| 6401 | #ifdef _SC_NPROCESSORS_ONLN |
| 6402 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6403 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6404 | #ifdef _SC_NPROC_CONF |
| 6405 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6406 | #endif |
| 6407 | #ifdef _SC_NPROC_ONLN |
| 6408 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6409 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6410 | #ifdef _SC_NZERO |
| 6411 | {"SC_NZERO", _SC_NZERO}, |
| 6412 | #endif |
| 6413 | #ifdef _SC_OPEN_MAX |
| 6414 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6415 | #endif |
| 6416 | #ifdef _SC_PAGESIZE |
| 6417 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6418 | #endif |
| 6419 | #ifdef _SC_PAGE_SIZE |
| 6420 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6421 | #endif |
| 6422 | #ifdef _SC_PASS_MAX |
| 6423 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6424 | #endif |
| 6425 | #ifdef _SC_PHYS_PAGES |
| 6426 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6427 | #endif |
| 6428 | #ifdef _SC_PII |
| 6429 | {"SC_PII", _SC_PII}, |
| 6430 | #endif |
| 6431 | #ifdef _SC_PII_INTERNET |
| 6432 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6433 | #endif |
| 6434 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6435 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6436 | #endif |
| 6437 | #ifdef _SC_PII_INTERNET_STREAM |
| 6438 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6439 | #endif |
| 6440 | #ifdef _SC_PII_OSI |
| 6441 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6442 | #endif |
| 6443 | #ifdef _SC_PII_OSI_CLTS |
| 6444 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6445 | #endif |
| 6446 | #ifdef _SC_PII_OSI_COTS |
| 6447 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6448 | #endif |
| 6449 | #ifdef _SC_PII_OSI_M |
| 6450 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6451 | #endif |
| 6452 | #ifdef _SC_PII_SOCKET |
| 6453 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6454 | #endif |
| 6455 | #ifdef _SC_PII_XTI |
| 6456 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6457 | #endif |
| 6458 | #ifdef _SC_POLL |
| 6459 | {"SC_POLL", _SC_POLL}, |
| 6460 | #endif |
| 6461 | #ifdef _SC_PRIORITIZED_IO |
| 6462 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6463 | #endif |
| 6464 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6465 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6466 | #endif |
| 6467 | #ifdef _SC_REALTIME_SIGNALS |
| 6468 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6469 | #endif |
| 6470 | #ifdef _SC_RE_DUP_MAX |
| 6471 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6472 | #endif |
| 6473 | #ifdef _SC_RTSIG_MAX |
| 6474 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6475 | #endif |
| 6476 | #ifdef _SC_SAVED_IDS |
| 6477 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6478 | #endif |
| 6479 | #ifdef _SC_SCHAR_MAX |
| 6480 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6481 | #endif |
| 6482 | #ifdef _SC_SCHAR_MIN |
| 6483 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6484 | #endif |
| 6485 | #ifdef _SC_SELECT |
| 6486 | {"SC_SELECT", _SC_SELECT}, |
| 6487 | #endif |
| 6488 | #ifdef _SC_SEMAPHORES |
| 6489 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6490 | #endif |
| 6491 | #ifdef _SC_SEM_NSEMS_MAX |
| 6492 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6493 | #endif |
| 6494 | #ifdef _SC_SEM_VALUE_MAX |
| 6495 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6496 | #endif |
| 6497 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6498 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6499 | #endif |
| 6500 | #ifdef _SC_SHRT_MAX |
| 6501 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6502 | #endif |
| 6503 | #ifdef _SC_SHRT_MIN |
| 6504 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6505 | #endif |
| 6506 | #ifdef _SC_SIGQUEUE_MAX |
| 6507 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6508 | #endif |
| 6509 | #ifdef _SC_SIGRT_MAX |
| 6510 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6511 | #endif |
| 6512 | #ifdef _SC_SIGRT_MIN |
| 6513 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6514 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6515 | #ifdef _SC_SOFTPOWER |
| 6516 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6517 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6518 | #ifdef _SC_SPLIT_CACHE |
| 6519 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6520 | #endif |
| 6521 | #ifdef _SC_SSIZE_MAX |
| 6522 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6523 | #endif |
| 6524 | #ifdef _SC_STACK_PROT |
| 6525 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6526 | #endif |
| 6527 | #ifdef _SC_STREAM_MAX |
| 6528 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6529 | #endif |
| 6530 | #ifdef _SC_SYNCHRONIZED_IO |
| 6531 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6532 | #endif |
| 6533 | #ifdef _SC_THREADS |
| 6534 | {"SC_THREADS", _SC_THREADS}, |
| 6535 | #endif |
| 6536 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6537 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6538 | #endif |
| 6539 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6540 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6541 | #endif |
| 6542 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6543 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6544 | #endif |
| 6545 | #ifdef _SC_THREAD_KEYS_MAX |
| 6546 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6547 | #endif |
| 6548 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6549 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6550 | #endif |
| 6551 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6552 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6553 | #endif |
| 6554 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6555 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6556 | #endif |
| 6557 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6558 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6559 | #endif |
| 6560 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6561 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6562 | #endif |
| 6563 | #ifdef _SC_THREAD_STACK_MIN |
| 6564 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6565 | #endif |
| 6566 | #ifdef _SC_THREAD_THREADS_MAX |
| 6567 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6568 | #endif |
| 6569 | #ifdef _SC_TIMERS |
| 6570 | {"SC_TIMERS", _SC_TIMERS}, |
| 6571 | #endif |
| 6572 | #ifdef _SC_TIMER_MAX |
| 6573 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6574 | #endif |
| 6575 | #ifdef _SC_TTY_NAME_MAX |
| 6576 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6577 | #endif |
| 6578 | #ifdef _SC_TZNAME_MAX |
| 6579 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6580 | #endif |
| 6581 | #ifdef _SC_T_IOV_MAX |
| 6582 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6583 | #endif |
| 6584 | #ifdef _SC_UCHAR_MAX |
| 6585 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6586 | #endif |
| 6587 | #ifdef _SC_UINT_MAX |
| 6588 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6589 | #endif |
| 6590 | #ifdef _SC_UIO_MAXIOV |
| 6591 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6592 | #endif |
| 6593 | #ifdef _SC_ULONG_MAX |
| 6594 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6595 | #endif |
| 6596 | #ifdef _SC_USHRT_MAX |
| 6597 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6598 | #endif |
| 6599 | #ifdef _SC_VERSION |
| 6600 | {"SC_VERSION", _SC_VERSION}, |
| 6601 | #endif |
| 6602 | #ifdef _SC_WORD_BIT |
| 6603 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6604 | #endif |
| 6605 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6606 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6607 | #endif |
| 6608 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6609 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6610 | #endif |
| 6611 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6612 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6613 | #endif |
| 6614 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6615 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6616 | #endif |
| 6617 | #ifdef _SC_XOPEN_CRYPT |
| 6618 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6619 | #endif |
| 6620 | #ifdef _SC_XOPEN_ENH_I18N |
| 6621 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6622 | #endif |
| 6623 | #ifdef _SC_XOPEN_LEGACY |
| 6624 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6625 | #endif |
| 6626 | #ifdef _SC_XOPEN_REALTIME |
| 6627 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6628 | #endif |
| 6629 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6630 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6631 | #endif |
| 6632 | #ifdef _SC_XOPEN_SHM |
| 6633 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6634 | #endif |
| 6635 | #ifdef _SC_XOPEN_UNIX |
| 6636 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6637 | #endif |
| 6638 | #ifdef _SC_XOPEN_VERSION |
| 6639 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6640 | #endif |
| 6641 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6642 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6643 | #endif |
| 6644 | #ifdef _SC_XOPEN_XPG2 |
| 6645 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6646 | #endif |
| 6647 | #ifdef _SC_XOPEN_XPG3 |
| 6648 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6649 | #endif |
| 6650 | #ifdef _SC_XOPEN_XPG4 |
| 6651 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6652 | #endif |
| 6653 | }; |
| 6654 | |
| 6655 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6656 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6657 | { |
| 6658 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6659 | sizeof(posix_constants_sysconf) |
| 6660 | / sizeof(struct constdef)); |
| 6661 | } |
| 6662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6663 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6664 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6665 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6666 | |
| 6667 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6668 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6669 | { |
| 6670 | PyObject *result = NULL; |
| 6671 | int name; |
| 6672 | |
| 6673 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6674 | int value; |
| 6675 | |
| 6676 | errno = 0; |
| 6677 | value = sysconf(name); |
| 6678 | if (value == -1 && errno != 0) |
| 6679 | posix_error(); |
| 6680 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6681 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6682 | } |
| 6683 | return result; |
| 6684 | } |
| 6685 | #endif |
| 6686 | |
| 6687 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6688 | /* This code is used to ensure that the tables of configuration value names |
| 6689 | * are in sorted order as required by conv_confname(), and also to build the |
| 6690 | * the exported dictionaries that are used to publish information about the |
| 6691 | * names available on the host platform. |
| 6692 | * |
| 6693 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6694 | * when used, even for platforms we're not able to test on. It also makes |
| 6695 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6696 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6697 | |
| 6698 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6699 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6700 | { |
| 6701 | const struct constdef *c1 = |
| 6702 | (const struct constdef *) v1; |
| 6703 | const struct constdef *c2 = |
| 6704 | (const struct constdef *) v2; |
| 6705 | |
| 6706 | return strcmp(c1->name, c2->name); |
| 6707 | } |
| 6708 | |
| 6709 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6710 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6711 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6712 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6713 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6714 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6715 | |
| 6716 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6717 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6718 | if (d == NULL) |
| 6719 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6720 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6721 | for (i=0; i < tablesize; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6722 | PyObject *o = PyLong_FromLong(table[i].value); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6723 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6724 | Py_XDECREF(o); |
| 6725 | Py_DECREF(d); |
| 6726 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6727 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6728 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6729 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6730 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6733 | /* Return -1 on failure, 0 on success. */ |
| 6734 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6735 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6736 | { |
| 6737 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6738 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6739 | sizeof(posix_constants_pathconf) |
| 6740 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6741 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6742 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6743 | #endif |
| 6744 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6745 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6746 | sizeof(posix_constants_confstr) |
| 6747 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6748 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6749 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6750 | #endif |
| 6751 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6752 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6753 | sizeof(posix_constants_sysconf) |
| 6754 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6755 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6756 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6757 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6758 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6759 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6760 | |
| 6761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6762 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6763 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6764 | 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] | 6765 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6766 | |
| 6767 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6768 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6769 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6770 | abort(); |
| 6771 | /*NOTREACHED*/ |
| 6772 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6773 | return NULL; |
| 6774 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6775 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6776 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6777 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6778 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6779 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6780 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6781 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6782 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6783 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6784 | application (if any) its extension is associated.\n\ |
| 6785 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6786 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6787 | \n\ |
| 6788 | startfile returns as soon as the associated application is launched.\n\ |
| 6789 | There is no option to wait for the application to close, and no way\n\ |
| 6790 | to retrieve the application's exit status.\n\ |
| 6791 | \n\ |
| 6792 | The filepath is relative to the current directory. If you want to use\n\ |
| 6793 | 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] | 6794 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6795 | |
| 6796 | static PyObject * |
| 6797 | win32_startfile(PyObject *self, PyObject *args) |
| 6798 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6799 | PyObject *ofilepath; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6800 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6801 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6802 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6803 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6804 | if (unicode_file_names()) { |
| 6805 | PyObject *unipath, *woperation = NULL; |
| 6806 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6807 | &unipath, &operation)) { |
| 6808 | PyErr_Clear(); |
| 6809 | goto normal; |
| 6810 | } |
| 6811 | |
| 6812 | |
| 6813 | if (operation) { |
| 6814 | woperation = PyUnicode_DecodeASCII(operation, |
| 6815 | strlen(operation), NULL); |
| 6816 | if (!woperation) { |
| 6817 | PyErr_Clear(); |
| 6818 | operation = NULL; |
| 6819 | goto normal; |
| 6820 | } |
| 6821 | } |
| 6822 | |
| 6823 | Py_BEGIN_ALLOW_THREADS |
| 6824 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6825 | PyUnicode_AS_UNICODE(unipath), |
| 6826 | NULL, NULL, SW_SHOWNORMAL); |
| 6827 | Py_END_ALLOW_THREADS |
| 6828 | |
| 6829 | Py_XDECREF(woperation); |
| 6830 | if (rc <= (HINSTANCE)32) { |
| 6831 | PyObject *errval = win32_error_unicode("startfile", |
| 6832 | PyUnicode_AS_UNICODE(unipath)); |
| 6833 | return errval; |
| 6834 | } |
| 6835 | Py_INCREF(Py_None); |
| 6836 | return Py_None; |
| 6837 | } |
| 6838 | #endif |
| 6839 | |
| 6840 | normal: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6841 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 6842 | PyUnicode_FSConverter, &ofilepath, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6843 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6844 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6845 | filepath = bytes2str(ofilepath, 1); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6846 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6847 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6848 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6849 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6850 | if (rc <= (HINSTANCE)32) { |
| 6851 | PyObject *errval = win32_error("startfile", filepath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6852 | release_bytes(ofilepath); |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6853 | return errval; |
| 6854 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6855 | release_bytes(ofilepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6856 | Py_INCREF(Py_None); |
| 6857 | return Py_None; |
| 6858 | } |
| 6859 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6860 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6861 | #ifdef HAVE_GETLOADAVG |
| 6862 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6863 | "getloadavg() -> (float, float, float)\n\n\ |
| 6864 | Return the number of processes in the system run queue averaged over\n\ |
| 6865 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6866 | was unobtainable"); |
| 6867 | |
| 6868 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6869 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6870 | { |
| 6871 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6872 | if (getloadavg(loadavg, 3)!=3) { |
| 6873 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6874 | return NULL; |
| 6875 | } else |
| 6876 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6877 | } |
| 6878 | #endif |
| 6879 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6880 | #ifdef MS_WINDOWS |
| 6881 | |
| 6882 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6883 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6884 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6885 | |
| 6886 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6887 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6888 | DWORD dwFlags ); |
| 6889 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6890 | BYTE *pbBuffer ); |
| 6891 | |
| 6892 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6893 | /* This handle is never explicitly released. Instead, the operating |
| 6894 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6895 | static HCRYPTPROV hCryptProv = 0; |
| 6896 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6897 | static PyObject* |
| 6898 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6899 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6900 | int howMany; |
| 6901 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6902 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6903 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6904 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6905 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6906 | if (howMany < 0) |
| 6907 | return PyErr_Format(PyExc_ValueError, |
| 6908 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6909 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6910 | if (hCryptProv == 0) { |
| 6911 | HINSTANCE hAdvAPI32 = NULL; |
| 6912 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6913 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6914 | /* Obtain handle to the DLL containing CryptoAPI |
| 6915 | This should not fail */ |
| 6916 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6917 | if(hAdvAPI32 == NULL) |
| 6918 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6919 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6920 | /* Obtain pointers to the CryptoAPI functions |
| 6921 | This will fail on some early versions of Win95 */ |
| 6922 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6923 | hAdvAPI32, |
| 6924 | "CryptAcquireContextA"); |
| 6925 | if (pCryptAcquireContext == NULL) |
| 6926 | return PyErr_Format(PyExc_NotImplementedError, |
| 6927 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6928 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6929 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6930 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6931 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6932 | return PyErr_Format(PyExc_NotImplementedError, |
| 6933 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6934 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6935 | /* Acquire context */ |
| 6936 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6937 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6938 | return win32_error("CryptAcquireContext", NULL); |
| 6939 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6940 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6941 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6942 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6943 | if (result != NULL) { |
| 6944 | /* Get random data */ |
Amaury Forgeot d'Arc | a05ada3 | 2008-07-21 21:13:14 +0000 | [diff] [blame] | 6945 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6946 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6947 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6948 | Py_DECREF(result); |
| 6949 | return win32_error("CryptGenRandom", NULL); |
| 6950 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6951 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6952 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6953 | } |
| 6954 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6955 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6956 | PyDoc_STRVAR(device_encoding__doc__, |
| 6957 | "device_encoding(fd) -> str\n\n\ |
| 6958 | Return a string describing the encoding of the device\n\ |
| 6959 | if the output is a terminal; else return None."); |
| 6960 | |
| 6961 | static PyObject * |
| 6962 | device_encoding(PyObject *self, PyObject *args) |
| 6963 | { |
| 6964 | int fd; |
| 6965 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6966 | return NULL; |
Kristján Valur Jónsson | 649170b | 2009-03-24 14:15:49 +0000 | [diff] [blame] | 6967 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6968 | Py_INCREF(Py_None); |
| 6969 | return Py_None; |
| 6970 | } |
| 6971 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6972 | if (fd == 0) { |
| 6973 | char buf[100]; |
| 6974 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6975 | return PyUnicode_FromString(buf); |
| 6976 | } |
| 6977 | if (fd == 1 || fd == 2) { |
| 6978 | char buf[100]; |
| 6979 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6980 | return PyUnicode_FromString(buf); |
| 6981 | } |
| 6982 | #elif defined(CODESET) |
| 6983 | { |
| 6984 | char *codeset = nl_langinfo(CODESET); |
Mark Dickinson | da2706b | 2008-12-11 18:03:03 +0000 | [diff] [blame] | 6985 | if (codeset != NULL && codeset[0] != 0) |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6986 | return PyUnicode_FromString(codeset); |
| 6987 | } |
| 6988 | #endif |
| 6989 | Py_INCREF(Py_None); |
| 6990 | return Py_None; |
| 6991 | } |
| 6992 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6993 | #ifdef __VMS |
| 6994 | /* Use openssl random routine */ |
| 6995 | #include <openssl/rand.h> |
| 6996 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6997 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6998 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6999 | |
| 7000 | static PyObject* |
| 7001 | vms_urandom(PyObject *self, PyObject *args) |
| 7002 | { |
| 7003 | int howMany; |
| 7004 | PyObject* result; |
| 7005 | |
| 7006 | /* Read arguments */ |
| 7007 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7008 | return NULL; |
| 7009 | if (howMany < 0) |
| 7010 | return PyErr_Format(PyExc_ValueError, |
| 7011 | "negative argument not allowed"); |
| 7012 | |
| 7013 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7014 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7015 | if (result != NULL) { |
| 7016 | /* Get random data */ |
| 7017 | if (RAND_pseudo_bytes((unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7018 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7019 | howMany) < 0) { |
| 7020 | Py_DECREF(result); |
| 7021 | return PyErr_Format(PyExc_ValueError, |
| 7022 | "RAND_pseudo_bytes"); |
| 7023 | } |
| 7024 | } |
| 7025 | return result; |
| 7026 | } |
| 7027 | #endif |
| 7028 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7029 | static PyMethodDef posix_methods[] = { |
| 7030 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7031 | #ifdef HAVE_TTYNAME |
| 7032 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7033 | #endif |
| 7034 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7035 | #ifdef HAVE_CHFLAGS |
| 7036 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 7037 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7038 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7039 | #ifdef HAVE_FCHMOD |
| 7040 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
| 7041 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7042 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7043 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7044 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7045 | #ifdef HAVE_LCHMOD |
| 7046 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
| 7047 | #endif /* HAVE_LCHMOD */ |
| 7048 | #ifdef HAVE_FCHOWN |
| 7049 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
| 7050 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7051 | #ifdef HAVE_LCHFLAGS |
| 7052 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 7053 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7054 | #ifdef HAVE_LCHOWN |
| 7055 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7056 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7057 | #ifdef HAVE_CHROOT |
| 7058 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7059 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7060 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7061 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7062 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7063 | #ifdef HAVE_GETCWD |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 7064 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 7065 | METH_NOARGS, posix_getcwd__doc__}, |
| 7066 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 7067 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7068 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7069 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7070 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7071 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7072 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7073 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7074 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7075 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7076 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7077 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7078 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7079 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7080 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7081 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7082 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7083 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7084 | {"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] | 7085 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7086 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7087 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7088 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7089 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7090 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7091 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7092 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7093 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7094 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7095 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7096 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7097 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7098 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7099 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7100 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7101 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7102 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7103 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7104 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7105 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7106 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7107 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7108 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7109 | #if defined(PYOS_OS2) |
| 7110 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7111 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7112 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7113 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7114 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7115 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7116 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7117 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7118 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7119 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7120 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7121 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7122 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7123 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7124 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7125 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7126 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7127 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7128 | #endif /* HAVE_GETEGID */ |
| 7129 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7130 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7131 | #endif /* HAVE_GETEUID */ |
| 7132 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7133 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7134 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7135 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7136 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7137 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7138 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7139 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7140 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7141 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7142 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7143 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7144 | #endif /* HAVE_GETPPID */ |
| 7145 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7146 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7147 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7148 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7149 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7150 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7151 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7152 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7153 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7154 | #ifdef HAVE_KILLPG |
| 7155 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7156 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7157 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7158 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7159 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7160 | #ifdef MS_WINDOWS |
| 7161 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 7162 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7163 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7164 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7165 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7166 | #ifdef HAVE_SETEUID |
| 7167 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7168 | #endif /* HAVE_SETEUID */ |
| 7169 | #ifdef HAVE_SETEGID |
| 7170 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7171 | #endif /* HAVE_SETEGID */ |
| 7172 | #ifdef HAVE_SETREUID |
| 7173 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7174 | #endif /* HAVE_SETREUID */ |
| 7175 | #ifdef HAVE_SETREGID |
| 7176 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7177 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7178 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7179 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7180 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7181 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 7182 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7183 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7184 | #ifdef HAVE_GETPGID |
| 7185 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7186 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7187 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7188 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7189 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7190 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7191 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7192 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7193 | #ifdef HAVE_WAIT3 |
| 7194 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7195 | #endif /* HAVE_WAIT3 */ |
| 7196 | #ifdef HAVE_WAIT4 |
| 7197 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7198 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7199 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7200 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7201 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7202 | #ifdef HAVE_GETSID |
| 7203 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7204 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7205 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7206 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7207 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7208 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7209 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7210 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7211 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7212 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7213 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7214 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7215 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7216 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7217 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7218 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7219 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7220 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7221 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7222 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7223 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7224 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7225 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7226 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7227 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7228 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7229 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7230 | #endif |
| 7231 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7232 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7233 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7234 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7235 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7236 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7237 | #ifdef HAVE_DEVICE_MACROS |
| 7238 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7239 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7240 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7241 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7242 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7243 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7244 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7245 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7246 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7247 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7248 | #ifdef HAVE_UNSETENV |
| 7249 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7250 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7251 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7252 | #ifdef HAVE_FCHDIR |
| 7253 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7254 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7255 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7256 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7257 | #endif |
| 7258 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7259 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7260 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7261 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7262 | #ifdef WCOREDUMP |
| 7263 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7264 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7265 | #ifdef WIFCONTINUED |
| 7266 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7267 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7268 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7269 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7270 | #endif /* WIFSTOPPED */ |
| 7271 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7272 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7273 | #endif /* WIFSIGNALED */ |
| 7274 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7275 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7276 | #endif /* WIFEXITED */ |
| 7277 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7278 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7279 | #endif /* WEXITSTATUS */ |
| 7280 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7281 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7282 | #endif /* WTERMSIG */ |
| 7283 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7284 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7285 | #endif /* WSTOPSIG */ |
| 7286 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7287 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7288 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7289 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7290 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7291 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7292 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7293 | #ifdef HAVE_CONFSTR |
| 7294 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7295 | #endif |
| 7296 | #ifdef HAVE_SYSCONF |
| 7297 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7298 | #endif |
| 7299 | #ifdef HAVE_FPATHCONF |
| 7300 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7301 | #endif |
| 7302 | #ifdef HAVE_PATHCONF |
| 7303 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7304 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7305 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7306 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7307 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7308 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7309 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7310 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7311 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7312 | #ifdef MS_WINDOWS |
| 7313 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7314 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7315 | #ifdef __VMS |
| 7316 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 7317 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7318 | {NULL, NULL} /* Sentinel */ |
| 7319 | }; |
| 7320 | |
| 7321 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7322 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7323 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7324 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7325 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7326 | } |
| 7327 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7328 | #if defined(PYOS_OS2) |
| 7329 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7330 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7331 | { |
| 7332 | APIRET rc; |
| 7333 | ULONG values[QSV_MAX+1]; |
| 7334 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7335 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7336 | |
| 7337 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7338 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7339 | Py_END_ALLOW_THREADS |
| 7340 | |
| 7341 | if (rc != NO_ERROR) { |
| 7342 | os2_error(rc); |
| 7343 | return -1; |
| 7344 | } |
| 7345 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7346 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7347 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7348 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7349 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7350 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7351 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7352 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7353 | |
| 7354 | switch (values[QSV_VERSION_MINOR]) { |
| 7355 | case 0: ver = "2.00"; break; |
| 7356 | case 10: ver = "2.10"; break; |
| 7357 | case 11: ver = "2.11"; break; |
| 7358 | case 30: ver = "3.00"; break; |
| 7359 | case 40: ver = "4.00"; break; |
| 7360 | case 50: ver = "5.00"; break; |
| 7361 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7362 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7363 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7364 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7365 | ver = &tmp[0]; |
| 7366 | } |
| 7367 | |
| 7368 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7369 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7370 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7371 | |
| 7372 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7373 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7374 | tmp[1] = ':'; |
| 7375 | tmp[2] = '\0'; |
| 7376 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7377 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7378 | } |
| 7379 | #endif |
| 7380 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7381 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7382 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7383 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7384 | #ifdef F_OK |
| 7385 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7386 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7387 | #ifdef R_OK |
| 7388 | if (ins(d, "R_OK", (long)R_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 W_OK |
| 7391 | if (ins(d, "W_OK", (long)W_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 X_OK |
| 7394 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7395 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7396 | #ifdef NGROUPS_MAX |
| 7397 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7398 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7399 | #ifdef TMP_MAX |
| 7400 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7401 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7402 | #ifdef WCONTINUED |
| 7403 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7404 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7405 | #ifdef WNOHANG |
| 7406 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7407 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7408 | #ifdef WUNTRACED |
| 7409 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7410 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7411 | #ifdef O_RDONLY |
| 7412 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7413 | #endif |
| 7414 | #ifdef O_WRONLY |
| 7415 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7416 | #endif |
| 7417 | #ifdef O_RDWR |
| 7418 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7419 | #endif |
| 7420 | #ifdef O_NDELAY |
| 7421 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7422 | #endif |
| 7423 | #ifdef O_NONBLOCK |
| 7424 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7425 | #endif |
| 7426 | #ifdef O_APPEND |
| 7427 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7428 | #endif |
| 7429 | #ifdef O_DSYNC |
| 7430 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7431 | #endif |
| 7432 | #ifdef O_RSYNC |
| 7433 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7434 | #endif |
| 7435 | #ifdef O_SYNC |
| 7436 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7437 | #endif |
| 7438 | #ifdef O_NOCTTY |
| 7439 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7440 | #endif |
| 7441 | #ifdef O_CREAT |
| 7442 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7443 | #endif |
| 7444 | #ifdef O_EXCL |
| 7445 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7446 | #endif |
| 7447 | #ifdef O_TRUNC |
| 7448 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7449 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7450 | #ifdef O_BINARY |
| 7451 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7452 | #endif |
| 7453 | #ifdef O_TEXT |
| 7454 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7455 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7456 | #ifdef O_LARGEFILE |
| 7457 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7458 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7459 | #ifdef O_SHLOCK |
| 7460 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7461 | #endif |
| 7462 | #ifdef O_EXLOCK |
| 7463 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7464 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7465 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7466 | /* MS Windows */ |
| 7467 | #ifdef O_NOINHERIT |
| 7468 | /* Don't inherit in child processes. */ |
| 7469 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7470 | #endif |
| 7471 | #ifdef _O_SHORT_LIVED |
| 7472 | /* Optimize for short life (keep in memory). */ |
| 7473 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7474 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7475 | #endif |
| 7476 | #ifdef O_TEMPORARY |
| 7477 | /* Automatically delete when last handle is closed. */ |
| 7478 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7479 | #endif |
| 7480 | #ifdef O_RANDOM |
| 7481 | /* Optimize for random access. */ |
| 7482 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7483 | #endif |
| 7484 | #ifdef O_SEQUENTIAL |
| 7485 | /* Optimize for sequential access. */ |
| 7486 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7487 | #endif |
| 7488 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7489 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7490 | #ifdef O_ASYNC |
| 7491 | /* Send a SIGIO signal whenever input or output |
| 7492 | becomes available on file descriptor */ |
| 7493 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
| 7494 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7495 | #ifdef O_DIRECT |
| 7496 | /* Direct disk access. */ |
| 7497 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7498 | #endif |
| 7499 | #ifdef O_DIRECTORY |
| 7500 | /* Must be a directory. */ |
| 7501 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7502 | #endif |
| 7503 | #ifdef O_NOFOLLOW |
| 7504 | /* Do not follow links. */ |
| 7505 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7506 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7507 | #ifdef O_NOATIME |
| 7508 | /* Do not update the access time. */ |
| 7509 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
| 7510 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7511 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7512 | /* These come from sysexits.h */ |
| 7513 | #ifdef EX_OK |
| 7514 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7515 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7516 | #ifdef EX_USAGE |
| 7517 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7518 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7519 | #ifdef EX_DATAERR |
| 7520 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7521 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7522 | #ifdef EX_NOINPUT |
| 7523 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7524 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7525 | #ifdef EX_NOUSER |
| 7526 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7527 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7528 | #ifdef EX_NOHOST |
| 7529 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7530 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7531 | #ifdef EX_UNAVAILABLE |
| 7532 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7533 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7534 | #ifdef EX_SOFTWARE |
| 7535 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7536 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7537 | #ifdef EX_OSERR |
| 7538 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7539 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7540 | #ifdef EX_OSFILE |
| 7541 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7542 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7543 | #ifdef EX_CANTCREAT |
| 7544 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7545 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7546 | #ifdef EX_IOERR |
| 7547 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7548 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7549 | #ifdef EX_TEMPFAIL |
| 7550 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7551 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7552 | #ifdef EX_PROTOCOL |
| 7553 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7554 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7555 | #ifdef EX_NOPERM |
| 7556 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7557 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7558 | #ifdef EX_CONFIG |
| 7559 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7560 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7561 | #ifdef EX_NOTFOUND |
| 7562 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7563 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7564 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7565 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7566 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7567 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7568 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7569 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7570 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7571 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7572 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7573 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7574 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7575 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7576 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7577 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7578 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7579 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7580 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7581 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7582 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7583 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7584 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7585 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7586 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7587 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7588 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7589 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7590 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7591 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7592 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7593 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7594 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7595 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7596 | #if defined(PYOS_OS2) |
| 7597 | if (insertvalues(d)) return -1; |
| 7598 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7599 | return 0; |
| 7600 | } |
| 7601 | |
| 7602 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7603 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7604 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7605 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7606 | |
| 7607 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7608 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7609 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7610 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7611 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7612 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7613 | #define MODNAME "posix" |
| 7614 | #endif |
| 7615 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7616 | static struct PyModuleDef posixmodule = { |
| 7617 | PyModuleDef_HEAD_INIT, |
| 7618 | MODNAME, |
| 7619 | posix__doc__, |
| 7620 | -1, |
| 7621 | posix_methods, |
| 7622 | NULL, |
| 7623 | NULL, |
| 7624 | NULL, |
| 7625 | NULL |
| 7626 | }; |
| 7627 | |
| 7628 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7629 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7630 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7631 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7632 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7633 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7634 | m = PyModule_Create(&posixmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7635 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7636 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7637 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7638 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7639 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7640 | Py_XINCREF(v); |
| 7641 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7642 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7643 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7644 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7645 | if (all_ins(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7646 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7647 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7648 | if (setup_confname_tables(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7649 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7650 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7651 | Py_INCREF(PyExc_OSError); |
| 7652 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7653 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7654 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7655 | if (posix_putenv_garbage == NULL) |
| 7656 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7657 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7658 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7659 | if (!initialized) { |
| 7660 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7661 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7662 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7663 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7664 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7665 | structseq_new = StatResultType.tp_new; |
| 7666 | StatResultType.tp_new = statresult_new; |
| 7667 | |
| 7668 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7669 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7670 | #ifdef NEED_TICKS_PER_SECOND |
| 7671 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
| 7672 | ticks_per_second = sysconf(_SC_CLK_TCK); |
| 7673 | # elif defined(HZ) |
| 7674 | ticks_per_second = HZ; |
| 7675 | # else |
| 7676 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
| 7677 | # endif |
| 7678 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7679 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7680 | Py_INCREF((PyObject*) &StatResultType); |
| 7681 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7682 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7683 | PyModule_AddObject(m, "statvfs_result", |
| 7684 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7685 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7686 | |
| 7687 | #ifdef __APPLE__ |
| 7688 | /* |
| 7689 | * Step 2 of weak-linking support on Mac OS X. |
| 7690 | * |
| 7691 | * The code below removes functions that are not available on the |
| 7692 | * currently active platform. |
| 7693 | * |
| 7694 | * This block allow one to use a python binary that was build on |
| 7695 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7696 | * OSX 10.4. |
| 7697 | */ |
| 7698 | #ifdef HAVE_FSTATVFS |
| 7699 | if (fstatvfs == NULL) { |
| 7700 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7701 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7702 | } |
| 7703 | } |
| 7704 | #endif /* HAVE_FSTATVFS */ |
| 7705 | |
| 7706 | #ifdef HAVE_STATVFS |
| 7707 | if (statvfs == NULL) { |
| 7708 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7709 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7710 | } |
| 7711 | } |
| 7712 | #endif /* HAVE_STATVFS */ |
| 7713 | |
| 7714 | # ifdef HAVE_LCHOWN |
| 7715 | if (lchown == NULL) { |
| 7716 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7717 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7718 | } |
| 7719 | } |
| 7720 | #endif /* HAVE_LCHOWN */ |
| 7721 | |
| 7722 | |
| 7723 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7724 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7725 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7726 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7727 | |
| 7728 | #ifdef __cplusplus |
| 7729 | } |
| 7730 | #endif |