Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
| 18 | * Step 1 of support for weak-linking a number of symbols existing on |
| 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
| 31 | #include "structseq.h" |
| 32 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #if defined(PYOS_OS2) |
| 49 | #define INCL_DOS |
| 50 | #define INCL_DOSERRORS |
| 51 | #define INCL_DOSPROCESS |
| 52 | #define INCL_NOPMAPI |
| 53 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 54 | #if defined(PYCC_GCC) |
| 55 | #include <ctype.h> |
| 56 | #include <io.h> |
| 57 | #include <stdio.h> |
| 58 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 59 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 60 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | #endif /* HAVE_SYS_TYPES_H */ |
| 66 | |
| 67 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYS_WAIT_H |
| 72 | #include <sys/wait.h> /* For WNOHANG */ |
| 73 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 76 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | #ifdef HAVE_FCNTL_H |
| 80 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 81 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | #ifdef HAVE_GRP_H |
| 84 | #include <grp.h> |
| 85 | #endif |
| 86 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 87 | #ifdef HAVE_SYSEXITS_H |
| 88 | #include <sysexits.h> |
| 89 | #endif /* HAVE_SYSEXITS_H */ |
| 90 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYS_LOADAVG_H |
| 92 | #include <sys/loadavg.h> |
| 93 | #endif |
| 94 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 95 | #ifdef HAVE_LANGINFO_H |
| 96 | #include <langinfo.h> |
| 97 | #endif |
| 98 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> |
| 103 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
| 106 | #define HAVE_OPENDIR 1 |
| 107 | #define HAVE_SYSTEM 1 |
| 108 | #if defined(__OS2__) |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> |
| 113 | #else |
| 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR 1 |
| 118 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 119 | #define HAVE_SYSTEM 1 |
| 120 | #define HAVE_WAIT 1 |
| 121 | #else |
| 122 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 123 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 124 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #define HAVE_EXECV 1 |
| 126 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 128 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 129 | #define HAVE_FSYNC 1 |
| 130 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 131 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 132 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 133 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #else /* all other compilers */ |
| 135 | /* Unix functions that the configure script doesn't check for */ |
| 136 | #define HAVE_EXECV 1 |
| 137 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 138 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 139 | #define HAVE_FORK1 1 |
| 140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #define HAVE_GETCWD 1 |
| 142 | #define HAVE_GETEGID 1 |
| 143 | #define HAVE_GETEUID 1 |
| 144 | #define HAVE_GETGID 1 |
| 145 | #define HAVE_GETPPID 1 |
| 146 | #define HAVE_GETUID 1 |
| 147 | #define HAVE_KILL 1 |
| 148 | #define HAVE_OPENDIR 1 |
| 149 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
| 151 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 152 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 153 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #endif /* _MSC_VER */ |
| 155 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 156 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 157 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 161 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 162 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 163 | (default) */ |
| 164 | extern char *ctermid_r(char *); |
| 165 | #endif |
| 166 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 167 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 170 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 171 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #endif |
| 177 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int chdir(char *); |
| 179 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 180 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int chdir(const char *); |
| 182 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 184 | #ifdef __BORLANDC__ |
| 185 | extern int chmod(const char *, int); |
| 186 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 188 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 189 | /*#ifdef HAVE_FCHMOD |
| 190 | extern int fchmod(int, mode_t); |
| 191 | #endif*/ |
| 192 | /*#ifdef HAVE_LCHMOD |
| 193 | extern int lchmod(const char *, mode_t); |
| 194 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int chown(const char *, uid_t, gid_t); |
| 196 | extern char *getcwd(char *, int); |
| 197 | extern char *strerror(int); |
| 198 | extern int link(const char *, const char *); |
| 199 | extern int rename(const char *, const char *); |
| 200 | extern int stat(const char *, struct stat *); |
| 201 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 206 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 207 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #ifdef HAVE_UTIME_H |
| 213 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 216 | #ifdef HAVE_SYS_UTIME_H |
| 217 | #include <sys/utime.h> |
| 218 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 219 | #endif /* HAVE_SYS_UTIME_H */ |
| 220 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #ifdef HAVE_SYS_TIMES_H |
| 222 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 223 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | |
| 225 | #ifdef HAVE_SYS_PARAM_H |
| 226 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 227 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | |
| 229 | #ifdef HAVE_SYS_UTSNAME_H |
| 230 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 231 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 236 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 237 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #include <direct.h> |
| 239 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 240 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 243 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 244 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 249 | #endif |
| 250 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 255 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 256 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 258 | #endif |
| 259 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 261 | #endif |
| 262 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 264 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 265 | #include "osdefs.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 267 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 268 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 270 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 272 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 273 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 274 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 275 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 276 | #define MAXPATHLEN PATH_MAX |
| 277 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 278 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 279 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 280 | #endif /* MAXPATHLEN */ |
| 281 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 282 | #ifdef UNION_WAIT |
| 283 | /* Emulate some macros on systems that have a union instead of macros */ |
| 284 | |
| 285 | #ifndef WIFEXITED |
| 286 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 287 | #endif |
| 288 | |
| 289 | #ifndef WEXITSTATUS |
| 290 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 291 | #endif |
| 292 | |
| 293 | #ifndef WTERMSIG |
| 294 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 295 | #endif |
| 296 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 297 | #define WAIT_TYPE union wait |
| 298 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 299 | |
| 300 | #else /* !UNION_WAIT */ |
| 301 | #define WAIT_TYPE int |
| 302 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 303 | #endif /* UNION_WAIT */ |
| 304 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 305 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 306 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 307 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 308 | #define USE_CTERMID_R |
| 309 | #endif |
| 310 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 311 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 312 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 313 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 314 | # define STAT win32_stat |
| 315 | # define FSTAT win32_fstat |
| 316 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 317 | #else |
| 318 | # define STAT stat |
| 319 | # define FSTAT fstat |
| 320 | # define STRUCT_STAT struct stat |
| 321 | #endif |
| 322 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 323 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 324 | #include <sys/mkdev.h> |
| 325 | #else |
| 326 | #if defined(MAJOR_IN_SYSMACROS) |
| 327 | #include <sys/sysmacros.h> |
| 328 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 329 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 330 | #include <sys/mkdev.h> |
| 331 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 332 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 334 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 335 | #ifdef WITH_NEXT_FRAMEWORK |
| 336 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 337 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 338 | */ |
| 339 | #include <crt_externs.h> |
| 340 | static char **environ; |
| 341 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 343 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 344 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 345 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 346 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 348 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 349 | #ifdef MS_WINDOWS |
| 350 | wchar_t **e; |
| 351 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 353 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 354 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 355 | if (d == NULL) |
| 356 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 357 | #ifdef WITH_NEXT_FRAMEWORK |
| 358 | if (environ == NULL) |
| 359 | environ = *_NSGetEnviron(); |
| 360 | #endif |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 361 | #ifdef MS_WINDOWS |
| 362 | /* _wenviron must be initialized in this way if the program is started |
| 363 | through main() instead of wmain(). */ |
| 364 | _wgetenv(L""); |
| 365 | if (_wenviron == NULL) |
| 366 | return d; |
| 367 | /* This part ignores errors */ |
| 368 | for (e = _wenviron; *e != NULL; e++) { |
| 369 | PyObject *k; |
| 370 | PyObject *v; |
| 371 | wchar_t *p = wcschr(*e, L'='); |
| 372 | if (p == NULL) |
| 373 | continue; |
| 374 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 375 | if (k == NULL) { |
| 376 | PyErr_Clear(); |
| 377 | continue; |
| 378 | } |
| 379 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 380 | if (v == NULL) { |
| 381 | PyErr_Clear(); |
| 382 | Py_DECREF(k); |
| 383 | continue; |
| 384 | } |
| 385 | if (PyDict_GetItem(d, k) == NULL) { |
| 386 | if (PyDict_SetItem(d, k, v) != 0) |
| 387 | PyErr_Clear(); |
| 388 | } |
| 389 | Py_DECREF(k); |
| 390 | Py_DECREF(v); |
| 391 | } |
| 392 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | if (environ == NULL) |
| 394 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 395 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 396 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 397 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 398 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 399 | char *p = strchr(*e, '='); |
| 400 | if (p == NULL) |
| 401 | continue; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 402 | k = PyUnicode_FromStringAndSize(*e, (int)(p-*e)); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 403 | if (k == NULL) { |
| 404 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 405 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 406 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 407 | v = PyUnicode_FromString(p+1); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 408 | if (v == NULL) { |
| 409 | PyErr_Clear(); |
| 410 | Py_DECREF(k); |
| 411 | continue; |
| 412 | } |
| 413 | if (PyDict_GetItem(d, k) == NULL) { |
| 414 | if (PyDict_SetItem(d, k, v) != 0) |
| 415 | PyErr_Clear(); |
| 416 | } |
| 417 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 418 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 420 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 421 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 422 | { |
| 423 | APIRET rc; |
| 424 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 425 | |
| 426 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 427 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 428 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 429 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 430 | Py_DECREF(v); |
| 431 | } |
| 432 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 433 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 434 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 435 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 436 | Py_DECREF(v); |
| 437 | } |
| 438 | } |
| 439 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 440 | return d; |
| 441 | } |
| 442 | |
| 443 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 444 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 445 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 446 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 447 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 448 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 449 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 450 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 451 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 452 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 453 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 454 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 457 | #ifdef Py_WIN_WIDE_FILENAMES |
| 458 | static PyObject * |
| 459 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 460 | { |
| 461 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 462 | } |
| 463 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 464 | |
| 465 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 466 | static PyObject * |
| 467 | posix_error_with_allocated_filename(char* name) |
| 468 | { |
| 469 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 470 | PyMem_Free(name); |
| 471 | return rc; |
| 472 | } |
| 473 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 474 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 475 | static PyObject * |
| 476 | win32_error(char* function, char* filename) |
| 477 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 478 | /* XXX We should pass the function name along in the future. |
| 479 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 480 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 481 | Windows error object, which is non-trivial. |
| 482 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 483 | errno = GetLastError(); |
| 484 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 485 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 486 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 487 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 488 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 489 | |
| 490 | #ifdef Py_WIN_WIDE_FILENAMES |
| 491 | static PyObject * |
| 492 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 493 | { |
| 494 | /* XXX - see win32_error for comments on 'function' */ |
| 495 | errno = GetLastError(); |
| 496 | if (filename) |
| 497 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 498 | else |
| 499 | return PyErr_SetFromWindowsErr(errno); |
| 500 | } |
| 501 | |
| 502 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 503 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* Function suitable for O& conversion */ |
| 507 | static int |
| 508 | convert_to_unicode(PyObject *arg, void* _param) |
| 509 | { |
| 510 | PyObject **param = (PyObject**)_param; |
| 511 | if (PyUnicode_CheckExact(arg)) { |
| 512 | Py_INCREF(arg); |
| 513 | *param = arg; |
| 514 | } |
| 515 | else if (PyUnicode_Check(arg)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 516 | /* For a Unicode subtype that's not a Unicode object, |
| 517 | return a true Unicode object with the same data. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 518 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), |
| 519 | PyUnicode_GET_SIZE(arg)); |
| 520 | return *param != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 521 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 522 | else |
| 523 | *param = PyUnicode_FromEncodedObject(arg, |
| 524 | Py_FileSystemDefaultEncoding, |
| 525 | "strict"); |
| 526 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 530 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 531 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 533 | #if defined(PYOS_OS2) |
| 534 | /********************************************************************** |
| 535 | * Helper Function to Trim and Format OS/2 Messages |
| 536 | **********************************************************************/ |
| 537 | static void |
| 538 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 539 | { |
| 540 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 541 | |
| 542 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 543 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 544 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 545 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 546 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 547 | } |
| 548 | |
| 549 | /* Add Optional Reason Text */ |
| 550 | if (reason) { |
| 551 | strcat(msgbuf, " : "); |
| 552 | strcat(msgbuf, reason); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /********************************************************************** |
| 557 | * Decode an OS/2 Operating System Error Code |
| 558 | * |
| 559 | * A convenience function to lookup an OS/2 error code and return a |
| 560 | * text message we can use to raise a Python exception. |
| 561 | * |
| 562 | * Notes: |
| 563 | * The messages for errors returned from the OS/2 kernel reside in |
| 564 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 565 | * |
| 566 | **********************************************************************/ |
| 567 | static char * |
| 568 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 569 | { |
| 570 | APIRET rc; |
| 571 | ULONG msglen; |
| 572 | |
| 573 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 574 | Py_BEGIN_ALLOW_THREADS |
| 575 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 576 | errorcode, "oso001.msg", &msglen); |
| 577 | Py_END_ALLOW_THREADS |
| 578 | |
| 579 | if (rc == NO_ERROR) |
| 580 | os2_formatmsg(msgbuf, msglen, reason); |
| 581 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 582 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 583 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 584 | |
| 585 | return msgbuf; |
| 586 | } |
| 587 | |
| 588 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 589 | errors are not in a global variable e.g. 'errno' nor are |
| 590 | they congruent with posix error numbers. */ |
| 591 | |
| 592 | static PyObject * os2_error(int code) |
| 593 | { |
| 594 | char text[1024]; |
| 595 | PyObject *v; |
| 596 | |
| 597 | os2_strerror(text, sizeof(text), code, ""); |
| 598 | |
| 599 | v = Py_BuildValue("(is)", code, text); |
| 600 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 601 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 602 | Py_DECREF(v); |
| 603 | } |
| 604 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 605 | } |
| 606 | |
| 607 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 608 | |
| 609 | /* POSIX generic methods */ |
| 610 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 611 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 612 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 613 | { |
| 614 | int fd; |
| 615 | int res; |
| 616 | fd = PyObject_AsFileDescriptor(fdobj); |
| 617 | if (fd < 0) |
| 618 | return NULL; |
| 619 | Py_BEGIN_ALLOW_THREADS |
| 620 | res = (*func)(fd); |
| 621 | Py_END_ALLOW_THREADS |
| 622 | if (res < 0) |
| 623 | return posix_error(); |
| 624 | Py_INCREF(Py_None); |
| 625 | return Py_None; |
| 626 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 627 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 628 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 629 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 630 | unicode_file_names(void) |
| 631 | { |
| 632 | static int canusewide = -1; |
| 633 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 634 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 635 | the Windows NT family. */ |
| 636 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 637 | } |
| 638 | return canusewide; |
| 639 | } |
| 640 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 641 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 642 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 643 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 644 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 645 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 646 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 647 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 648 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 649 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 650 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 651 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 652 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 653 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 654 | return posix_error_with_allocated_filename(path1); |
| 655 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 656 | Py_INCREF(Py_None); |
| 657 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 660 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 661 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 662 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 663 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 664 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 665 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 666 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 667 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 668 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 669 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 670 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 671 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 672 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 673 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 674 | PyMem_Free(path1); |
| 675 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 676 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 677 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 678 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 679 | Py_INCREF(Py_None); |
| 680 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 683 | #ifdef Py_WIN_WIDE_FILENAMES |
| 684 | static PyObject* |
| 685 | win32_1str(PyObject* args, char* func, |
| 686 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 687 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 688 | { |
| 689 | PyObject *uni; |
| 690 | char *ansi; |
| 691 | BOOL result; |
| 692 | if (unicode_file_names()) { |
| 693 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 694 | PyErr_Clear(); |
| 695 | else { |
| 696 | Py_BEGIN_ALLOW_THREADS |
| 697 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 698 | Py_END_ALLOW_THREADS |
| 699 | if (!result) |
| 700 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 701 | Py_INCREF(Py_None); |
| 702 | return Py_None; |
| 703 | } |
| 704 | } |
| 705 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 706 | return NULL; |
| 707 | Py_BEGIN_ALLOW_THREADS |
| 708 | result = funcA(ansi); |
| 709 | Py_END_ALLOW_THREADS |
| 710 | if (!result) |
| 711 | return win32_error(func, ansi); |
| 712 | Py_INCREF(Py_None); |
| 713 | return Py_None; |
| 714 | |
| 715 | } |
| 716 | |
| 717 | /* This is a reimplementation of the C library's chdir function, |
| 718 | but one that produces Win32 errors instead of DOS error codes. |
| 719 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 720 | it also needs to set "magic" environment variables indicating |
| 721 | the per-drive current directory, which are of the form =<drive>: */ |
| 722 | BOOL __stdcall |
| 723 | win32_chdir(LPCSTR path) |
| 724 | { |
| 725 | char new_path[MAX_PATH+1]; |
| 726 | int result; |
| 727 | char env[4] = "=x:"; |
| 728 | |
| 729 | if(!SetCurrentDirectoryA(path)) |
| 730 | return FALSE; |
| 731 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 732 | if (!result) |
| 733 | return FALSE; |
| 734 | /* In the ANSI API, there should not be any paths longer |
| 735 | than MAX_PATH. */ |
| 736 | assert(result <= MAX_PATH+1); |
| 737 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 738 | strncmp(new_path, "//", 2) == 0) |
| 739 | /* UNC path, nothing to do. */ |
| 740 | return TRUE; |
| 741 | env[1] = new_path[0]; |
| 742 | return SetEnvironmentVariableA(env, new_path); |
| 743 | } |
| 744 | |
| 745 | /* The Unicode version differs from the ANSI version |
| 746 | since the current directory might exceed MAX_PATH characters */ |
| 747 | BOOL __stdcall |
| 748 | win32_wchdir(LPCWSTR path) |
| 749 | { |
| 750 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 751 | int result; |
| 752 | wchar_t env[4] = L"=x:"; |
| 753 | |
| 754 | if(!SetCurrentDirectoryW(path)) |
| 755 | return FALSE; |
| 756 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 757 | if (!result) |
| 758 | return FALSE; |
| 759 | if (result > MAX_PATH+1) { |
| 760 | new_path = malloc(result); |
| 761 | if (!new_path) { |
| 762 | SetLastError(ERROR_OUTOFMEMORY); |
| 763 | return FALSE; |
| 764 | } |
| 765 | } |
| 766 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 767 | wcsncmp(new_path, L"//", 2) == 0) |
| 768 | /* UNC path, nothing to do. */ |
| 769 | return TRUE; |
| 770 | env[1] = new_path[0]; |
| 771 | result = SetEnvironmentVariableW(env, new_path); |
| 772 | if (new_path != _new_path) |
| 773 | free(new_path); |
| 774 | return result; |
| 775 | } |
| 776 | #endif |
| 777 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 778 | #ifdef MS_WINDOWS |
| 779 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 780 | - time stamps are restricted to second resolution |
| 781 | - file modification times suffer from forth-and-back conversions between |
| 782 | UTC and local time |
| 783 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 784 | */ |
| 785 | #define HAVE_STAT_NSEC 1 |
| 786 | |
| 787 | struct win32_stat{ |
| 788 | int st_dev; |
| 789 | __int64 st_ino; |
| 790 | unsigned short st_mode; |
| 791 | int st_nlink; |
| 792 | int st_uid; |
| 793 | int st_gid; |
| 794 | int st_rdev; |
| 795 | __int64 st_size; |
| 796 | int st_atime; |
| 797 | int st_atime_nsec; |
| 798 | int st_mtime; |
| 799 | int st_mtime_nsec; |
| 800 | int st_ctime; |
| 801 | int st_ctime_nsec; |
| 802 | }; |
| 803 | |
| 804 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 805 | |
| 806 | static void |
| 807 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 808 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 809 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 810 | /* Cannot simply cast and dereference in_ptr, |
| 811 | since it might not be aligned properly */ |
| 812 | __int64 in; |
| 813 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 814 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 815 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 816 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 817 | } |
| 818 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 819 | static void |
| 820 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 821 | { |
| 822 | /* XXX endianness */ |
| 823 | __int64 out; |
| 824 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 825 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 826 | memcpy(out_ptr, &out, sizeof(out)); |
| 827 | } |
| 828 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 829 | /* Below, we *know* that ugo+r is 0444 */ |
| 830 | #if _S_IREAD != 0400 |
| 831 | #error Unsupported C library |
| 832 | #endif |
| 833 | static int |
| 834 | attributes_to_mode(DWORD attr) |
| 835 | { |
| 836 | int m = 0; |
| 837 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 838 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 839 | else |
| 840 | m |= _S_IFREG; |
| 841 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 842 | m |= 0444; |
| 843 | else |
| 844 | m |= 0666; |
| 845 | return m; |
| 846 | } |
| 847 | |
| 848 | static int |
| 849 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 850 | { |
| 851 | memset(result, 0, sizeof(*result)); |
| 852 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 853 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 854 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 855 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 856 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 861 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 862 | static int checked = 0; |
| 863 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 864 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 865 | static void |
| 866 | check_gfax() |
| 867 | { |
| 868 | HINSTANCE hKernel32; |
| 869 | if (checked) |
| 870 | return; |
| 871 | checked = 1; |
| 872 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 873 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 874 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 875 | } |
| 876 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 877 | static BOOL |
| 878 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 879 | { |
| 880 | HANDLE hFindFile; |
| 881 | WIN32_FIND_DATAA FileData; |
| 882 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 883 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 884 | return FALSE; |
| 885 | FindClose(hFindFile); |
| 886 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 887 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 888 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 889 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 890 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 891 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 892 | return TRUE; |
| 893 | } |
| 894 | |
| 895 | static BOOL |
| 896 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 897 | { |
| 898 | HANDLE hFindFile; |
| 899 | WIN32_FIND_DATAW FileData; |
| 900 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 901 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 902 | return FALSE; |
| 903 | FindClose(hFindFile); |
| 904 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 905 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 906 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 907 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 908 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 909 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 910 | return TRUE; |
| 911 | } |
| 912 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 913 | static BOOL WINAPI |
| 914 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 915 | GET_FILEEX_INFO_LEVELS level, |
| 916 | LPVOID pv) |
| 917 | { |
| 918 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 919 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 920 | /* First try to use the system's implementation, if that is |
| 921 | available and either succeeds to gives an error other than |
| 922 | that it isn't implemented. */ |
| 923 | check_gfax(); |
| 924 | if (gfaxa) { |
| 925 | result = gfaxa(pszFile, level, pv); |
| 926 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 927 | return result; |
| 928 | } |
| 929 | /* It's either not present, or not implemented. |
| 930 | Emulate using FindFirstFile. */ |
| 931 | if (level != GetFileExInfoStandard) { |
| 932 | SetLastError(ERROR_INVALID_PARAMETER); |
| 933 | return FALSE; |
| 934 | } |
| 935 | /* Use GetFileAttributes to validate that the file name |
| 936 | does not contain wildcards (which FindFirstFile would |
| 937 | accept). */ |
| 938 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 939 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 940 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static BOOL WINAPI |
| 944 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 945 | GET_FILEEX_INFO_LEVELS level, |
| 946 | LPVOID pv) |
| 947 | { |
| 948 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 949 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 950 | /* First try to use the system's implementation, if that is |
| 951 | available and either succeeds to gives an error other than |
| 952 | that it isn't implemented. */ |
| 953 | check_gfax(); |
| 954 | if (gfaxa) { |
| 955 | result = gfaxw(pszFile, level, pv); |
| 956 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 957 | return result; |
| 958 | } |
| 959 | /* It's either not present, or not implemented. |
| 960 | Emulate using FindFirstFile. */ |
| 961 | if (level != GetFileExInfoStandard) { |
| 962 | SetLastError(ERROR_INVALID_PARAMETER); |
| 963 | return FALSE; |
| 964 | } |
| 965 | /* Use GetFileAttributes to validate that the file name |
| 966 | does not contain wildcards (which FindFirstFile would |
| 967 | accept). */ |
| 968 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 969 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 970 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 973 | static int |
| 974 | win32_stat(const char* path, struct win32_stat *result) |
| 975 | { |
| 976 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 977 | int code; |
| 978 | char *dot; |
| 979 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 980 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 981 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 982 | /* Protocol violation: we explicitly clear errno, instead of |
| 983 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 984 | errno = 0; |
| 985 | return -1; |
| 986 | } else { |
| 987 | /* Could not get attributes on open file. Fall back to |
| 988 | reading the directory. */ |
| 989 | if (!attributes_from_dir(path, &info)) { |
| 990 | /* Very strange. This should not fail now */ |
| 991 | errno = 0; |
| 992 | return -1; |
| 993 | } |
| 994 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 995 | } |
| 996 | code = attribute_data_to_stat(&info, result); |
| 997 | if (code != 0) |
| 998 | return code; |
| 999 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1000 | dot = strrchr(path, '.'); |
| 1001 | if (dot) { |
| 1002 | if (stricmp(dot, ".bat") == 0 || |
| 1003 | stricmp(dot, ".cmd") == 0 || |
| 1004 | stricmp(dot, ".exe") == 0 || |
| 1005 | stricmp(dot, ".com") == 0) |
| 1006 | result->st_mode |= 0111; |
| 1007 | } |
| 1008 | return code; |
| 1009 | } |
| 1010 | |
| 1011 | static int |
| 1012 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1013 | { |
| 1014 | int code; |
| 1015 | const wchar_t *dot; |
| 1016 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1017 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1018 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1019 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1020 | /* Protocol violation: we explicitly clear errno, instead of |
| 1021 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1022 | errno = 0; |
| 1023 | return -1; |
| 1024 | } else { |
| 1025 | /* Could not get attributes on open file. Fall back to |
| 1026 | reading the directory. */ |
| 1027 | if (!attributes_from_dir_w(path, &info)) { |
| 1028 | /* Very strange. This should not fail now */ |
| 1029 | errno = 0; |
| 1030 | return -1; |
| 1031 | } |
| 1032 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1033 | } |
| 1034 | code = attribute_data_to_stat(&info, result); |
| 1035 | if (code < 0) |
| 1036 | return code; |
| 1037 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1038 | dot = wcsrchr(path, '.'); |
| 1039 | if (dot) { |
| 1040 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1041 | _wcsicmp(dot, L".cmd") == 0 || |
| 1042 | _wcsicmp(dot, L".exe") == 0 || |
| 1043 | _wcsicmp(dot, L".com") == 0) |
| 1044 | result->st_mode |= 0111; |
| 1045 | } |
| 1046 | return code; |
| 1047 | } |
| 1048 | |
| 1049 | static int |
| 1050 | win32_fstat(int file_number, struct win32_stat *result) |
| 1051 | { |
| 1052 | BY_HANDLE_FILE_INFORMATION info; |
| 1053 | HANDLE h; |
| 1054 | int type; |
| 1055 | |
| 1056 | h = (HANDLE)_get_osfhandle(file_number); |
| 1057 | |
| 1058 | /* Protocol violation: we explicitly clear errno, instead of |
| 1059 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1060 | errno = 0; |
| 1061 | |
| 1062 | if (h == INVALID_HANDLE_VALUE) { |
| 1063 | /* This is really a C library error (invalid file handle). |
| 1064 | We set the Win32 error to the closes one matching. */ |
| 1065 | SetLastError(ERROR_INVALID_HANDLE); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | memset(result, 0, sizeof(*result)); |
| 1069 | |
| 1070 | type = GetFileType(h); |
| 1071 | if (type == FILE_TYPE_UNKNOWN) { |
| 1072 | DWORD error = GetLastError(); |
| 1073 | if (error != 0) { |
| 1074 | return -1; |
| 1075 | } |
| 1076 | /* else: valid but unknown file */ |
| 1077 | } |
| 1078 | |
| 1079 | if (type != FILE_TYPE_DISK) { |
| 1080 | if (type == FILE_TYPE_CHAR) |
| 1081 | result->st_mode = _S_IFCHR; |
| 1082 | else if (type == FILE_TYPE_PIPE) |
| 1083 | result->st_mode = _S_IFIFO; |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | if (!GetFileInformationByHandle(h, &info)) { |
| 1088 | return -1; |
| 1089 | } |
| 1090 | |
| 1091 | /* similar to stat() */ |
| 1092 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1093 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1094 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1095 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1096 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1097 | /* specific to fstat() */ |
| 1098 | result->st_nlink = info.nNumberOfLinks; |
| 1099 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | #endif /* MS_WINDOWS */ |
| 1104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1105 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1106 | "stat_result: Result from stat or lstat.\n\n\ |
| 1107 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1108 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1109 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1110 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1111 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1112 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1113 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1114 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1115 | |
| 1116 | static PyStructSequence_Field stat_result_fields[] = { |
| 1117 | {"st_mode", "protection bits"}, |
| 1118 | {"st_ino", "inode"}, |
| 1119 | {"st_dev", "device"}, |
| 1120 | {"st_nlink", "number of hard links"}, |
| 1121 | {"st_uid", "user ID of owner"}, |
| 1122 | {"st_gid", "group ID of owner"}, |
| 1123 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1124 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1125 | {NULL, "integer time of last access"}, |
| 1126 | {NULL, "integer time of last modification"}, |
| 1127 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1128 | {"st_atime", "time of last access"}, |
| 1129 | {"st_mtime", "time of last modification"}, |
| 1130 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1131 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1132 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1133 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1134 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1135 | {"st_blocks", "number of blocks allocated"}, |
| 1136 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1137 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1138 | {"st_rdev", "device type (if inode device)"}, |
| 1139 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1140 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1141 | {"st_flags", "user defined flags for file"}, |
| 1142 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1143 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1144 | {"st_gen", "generation number"}, |
| 1145 | #endif |
| 1146 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1147 | {"st_birthtime", "time of creation"}, |
| 1148 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1149 | {0} |
| 1150 | }; |
| 1151 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1152 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1153 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1154 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1155 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1156 | #endif |
| 1157 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1158 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1159 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1160 | #else |
| 1161 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1162 | #endif |
| 1163 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1164 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1165 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1166 | #else |
| 1167 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1168 | #endif |
| 1169 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1170 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1171 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1172 | #else |
| 1173 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1174 | #endif |
| 1175 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1176 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1177 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1178 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1179 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1180 | #endif |
| 1181 | |
| 1182 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1183 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1184 | #else |
| 1185 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1186 | #endif |
| 1187 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1188 | static PyStructSequence_Desc stat_result_desc = { |
| 1189 | "stat_result", /* name */ |
| 1190 | stat_result__doc__, /* doc */ |
| 1191 | stat_result_fields, |
| 1192 | 10 |
| 1193 | }; |
| 1194 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1195 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1196 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1197 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1198 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1199 | 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] | 1200 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1201 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1202 | |
| 1203 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1204 | {"f_bsize", }, |
| 1205 | {"f_frsize", }, |
| 1206 | {"f_blocks", }, |
| 1207 | {"f_bfree", }, |
| 1208 | {"f_bavail", }, |
| 1209 | {"f_files", }, |
| 1210 | {"f_ffree", }, |
| 1211 | {"f_favail", }, |
| 1212 | {"f_flag", }, |
| 1213 | {"f_namemax",}, |
| 1214 | {0} |
| 1215 | }; |
| 1216 | |
| 1217 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1218 | "statvfs_result", /* name */ |
| 1219 | statvfs_result__doc__, /* doc */ |
| 1220 | statvfs_result_fields, |
| 1221 | 10 |
| 1222 | }; |
| 1223 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1224 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1225 | static PyTypeObject StatResultType; |
| 1226 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1227 | static newfunc structseq_new; |
| 1228 | |
| 1229 | static PyObject * |
| 1230 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1231 | { |
| 1232 | PyStructSequence *result; |
| 1233 | int i; |
| 1234 | |
| 1235 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1236 | if (!result) |
| 1237 | return NULL; |
| 1238 | /* If we have been initialized from a tuple, |
| 1239 | st_?time might be set to None. Initialize it |
| 1240 | from the int slots. */ |
| 1241 | for (i = 7; i <= 9; i++) { |
| 1242 | if (result->ob_item[i+3] == Py_None) { |
| 1243 | Py_DECREF(Py_None); |
| 1244 | Py_INCREF(result->ob_item[i]); |
| 1245 | result->ob_item[i+3] = result->ob_item[i]; |
| 1246 | } |
| 1247 | } |
| 1248 | return (PyObject*)result; |
| 1249 | } |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1254 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1255 | |
| 1256 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1257 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1258 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1259 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1260 | future calls return ints. \n\ |
| 1261 | If newval is omitted, return the current setting.\n"); |
| 1262 | |
| 1263 | static PyObject* |
| 1264 | stat_float_times(PyObject* self, PyObject *args) |
| 1265 | { |
| 1266 | int newval = -1; |
| 1267 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1268 | return NULL; |
| 1269 | if (newval == -1) |
| 1270 | /* Return old value */ |
| 1271 | return PyBool_FromLong(_stat_float_times); |
| 1272 | _stat_float_times = newval; |
| 1273 | Py_INCREF(Py_None); |
| 1274 | return Py_None; |
| 1275 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1276 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1277 | static void |
| 1278 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1279 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1280 | PyObject *fval,*ival; |
| 1281 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1282 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1283 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1284 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1285 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1286 | if (!ival) |
| 1287 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1288 | if (_stat_float_times) { |
| 1289 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1290 | } else { |
| 1291 | fval = ival; |
| 1292 | Py_INCREF(fval); |
| 1293 | } |
| 1294 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1295 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1298 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1299 | (used by posix_stat() and posix_fstat()) */ |
| 1300 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1301 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1302 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1303 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1304 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1305 | if (v == NULL) |
| 1306 | return NULL; |
| 1307 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1308 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1309 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1310 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1311 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1312 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1313 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1314 | #endif |
| 1315 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1316 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1317 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1318 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1319 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1320 | #endif |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1321 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1322 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1323 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1324 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1325 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1326 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1327 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1328 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1329 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1330 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1331 | #if defined(HAVE_STAT_TV_NSEC) |
| 1332 | ansec = st->st_atim.tv_nsec; |
| 1333 | mnsec = st->st_mtim.tv_nsec; |
| 1334 | cnsec = st->st_ctim.tv_nsec; |
| 1335 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1336 | ansec = st->st_atimespec.tv_nsec; |
| 1337 | mnsec = st->st_mtimespec.tv_nsec; |
| 1338 | cnsec = st->st_ctimespec.tv_nsec; |
| 1339 | #elif defined(HAVE_STAT_NSEC) |
| 1340 | ansec = st->st_atime_nsec; |
| 1341 | mnsec = st->st_mtime_nsec; |
| 1342 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1343 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1344 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1345 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1346 | fill_time(v, 7, st->st_atime, ansec); |
| 1347 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1348 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1349 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1350 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1351 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1352 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1353 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1354 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1355 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1356 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1357 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1358 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1359 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1360 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1361 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1362 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1363 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1364 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1365 | #endif |
| 1366 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1367 | { |
| 1368 | PyObject *val; |
| 1369 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1370 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1371 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1372 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1373 | #else |
| 1374 | bnsec = 0; |
| 1375 | #endif |
| 1376 | if (_stat_float_times) { |
| 1377 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1378 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1379 | val = PyLong_FromLong((long)bsec); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1380 | } |
| 1381 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1382 | val); |
| 1383 | } |
| 1384 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1385 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1386 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1387 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1388 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (PyErr_Occurred()) { |
| 1391 | Py_DECREF(v); |
| 1392 | return NULL; |
| 1393 | } |
| 1394 | |
| 1395 | return v; |
| 1396 | } |
| 1397 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1398 | #ifdef MS_WINDOWS |
| 1399 | |
| 1400 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1401 | where / can be used in place of \ and the trailing slash is optional. |
| 1402 | Both SERVER and SHARE must have at least one character. |
| 1403 | */ |
| 1404 | |
| 1405 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1406 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1407 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1408 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1409 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1410 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1411 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1412 | IsUNCRootA(char *path, int pathlen) |
| 1413 | { |
| 1414 | #define ISSLASH ISSLASHA |
| 1415 | |
| 1416 | int i, share; |
| 1417 | |
| 1418 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1419 | /* minimum UNCRoot is \\x\y */ |
| 1420 | return FALSE; |
| 1421 | for (i = 2; i < pathlen ; i++) |
| 1422 | if (ISSLASH(path[i])) break; |
| 1423 | if (i == 2 || i == pathlen) |
| 1424 | /* do not allow \\\SHARE or \\SERVER */ |
| 1425 | return FALSE; |
| 1426 | share = i+1; |
| 1427 | for (i = share; i < pathlen; i++) |
| 1428 | if (ISSLASH(path[i])) break; |
| 1429 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1430 | |
| 1431 | #undef ISSLASH |
| 1432 | } |
| 1433 | |
| 1434 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1435 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1436 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1437 | { |
| 1438 | #define ISSLASH ISSLASHW |
| 1439 | |
| 1440 | int i, share; |
| 1441 | |
| 1442 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1443 | /* minimum UNCRoot is \\x\y */ |
| 1444 | return FALSE; |
| 1445 | for (i = 2; i < pathlen ; i++) |
| 1446 | if (ISSLASH(path[i])) break; |
| 1447 | if (i == 2 || i == pathlen) |
| 1448 | /* do not allow \\\SHARE or \\SERVER */ |
| 1449 | return FALSE; |
| 1450 | share = i+1; |
| 1451 | for (i = share; i < pathlen; i++) |
| 1452 | if (ISSLASH(path[i])) break; |
| 1453 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1454 | |
| 1455 | #undef ISSLASH |
| 1456 | } |
| 1457 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1458 | #endif /* MS_WINDOWS */ |
| 1459 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1460 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1461 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1462 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1463 | #ifdef __VMS |
| 1464 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1465 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1466 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1467 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1468 | char *wformat, |
| 1469 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1470 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1471 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1472 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1473 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1474 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1475 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1476 | |
| 1477 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1478 | /* If on wide-character-capable OS see if argument |
| 1479 | is Unicode and if so use wide API. */ |
| 1480 | if (unicode_file_names()) { |
| 1481 | PyUnicodeObject *po; |
| 1482 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1483 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1484 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1485 | Py_BEGIN_ALLOW_THREADS |
| 1486 | /* PyUnicode_AS_UNICODE result OK without |
| 1487 | thread lock as it is a simple dereference. */ |
| 1488 | res = wstatfunc(wpath, &st); |
| 1489 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1490 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1491 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1492 | return win32_error_unicode("stat", wpath); |
| 1493 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1494 | } |
| 1495 | /* Drop the argument parsing error as narrow strings |
| 1496 | are also valid. */ |
| 1497 | PyErr_Clear(); |
| 1498 | } |
| 1499 | #endif |
| 1500 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1501 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1502 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1503 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1504 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1505 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1506 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1507 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1508 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1509 | |
| 1510 | if (res != 0) { |
| 1511 | #ifdef MS_WINDOWS |
| 1512 | result = win32_error("stat", pathfree); |
| 1513 | #else |
| 1514 | result = posix_error_with_filename(pathfree); |
| 1515 | #endif |
| 1516 | } |
| 1517 | else |
| 1518 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1519 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1520 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1521 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1524 | /* POSIX methods */ |
| 1525 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1526 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1527 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1528 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1529 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1530 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1531 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1532 | 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] | 1533 | |
| 1534 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1535 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1536 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1537 | char *path; |
| 1538 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1539 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1540 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1541 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1542 | if (unicode_file_names()) { |
| 1543 | PyUnicodeObject *po; |
| 1544 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1545 | Py_BEGIN_ALLOW_THREADS |
| 1546 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1547 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1548 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1549 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1550 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1551 | } |
| 1552 | /* Drop the argument parsing error as narrow strings |
| 1553 | are also valid. */ |
| 1554 | PyErr_Clear(); |
| 1555 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1556 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1557 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1558 | return 0; |
| 1559 | Py_BEGIN_ALLOW_THREADS |
| 1560 | attr = GetFileAttributesA(path); |
| 1561 | Py_END_ALLOW_THREADS |
| 1562 | PyMem_Free(path); |
| 1563 | finish: |
| 1564 | if (attr == 0xFFFFFFFF) |
| 1565 | /* File does not exist, or cannot read attributes */ |
| 1566 | return PyBool_FromLong(0); |
| 1567 | /* Access is possible if either write access wasn't requested, or |
Guido van Rossum | b00324f | 2007-12-04 01:13:14 +0000 | [diff] [blame] | 1568 | the file isn't read-only, or if it's a directory, as there are |
| 1569 | no read-only directories on Windows. */ |
| 1570 | return PyBool_FromLong(!(mode & 2) |
| 1571 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1572 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1573 | #else |
| 1574 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1575 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1576 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1577 | return NULL; |
| 1578 | Py_BEGIN_ALLOW_THREADS |
| 1579 | res = access(path, mode); |
| 1580 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1581 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1582 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1583 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1586 | #ifndef F_OK |
| 1587 | #define F_OK 0 |
| 1588 | #endif |
| 1589 | #ifndef R_OK |
| 1590 | #define R_OK 4 |
| 1591 | #endif |
| 1592 | #ifndef W_OK |
| 1593 | #define W_OK 2 |
| 1594 | #endif |
| 1595 | #ifndef X_OK |
| 1596 | #define X_OK 1 |
| 1597 | #endif |
| 1598 | |
| 1599 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1600 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1601 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1602 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1603 | |
| 1604 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1605 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1606 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1607 | int id; |
| 1608 | char *ret; |
| 1609 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1610 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1611 | return NULL; |
| 1612 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1613 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1614 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1615 | if (id == 0) { |
| 1616 | ret = ttyname(); |
| 1617 | } |
| 1618 | else { |
| 1619 | ret = NULL; |
| 1620 | } |
| 1621 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1622 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1623 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1624 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1625 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1626 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1627 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1628 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1629 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1630 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1631 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1632 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1633 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1634 | |
| 1635 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1636 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1637 | { |
| 1638 | char *ret; |
| 1639 | char buffer[L_ctermid]; |
| 1640 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1641 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1642 | ret = ctermid_r(buffer); |
| 1643 | #else |
| 1644 | ret = ctermid(buffer); |
| 1645 | #endif |
| 1646 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1647 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1648 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1649 | } |
| 1650 | #endif |
| 1651 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1652 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1653 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1654 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1655 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1656 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1657 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1658 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1659 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1660 | return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1661 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1662 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1663 | #elif defined(__VMS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1664 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1665 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1666 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1667 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1670 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1671 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1672 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1673 | 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] | 1674 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1675 | |
| 1676 | static PyObject * |
| 1677 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1678 | { |
| 1679 | return posix_fildes(fdobj, fchdir); |
| 1680 | } |
| 1681 | #endif /* HAVE_FCHDIR */ |
| 1682 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1684 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1685 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1686 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1687 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1688 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1689 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1690 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1691 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1692 | int i; |
| 1693 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1694 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1695 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1696 | if (unicode_file_names()) { |
| 1697 | PyUnicodeObject *po; |
| 1698 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1699 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1700 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1701 | if (attr != 0xFFFFFFFF) { |
| 1702 | if (i & _S_IWRITE) |
| 1703 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1704 | else |
| 1705 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1706 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1707 | } |
| 1708 | else |
| 1709 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1710 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1711 | if (!res) |
| 1712 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1713 | PyUnicode_AS_UNICODE(po)); |
| 1714 | Py_INCREF(Py_None); |
| 1715 | return Py_None; |
| 1716 | } |
| 1717 | /* Drop the argument parsing error as narrow strings |
| 1718 | are also valid. */ |
| 1719 | PyErr_Clear(); |
| 1720 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1721 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1722 | &path, &i)) |
| 1723 | return NULL; |
| 1724 | Py_BEGIN_ALLOW_THREADS |
| 1725 | attr = GetFileAttributesA(path); |
| 1726 | if (attr != 0xFFFFFFFF) { |
| 1727 | if (i & _S_IWRITE) |
| 1728 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1729 | else |
| 1730 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1731 | res = SetFileAttributesA(path, attr); |
| 1732 | } |
| 1733 | else |
| 1734 | res = 0; |
| 1735 | Py_END_ALLOW_THREADS |
| 1736 | if (!res) { |
| 1737 | win32_error("chmod", path); |
| 1738 | PyMem_Free(path); |
| 1739 | return NULL; |
| 1740 | } |
| 1741 | PyMem_Free(path); |
| 1742 | Py_INCREF(Py_None); |
| 1743 | return Py_None; |
| 1744 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1745 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1746 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1747 | return NULL; |
| 1748 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1749 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1750 | Py_END_ALLOW_THREADS |
| 1751 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1752 | return posix_error_with_allocated_filename(path); |
| 1753 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1754 | Py_INCREF(Py_None); |
| 1755 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1756 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1759 | #ifdef HAVE_FCHMOD |
| 1760 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1761 | "fchmod(fd, mode)\n\n\ |
| 1762 | Change the access permissions of the file given by file\n\ |
| 1763 | descriptor fd."); |
| 1764 | |
| 1765 | static PyObject * |
| 1766 | posix_fchmod(PyObject *self, PyObject *args) |
| 1767 | { |
| 1768 | int fd, mode, res; |
| 1769 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1770 | return NULL; |
| 1771 | Py_BEGIN_ALLOW_THREADS |
| 1772 | res = fchmod(fd, mode); |
| 1773 | Py_END_ALLOW_THREADS |
| 1774 | if (res < 0) |
| 1775 | return posix_error(); |
| 1776 | Py_RETURN_NONE; |
| 1777 | } |
| 1778 | #endif /* HAVE_FCHMOD */ |
| 1779 | |
| 1780 | #ifdef HAVE_LCHMOD |
| 1781 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1782 | "lchmod(path, mode)\n\n\ |
| 1783 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1784 | affects the link itself rather than the target."); |
| 1785 | |
| 1786 | static PyObject * |
| 1787 | posix_lchmod(PyObject *self, PyObject *args) |
| 1788 | { |
| 1789 | char *path = NULL; |
| 1790 | int i; |
| 1791 | int res; |
| 1792 | if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding, |
| 1793 | &path, &i)) |
| 1794 | return NULL; |
| 1795 | Py_BEGIN_ALLOW_THREADS |
| 1796 | res = lchmod(path, i); |
| 1797 | Py_END_ALLOW_THREADS |
| 1798 | if (res < 0) |
| 1799 | return posix_error_with_allocated_filename(path); |
| 1800 | PyMem_Free(path); |
| 1801 | Py_RETURN_NONE; |
| 1802 | } |
| 1803 | #endif /* HAVE_LCHMOD */ |
| 1804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1805 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1806 | #ifdef HAVE_CHFLAGS |
| 1807 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1808 | "chflags(path, flags)\n\n\ |
| 1809 | Set file flags."); |
| 1810 | |
| 1811 | static PyObject * |
| 1812 | posix_chflags(PyObject *self, PyObject *args) |
| 1813 | { |
| 1814 | char *path; |
| 1815 | unsigned long flags; |
| 1816 | int res; |
| 1817 | if (!PyArg_ParseTuple(args, "etk:chflags", |
| 1818 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1819 | return NULL; |
| 1820 | Py_BEGIN_ALLOW_THREADS |
| 1821 | res = chflags(path, flags); |
| 1822 | Py_END_ALLOW_THREADS |
| 1823 | if (res < 0) |
| 1824 | return posix_error_with_allocated_filename(path); |
| 1825 | PyMem_Free(path); |
| 1826 | Py_INCREF(Py_None); |
| 1827 | return Py_None; |
| 1828 | } |
| 1829 | #endif /* HAVE_CHFLAGS */ |
| 1830 | |
| 1831 | #ifdef HAVE_LCHFLAGS |
| 1832 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1833 | "lchflags(path, flags)\n\n\ |
| 1834 | Set file flags.\n\ |
| 1835 | This function will not follow symbolic links."); |
| 1836 | |
| 1837 | static PyObject * |
| 1838 | posix_lchflags(PyObject *self, PyObject *args) |
| 1839 | { |
| 1840 | char *path; |
| 1841 | unsigned long flags; |
| 1842 | int res; |
| 1843 | if (!PyArg_ParseTuple(args, "etk:lchflags", |
| 1844 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1845 | return NULL; |
| 1846 | Py_BEGIN_ALLOW_THREADS |
| 1847 | res = lchflags(path, flags); |
| 1848 | Py_END_ALLOW_THREADS |
| 1849 | if (res < 0) |
| 1850 | return posix_error_with_allocated_filename(path); |
| 1851 | PyMem_Free(path); |
| 1852 | Py_INCREF(Py_None); |
| 1853 | return Py_None; |
| 1854 | } |
| 1855 | #endif /* HAVE_LCHFLAGS */ |
| 1856 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1857 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1858 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1859 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1860 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1861 | |
| 1862 | static PyObject * |
| 1863 | posix_chroot(PyObject *self, PyObject *args) |
| 1864 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1865 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1866 | } |
| 1867 | #endif |
| 1868 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1869 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1870 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1871 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1872 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1873 | |
| 1874 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1875 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1876 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1877 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1878 | } |
| 1879 | #endif /* HAVE_FSYNC */ |
| 1880 | |
| 1881 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1882 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1883 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1884 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1885 | #endif |
| 1886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1887 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1888 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1889 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1890 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1891 | |
| 1892 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1893 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1894 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1895 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1896 | } |
| 1897 | #endif /* HAVE_FDATASYNC */ |
| 1898 | |
| 1899 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1900 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1901 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1902 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1903 | 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] | 1904 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1905 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1906 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1907 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1908 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1909 | int uid, gid; |
| 1910 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1911 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1912 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1913 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1914 | return NULL; |
| 1915 | Py_BEGIN_ALLOW_THREADS |
| 1916 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1917 | Py_END_ALLOW_THREADS |
| 1918 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1919 | return posix_error_with_allocated_filename(path); |
| 1920 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1921 | Py_INCREF(Py_None); |
| 1922 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1923 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1924 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1925 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1926 | #ifdef HAVE_FCHOWN |
| 1927 | PyDoc_STRVAR(posix_fchown__doc__, |
| 1928 | "fchown(fd, uid, gid)\n\n\ |
| 1929 | Change the owner and group id of the file given by file descriptor\n\ |
| 1930 | fd to the numeric uid and gid."); |
| 1931 | |
| 1932 | static PyObject * |
| 1933 | posix_fchown(PyObject *self, PyObject *args) |
| 1934 | { |
| 1935 | int fd, uid, gid; |
| 1936 | int res; |
| 1937 | if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid)) |
| 1938 | return NULL; |
| 1939 | Py_BEGIN_ALLOW_THREADS |
| 1940 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 1941 | Py_END_ALLOW_THREADS |
| 1942 | if (res < 0) |
| 1943 | return posix_error(); |
| 1944 | Py_RETURN_NONE; |
| 1945 | } |
| 1946 | #endif /* HAVE_FCHOWN */ |
| 1947 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1948 | #ifdef HAVE_LCHOWN |
| 1949 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1950 | "lchown(path, uid, gid)\n\n\ |
| 1951 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1952 | This function will not follow symbolic links."); |
| 1953 | |
| 1954 | static PyObject * |
| 1955 | posix_lchown(PyObject *self, PyObject *args) |
| 1956 | { |
| 1957 | char *path = NULL; |
| 1958 | int uid, gid; |
| 1959 | int res; |
| 1960 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1961 | Py_FileSystemDefaultEncoding, &path, |
| 1962 | &uid, &gid)) |
| 1963 | return NULL; |
| 1964 | Py_BEGIN_ALLOW_THREADS |
| 1965 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1966 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1967 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1968 | return posix_error_with_allocated_filename(path); |
| 1969 | PyMem_Free(path); |
| 1970 | Py_INCREF(Py_None); |
| 1971 | return Py_None; |
| 1972 | } |
| 1973 | #endif /* HAVE_LCHOWN */ |
| 1974 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1975 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1976 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1977 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1978 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1979 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1980 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1981 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1982 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1983 | { |
| 1984 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1985 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1987 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1988 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1989 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1990 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1991 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1992 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1993 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1994 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1995 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1996 | return PyUnicode_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1997 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1998 | |
| 1999 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 2000 | "getcwdu() -> path\n\n\ |
| 2001 | Return a unicode string representing the current working directory."); |
| 2002 | |
| 2003 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2004 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2005 | { |
| 2006 | char buf[1026]; |
| 2007 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2008 | |
| 2009 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2010 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2011 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2012 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2013 | wchar_t *wbuf2 = wbuf; |
| 2014 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2015 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2016 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2017 | /* If the buffer is large enough, len does not include the |
| 2018 | terminating \0. If the buffer is too small, len includes |
| 2019 | the space needed for the terminator. */ |
| 2020 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2021 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2022 | if (wbuf2) |
| 2023 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2024 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2025 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2026 | if (!wbuf2) { |
| 2027 | PyErr_NoMemory(); |
| 2028 | return NULL; |
| 2029 | } |
| 2030 | if (!len) { |
| 2031 | if (wbuf2 != wbuf) free(wbuf2); |
| 2032 | return win32_error("getcwdu", NULL); |
| 2033 | } |
| 2034 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2035 | if (wbuf2 != wbuf) free(wbuf2); |
| 2036 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2037 | } |
| 2038 | #endif |
| 2039 | |
| 2040 | Py_BEGIN_ALLOW_THREADS |
| 2041 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2042 | res = _getcwd2(buf, sizeof buf); |
| 2043 | #else |
| 2044 | res = getcwd(buf, sizeof buf); |
| 2045 | #endif |
| 2046 | Py_END_ALLOW_THREADS |
| 2047 | if (res == NULL) |
| 2048 | return posix_error(); |
| 2049 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 2050 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2051 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2052 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2053 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2054 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2055 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2056 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2057 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2058 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2059 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2060 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2061 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2062 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2063 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2064 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2065 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2067 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2068 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2069 | Return a list containing the names of the entries in the directory.\n\ |
| 2070 | \n\ |
| 2071 | path: path of directory to list\n\ |
| 2072 | \n\ |
| 2073 | 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] | 2074 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2075 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2076 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2077 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2078 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2079 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2080 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2081 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2082 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2083 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2084 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2085 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2086 | WIN32_FIND_DATA FileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2087 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2088 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2089 | 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] | 2090 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2091 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2092 | /* If on wide-character-capable OS see if argument |
| 2093 | is Unicode and if so use wide API. */ |
| 2094 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2095 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2096 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2097 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2098 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2099 | Py_UNICODE wch; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2100 | /* Overallocate for \\*.*\0 */ |
| 2101 | len = PyUnicode_GET_SIZE(po); |
| 2102 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2103 | if (!wnamebuf) { |
| 2104 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2105 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2106 | } |
| 2107 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2108 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 2109 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2110 | wnamebuf[len++] = L'\\'; |
| 2111 | wcscpy(wnamebuf + len, L"*.*"); |
| 2112 | if ((d = PyList_New(0)) == NULL) { |
| 2113 | free(wnamebuf); |
| 2114 | return NULL; |
| 2115 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2116 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2117 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2118 | int error = GetLastError(); |
| 2119 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2120 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2121 | return d; |
| 2122 | } |
| 2123 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2124 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2125 | free(wnamebuf); |
| 2126 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2127 | } |
| 2128 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2129 | /* Skip over . and .. */ |
| 2130 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2131 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2132 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2133 | if (v == NULL) { |
| 2134 | Py_DECREF(d); |
| 2135 | d = NULL; |
| 2136 | break; |
| 2137 | } |
| 2138 | if (PyList_Append(d, v) != 0) { |
| 2139 | Py_DECREF(v); |
| 2140 | Py_DECREF(d); |
| 2141 | d = NULL; |
| 2142 | break; |
| 2143 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2144 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2145 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2146 | Py_BEGIN_ALLOW_THREADS |
| 2147 | result = FindNextFileW(hFindFile, &wFileData); |
| 2148 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2149 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2150 | it got to the end of the directory. */ |
| 2151 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2152 | Py_DECREF(d); |
| 2153 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2154 | FindClose(hFindFile); |
| 2155 | free(wnamebuf); |
| 2156 | return NULL; |
| 2157 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2158 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2159 | |
| 2160 | if (FindClose(hFindFile) == FALSE) { |
| 2161 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2162 | win32_error_unicode("FindClose", wnamebuf); |
| 2163 | free(wnamebuf); |
| 2164 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2165 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2166 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2167 | return d; |
| 2168 | } |
| 2169 | /* Drop the argument parsing error as narrow strings |
| 2170 | are also valid. */ |
| 2171 | PyErr_Clear(); |
| 2172 | } |
| 2173 | #endif |
| 2174 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2175 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2176 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2177 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2178 | if (len > 0) { |
| 2179 | char ch = namebuf[len-1]; |
| 2180 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2181 | namebuf[len++] = '/'; |
| 2182 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2183 | strcpy(namebuf + len, "*.*"); |
| 2184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2185 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2186 | return NULL; |
| 2187 | |
| 2188 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2189 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2190 | int error = GetLastError(); |
| 2191 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2192 | return d; |
| 2193 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2194 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2195 | } |
| 2196 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2197 | /* Skip over . and .. */ |
| 2198 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2199 | strcmp(FileData.cFileName, "..") != 0) { |
| 2200 | v = PyString_FromString(FileData.cFileName); |
| 2201 | if (v == NULL) { |
| 2202 | Py_DECREF(d); |
| 2203 | d = NULL; |
| 2204 | break; |
| 2205 | } |
| 2206 | if (PyList_Append(d, v) != 0) { |
| 2207 | Py_DECREF(v); |
| 2208 | Py_DECREF(d); |
| 2209 | d = NULL; |
| 2210 | break; |
| 2211 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2212 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2213 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2214 | Py_BEGIN_ALLOW_THREADS |
| 2215 | result = FindNextFile(hFindFile, &FileData); |
| 2216 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2217 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2218 | it got to the end of the directory. */ |
| 2219 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2220 | Py_DECREF(d); |
| 2221 | win32_error("FindNextFile", namebuf); |
| 2222 | FindClose(hFindFile); |
| 2223 | return NULL; |
| 2224 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2225 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2226 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2227 | if (FindClose(hFindFile) == FALSE) { |
| 2228 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2229 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2230 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2231 | |
| 2232 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2233 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2234 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2235 | |
| 2236 | #ifndef MAX_PATH |
| 2237 | #define MAX_PATH CCHMAXPATH |
| 2238 | #endif |
| 2239 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2240 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2241 | PyObject *d, *v; |
| 2242 | char namebuf[MAX_PATH+5]; |
| 2243 | HDIR hdir = 1; |
| 2244 | ULONG srchcnt = 1; |
| 2245 | FILEFINDBUF3 ep; |
| 2246 | APIRET rc; |
| 2247 | |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 2248 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 2249 | Py_FileSystemDefaultEncoding, &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2250 | return NULL; |
| 2251 | if (len >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2252 | PyMem_Free(name); |
| 2253 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2254 | return NULL; |
| 2255 | } |
| 2256 | strcpy(namebuf, name); |
| 2257 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2258 | if (*pt == ALTSEP) |
| 2259 | *pt = SEP; |
| 2260 | if (namebuf[len-1] != SEP) |
| 2261 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2262 | strcpy(namebuf + len, "*.*"); |
| 2263 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2264 | if ((d = PyList_New(0)) == NULL) { |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2265 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2266 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2267 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2268 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2269 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2270 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2271 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2272 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2273 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2274 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2275 | |
| 2276 | if (rc != NO_ERROR) { |
| 2277 | errno = ENOENT; |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2278 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2281 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2282 | do { |
| 2283 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2284 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2285 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2286 | |
| 2287 | strcpy(namebuf, ep.achName); |
| 2288 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2289 | /* Leave Case of Name Alone -- In Native Form */ |
| 2290 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2291 | |
| 2292 | v = PyString_FromString(namebuf); |
| 2293 | if (v == NULL) { |
| 2294 | Py_DECREF(d); |
| 2295 | d = NULL; |
| 2296 | break; |
| 2297 | } |
| 2298 | if (PyList_Append(d, v) != 0) { |
| 2299 | Py_DECREF(v); |
| 2300 | Py_DECREF(d); |
| 2301 | d = NULL; |
| 2302 | break; |
| 2303 | } |
| 2304 | Py_DECREF(v); |
| 2305 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2306 | } |
| 2307 | |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2308 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2309 | return d; |
| 2310 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2311 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2312 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2313 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2314 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2315 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2316 | int arg_is_unicode = 1; |
| 2317 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2318 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2319 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2320 | arg_is_unicode = 0; |
| 2321 | PyErr_Clear(); |
| 2322 | } |
| 2323 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2324 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2325 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2326 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2327 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2328 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2329 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2330 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2331 | return NULL; |
| 2332 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2333 | for (;;) { |
| 2334 | Py_BEGIN_ALLOW_THREADS |
| 2335 | ep = readdir(dirp); |
| 2336 | Py_END_ALLOW_THREADS |
| 2337 | if (ep == NULL) |
| 2338 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2339 | if (ep->d_name[0] == '.' && |
| 2340 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2341 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2342 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2343 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2344 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2345 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2346 | d = NULL; |
| 2347 | break; |
| 2348 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2349 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2350 | PyObject *w; |
| 2351 | |
| 2352 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2353 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2354 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2355 | if (w != NULL) { |
| 2356 | Py_DECREF(v); |
| 2357 | v = w; |
| 2358 | } |
| 2359 | else { |
| 2360 | /* fall back to the original byte string, as |
| 2361 | discussed in patch #683592 */ |
| 2362 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2363 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2364 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2365 | if (PyList_Append(d, v) != 0) { |
| 2366 | Py_DECREF(v); |
| 2367 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2368 | d = NULL; |
| 2369 | break; |
| 2370 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2371 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2372 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2373 | if (errno != 0 && d != NULL) { |
| 2374 | /* readdir() returned NULL and set errno */ |
| 2375 | closedir(dirp); |
| 2376 | Py_DECREF(d); |
| 2377 | return posix_error_with_allocated_filename(name); |
| 2378 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2379 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2380 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2381 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | #endif /* which OS */ |
| 2385 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2386 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2387 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2388 | /* A helper function for abspath on win32 */ |
| 2389 | static PyObject * |
| 2390 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2391 | { |
| 2392 | /* assume encoded strings wont more than double no of chars */ |
| 2393 | char inbuf[MAX_PATH*2]; |
| 2394 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2395 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2396 | char outbuf[MAX_PATH*2]; |
| 2397 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2398 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2399 | if (unicode_file_names()) { |
| 2400 | PyUnicodeObject *po; |
| 2401 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2402 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2403 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2404 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2405 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2406 | woutbuf, &wtemp)) |
| 2407 | return win32_error("GetFullPathName", ""); |
| 2408 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2409 | } |
| 2410 | /* Drop the argument parsing error as narrow strings |
| 2411 | are also valid. */ |
| 2412 | PyErr_Clear(); |
| 2413 | } |
| 2414 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2415 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2416 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2417 | &insize)) |
| 2418 | return NULL; |
| 2419 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2420 | outbuf, &temp)) |
| 2421 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2422 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2423 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2424 | Py_FileSystemDefaultEncoding, NULL); |
| 2425 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2426 | return PyString_FromString(outbuf); |
| 2427 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2428 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2429 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2430 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2431 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2432 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2433 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2434 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2435 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2436 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2437 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2438 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2439 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2440 | |
| 2441 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2442 | if (unicode_file_names()) { |
| 2443 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2444 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2445 | Py_BEGIN_ALLOW_THREADS |
| 2446 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2447 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2448 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2449 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2450 | if (!res) |
| 2451 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2452 | Py_INCREF(Py_None); |
| 2453 | return Py_None; |
| 2454 | } |
| 2455 | /* Drop the argument parsing error as narrow strings |
| 2456 | are also valid. */ |
| 2457 | PyErr_Clear(); |
| 2458 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2459 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2460 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2461 | return NULL; |
| 2462 | Py_BEGIN_ALLOW_THREADS |
| 2463 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2464 | it is a simple dereference. */ |
| 2465 | res = CreateDirectoryA(path, NULL); |
| 2466 | Py_END_ALLOW_THREADS |
| 2467 | if (!res) { |
| 2468 | win32_error("mkdir", path); |
| 2469 | PyMem_Free(path); |
| 2470 | return NULL; |
| 2471 | } |
| 2472 | PyMem_Free(path); |
| 2473 | Py_INCREF(Py_None); |
| 2474 | return Py_None; |
| 2475 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2476 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2477 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2478 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2479 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2480 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2481 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2482 | res = mkdir(path); |
| 2483 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2484 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2485 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2486 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2487 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2488 | return posix_error_with_allocated_filename(path); |
| 2489 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2490 | Py_INCREF(Py_None); |
| 2491 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2492 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2495 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2496 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2497 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2498 | #include <sys/resource.h> |
| 2499 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2500 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2501 | |
| 2502 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2503 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2504 | "nice(inc) -> new_priority\n\n\ |
| 2505 | 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] | 2506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2507 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2508 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2509 | { |
| 2510 | int increment, value; |
| 2511 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2512 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2513 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2514 | |
| 2515 | /* There are two flavours of 'nice': one that returns the new |
| 2516 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2517 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2518 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2519 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2520 | If we are of the nice family that returns the new priority, we |
| 2521 | need to clear errno before the call, and check if errno is filled |
| 2522 | before calling posix_error() on a returnvalue of -1, because the |
| 2523 | -1 may be the actual new priority! */ |
| 2524 | |
| 2525 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2526 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2527 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2528 | if (value == 0) |
| 2529 | value = getpriority(PRIO_PROCESS, 0); |
| 2530 | #endif |
| 2531 | if (value == -1 && errno != 0) |
| 2532 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2533 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2534 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2535 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2536 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2537 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2538 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2539 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2540 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2542 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2543 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2544 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2545 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2546 | PyObject *o1, *o2; |
| 2547 | char *p1, *p2; |
| 2548 | BOOL result; |
| 2549 | if (unicode_file_names()) { |
| 2550 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2551 | convert_to_unicode, &o1, |
| 2552 | convert_to_unicode, &o2)) |
| 2553 | PyErr_Clear(); |
| 2554 | else { |
| 2555 | Py_BEGIN_ALLOW_THREADS |
| 2556 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2557 | PyUnicode_AsUnicode(o2)); |
| 2558 | Py_END_ALLOW_THREADS |
| 2559 | Py_DECREF(o1); |
| 2560 | Py_DECREF(o2); |
| 2561 | if (!result) |
| 2562 | return win32_error("rename", NULL); |
| 2563 | Py_INCREF(Py_None); |
| 2564 | return Py_None; |
| 2565 | } |
| 2566 | } |
| 2567 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2568 | return NULL; |
| 2569 | Py_BEGIN_ALLOW_THREADS |
| 2570 | result = MoveFileA(p1, p2); |
| 2571 | Py_END_ALLOW_THREADS |
| 2572 | if (!result) |
| 2573 | return win32_error("rename", NULL); |
| 2574 | Py_INCREF(Py_None); |
| 2575 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2576 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2577 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2578 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2581 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2582 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2583 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2584 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2585 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2586 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2587 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2588 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2589 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2590 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2591 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2592 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2593 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2597 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2598 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2599 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2600 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2601 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2602 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2603 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2604 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2605 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2606 | #else |
| 2607 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2608 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2611 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2612 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2613 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2614 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2615 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2616 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2617 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2618 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2619 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2620 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2621 | #ifdef MS_WINDOWS |
| 2622 | wchar_t *command; |
| 2623 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2624 | return NULL; |
| 2625 | #else |
| 2626 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2627 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2628 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2629 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2630 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2631 | #ifdef MS_WINDOWS |
| 2632 | sts = _wsystem(command); |
| 2633 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2634 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2635 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2636 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2637 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2638 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2639 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2640 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2641 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2642 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2643 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2644 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2645 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2646 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2647 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2648 | { |
| 2649 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2650 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2651 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2652 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2653 | if (i < 0) |
| 2654 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2655 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2658 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2659 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2660 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2661 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2663 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2664 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2665 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2666 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2667 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2668 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2669 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2670 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2671 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2672 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2673 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2674 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2677 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2678 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2679 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2680 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2681 | Return a tuple identifying the current operating system."); |
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 * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2684 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2685 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2686 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2687 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2688 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2689 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2690 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2691 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2692 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2693 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2694 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2695 | u.sysname, |
| 2696 | u.nodename, |
| 2697 | u.release, |
| 2698 | u.version, |
| 2699 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2700 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2701 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2702 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2703 | static int |
| 2704 | extract_time(PyObject *t, long* sec, long* usec) |
| 2705 | { |
| 2706 | long intval; |
| 2707 | if (PyFloat_Check(t)) { |
| 2708 | double tval = PyFloat_AsDouble(t); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2709 | 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] | 2710 | if (!intobj) |
| 2711 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2712 | intval = PyLong_AsLong(intobj); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2713 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2714 | if (intval == -1 && PyErr_Occurred()) |
| 2715 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2716 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2717 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2718 | if (*usec < 0) |
| 2719 | /* If rounding gave us a negative number, |
| 2720 | truncate. */ |
| 2721 | *usec = 0; |
| 2722 | return 0; |
| 2723 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2724 | intval = PyLong_AsLong(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2725 | if (intval == -1 && PyErr_Occurred()) |
| 2726 | return -1; |
| 2727 | *sec = intval; |
| 2728 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2729 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2730 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2732 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2733 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2734 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2735 | 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] | 2736 | 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] | 2737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2738 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2739 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2740 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2741 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2742 | PyObject *arg; |
| 2743 | PyUnicodeObject *obwpath; |
| 2744 | wchar_t *wpath = NULL; |
| 2745 | char *apath = NULL; |
| 2746 | HANDLE hFile; |
| 2747 | long atimesec, mtimesec, ausec, musec; |
| 2748 | FILETIME atime, mtime; |
| 2749 | PyObject *result = NULL; |
| 2750 | |
| 2751 | if (unicode_file_names()) { |
| 2752 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2753 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2754 | Py_BEGIN_ALLOW_THREADS |
| 2755 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2756 | NULL, OPEN_EXISTING, |
| 2757 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2758 | Py_END_ALLOW_THREADS |
| 2759 | if (hFile == INVALID_HANDLE_VALUE) |
| 2760 | return win32_error_unicode("utime", wpath); |
| 2761 | } else |
| 2762 | /* Drop the argument parsing error as narrow strings |
| 2763 | are also valid. */ |
| 2764 | PyErr_Clear(); |
| 2765 | } |
| 2766 | if (!wpath) { |
| 2767 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2768 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2769 | return NULL; |
| 2770 | Py_BEGIN_ALLOW_THREADS |
| 2771 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2772 | NULL, OPEN_EXISTING, |
| 2773 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2774 | Py_END_ALLOW_THREADS |
| 2775 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2776 | win32_error("utime", apath); |
| 2777 | PyMem_Free(apath); |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | PyMem_Free(apath); |
| 2781 | } |
| 2782 | |
| 2783 | if (arg == Py_None) { |
| 2784 | SYSTEMTIME now; |
| 2785 | GetSystemTime(&now); |
| 2786 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2787 | !SystemTimeToFileTime(&now, &atime)) { |
| 2788 | win32_error("utime", NULL); |
| 2789 | goto done; |
| 2790 | } |
| 2791 | } |
| 2792 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2793 | PyErr_SetString(PyExc_TypeError, |
| 2794 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2795 | goto done; |
| 2796 | } |
| 2797 | else { |
| 2798 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2799 | &atimesec, &ausec) == -1) |
| 2800 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2801 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2802 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2803 | &mtimesec, &musec) == -1) |
| 2804 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2805 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2806 | } |
| 2807 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2808 | /* Avoid putting the file name into the error here, |
| 2809 | as that may confuse the user into believing that |
| 2810 | something is wrong with the file, when it also |
| 2811 | could be the time stamp that gives a problem. */ |
| 2812 | win32_error("utime", NULL); |
| 2813 | } |
| 2814 | Py_INCREF(Py_None); |
| 2815 | result = Py_None; |
| 2816 | done: |
| 2817 | CloseHandle(hFile); |
| 2818 | return result; |
| 2819 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2820 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2821 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2822 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2823 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2824 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2825 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2826 | #if defined(HAVE_UTIMES) |
| 2827 | struct timeval buf[2]; |
| 2828 | #define ATIME buf[0].tv_sec |
| 2829 | #define MTIME buf[1].tv_sec |
| 2830 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2831 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2832 | struct utimbuf buf; |
| 2833 | #define ATIME buf.actime |
| 2834 | #define MTIME buf.modtime |
| 2835 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2836 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2837 | time_t buf[2]; |
| 2838 | #define ATIME buf[0] |
| 2839 | #define MTIME buf[1] |
| 2840 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2841 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2842 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2843 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2844 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2845 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2846 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2847 | if (arg == Py_None) { |
| 2848 | /* optional time values not given */ |
| 2849 | Py_BEGIN_ALLOW_THREADS |
| 2850 | res = utime(path, NULL); |
| 2851 | Py_END_ALLOW_THREADS |
| 2852 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2853 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2854 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2855 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2856 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2857 | return NULL; |
| 2858 | } |
| 2859 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2860 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2861 | &atime, &ausec) == -1) { |
| 2862 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2863 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2864 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2865 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2866 | &mtime, &musec) == -1) { |
| 2867 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2868 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2869 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2870 | ATIME = atime; |
| 2871 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2872 | #ifdef HAVE_UTIMES |
| 2873 | buf[0].tv_usec = ausec; |
| 2874 | buf[1].tv_usec = musec; |
| 2875 | Py_BEGIN_ALLOW_THREADS |
| 2876 | res = utimes(path, buf); |
| 2877 | Py_END_ALLOW_THREADS |
| 2878 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2879 | Py_BEGIN_ALLOW_THREADS |
| 2880 | res = utime(path, UTIME_ARG); |
| 2881 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2882 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2883 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2884 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2885 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2886 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2887 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2888 | Py_INCREF(Py_None); |
| 2889 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2890 | #undef UTIME_ARG |
| 2891 | #undef ATIME |
| 2892 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2893 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2896 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2897 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2898 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2899 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2900 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2901 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2902 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2903 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2904 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2905 | { |
| 2906 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2907 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2908 | return NULL; |
| 2909 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2910 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2913 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2914 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2915 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2916 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2917 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2918 | for (i = 0; i < count; i++) |
| 2919 | PyMem_Free(array[i]); |
| 2920 | PyMem_DEL(array); |
| 2921 | } |
| 2922 | #endif |
| 2923 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2924 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2925 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2926 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2927 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2928 | Execute an executable path with arguments, replacing current process.\n\ |
| 2929 | \n\ |
| 2930 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2931 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2934 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2935 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2936 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2937 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2938 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2939 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2940 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2941 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2942 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2943 | argv is a list or tuple of strings. */ |
| 2944 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2945 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2946 | Py_FileSystemDefaultEncoding, |
| 2947 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2948 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2949 | if (PyList_Check(argv)) { |
| 2950 | argc = PyList_Size(argv); |
| 2951 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2952 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2953 | else if (PyTuple_Check(argv)) { |
| 2954 | argc = PyTuple_Size(argv); |
| 2955 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2956 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2957 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2958 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2959 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2960 | return NULL; |
| 2961 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 2962 | if (argc < 1) { |
| 2963 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 2964 | PyMem_Free(path); |
| 2965 | return NULL; |
| 2966 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2967 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2968 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2969 | if (argvlist == NULL) { |
| 2970 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2971 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2972 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2973 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2974 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2975 | Py_FileSystemDefaultEncoding, |
| 2976 | &argvlist[i])) { |
| 2977 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2978 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2979 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2980 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2981 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2982 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2983 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2984 | } |
| 2985 | argvlist[argc] = NULL; |
| 2986 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2987 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2988 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2989 | /* If we get here it's definitely an error */ |
| 2990 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2991 | free_string_array(argvlist, argc); |
| 2992 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2993 | return posix_error(); |
| 2994 | } |
| 2995 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2997 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2998 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2999 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3000 | \n\ |
| 3001 | path: path of executable file\n\ |
| 3002 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3003 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3004 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3005 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3006 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3007 | { |
| 3008 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3009 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3010 | char **argvlist; |
| 3011 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3012 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3013 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3014 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3015 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3016 | |
| 3017 | /* execve has three arguments: (path, argv, env), where |
| 3018 | argv is a list or tuple of strings and env is a dictionary |
| 3019 | like posix.environ. */ |
| 3020 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3021 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 3022 | Py_FileSystemDefaultEncoding, |
| 3023 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3024 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3025 | if (PyList_Check(argv)) { |
| 3026 | argc = PyList_Size(argv); |
| 3027 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3028 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3029 | else if (PyTuple_Check(argv)) { |
| 3030 | argc = PyTuple_Size(argv); |
| 3031 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3032 | } |
| 3033 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3034 | PyErr_SetString(PyExc_TypeError, |
| 3035 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3036 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3037 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3038 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3039 | PyErr_SetString(PyExc_TypeError, |
| 3040 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3041 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3044 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3045 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3046 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3047 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3048 | } |
| 3049 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3050 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3051 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 3052 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3053 | &argvlist[i])) |
| 3054 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3055 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3056 | goto fail_1; |
| 3057 | } |
| 3058 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3059 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3060 | argvlist[argc] = NULL; |
| 3061 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3062 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3063 | if (i < 0) |
| 3064 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3065 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3066 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3067 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3068 | goto fail_1; |
| 3069 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3070 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3071 | keys = PyMapping_Keys(env); |
| 3072 | vals = PyMapping_Values(env); |
| 3073 | if (!keys || !vals) |
| 3074 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3075 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3076 | PyErr_SetString(PyExc_TypeError, |
| 3077 | "execve(): env.keys() or env.values() is not a list"); |
| 3078 | goto fail_2; |
| 3079 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3080 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3081 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3082 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3083 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3084 | |
| 3085 | key = PyList_GetItem(keys, pos); |
| 3086 | val = PyList_GetItem(vals, pos); |
| 3087 | if (!key || !val) |
| 3088 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3089 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3090 | if (!PyArg_Parse( |
| 3091 | key, |
| 3092 | "s;execve() arg 3 contains a non-string key", |
| 3093 | &k) || |
| 3094 | !PyArg_Parse( |
| 3095 | val, |
| 3096 | "s;execve() arg 3 contains a non-string value", |
| 3097 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3098 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3099 | goto fail_2; |
| 3100 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3101 | |
| 3102 | #if defined(PYOS_OS2) |
| 3103 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3104 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3105 | #endif |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3106 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3107 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3108 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3109 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3110 | goto fail_2; |
| 3111 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3112 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3113 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3114 | #if defined(PYOS_OS2) |
| 3115 | } |
| 3116 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3117 | } |
| 3118 | envlist[envc] = 0; |
| 3119 | |
| 3120 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3121 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3122 | /* If we get here it's definitely an error */ |
| 3123 | |
| 3124 | (void) posix_error(); |
| 3125 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3126 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3127 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3128 | PyMem_DEL(envlist[envc]); |
| 3129 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3130 | fail_1: |
| 3131 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3132 | Py_XDECREF(vals); |
| 3133 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3134 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3135 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3136 | return NULL; |
| 3137 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3138 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3139 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3140 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3141 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3142 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3143 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3144 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3145 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3146 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3147 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3148 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3149 | |
| 3150 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3151 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3152 | { |
| 3153 | char *path; |
| 3154 | PyObject *argv; |
| 3155 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3156 | int mode, i; |
| 3157 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3158 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3159 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3160 | |
| 3161 | /* spawnv has three arguments: (mode, path, argv), where |
| 3162 | argv is a list or tuple of strings. */ |
| 3163 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3164 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3165 | Py_FileSystemDefaultEncoding, |
| 3166 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3167 | return NULL; |
| 3168 | if (PyList_Check(argv)) { |
| 3169 | argc = PyList_Size(argv); |
| 3170 | getitem = PyList_GetItem; |
| 3171 | } |
| 3172 | else if (PyTuple_Check(argv)) { |
| 3173 | argc = PyTuple_Size(argv); |
| 3174 | getitem = PyTuple_GetItem; |
| 3175 | } |
| 3176 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3177 | PyErr_SetString(PyExc_TypeError, |
| 3178 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3179 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3180 | return NULL; |
| 3181 | } |
| 3182 | |
| 3183 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3184 | if (argvlist == NULL) { |
| 3185 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3186 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3187 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3188 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3189 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3190 | Py_FileSystemDefaultEncoding, |
| 3191 | &argvlist[i])) { |
| 3192 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3193 | PyErr_SetString( |
| 3194 | PyExc_TypeError, |
| 3195 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3196 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3197 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3198 | } |
| 3199 | } |
| 3200 | argvlist[argc] = NULL; |
| 3201 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3202 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3203 | Py_BEGIN_ALLOW_THREADS |
| 3204 | spawnval = spawnv(mode, path, argvlist); |
| 3205 | Py_END_ALLOW_THREADS |
| 3206 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3207 | if (mode == _OLD_P_OVERLAY) |
| 3208 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3209 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3210 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3211 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3212 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3213 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3214 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3215 | free_string_array(argvlist, argc); |
| 3216 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3217 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3218 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3219 | return posix_error(); |
| 3220 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3221 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3222 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3223 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3224 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3225 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3229 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3230 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3231 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3232 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3233 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3234 | path: path of executable file\n\ |
| 3235 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3236 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3237 | |
| 3238 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3239 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3240 | { |
| 3241 | char *path; |
| 3242 | PyObject *argv, *env; |
| 3243 | char **argvlist; |
| 3244 | char **envlist; |
| 3245 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3246 | int mode, pos, envc; |
| 3247 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3248 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3249 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3250 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3251 | |
| 3252 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3253 | argv is a list or tuple of strings and env is a dictionary |
| 3254 | like posix.environ. */ |
| 3255 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3256 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3257 | Py_FileSystemDefaultEncoding, |
| 3258 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3259 | return NULL; |
| 3260 | if (PyList_Check(argv)) { |
| 3261 | argc = PyList_Size(argv); |
| 3262 | getitem = PyList_GetItem; |
| 3263 | } |
| 3264 | else if (PyTuple_Check(argv)) { |
| 3265 | argc = PyTuple_Size(argv); |
| 3266 | getitem = PyTuple_GetItem; |
| 3267 | } |
| 3268 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3269 | PyErr_SetString(PyExc_TypeError, |
| 3270 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3271 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3272 | } |
| 3273 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3274 | PyErr_SetString(PyExc_TypeError, |
| 3275 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3276 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | argvlist = PyMem_NEW(char *, argc+1); |
| 3280 | if (argvlist == NULL) { |
| 3281 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3282 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3283 | } |
| 3284 | for (i = 0; i < argc; i++) { |
| 3285 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3286 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3287 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3288 | &argvlist[i])) |
| 3289 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3290 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3291 | goto fail_1; |
| 3292 | } |
| 3293 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3294 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3295 | argvlist[argc] = NULL; |
| 3296 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3297 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3298 | if (i < 0) |
| 3299 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3300 | envlist = PyMem_NEW(char *, i + 1); |
| 3301 | if (envlist == NULL) { |
| 3302 | PyErr_NoMemory(); |
| 3303 | goto fail_1; |
| 3304 | } |
| 3305 | envc = 0; |
| 3306 | keys = PyMapping_Keys(env); |
| 3307 | vals = PyMapping_Values(env); |
| 3308 | if (!keys || !vals) |
| 3309 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3310 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3311 | PyErr_SetString(PyExc_TypeError, |
| 3312 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3313 | goto fail_2; |
| 3314 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3315 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3316 | for (pos = 0; pos < i; pos++) { |
| 3317 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3318 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3319 | |
| 3320 | key = PyList_GetItem(keys, pos); |
| 3321 | val = PyList_GetItem(vals, pos); |
| 3322 | if (!key || !val) |
| 3323 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3324 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3325 | if (!PyArg_Parse( |
| 3326 | key, |
| 3327 | "s;spawnve() arg 3 contains a non-string key", |
| 3328 | &k) || |
| 3329 | !PyArg_Parse( |
| 3330 | val, |
| 3331 | "s;spawnve() arg 3 contains a non-string value", |
| 3332 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3333 | { |
| 3334 | goto fail_2; |
| 3335 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3336 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3337 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3338 | if (p == NULL) { |
| 3339 | PyErr_NoMemory(); |
| 3340 | goto fail_2; |
| 3341 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3342 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3343 | envlist[envc++] = p; |
| 3344 | } |
| 3345 | envlist[envc] = 0; |
| 3346 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3347 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3348 | Py_BEGIN_ALLOW_THREADS |
| 3349 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3350 | Py_END_ALLOW_THREADS |
| 3351 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3352 | if (mode == _OLD_P_OVERLAY) |
| 3353 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3354 | |
| 3355 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3356 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3357 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3358 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3359 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3360 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3361 | (void) posix_error(); |
| 3362 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3363 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3364 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3365 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3366 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3367 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3368 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3369 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3370 | while (--envc >= 0) |
| 3371 | PyMem_DEL(envlist[envc]); |
| 3372 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3373 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3374 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3375 | Py_XDECREF(vals); |
| 3376 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3377 | fail_0: |
| 3378 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3379 | return res; |
| 3380 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3381 | |
| 3382 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3383 | #if defined(PYOS_OS2) |
| 3384 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3385 | "spawnvp(mode, file, args)\n\n\ |
| 3386 | Execute the program 'file' in a new process, using the environment\n\ |
| 3387 | search path to find the file.\n\ |
| 3388 | \n\ |
| 3389 | mode: mode of process creation\n\ |
| 3390 | file: executable file name\n\ |
| 3391 | args: tuple or list of strings"); |
| 3392 | |
| 3393 | static PyObject * |
| 3394 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3395 | { |
| 3396 | char *path; |
| 3397 | PyObject *argv; |
| 3398 | char **argvlist; |
| 3399 | int mode, i, argc; |
| 3400 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3401 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3402 | |
| 3403 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3404 | argv is a list or tuple of strings. */ |
| 3405 | |
| 3406 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3407 | Py_FileSystemDefaultEncoding, |
| 3408 | &path, &argv)) |
| 3409 | return NULL; |
| 3410 | if (PyList_Check(argv)) { |
| 3411 | argc = PyList_Size(argv); |
| 3412 | getitem = PyList_GetItem; |
| 3413 | } |
| 3414 | else if (PyTuple_Check(argv)) { |
| 3415 | argc = PyTuple_Size(argv); |
| 3416 | getitem = PyTuple_GetItem; |
| 3417 | } |
| 3418 | else { |
| 3419 | PyErr_SetString(PyExc_TypeError, |
| 3420 | "spawnvp() arg 2 must be a tuple or list"); |
| 3421 | PyMem_Free(path); |
| 3422 | return NULL; |
| 3423 | } |
| 3424 | |
| 3425 | argvlist = PyMem_NEW(char *, argc+1); |
| 3426 | if (argvlist == NULL) { |
| 3427 | PyMem_Free(path); |
| 3428 | return PyErr_NoMemory(); |
| 3429 | } |
| 3430 | for (i = 0; i < argc; i++) { |
| 3431 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3432 | Py_FileSystemDefaultEncoding, |
| 3433 | &argvlist[i])) { |
| 3434 | free_string_array(argvlist, i); |
| 3435 | PyErr_SetString( |
| 3436 | PyExc_TypeError, |
| 3437 | "spawnvp() arg 2 must contain only strings"); |
| 3438 | PyMem_Free(path); |
| 3439 | return NULL; |
| 3440 | } |
| 3441 | } |
| 3442 | argvlist[argc] = NULL; |
| 3443 | |
| 3444 | Py_BEGIN_ALLOW_THREADS |
| 3445 | #if defined(PYCC_GCC) |
| 3446 | spawnval = spawnvp(mode, path, argvlist); |
| 3447 | #else |
| 3448 | spawnval = _spawnvp(mode, path, argvlist); |
| 3449 | #endif |
| 3450 | Py_END_ALLOW_THREADS |
| 3451 | |
| 3452 | free_string_array(argvlist, argc); |
| 3453 | PyMem_Free(path); |
| 3454 | |
| 3455 | if (spawnval == -1) |
| 3456 | return posix_error(); |
| 3457 | else |
| 3458 | return Py_BuildValue("l", (long) spawnval); |
| 3459 | } |
| 3460 | |
| 3461 | |
| 3462 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3463 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3464 | Execute the program 'file' in a new process, using the environment\n\ |
| 3465 | search path to find the file.\n\ |
| 3466 | \n\ |
| 3467 | mode: mode of process creation\n\ |
| 3468 | file: executable file name\n\ |
| 3469 | args: tuple or list of arguments\n\ |
| 3470 | env: dictionary of strings mapping to strings"); |
| 3471 | |
| 3472 | static PyObject * |
| 3473 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3474 | { |
| 3475 | char *path; |
| 3476 | PyObject *argv, *env; |
| 3477 | char **argvlist; |
| 3478 | char **envlist; |
| 3479 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3480 | int mode, i, pos, argc, envc; |
| 3481 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3482 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3483 | int lastarg = 0; |
| 3484 | |
| 3485 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3486 | argv is a list or tuple of strings and env is a dictionary |
| 3487 | like posix.environ. */ |
| 3488 | |
| 3489 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3490 | Py_FileSystemDefaultEncoding, |
| 3491 | &path, &argv, &env)) |
| 3492 | return NULL; |
| 3493 | if (PyList_Check(argv)) { |
| 3494 | argc = PyList_Size(argv); |
| 3495 | getitem = PyList_GetItem; |
| 3496 | } |
| 3497 | else if (PyTuple_Check(argv)) { |
| 3498 | argc = PyTuple_Size(argv); |
| 3499 | getitem = PyTuple_GetItem; |
| 3500 | } |
| 3501 | else { |
| 3502 | PyErr_SetString(PyExc_TypeError, |
| 3503 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3504 | goto fail_0; |
| 3505 | } |
| 3506 | if (!PyMapping_Check(env)) { |
| 3507 | PyErr_SetString(PyExc_TypeError, |
| 3508 | "spawnvpe() arg 3 must be a mapping object"); |
| 3509 | goto fail_0; |
| 3510 | } |
| 3511 | |
| 3512 | argvlist = PyMem_NEW(char *, argc+1); |
| 3513 | if (argvlist == NULL) { |
| 3514 | PyErr_NoMemory(); |
| 3515 | goto fail_0; |
| 3516 | } |
| 3517 | for (i = 0; i < argc; i++) { |
| 3518 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3519 | "et;spawnvpe() arg 2 must contain only strings", |
| 3520 | Py_FileSystemDefaultEncoding, |
| 3521 | &argvlist[i])) |
| 3522 | { |
| 3523 | lastarg = i; |
| 3524 | goto fail_1; |
| 3525 | } |
| 3526 | } |
| 3527 | lastarg = argc; |
| 3528 | argvlist[argc] = NULL; |
| 3529 | |
| 3530 | i = PyMapping_Size(env); |
| 3531 | if (i < 0) |
| 3532 | goto fail_1; |
| 3533 | envlist = PyMem_NEW(char *, i + 1); |
| 3534 | if (envlist == NULL) { |
| 3535 | PyErr_NoMemory(); |
| 3536 | goto fail_1; |
| 3537 | } |
| 3538 | envc = 0; |
| 3539 | keys = PyMapping_Keys(env); |
| 3540 | vals = PyMapping_Values(env); |
| 3541 | if (!keys || !vals) |
| 3542 | goto fail_2; |
| 3543 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3544 | PyErr_SetString(PyExc_TypeError, |
| 3545 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3546 | goto fail_2; |
| 3547 | } |
| 3548 | |
| 3549 | for (pos = 0; pos < i; pos++) { |
| 3550 | char *p, *k, *v; |
| 3551 | size_t len; |
| 3552 | |
| 3553 | key = PyList_GetItem(keys, pos); |
| 3554 | val = PyList_GetItem(vals, pos); |
| 3555 | if (!key || !val) |
| 3556 | goto fail_2; |
| 3557 | |
| 3558 | if (!PyArg_Parse( |
| 3559 | key, |
| 3560 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3561 | &k) || |
| 3562 | !PyArg_Parse( |
| 3563 | val, |
| 3564 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3565 | &v)) |
| 3566 | { |
| 3567 | goto fail_2; |
| 3568 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3569 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3570 | p = PyMem_NEW(char, len); |
| 3571 | if (p == NULL) { |
| 3572 | PyErr_NoMemory(); |
| 3573 | goto fail_2; |
| 3574 | } |
| 3575 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3576 | envlist[envc++] = p; |
| 3577 | } |
| 3578 | envlist[envc] = 0; |
| 3579 | |
| 3580 | Py_BEGIN_ALLOW_THREADS |
| 3581 | #if defined(PYCC_GCC) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3582 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3583 | #else |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3584 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3585 | #endif |
| 3586 | Py_END_ALLOW_THREADS |
| 3587 | |
| 3588 | if (spawnval == -1) |
| 3589 | (void) posix_error(); |
| 3590 | else |
| 3591 | res = Py_BuildValue("l", (long) spawnval); |
| 3592 | |
| 3593 | fail_2: |
| 3594 | while (--envc >= 0) |
| 3595 | PyMem_DEL(envlist[envc]); |
| 3596 | PyMem_DEL(envlist); |
| 3597 | fail_1: |
| 3598 | free_string_array(argvlist, lastarg); |
| 3599 | Py_XDECREF(vals); |
| 3600 | Py_XDECREF(keys); |
| 3601 | fail_0: |
| 3602 | PyMem_Free(path); |
| 3603 | return res; |
| 3604 | } |
| 3605 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3606 | #endif /* HAVE_SPAWNV */ |
| 3607 | |
| 3608 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3609 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3610 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3611 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3612 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3613 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3614 | 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] | 3615 | |
| 3616 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3617 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3618 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3619 | pid_t pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3620 | if (pid == -1) |
| 3621 | return posix_error(); |
| 3622 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3623 | return PyLong_FromLong(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3624 | } |
| 3625 | #endif |
| 3626 | |
| 3627 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3628 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3629 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3630 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3631 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3632 | 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] | 3633 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3634 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3635 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3636 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3637 | pid_t pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3638 | if (pid == -1) |
| 3639 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3640 | if (pid == 0) |
| 3641 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3642 | return PyLong_FromLong(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3643 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3644 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3645 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3646 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3647 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3648 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3649 | #define DEV_PTY_FILE "/dev/ptc" |
| 3650 | #define HAVE_DEV_PTMX |
| 3651 | #else |
| 3652 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3653 | #endif |
| 3654 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3655 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3656 | #ifdef HAVE_PTY_H |
| 3657 | #include <pty.h> |
| 3658 | #else |
| 3659 | #ifdef HAVE_LIBUTIL_H |
| 3660 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3661 | #endif /* HAVE_LIBUTIL_H */ |
| 3662 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3663 | #ifdef HAVE_STROPTS_H |
| 3664 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3665 | #endif |
| 3666 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3667 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3668 | #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] | 3669 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3670 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3671 | 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] | 3672 | |
| 3673 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3674 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3675 | { |
| 3676 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3677 | #ifndef HAVE_OPENPTY |
| 3678 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3679 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3680 | #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] | 3681 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3682 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3683 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3684 | #endif |
| 3685 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3686 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3687 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3688 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3689 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3690 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3691 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3692 | if (slave_name == NULL) |
| 3693 | return posix_error(); |
| 3694 | |
| 3695 | slave_fd = open(slave_name, O_RDWR); |
| 3696 | if (slave_fd < 0) |
| 3697 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3698 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3699 | 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] | 3700 | if (master_fd < 0) |
| 3701 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3702 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3703 | /* change permission of slave */ |
| 3704 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3705 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3706 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3707 | } |
| 3708 | /* unlock slave */ |
| 3709 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3710 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3711 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3712 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3713 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3714 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3715 | if (slave_name == NULL) |
| 3716 | return posix_error(); |
| 3717 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3718 | if (slave_fd < 0) |
| 3719 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3720 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3721 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3722 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3723 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3724 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3725 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3726 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3727 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3728 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3729 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3730 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3731 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3732 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3733 | |
| 3734 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3735 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3736 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3737 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3738 | 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] | 3739 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3740 | |
| 3741 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3742 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3743 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3744 | int master_fd = -1; |
| 3745 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3746 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3747 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3748 | if (pid == -1) |
| 3749 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3750 | if (pid == 0) |
| 3751 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3752 | return Py_BuildValue("(li)", pid, master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3753 | } |
| 3754 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3755 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3756 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3757 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3758 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3759 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3760 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3761 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3762 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3763 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3764 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3765 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3766 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3767 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3768 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3769 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3770 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3771 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3772 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3773 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3774 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3775 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3776 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3777 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3778 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3779 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3780 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3781 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3782 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3783 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3784 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3785 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3786 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3787 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3788 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3789 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3790 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3791 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3792 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3793 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3794 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3795 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3796 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3797 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3798 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3799 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3800 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3801 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3802 | return PyLong_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3805 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3806 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3807 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3808 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3809 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3810 | |
| 3811 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3812 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3813 | { |
| 3814 | PyObject *result = NULL; |
| 3815 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3816 | #ifdef NGROUPS_MAX |
| 3817 | #define MAX_GROUPS NGROUPS_MAX |
| 3818 | #else |
| 3819 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3820 | #define MAX_GROUPS 64 |
| 3821 | #endif |
| 3822 | gid_t grouplist[MAX_GROUPS]; |
| 3823 | int n; |
| 3824 | |
| 3825 | n = getgroups(MAX_GROUPS, grouplist); |
| 3826 | if (n < 0) |
| 3827 | posix_error(); |
| 3828 | else { |
| 3829 | result = PyList_New(n); |
| 3830 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3831 | int i; |
| 3832 | for (i = 0; i < n; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3833 | PyObject *o = PyLong_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3834 | if (o == NULL) { |
| 3835 | Py_DECREF(result); |
| 3836 | result = NULL; |
| 3837 | break; |
| 3838 | } |
| 3839 | PyList_SET_ITEM(result, i, o); |
| 3840 | } |
| 3841 | } |
| 3842 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3843 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3844 | return result; |
| 3845 | } |
| 3846 | #endif |
| 3847 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3848 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3849 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3850 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3851 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3852 | |
| 3853 | static PyObject * |
| 3854 | posix_getpgid(PyObject *self, PyObject *args) |
| 3855 | { |
| 3856 | int pid, pgid; |
| 3857 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3858 | return NULL; |
| 3859 | pgid = getpgid(pid); |
| 3860 | if (pgid < 0) |
| 3861 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3862 | return PyLong_FromLong((long)pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3863 | } |
| 3864 | #endif /* HAVE_GETPGID */ |
| 3865 | |
| 3866 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3867 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3868 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3869 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3870 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3871 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3872 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3873 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3874 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3875 | #ifdef GETPGRP_HAVE_ARG |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3876 | return PyLong_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3877 | #else /* GETPGRP_HAVE_ARG */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3878 | return PyLong_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3879 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3880 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3881 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3882 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3883 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3884 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3885 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3886 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3887 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3888 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3889 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3890 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3891 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3892 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3893 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3894 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3895 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3896 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3897 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3898 | Py_INCREF(Py_None); |
| 3899 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3902 | #endif /* HAVE_SETPGRP */ |
| 3903 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3904 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3905 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3906 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3907 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3908 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3909 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3910 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3911 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3912 | return PyLong_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3913 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3914 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3915 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3916 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3917 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3918 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3919 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3920 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3921 | |
| 3922 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3923 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3924 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3925 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3926 | char *name; |
| 3927 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3928 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3929 | errno = 0; |
| 3930 | name = getlogin(); |
| 3931 | if (name == NULL) { |
| 3932 | if (errno) |
| 3933 | posix_error(); |
| 3934 | else |
| 3935 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3936 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3937 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3938 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 3939 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3940 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3941 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3942 | return result; |
| 3943 | } |
| 3944 | #endif |
| 3945 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3946 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3947 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3948 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3949 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3950 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3951 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3952 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3953 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3954 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3955 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3956 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3957 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3958 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3959 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3960 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3961 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3962 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3963 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3964 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3965 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3966 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3967 | pid_t pid; |
| 3968 | int sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3969 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3970 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3971 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3972 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3973 | APIRET rc; |
| 3974 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3975 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3976 | |
| 3977 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3978 | APIRET rc; |
| 3979 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3980 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3981 | |
| 3982 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3983 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3984 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3985 | if (kill(pid, sig) == -1) |
| 3986 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3987 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3988 | Py_INCREF(Py_None); |
| 3989 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3990 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3991 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3992 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3993 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3994 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3995 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3996 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3997 | |
| 3998 | static PyObject * |
| 3999 | posix_killpg(PyObject *self, PyObject *args) |
| 4000 | { |
| 4001 | int pgid, sig; |
| 4002 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 4003 | return NULL; |
| 4004 | if (killpg(pgid, sig) == -1) |
| 4005 | return posix_error(); |
| 4006 | Py_INCREF(Py_None); |
| 4007 | return Py_None; |
| 4008 | } |
| 4009 | #endif |
| 4010 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4011 | #ifdef HAVE_PLOCK |
| 4012 | |
| 4013 | #ifdef HAVE_SYS_LOCK_H |
| 4014 | #include <sys/lock.h> |
| 4015 | #endif |
| 4016 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4017 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4018 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4019 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4020 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4021 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4022 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4023 | { |
| 4024 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4025 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4026 | return NULL; |
| 4027 | if (plock(op) == -1) |
| 4028 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4029 | Py_INCREF(Py_None); |
| 4030 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4031 | } |
| 4032 | #endif |
| 4033 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4034 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4035 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4036 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4037 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4038 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4039 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4040 | Set the current process's user id."); |
| 4041 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4042 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4043 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4044 | { |
| 4045 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4046 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4047 | return NULL; |
| 4048 | if (setuid(uid) < 0) |
| 4049 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4050 | Py_INCREF(Py_None); |
| 4051 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4052 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4053 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4054 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4055 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4056 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4057 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4058 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4059 | Set the current process's effective user id."); |
| 4060 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4061 | static PyObject * |
| 4062 | posix_seteuid (PyObject *self, PyObject *args) |
| 4063 | { |
| 4064 | int euid; |
| 4065 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4066 | return NULL; |
| 4067 | } else if (seteuid(euid) < 0) { |
| 4068 | return posix_error(); |
| 4069 | } else { |
| 4070 | Py_INCREF(Py_None); |
| 4071 | return Py_None; |
| 4072 | } |
| 4073 | } |
| 4074 | #endif /* HAVE_SETEUID */ |
| 4075 | |
| 4076 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4077 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4078 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4079 | Set the current process's effective group id."); |
| 4080 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4081 | static PyObject * |
| 4082 | posix_setegid (PyObject *self, PyObject *args) |
| 4083 | { |
| 4084 | int egid; |
| 4085 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4086 | return NULL; |
| 4087 | } else if (setegid(egid) < 0) { |
| 4088 | return posix_error(); |
| 4089 | } else { |
| 4090 | Py_INCREF(Py_None); |
| 4091 | return Py_None; |
| 4092 | } |
| 4093 | } |
| 4094 | #endif /* HAVE_SETEGID */ |
| 4095 | |
| 4096 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4097 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4098 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4099 | Set the current process's real and effective user ids."); |
| 4100 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4101 | static PyObject * |
| 4102 | posix_setreuid (PyObject *self, PyObject *args) |
| 4103 | { |
| 4104 | int ruid, euid; |
| 4105 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4106 | return NULL; |
| 4107 | } else if (setreuid(ruid, euid) < 0) { |
| 4108 | return posix_error(); |
| 4109 | } else { |
| 4110 | Py_INCREF(Py_None); |
| 4111 | return Py_None; |
| 4112 | } |
| 4113 | } |
| 4114 | #endif /* HAVE_SETREUID */ |
| 4115 | |
| 4116 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4117 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4118 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4119 | Set the current process's real and effective group ids."); |
| 4120 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4121 | static PyObject * |
| 4122 | posix_setregid (PyObject *self, PyObject *args) |
| 4123 | { |
| 4124 | int rgid, egid; |
| 4125 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4126 | return NULL; |
| 4127 | } else if (setregid(rgid, egid) < 0) { |
| 4128 | return posix_error(); |
| 4129 | } else { |
| 4130 | Py_INCREF(Py_None); |
| 4131 | return Py_None; |
| 4132 | } |
| 4133 | } |
| 4134 | #endif /* HAVE_SETREGID */ |
| 4135 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4136 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4137 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4138 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4139 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4140 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4141 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4142 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4143 | { |
| 4144 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4145 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4146 | return NULL; |
| 4147 | if (setgid(gid) < 0) |
| 4148 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4149 | Py_INCREF(Py_None); |
| 4150 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4151 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4152 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4153 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4154 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4155 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4156 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4157 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4158 | |
| 4159 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4160 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4161 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4162 | int i, len; |
| 4163 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4164 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4165 | if (!PySequence_Check(groups)) { |
| 4166 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4167 | return NULL; |
| 4168 | } |
| 4169 | len = PySequence_Size(groups); |
| 4170 | if (len > MAX_GROUPS) { |
| 4171 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4172 | return NULL; |
| 4173 | } |
| 4174 | for(i = 0; i < len; i++) { |
| 4175 | PyObject *elem; |
| 4176 | elem = PySequence_GetItem(groups, i); |
| 4177 | if (!elem) |
| 4178 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4179 | if (!PyLong_Check(elem)) { |
| 4180 | PyErr_SetString(PyExc_TypeError, |
| 4181 | "groups must be integers"); |
| 4182 | Py_DECREF(elem); |
| 4183 | return NULL; |
| 4184 | } else { |
| 4185 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4186 | if (PyErr_Occurred()) { |
| 4187 | PyErr_SetString(PyExc_TypeError, |
| 4188 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4189 | Py_DECREF(elem); |
| 4190 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4191 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4192 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4193 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4194 | if (grouplist[i] != x) { |
| 4195 | PyErr_SetString(PyExc_TypeError, |
| 4196 | "group id too big"); |
| 4197 | Py_DECREF(elem); |
| 4198 | return NULL; |
| 4199 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4200 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4201 | Py_DECREF(elem); |
| 4202 | } |
| 4203 | |
| 4204 | if (setgroups(len, grouplist) < 0) |
| 4205 | return posix_error(); |
| 4206 | Py_INCREF(Py_None); |
| 4207 | return Py_None; |
| 4208 | } |
| 4209 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4210 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4211 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4212 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4213 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4214 | { |
| 4215 | PyObject *result; |
| 4216 | static PyObject *struct_rusage; |
| 4217 | |
| 4218 | if (pid == -1) |
| 4219 | return posix_error(); |
| 4220 | |
| 4221 | if (struct_rusage == NULL) { |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4222 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4223 | if (m == NULL) |
| 4224 | return NULL; |
| 4225 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4226 | Py_DECREF(m); |
| 4227 | if (struct_rusage == NULL) |
| 4228 | return NULL; |
| 4229 | } |
| 4230 | |
| 4231 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4232 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4233 | if (!result) |
| 4234 | return NULL; |
| 4235 | |
| 4236 | #ifndef doubletime |
| 4237 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4238 | #endif |
| 4239 | |
| 4240 | PyStructSequence_SET_ITEM(result, 0, |
| 4241 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4242 | PyStructSequence_SET_ITEM(result, 1, |
| 4243 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4244 | #define SET_INT(result, index, value)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4245 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4246 | SET_INT(result, 2, ru->ru_maxrss); |
| 4247 | SET_INT(result, 3, ru->ru_ixrss); |
| 4248 | SET_INT(result, 4, ru->ru_idrss); |
| 4249 | SET_INT(result, 5, ru->ru_isrss); |
| 4250 | SET_INT(result, 6, ru->ru_minflt); |
| 4251 | SET_INT(result, 7, ru->ru_majflt); |
| 4252 | SET_INT(result, 8, ru->ru_nswap); |
| 4253 | SET_INT(result, 9, ru->ru_inblock); |
| 4254 | SET_INT(result, 10, ru->ru_oublock); |
| 4255 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4256 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4257 | SET_INT(result, 13, ru->ru_nsignals); |
| 4258 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4259 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4260 | #undef SET_INT |
| 4261 | |
| 4262 | if (PyErr_Occurred()) { |
| 4263 | Py_DECREF(result); |
| 4264 | return NULL; |
| 4265 | } |
| 4266 | |
| 4267 | return Py_BuildValue("iiN", pid, status, result); |
| 4268 | } |
| 4269 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4270 | |
| 4271 | #ifdef HAVE_WAIT3 |
| 4272 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4273 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4274 | Wait for completion of a child process."); |
| 4275 | |
| 4276 | static PyObject * |
| 4277 | posix_wait3(PyObject *self, PyObject *args) |
| 4278 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4279 | pid_t pid; |
| 4280 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4281 | struct rusage ru; |
| 4282 | WAIT_TYPE status; |
| 4283 | WAIT_STATUS_INT(status) = 0; |
| 4284 | |
| 4285 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4286 | return NULL; |
| 4287 | |
| 4288 | Py_BEGIN_ALLOW_THREADS |
| 4289 | pid = wait3(&status, options, &ru); |
| 4290 | Py_END_ALLOW_THREADS |
| 4291 | |
| 4292 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4293 | } |
| 4294 | #endif /* HAVE_WAIT3 */ |
| 4295 | |
| 4296 | #ifdef HAVE_WAIT4 |
| 4297 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4298 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4299 | Wait for completion of a given child process."); |
| 4300 | |
| 4301 | static PyObject * |
| 4302 | posix_wait4(PyObject *self, PyObject *args) |
| 4303 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4304 | pid_t pid; |
| 4305 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4306 | struct rusage ru; |
| 4307 | WAIT_TYPE status; |
| 4308 | WAIT_STATUS_INT(status) = 0; |
| 4309 | |
| 4310 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4311 | return NULL; |
| 4312 | |
| 4313 | Py_BEGIN_ALLOW_THREADS |
| 4314 | pid = wait4(pid, &status, options, &ru); |
| 4315 | Py_END_ALLOW_THREADS |
| 4316 | |
| 4317 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4318 | } |
| 4319 | #endif /* HAVE_WAIT4 */ |
| 4320 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4321 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4322 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4323 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4324 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4325 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4326 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4327 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4328 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4329 | pid_t pid; |
| 4330 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4331 | WAIT_TYPE status; |
| 4332 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4333 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4334 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4335 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4336 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4337 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4338 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4339 | if (pid == -1) |
| 4340 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4341 | |
| 4342 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4345 | #elif defined(HAVE_CWAIT) |
| 4346 | |
| 4347 | /* 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] | 4348 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4349 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4350 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4351 | |
| 4352 | static PyObject * |
| 4353 | posix_waitpid(PyObject *self, PyObject *args) |
| 4354 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4355 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4356 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4357 | |
| 4358 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4359 | return NULL; |
| 4360 | Py_BEGIN_ALLOW_THREADS |
| 4361 | pid = _cwait(&status, pid, options); |
| 4362 | Py_END_ALLOW_THREADS |
| 4363 | if (pid == -1) |
| 4364 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4365 | |
| 4366 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4367 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4368 | } |
| 4369 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4370 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4371 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4372 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4373 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4374 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4375 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4376 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4377 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4378 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4379 | pid_t pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4380 | WAIT_TYPE status; |
| 4381 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4382 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4383 | Py_BEGIN_ALLOW_THREADS |
| 4384 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4385 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4386 | if (pid == -1) |
| 4387 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4388 | |
| 4389 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4390 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4391 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4392 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4393 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4394 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4395 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4396 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4397 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4398 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4399 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4400 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4401 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4402 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4403 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4404 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4405 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4406 | #else |
| 4407 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4408 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4409 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4412 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4413 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4414 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4415 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4416 | 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] | 4417 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4418 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4419 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4420 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4421 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4422 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4423 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4424 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4425 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4426 | |
| 4427 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 4428 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4429 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4430 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4431 | if (v == NULL) { |
| 4432 | PyMem_Free(path); |
| 4433 | return NULL; |
| 4434 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4435 | |
| 4436 | if (PyUnicode_Check(v)) { |
| 4437 | arg_is_unicode = 1; |
| 4438 | } |
| 4439 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4440 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4441 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4442 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4443 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4444 | if (n < 0) |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4445 | return posix_error_with_allocated_filename(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4446 | |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4447 | PyMem_Free(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4448 | v = PyString_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4449 | if (arg_is_unicode) { |
| 4450 | PyObject *w; |
| 4451 | |
| 4452 | w = PyUnicode_FromEncodedObject(v, |
| 4453 | Py_FileSystemDefaultEncoding, |
| 4454 | "strict"); |
| 4455 | if (w != NULL) { |
| 4456 | Py_DECREF(v); |
| 4457 | v = w; |
| 4458 | } |
| 4459 | else { |
| 4460 | /* fall back to the original byte string, as |
| 4461 | discussed in patch #683592 */ |
| 4462 | PyErr_Clear(); |
| 4463 | } |
| 4464 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4465 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4466 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4467 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4468 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4469 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4470 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4471 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4472 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4473 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4474 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4475 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4476 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4477 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4478 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4479 | } |
| 4480 | #endif /* HAVE_SYMLINK */ |
| 4481 | |
| 4482 | |
| 4483 | #ifdef HAVE_TIMES |
| 4484 | #ifndef HZ |
| 4485 | #define HZ 60 /* Universal constant :-) */ |
| 4486 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4487 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4488 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4489 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4490 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4491 | { |
| 4492 | ULONG value = 0; |
| 4493 | |
| 4494 | Py_BEGIN_ALLOW_THREADS |
| 4495 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4496 | Py_END_ALLOW_THREADS |
| 4497 | |
| 4498 | return value; |
| 4499 | } |
| 4500 | |
| 4501 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4502 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4503 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4504 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4505 | return Py_BuildValue("ddddd", |
| 4506 | (double)0 /* t.tms_utime / HZ */, |
| 4507 | (double)0 /* t.tms_stime / HZ */, |
| 4508 | (double)0 /* t.tms_cutime / HZ */, |
| 4509 | (double)0 /* t.tms_cstime / HZ */, |
| 4510 | (double)system_uptime() / 1000); |
| 4511 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4512 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4513 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4514 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4515 | { |
| 4516 | struct tms t; |
| 4517 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4518 | errno = 0; |
| 4519 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4520 | if (c == (clock_t) -1) |
| 4521 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4522 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4523 | (double)t.tms_utime / HZ, |
| 4524 | (double)t.tms_stime / HZ, |
| 4525 | (double)t.tms_cutime / HZ, |
| 4526 | (double)t.tms_cstime / HZ, |
| 4527 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4528 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4529 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4530 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4531 | |
| 4532 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4533 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4534 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4535 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4536 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4537 | { |
| 4538 | FILETIME create, exit, kernel, user; |
| 4539 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4540 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4541 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4542 | /* The fields of a FILETIME structure are the hi and lo part |
| 4543 | of a 64-bit value expressed in 100 nanosecond units. |
| 4544 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4545 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4546 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4547 | return Py_BuildValue( |
| 4548 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4549 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4550 | kernel.dwLowDateTime*1e-7), |
| 4551 | (double)(user.dwHighDateTime*429.4967296 + |
| 4552 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4553 | (double)0, |
| 4554 | (double)0, |
| 4555 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4556 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4557 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4558 | |
| 4559 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4560 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4561 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4562 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4563 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4564 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4565 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4566 | #ifdef HAVE_GETSID |
| 4567 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4568 | "getsid(pid) -> sid\n\n\ |
| 4569 | Call the system call getsid()."); |
| 4570 | |
| 4571 | static PyObject * |
| 4572 | posix_getsid(PyObject *self, PyObject *args) |
| 4573 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4574 | pid_t pid; |
| 4575 | int sid; |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4576 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4577 | return NULL; |
| 4578 | sid = getsid(pid); |
| 4579 | if (sid < 0) |
| 4580 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4581 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4582 | } |
| 4583 | #endif /* HAVE_GETSID */ |
| 4584 | |
| 4585 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4586 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4587 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4588 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4589 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4590 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4591 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4592 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4593 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4594 | if (setsid() < 0) |
| 4595 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4596 | Py_INCREF(Py_None); |
| 4597 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4598 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4599 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4600 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4601 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4602 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4603 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4604 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4606 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4607 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4608 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4609 | pid_t pid; |
| 4610 | int pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4611 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4612 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4613 | if (setpgid(pid, pgrp) < 0) |
| 4614 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4615 | Py_INCREF(Py_None); |
| 4616 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4617 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4618 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4619 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4620 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4621 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4622 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4623 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4624 | 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] | 4625 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4626 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4627 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4628 | { |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4629 | int fd; |
| 4630 | pid_t pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4631 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4632 | return NULL; |
| 4633 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4634 | if (pgid < 0) |
| 4635 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4636 | return PyLong_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4637 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4638 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4639 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4640 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4641 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4642 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4643 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4644 | 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] | 4645 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4646 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4647 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4648 | { |
| 4649 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4650 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4651 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4652 | if (tcsetpgrp(fd, pgid) < 0) |
| 4653 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4654 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4655 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4656 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4657 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4658 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4659 | /* Functions acting on file descriptors */ |
| 4660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4661 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4662 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4663 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4664 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4665 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4666 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4667 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4668 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4669 | int flag; |
| 4670 | int mode = 0777; |
| 4671 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4672 | |
| 4673 | #ifdef MS_WINDOWS |
| 4674 | if (unicode_file_names()) { |
| 4675 | PyUnicodeObject *po; |
| 4676 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4677 | Py_BEGIN_ALLOW_THREADS |
| 4678 | /* PyUnicode_AS_UNICODE OK without thread |
| 4679 | lock as it is a simple dereference. */ |
| 4680 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4681 | Py_END_ALLOW_THREADS |
| 4682 | if (fd < 0) |
| 4683 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4684 | return PyLong_FromLong((long)fd); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4685 | } |
| 4686 | /* Drop the argument parsing error as narrow strings |
| 4687 | are also valid. */ |
| 4688 | PyErr_Clear(); |
| 4689 | } |
| 4690 | #endif |
| 4691 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4692 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4693 | Py_FileSystemDefaultEncoding, &file, |
| 4694 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4695 | return NULL; |
| 4696 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4697 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4698 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4699 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4700 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4701 | return posix_error_with_allocated_filename(file); |
| 4702 | PyMem_Free(file); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4703 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4707 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4708 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4709 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4710 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4711 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4712 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4713 | { |
| 4714 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4715 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4716 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4717 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4718 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4719 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4720 | if (res < 0) |
| 4721 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4722 | Py_INCREF(Py_None); |
| 4723 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4726 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4727 | PyDoc_STRVAR(posix_closerange__doc__, |
| 4728 | "closerange(fd_low, fd_high)\n\n\ |
| 4729 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 4730 | |
| 4731 | static PyObject * |
| 4732 | posix_closerange(PyObject *self, PyObject *args) |
| 4733 | { |
| 4734 | int fd_from, fd_to, i; |
| 4735 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 4736 | return NULL; |
| 4737 | Py_BEGIN_ALLOW_THREADS |
| 4738 | for (i = fd_from; i < fd_to; i++) |
| 4739 | close(i); |
| 4740 | Py_END_ALLOW_THREADS |
| 4741 | Py_RETURN_NONE; |
| 4742 | } |
| 4743 | |
| 4744 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4745 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4746 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4747 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4748 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4749 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4750 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4751 | { |
| 4752 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4753 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4754 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4755 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4756 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4757 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4758 | if (fd < 0) |
| 4759 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4760 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4763 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4764 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4765 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4766 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4767 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4768 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4769 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4770 | { |
| 4771 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4772 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4773 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4774 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4775 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4776 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4777 | if (res < 0) |
| 4778 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4779 | Py_INCREF(Py_None); |
| 4780 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4784 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4785 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4786 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4787 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4788 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4789 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4790 | { |
| 4791 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4792 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4793 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4794 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4795 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4796 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4797 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4798 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4799 | return NULL; |
| 4800 | #ifdef SEEK_SET |
| 4801 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4802 | switch (how) { |
| 4803 | case 0: how = SEEK_SET; break; |
| 4804 | case 1: how = SEEK_CUR; break; |
| 4805 | case 2: how = SEEK_END; break; |
| 4806 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4807 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4808 | |
| 4809 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4810 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4811 | #else |
| 4812 | pos = PyLong_Check(posobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4813 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4814 | #endif |
| 4815 | if (PyErr_Occurred()) |
| 4816 | return NULL; |
| 4817 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4818 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4819 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4820 | res = _lseeki64(fd, pos, how); |
| 4821 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4822 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4823 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4824 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4825 | if (res < 0) |
| 4826 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4827 | |
| 4828 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4829 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4830 | #else |
| 4831 | return PyLong_FromLongLong(res); |
| 4832 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4836 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4837 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4838 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4839 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4840 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4841 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4842 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4843 | int fd, size; |
| 4844 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4845 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4846 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4847 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 4848 | if (size < 0) { |
| 4849 | errno = EINVAL; |
| 4850 | return posix_error(); |
| 4851 | } |
Guido van Rossum | f9e443c | 2007-11-21 20:17:11 +0000 | [diff] [blame] | 4852 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4853 | if (buffer == NULL) |
| 4854 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4855 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | f9e443c | 2007-11-21 20:17:11 +0000 | [diff] [blame] | 4856 | n = read(fd, PyString_AS_STRING(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4857 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4858 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4859 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4860 | return posix_error(); |
| 4861 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4862 | if (n != size) |
Guido van Rossum | f9e443c | 2007-11-21 20:17:11 +0000 | [diff] [blame] | 4863 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4864 | return buffer; |
| 4865 | } |
| 4866 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4868 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4869 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4870 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4871 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4872 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4873 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4874 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4875 | int fd; |
| 4876 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4877 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4878 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4879 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4880 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4881 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4882 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4883 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4884 | if (size < 0) |
| 4885 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4886 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4890 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4891 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4892 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4893 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4894 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4895 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4896 | { |
| 4897 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4898 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4899 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4900 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4901 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 4902 | #ifdef __VMS |
| 4903 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 4904 | fsync(fd); |
| 4905 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4906 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4907 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4908 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4909 | if (res != 0) { |
| 4910 | #ifdef MS_WINDOWS |
| 4911 | return win32_error("fstat", NULL); |
| 4912 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4913 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4914 | #endif |
| 4915 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4916 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4917 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4920 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4921 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4922 | 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] | 4923 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4924 | |
| 4925 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4926 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4927 | { |
| 4928 | int fd; |
| 4929 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4930 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4931 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4932 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4933 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4934 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4935 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4936 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4937 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4938 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4939 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4940 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4941 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4942 | #if defined(PYOS_OS2) |
| 4943 | HFILE read, write; |
| 4944 | APIRET rc; |
| 4945 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4946 | Py_BEGIN_ALLOW_THREADS |
| 4947 | rc = DosCreatePipe( &read, &write, 4096); |
| 4948 | Py_END_ALLOW_THREADS |
| 4949 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4950 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4951 | |
| 4952 | return Py_BuildValue("(ii)", read, write); |
| 4953 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4954 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4955 | int fds[2]; |
| 4956 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4957 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4958 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4959 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4960 | if (res != 0) |
| 4961 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4962 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4963 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4964 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4965 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4966 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4967 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4968 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4969 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4970 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4971 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4972 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4973 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4974 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4975 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4976 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4977 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4978 | #endif /* HAVE_PIPE */ |
| 4979 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4980 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4981 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4982 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4983 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4984 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4985 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4986 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4987 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4988 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4989 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4990 | int mode = 0666; |
| 4991 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4992 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4993 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4994 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4995 | res = mkfifo(filename, mode); |
| 4996 | Py_END_ALLOW_THREADS |
| 4997 | if (res < 0) |
| 4998 | return posix_error(); |
| 4999 | Py_INCREF(Py_None); |
| 5000 | return Py_None; |
| 5001 | } |
| 5002 | #endif |
| 5003 | |
| 5004 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5005 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5006 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5007 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5008 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5009 | named filename. mode specifies both the permissions to use and the\n\ |
| 5010 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5011 | 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] | 5012 | device defines the newly created device special file (probably using\n\ |
| 5013 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5014 | |
| 5015 | |
| 5016 | static PyObject * |
| 5017 | posix_mknod(PyObject *self, PyObject *args) |
| 5018 | { |
| 5019 | char *filename; |
| 5020 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5021 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5022 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5023 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5024 | return NULL; |
| 5025 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5026 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5027 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5028 | if (res < 0) |
| 5029 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5030 | Py_INCREF(Py_None); |
| 5031 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5032 | } |
| 5033 | #endif |
| 5034 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5035 | #ifdef HAVE_DEVICE_MACROS |
| 5036 | PyDoc_STRVAR(posix_major__doc__, |
| 5037 | "major(device) -> major number\n\ |
| 5038 | Extracts a device major number from a raw device number."); |
| 5039 | |
| 5040 | static PyObject * |
| 5041 | posix_major(PyObject *self, PyObject *args) |
| 5042 | { |
| 5043 | int device; |
| 5044 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5045 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5046 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | PyDoc_STRVAR(posix_minor__doc__, |
| 5050 | "minor(device) -> minor number\n\ |
| 5051 | Extracts a device minor number from a raw device number."); |
| 5052 | |
| 5053 | static PyObject * |
| 5054 | posix_minor(PyObject *self, PyObject *args) |
| 5055 | { |
| 5056 | int device; |
| 5057 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5058 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5059 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5063 | "makedev(major, minor) -> device number\n\ |
| 5064 | Composes a raw device number from the major and minor device numbers."); |
| 5065 | |
| 5066 | static PyObject * |
| 5067 | posix_makedev(PyObject *self, PyObject *args) |
| 5068 | { |
| 5069 | int major, minor; |
| 5070 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5071 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5072 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5073 | } |
| 5074 | #endif /* device macros */ |
| 5075 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5076 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5077 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5078 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5079 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5080 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5081 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5082 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5083 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5084 | { |
| 5085 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5086 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5087 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5088 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5089 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5090 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5091 | return NULL; |
| 5092 | |
| 5093 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5094 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5095 | #else |
| 5096 | length = PyLong_Check(lenobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5097 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5098 | #endif |
| 5099 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5100 | return NULL; |
| 5101 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5102 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5103 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5104 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5105 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5106 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5107 | return NULL; |
| 5108 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5109 | Py_INCREF(Py_None); |
| 5110 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5111 | } |
| 5112 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5113 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5114 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5115 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5116 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5117 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5118 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5119 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5120 | * get re-set with another call for the same key. */ |
| 5121 | static PyObject *posix_putenv_garbage; |
| 5122 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5123 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5124 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5125 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5126 | #ifdef MS_WINDOWS |
| 5127 | wchar_t *s1, *s2; |
| 5128 | wchar_t *newenv; |
| 5129 | #else |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5130 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5131 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5132 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5133 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5134 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5135 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5136 | if (!PyArg_ParseTuple(args, |
| 5137 | #ifdef MS_WINDOWS |
| 5138 | "uu:putenv", |
| 5139 | #else |
| 5140 | "ss:putenv", |
| 5141 | #endif |
| 5142 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5143 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5144 | |
| 5145 | #if defined(PYOS_OS2) |
| 5146 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5147 | APIRET rc; |
| 5148 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5149 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5150 | if (rc != NO_ERROR) |
| 5151 | return os2_error(rc); |
| 5152 | |
| 5153 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5154 | APIRET rc; |
| 5155 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5156 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5157 | if (rc != NO_ERROR) |
| 5158 | return os2_error(rc); |
| 5159 | } else { |
| 5160 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5161 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5162 | /* len includes space for a trailing \0; the size arg to |
| 5163 | PyString_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5164 | #ifdef MS_WINDOWS |
| 5165 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5166 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5167 | #else |
| 5168 | len = strlen(s1) + strlen(s2) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5169 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5170 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5171 | if (newstr == NULL) |
| 5172 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5173 | #ifdef MS_WINDOWS |
| 5174 | newenv = PyUnicode_AsUnicode(newstr); |
| 5175 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5176 | if (_wputenv(newenv)) { |
| 5177 | Py_DECREF(newstr); |
| 5178 | posix_error(); |
| 5179 | return NULL; |
| 5180 | } |
| 5181 | #else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5182 | newenv = PyString_AS_STRING(newstr); |
| 5183 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5184 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5185 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5186 | posix_error(); |
| 5187 | return NULL; |
| 5188 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5189 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5190 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5191 | * this will cause previous value to be collected. This has to |
| 5192 | * happen after the real putenv() call because the old value |
| 5193 | * was still accessible until then. */ |
| 5194 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5195 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5196 | /* really not much we can do; just leak */ |
| 5197 | PyErr_Clear(); |
| 5198 | } |
| 5199 | else { |
| 5200 | Py_DECREF(newstr); |
| 5201 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5202 | |
| 5203 | #if defined(PYOS_OS2) |
| 5204 | } |
| 5205 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5206 | Py_INCREF(Py_None); |
| 5207 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5208 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5209 | #endif /* putenv */ |
| 5210 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5211 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5212 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5213 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5214 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5215 | |
| 5216 | static PyObject * |
| 5217 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5218 | { |
| 5219 | char *s1; |
| 5220 | |
| 5221 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5222 | return NULL; |
| 5223 | |
| 5224 | unsetenv(s1); |
| 5225 | |
| 5226 | /* Remove the key from posix_putenv_garbage; |
| 5227 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5228 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5229 | * old value was still accessible until then. |
| 5230 | */ |
| 5231 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5232 | PyTuple_GET_ITEM(args, 0))) { |
| 5233 | /* really not much we can do; just leak */ |
| 5234 | PyErr_Clear(); |
| 5235 | } |
| 5236 | |
| 5237 | Py_INCREF(Py_None); |
| 5238 | return Py_None; |
| 5239 | } |
| 5240 | #endif /* unsetenv */ |
| 5241 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5242 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5243 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5244 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5245 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5246 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5247 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5248 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5249 | { |
| 5250 | int code; |
| 5251 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5252 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5253 | return NULL; |
| 5254 | message = strerror(code); |
| 5255 | if (message == NULL) { |
| 5256 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5257 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5258 | return NULL; |
| 5259 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5260 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5261 | } |
| 5262 | #endif /* strerror */ |
| 5263 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5264 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5265 | #ifdef HAVE_SYS_WAIT_H |
| 5266 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5267 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5268 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5269 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5270 | 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] | 5271 | |
| 5272 | static PyObject * |
| 5273 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5274 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5275 | WAIT_TYPE status; |
| 5276 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5277 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5278 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5279 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5280 | |
| 5281 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5282 | } |
| 5283 | #endif /* WCOREDUMP */ |
| 5284 | |
| 5285 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5286 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5287 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5288 | 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] | 5289 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5290 | |
| 5291 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5292 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5293 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5294 | WAIT_TYPE status; |
| 5295 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5296 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5297 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5298 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5299 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5300 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5301 | } |
| 5302 | #endif /* WIFCONTINUED */ |
| 5303 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5304 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5305 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5306 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5307 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5308 | |
| 5309 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5310 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5311 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5312 | WAIT_TYPE status; |
| 5313 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5314 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5315 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5316 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5317 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5318 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5319 | } |
| 5320 | #endif /* WIFSTOPPED */ |
| 5321 | |
| 5322 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5323 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5324 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5325 | 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] | 5326 | |
| 5327 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5328 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5329 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5330 | WAIT_TYPE status; |
| 5331 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5332 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5333 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5334 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5335 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5336 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5337 | } |
| 5338 | #endif /* WIFSIGNALED */ |
| 5339 | |
| 5340 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5341 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5342 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5343 | 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] | 5344 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5345 | |
| 5346 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5347 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5348 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5349 | WAIT_TYPE status; |
| 5350 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5351 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5352 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5353 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5354 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5355 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5356 | } |
| 5357 | #endif /* WIFEXITED */ |
| 5358 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5359 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5360 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5361 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5362 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5363 | |
| 5364 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5365 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5366 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5367 | WAIT_TYPE status; |
| 5368 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5369 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5370 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5371 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5372 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5373 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5374 | } |
| 5375 | #endif /* WEXITSTATUS */ |
| 5376 | |
| 5377 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5378 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5379 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5380 | 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] | 5381 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5382 | |
| 5383 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5384 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5385 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5386 | WAIT_TYPE status; |
| 5387 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5388 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5389 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5390 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5391 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5392 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5393 | } |
| 5394 | #endif /* WTERMSIG */ |
| 5395 | |
| 5396 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5397 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5398 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5399 | Return the signal that stopped the process that provided\n\ |
| 5400 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5401 | |
| 5402 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5403 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5404 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5405 | WAIT_TYPE status; |
| 5406 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5407 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5408 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5409 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5410 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5411 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5412 | } |
| 5413 | #endif /* WSTOPSIG */ |
| 5414 | |
| 5415 | #endif /* HAVE_SYS_WAIT_H */ |
| 5416 | |
| 5417 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5418 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5419 | #ifdef _SCO_DS |
| 5420 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5421 | needed definitions in sys/statvfs.h */ |
| 5422 | #define _SVID3 |
| 5423 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5424 | #include <sys/statvfs.h> |
| 5425 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5426 | static PyObject* |
| 5427 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5428 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5429 | if (v == NULL) |
| 5430 | return NULL; |
| 5431 | |
| 5432 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5433 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5434 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5435 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5436 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5437 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5438 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5439 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5440 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 5441 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5442 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5443 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5444 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5445 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5446 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5447 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5448 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5449 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5450 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5451 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5452 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5453 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5454 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5455 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5456 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5457 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5458 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5459 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5460 | #endif |
| 5461 | |
| 5462 | return v; |
| 5463 | } |
| 5464 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5465 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5466 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5467 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5468 | |
| 5469 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5470 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5471 | { |
| 5472 | int fd, res; |
| 5473 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5474 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5475 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5476 | return NULL; |
| 5477 | Py_BEGIN_ALLOW_THREADS |
| 5478 | res = fstatvfs(fd, &st); |
| 5479 | Py_END_ALLOW_THREADS |
| 5480 | if (res != 0) |
| 5481 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5482 | |
| 5483 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5484 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5485 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5486 | |
| 5487 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5488 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5489 | #include <sys/statvfs.h> |
| 5490 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5491 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5492 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5493 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5494 | |
| 5495 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5496 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5497 | { |
| 5498 | char *path; |
| 5499 | int res; |
| 5500 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5501 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5502 | return NULL; |
| 5503 | Py_BEGIN_ALLOW_THREADS |
| 5504 | res = statvfs(path, &st); |
| 5505 | Py_END_ALLOW_THREADS |
| 5506 | if (res != 0) |
| 5507 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5508 | |
| 5509 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5510 | } |
| 5511 | #endif /* HAVE_STATVFS */ |
| 5512 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5513 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5514 | * It maps strings representing configuration variable names to |
| 5515 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5516 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5517 | * rarely-used constants. There are three separate tables that use |
| 5518 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5519 | * |
| 5520 | * This code is always included, even if none of the interfaces that |
| 5521 | * need it are included. The #if hackery needed to avoid it would be |
| 5522 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5523 | */ |
| 5524 | struct constdef { |
| 5525 | char *name; |
| 5526 | long value; |
| 5527 | }; |
| 5528 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5529 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5530 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5531 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5532 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5533 | if (PyLong_Check(arg)) { |
| 5534 | *valuep = PyLong_AS_LONG(arg); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5535 | return 1; |
| 5536 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5537 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5538 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5539 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5540 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5541 | size_t hi = tablesize; |
| 5542 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5543 | const char *confname; |
| 5544 | Py_ssize_t namelen; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5545 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5546 | PyErr_SetString(PyExc_TypeError, |
| 5547 | "configuration names must be strings or integers"); |
| 5548 | return 0; |
| 5549 | } |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5550 | confname = PyUnicode_AsString(arg); |
| 5551 | if (confname == NULL) |
| 5552 | return 0; |
| 5553 | namelen = strlen(confname); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5554 | while (lo < hi) { |
| 5555 | mid = (lo + hi) / 2; |
| 5556 | cmp = strcmp(confname, table[mid].name); |
| 5557 | if (cmp < 0) |
| 5558 | hi = mid; |
| 5559 | else if (cmp > 0) |
| 5560 | lo = mid + 1; |
| 5561 | else { |
| 5562 | *valuep = table[mid].value; |
| 5563 | return 1; |
| 5564 | } |
| 5565 | } |
| 5566 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5567 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5568 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
| 5571 | |
| 5572 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5573 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5574 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5575 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5576 | #endif |
| 5577 | #ifdef _PC_ABI_ASYNC_IO |
| 5578 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5579 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5580 | #ifdef _PC_ASYNC_IO |
| 5581 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5582 | #endif |
| 5583 | #ifdef _PC_CHOWN_RESTRICTED |
| 5584 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5585 | #endif |
| 5586 | #ifdef _PC_FILESIZEBITS |
| 5587 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5588 | #endif |
| 5589 | #ifdef _PC_LAST |
| 5590 | {"PC_LAST", _PC_LAST}, |
| 5591 | #endif |
| 5592 | #ifdef _PC_LINK_MAX |
| 5593 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5594 | #endif |
| 5595 | #ifdef _PC_MAX_CANON |
| 5596 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5597 | #endif |
| 5598 | #ifdef _PC_MAX_INPUT |
| 5599 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5600 | #endif |
| 5601 | #ifdef _PC_NAME_MAX |
| 5602 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5603 | #endif |
| 5604 | #ifdef _PC_NO_TRUNC |
| 5605 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5606 | #endif |
| 5607 | #ifdef _PC_PATH_MAX |
| 5608 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5609 | #endif |
| 5610 | #ifdef _PC_PIPE_BUF |
| 5611 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5612 | #endif |
| 5613 | #ifdef _PC_PRIO_IO |
| 5614 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5615 | #endif |
| 5616 | #ifdef _PC_SOCK_MAXBUF |
| 5617 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5618 | #endif |
| 5619 | #ifdef _PC_SYNC_IO |
| 5620 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5621 | #endif |
| 5622 | #ifdef _PC_VDISABLE |
| 5623 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5624 | #endif |
| 5625 | }; |
| 5626 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5627 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5628 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5629 | { |
| 5630 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5631 | sizeof(posix_constants_pathconf) |
| 5632 | / sizeof(struct constdef)); |
| 5633 | } |
| 5634 | #endif |
| 5635 | |
| 5636 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5637 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5638 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5639 | 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] | 5640 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5641 | |
| 5642 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5643 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5644 | { |
| 5645 | PyObject *result = NULL; |
| 5646 | int name, fd; |
| 5647 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5648 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5649 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5650 | long limit; |
| 5651 | |
| 5652 | errno = 0; |
| 5653 | limit = fpathconf(fd, name); |
| 5654 | if (limit == -1 && errno != 0) |
| 5655 | posix_error(); |
| 5656 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5657 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5658 | } |
| 5659 | return result; |
| 5660 | } |
| 5661 | #endif |
| 5662 | |
| 5663 | |
| 5664 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5665 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5666 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5667 | 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] | 5668 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5669 | |
| 5670 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5671 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5672 | { |
| 5673 | PyObject *result = NULL; |
| 5674 | int name; |
| 5675 | char *path; |
| 5676 | |
| 5677 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5678 | conv_path_confname, &name)) { |
| 5679 | long limit; |
| 5680 | |
| 5681 | errno = 0; |
| 5682 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5683 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5684 | if (errno == EINVAL) |
| 5685 | /* could be a path or name problem */ |
| 5686 | posix_error(); |
| 5687 | else |
| 5688 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5689 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5690 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5691 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5692 | } |
| 5693 | return result; |
| 5694 | } |
| 5695 | #endif |
| 5696 | |
| 5697 | #ifdef HAVE_CONFSTR |
| 5698 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5699 | #ifdef _CS_ARCHITECTURE |
| 5700 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5701 | #endif |
| 5702 | #ifdef _CS_HOSTNAME |
| 5703 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5704 | #endif |
| 5705 | #ifdef _CS_HW_PROVIDER |
| 5706 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5707 | #endif |
| 5708 | #ifdef _CS_HW_SERIAL |
| 5709 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5710 | #endif |
| 5711 | #ifdef _CS_INITTAB_NAME |
| 5712 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5713 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5714 | #ifdef _CS_LFS64_CFLAGS |
| 5715 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5716 | #endif |
| 5717 | #ifdef _CS_LFS64_LDFLAGS |
| 5718 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5719 | #endif |
| 5720 | #ifdef _CS_LFS64_LIBS |
| 5721 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5722 | #endif |
| 5723 | #ifdef _CS_LFS64_LINTFLAGS |
| 5724 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5725 | #endif |
| 5726 | #ifdef _CS_LFS_CFLAGS |
| 5727 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5728 | #endif |
| 5729 | #ifdef _CS_LFS_LDFLAGS |
| 5730 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5731 | #endif |
| 5732 | #ifdef _CS_LFS_LIBS |
| 5733 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5734 | #endif |
| 5735 | #ifdef _CS_LFS_LINTFLAGS |
| 5736 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5737 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5738 | #ifdef _CS_MACHINE |
| 5739 | {"CS_MACHINE", _CS_MACHINE}, |
| 5740 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5741 | #ifdef _CS_PATH |
| 5742 | {"CS_PATH", _CS_PATH}, |
| 5743 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5744 | #ifdef _CS_RELEASE |
| 5745 | {"CS_RELEASE", _CS_RELEASE}, |
| 5746 | #endif |
| 5747 | #ifdef _CS_SRPC_DOMAIN |
| 5748 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5749 | #endif |
| 5750 | #ifdef _CS_SYSNAME |
| 5751 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5752 | #endif |
| 5753 | #ifdef _CS_VERSION |
| 5754 | {"CS_VERSION", _CS_VERSION}, |
| 5755 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5756 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5757 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5758 | #endif |
| 5759 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5760 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5761 | #endif |
| 5762 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5763 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5764 | #endif |
| 5765 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5766 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5767 | #endif |
| 5768 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5769 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5770 | #endif |
| 5771 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5772 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5773 | #endif |
| 5774 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5775 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5776 | #endif |
| 5777 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5778 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5779 | #endif |
| 5780 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5781 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5782 | #endif |
| 5783 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5784 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5785 | #endif |
| 5786 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5787 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5788 | #endif |
| 5789 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5790 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5791 | #endif |
| 5792 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5793 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5794 | #endif |
| 5795 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5796 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5797 | #endif |
| 5798 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5799 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5800 | #endif |
| 5801 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5802 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5803 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5804 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5805 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5806 | #endif |
| 5807 | #ifdef _MIPS_CS_BASE |
| 5808 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5809 | #endif |
| 5810 | #ifdef _MIPS_CS_HOSTID |
| 5811 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5812 | #endif |
| 5813 | #ifdef _MIPS_CS_HW_NAME |
| 5814 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5815 | #endif |
| 5816 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5817 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5818 | #endif |
| 5819 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5820 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5821 | #endif |
| 5822 | #ifdef _MIPS_CS_OSREL_MIN |
| 5823 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5824 | #endif |
| 5825 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5826 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5827 | #endif |
| 5828 | #ifdef _MIPS_CS_OS_NAME |
| 5829 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5830 | #endif |
| 5831 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5832 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5833 | #endif |
| 5834 | #ifdef _MIPS_CS_PROCESSORS |
| 5835 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5836 | #endif |
| 5837 | #ifdef _MIPS_CS_SERIAL |
| 5838 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5839 | #endif |
| 5840 | #ifdef _MIPS_CS_VENDOR |
| 5841 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5842 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5843 | }; |
| 5844 | |
| 5845 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5846 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5847 | { |
| 5848 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5849 | sizeof(posix_constants_confstr) |
| 5850 | / sizeof(struct constdef)); |
| 5851 | } |
| 5852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5853 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5854 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5855 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5856 | |
| 5857 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5858 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5859 | { |
| 5860 | PyObject *result = NULL; |
| 5861 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5862 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5863 | |
| 5864 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5865 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5866 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5867 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5868 | len = confstr(name, buffer, sizeof(buffer)); |
| 5869 | if (len == 0) { |
| 5870 | if (errno) { |
| 5871 | posix_error(); |
| 5872 | } |
| 5873 | else { |
| 5874 | result = Py_None; |
| 5875 | Py_INCREF(Py_None); |
| 5876 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5877 | } |
| 5878 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5879 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5880 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5881 | if (result != NULL) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5882 | confstr(name, PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5883 | } |
| 5884 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5885 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5886 | } |
| 5887 | } |
| 5888 | return result; |
| 5889 | } |
| 5890 | #endif |
| 5891 | |
| 5892 | |
| 5893 | #ifdef HAVE_SYSCONF |
| 5894 | static struct constdef posix_constants_sysconf[] = { |
| 5895 | #ifdef _SC_2_CHAR_TERM |
| 5896 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5897 | #endif |
| 5898 | #ifdef _SC_2_C_BIND |
| 5899 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5900 | #endif |
| 5901 | #ifdef _SC_2_C_DEV |
| 5902 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5903 | #endif |
| 5904 | #ifdef _SC_2_C_VERSION |
| 5905 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5906 | #endif |
| 5907 | #ifdef _SC_2_FORT_DEV |
| 5908 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5909 | #endif |
| 5910 | #ifdef _SC_2_FORT_RUN |
| 5911 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5912 | #endif |
| 5913 | #ifdef _SC_2_LOCALEDEF |
| 5914 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5915 | #endif |
| 5916 | #ifdef _SC_2_SW_DEV |
| 5917 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5918 | #endif |
| 5919 | #ifdef _SC_2_UPE |
| 5920 | {"SC_2_UPE", _SC_2_UPE}, |
| 5921 | #endif |
| 5922 | #ifdef _SC_2_VERSION |
| 5923 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5924 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5925 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5926 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5927 | #endif |
| 5928 | #ifdef _SC_ACL |
| 5929 | {"SC_ACL", _SC_ACL}, |
| 5930 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5931 | #ifdef _SC_AIO_LISTIO_MAX |
| 5932 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5933 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5934 | #ifdef _SC_AIO_MAX |
| 5935 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5936 | #endif |
| 5937 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5938 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5939 | #endif |
| 5940 | #ifdef _SC_ARG_MAX |
| 5941 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5942 | #endif |
| 5943 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5944 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5945 | #endif |
| 5946 | #ifdef _SC_ATEXIT_MAX |
| 5947 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5948 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5949 | #ifdef _SC_AUDIT |
| 5950 | {"SC_AUDIT", _SC_AUDIT}, |
| 5951 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5952 | #ifdef _SC_AVPHYS_PAGES |
| 5953 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5954 | #endif |
| 5955 | #ifdef _SC_BC_BASE_MAX |
| 5956 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5957 | #endif |
| 5958 | #ifdef _SC_BC_DIM_MAX |
| 5959 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5960 | #endif |
| 5961 | #ifdef _SC_BC_SCALE_MAX |
| 5962 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5963 | #endif |
| 5964 | #ifdef _SC_BC_STRING_MAX |
| 5965 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5966 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5967 | #ifdef _SC_CAP |
| 5968 | {"SC_CAP", _SC_CAP}, |
| 5969 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5970 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5971 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5972 | #endif |
| 5973 | #ifdef _SC_CHAR_BIT |
| 5974 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5975 | #endif |
| 5976 | #ifdef _SC_CHAR_MAX |
| 5977 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5978 | #endif |
| 5979 | #ifdef _SC_CHAR_MIN |
| 5980 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5981 | #endif |
| 5982 | #ifdef _SC_CHILD_MAX |
| 5983 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5984 | #endif |
| 5985 | #ifdef _SC_CLK_TCK |
| 5986 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 5987 | #endif |
| 5988 | #ifdef _SC_COHER_BLKSZ |
| 5989 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 5990 | #endif |
| 5991 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 5992 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 5993 | #endif |
| 5994 | #ifdef _SC_DCACHE_ASSOC |
| 5995 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5996 | #endif |
| 5997 | #ifdef _SC_DCACHE_BLKSZ |
| 5998 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5999 | #endif |
| 6000 | #ifdef _SC_DCACHE_LINESZ |
| 6001 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6002 | #endif |
| 6003 | #ifdef _SC_DCACHE_SZ |
| 6004 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6005 | #endif |
| 6006 | #ifdef _SC_DCACHE_TBLKSZ |
| 6007 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6008 | #endif |
| 6009 | #ifdef _SC_DELAYTIMER_MAX |
| 6010 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6011 | #endif |
| 6012 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6013 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6014 | #endif |
| 6015 | #ifdef _SC_EXPR_NEST_MAX |
| 6016 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6017 | #endif |
| 6018 | #ifdef _SC_FSYNC |
| 6019 | {"SC_FSYNC", _SC_FSYNC}, |
| 6020 | #endif |
| 6021 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6022 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6023 | #endif |
| 6024 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6025 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6026 | #endif |
| 6027 | #ifdef _SC_ICACHE_ASSOC |
| 6028 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6029 | #endif |
| 6030 | #ifdef _SC_ICACHE_BLKSZ |
| 6031 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6032 | #endif |
| 6033 | #ifdef _SC_ICACHE_LINESZ |
| 6034 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6035 | #endif |
| 6036 | #ifdef _SC_ICACHE_SZ |
| 6037 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6038 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6039 | #ifdef _SC_INF |
| 6040 | {"SC_INF", _SC_INF}, |
| 6041 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6042 | #ifdef _SC_INT_MAX |
| 6043 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6044 | #endif |
| 6045 | #ifdef _SC_INT_MIN |
| 6046 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6047 | #endif |
| 6048 | #ifdef _SC_IOV_MAX |
| 6049 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6050 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6051 | #ifdef _SC_IP_SECOPTS |
| 6052 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6053 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6054 | #ifdef _SC_JOB_CONTROL |
| 6055 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6056 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6057 | #ifdef _SC_KERN_POINTERS |
| 6058 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6059 | #endif |
| 6060 | #ifdef _SC_KERN_SIM |
| 6061 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6062 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6063 | #ifdef _SC_LINE_MAX |
| 6064 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6065 | #endif |
| 6066 | #ifdef _SC_LOGIN_NAME_MAX |
| 6067 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6068 | #endif |
| 6069 | #ifdef _SC_LOGNAME_MAX |
| 6070 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6071 | #endif |
| 6072 | #ifdef _SC_LONG_BIT |
| 6073 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6074 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6075 | #ifdef _SC_MAC |
| 6076 | {"SC_MAC", _SC_MAC}, |
| 6077 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6078 | #ifdef _SC_MAPPED_FILES |
| 6079 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6080 | #endif |
| 6081 | #ifdef _SC_MAXPID |
| 6082 | {"SC_MAXPID", _SC_MAXPID}, |
| 6083 | #endif |
| 6084 | #ifdef _SC_MB_LEN_MAX |
| 6085 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6086 | #endif |
| 6087 | #ifdef _SC_MEMLOCK |
| 6088 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6089 | #endif |
| 6090 | #ifdef _SC_MEMLOCK_RANGE |
| 6091 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6092 | #endif |
| 6093 | #ifdef _SC_MEMORY_PROTECTION |
| 6094 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6095 | #endif |
| 6096 | #ifdef _SC_MESSAGE_PASSING |
| 6097 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6098 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6099 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6100 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6101 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6102 | #ifdef _SC_MQ_OPEN_MAX |
| 6103 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6104 | #endif |
| 6105 | #ifdef _SC_MQ_PRIO_MAX |
| 6106 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6107 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6108 | #ifdef _SC_NACLS_MAX |
| 6109 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6110 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6111 | #ifdef _SC_NGROUPS_MAX |
| 6112 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6113 | #endif |
| 6114 | #ifdef _SC_NL_ARGMAX |
| 6115 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6116 | #endif |
| 6117 | #ifdef _SC_NL_LANGMAX |
| 6118 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6119 | #endif |
| 6120 | #ifdef _SC_NL_MSGMAX |
| 6121 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6122 | #endif |
| 6123 | #ifdef _SC_NL_NMAX |
| 6124 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6125 | #endif |
| 6126 | #ifdef _SC_NL_SETMAX |
| 6127 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6128 | #endif |
| 6129 | #ifdef _SC_NL_TEXTMAX |
| 6130 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6131 | #endif |
| 6132 | #ifdef _SC_NPROCESSORS_CONF |
| 6133 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6134 | #endif |
| 6135 | #ifdef _SC_NPROCESSORS_ONLN |
| 6136 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6137 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6138 | #ifdef _SC_NPROC_CONF |
| 6139 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6140 | #endif |
| 6141 | #ifdef _SC_NPROC_ONLN |
| 6142 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6143 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6144 | #ifdef _SC_NZERO |
| 6145 | {"SC_NZERO", _SC_NZERO}, |
| 6146 | #endif |
| 6147 | #ifdef _SC_OPEN_MAX |
| 6148 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6149 | #endif |
| 6150 | #ifdef _SC_PAGESIZE |
| 6151 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6152 | #endif |
| 6153 | #ifdef _SC_PAGE_SIZE |
| 6154 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6155 | #endif |
| 6156 | #ifdef _SC_PASS_MAX |
| 6157 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6158 | #endif |
| 6159 | #ifdef _SC_PHYS_PAGES |
| 6160 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6161 | #endif |
| 6162 | #ifdef _SC_PII |
| 6163 | {"SC_PII", _SC_PII}, |
| 6164 | #endif |
| 6165 | #ifdef _SC_PII_INTERNET |
| 6166 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6167 | #endif |
| 6168 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6169 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6170 | #endif |
| 6171 | #ifdef _SC_PII_INTERNET_STREAM |
| 6172 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6173 | #endif |
| 6174 | #ifdef _SC_PII_OSI |
| 6175 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6176 | #endif |
| 6177 | #ifdef _SC_PII_OSI_CLTS |
| 6178 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6179 | #endif |
| 6180 | #ifdef _SC_PII_OSI_COTS |
| 6181 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6182 | #endif |
| 6183 | #ifdef _SC_PII_OSI_M |
| 6184 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6185 | #endif |
| 6186 | #ifdef _SC_PII_SOCKET |
| 6187 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6188 | #endif |
| 6189 | #ifdef _SC_PII_XTI |
| 6190 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6191 | #endif |
| 6192 | #ifdef _SC_POLL |
| 6193 | {"SC_POLL", _SC_POLL}, |
| 6194 | #endif |
| 6195 | #ifdef _SC_PRIORITIZED_IO |
| 6196 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6197 | #endif |
| 6198 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6199 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6200 | #endif |
| 6201 | #ifdef _SC_REALTIME_SIGNALS |
| 6202 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6203 | #endif |
| 6204 | #ifdef _SC_RE_DUP_MAX |
| 6205 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6206 | #endif |
| 6207 | #ifdef _SC_RTSIG_MAX |
| 6208 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6209 | #endif |
| 6210 | #ifdef _SC_SAVED_IDS |
| 6211 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6212 | #endif |
| 6213 | #ifdef _SC_SCHAR_MAX |
| 6214 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6215 | #endif |
| 6216 | #ifdef _SC_SCHAR_MIN |
| 6217 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6218 | #endif |
| 6219 | #ifdef _SC_SELECT |
| 6220 | {"SC_SELECT", _SC_SELECT}, |
| 6221 | #endif |
| 6222 | #ifdef _SC_SEMAPHORES |
| 6223 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6224 | #endif |
| 6225 | #ifdef _SC_SEM_NSEMS_MAX |
| 6226 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6227 | #endif |
| 6228 | #ifdef _SC_SEM_VALUE_MAX |
| 6229 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6230 | #endif |
| 6231 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6232 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6233 | #endif |
| 6234 | #ifdef _SC_SHRT_MAX |
| 6235 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6236 | #endif |
| 6237 | #ifdef _SC_SHRT_MIN |
| 6238 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6239 | #endif |
| 6240 | #ifdef _SC_SIGQUEUE_MAX |
| 6241 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6242 | #endif |
| 6243 | #ifdef _SC_SIGRT_MAX |
| 6244 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6245 | #endif |
| 6246 | #ifdef _SC_SIGRT_MIN |
| 6247 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6248 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6249 | #ifdef _SC_SOFTPOWER |
| 6250 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6251 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6252 | #ifdef _SC_SPLIT_CACHE |
| 6253 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6254 | #endif |
| 6255 | #ifdef _SC_SSIZE_MAX |
| 6256 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6257 | #endif |
| 6258 | #ifdef _SC_STACK_PROT |
| 6259 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6260 | #endif |
| 6261 | #ifdef _SC_STREAM_MAX |
| 6262 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6263 | #endif |
| 6264 | #ifdef _SC_SYNCHRONIZED_IO |
| 6265 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6266 | #endif |
| 6267 | #ifdef _SC_THREADS |
| 6268 | {"SC_THREADS", _SC_THREADS}, |
| 6269 | #endif |
| 6270 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6271 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6272 | #endif |
| 6273 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6274 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6275 | #endif |
| 6276 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6277 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6278 | #endif |
| 6279 | #ifdef _SC_THREAD_KEYS_MAX |
| 6280 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6281 | #endif |
| 6282 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6283 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6284 | #endif |
| 6285 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6286 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6287 | #endif |
| 6288 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6289 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6290 | #endif |
| 6291 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6292 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6293 | #endif |
| 6294 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6295 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6296 | #endif |
| 6297 | #ifdef _SC_THREAD_STACK_MIN |
| 6298 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6299 | #endif |
| 6300 | #ifdef _SC_THREAD_THREADS_MAX |
| 6301 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6302 | #endif |
| 6303 | #ifdef _SC_TIMERS |
| 6304 | {"SC_TIMERS", _SC_TIMERS}, |
| 6305 | #endif |
| 6306 | #ifdef _SC_TIMER_MAX |
| 6307 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6308 | #endif |
| 6309 | #ifdef _SC_TTY_NAME_MAX |
| 6310 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6311 | #endif |
| 6312 | #ifdef _SC_TZNAME_MAX |
| 6313 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6314 | #endif |
| 6315 | #ifdef _SC_T_IOV_MAX |
| 6316 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6317 | #endif |
| 6318 | #ifdef _SC_UCHAR_MAX |
| 6319 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6320 | #endif |
| 6321 | #ifdef _SC_UINT_MAX |
| 6322 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6323 | #endif |
| 6324 | #ifdef _SC_UIO_MAXIOV |
| 6325 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6326 | #endif |
| 6327 | #ifdef _SC_ULONG_MAX |
| 6328 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6329 | #endif |
| 6330 | #ifdef _SC_USHRT_MAX |
| 6331 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6332 | #endif |
| 6333 | #ifdef _SC_VERSION |
| 6334 | {"SC_VERSION", _SC_VERSION}, |
| 6335 | #endif |
| 6336 | #ifdef _SC_WORD_BIT |
| 6337 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6338 | #endif |
| 6339 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6340 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6341 | #endif |
| 6342 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6343 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6344 | #endif |
| 6345 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6346 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6347 | #endif |
| 6348 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6349 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6350 | #endif |
| 6351 | #ifdef _SC_XOPEN_CRYPT |
| 6352 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6353 | #endif |
| 6354 | #ifdef _SC_XOPEN_ENH_I18N |
| 6355 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6356 | #endif |
| 6357 | #ifdef _SC_XOPEN_LEGACY |
| 6358 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6359 | #endif |
| 6360 | #ifdef _SC_XOPEN_REALTIME |
| 6361 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6362 | #endif |
| 6363 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6364 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6365 | #endif |
| 6366 | #ifdef _SC_XOPEN_SHM |
| 6367 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6368 | #endif |
| 6369 | #ifdef _SC_XOPEN_UNIX |
| 6370 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6371 | #endif |
| 6372 | #ifdef _SC_XOPEN_VERSION |
| 6373 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6374 | #endif |
| 6375 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6376 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6377 | #endif |
| 6378 | #ifdef _SC_XOPEN_XPG2 |
| 6379 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6380 | #endif |
| 6381 | #ifdef _SC_XOPEN_XPG3 |
| 6382 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6383 | #endif |
| 6384 | #ifdef _SC_XOPEN_XPG4 |
| 6385 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6386 | #endif |
| 6387 | }; |
| 6388 | |
| 6389 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6390 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6391 | { |
| 6392 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6393 | sizeof(posix_constants_sysconf) |
| 6394 | / sizeof(struct constdef)); |
| 6395 | } |
| 6396 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6397 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6398 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6399 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6400 | |
| 6401 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6402 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6403 | { |
| 6404 | PyObject *result = NULL; |
| 6405 | int name; |
| 6406 | |
| 6407 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6408 | int value; |
| 6409 | |
| 6410 | errno = 0; |
| 6411 | value = sysconf(name); |
| 6412 | if (value == -1 && errno != 0) |
| 6413 | posix_error(); |
| 6414 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6415 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6416 | } |
| 6417 | return result; |
| 6418 | } |
| 6419 | #endif |
| 6420 | |
| 6421 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6422 | /* This code is used to ensure that the tables of configuration value names |
| 6423 | * are in sorted order as required by conv_confname(), and also to build the |
| 6424 | * the exported dictionaries that are used to publish information about the |
| 6425 | * names available on the host platform. |
| 6426 | * |
| 6427 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6428 | * when used, even for platforms we're not able to test on. It also makes |
| 6429 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6430 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6431 | |
| 6432 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6433 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6434 | { |
| 6435 | const struct constdef *c1 = |
| 6436 | (const struct constdef *) v1; |
| 6437 | const struct constdef *c2 = |
| 6438 | (const struct constdef *) v2; |
| 6439 | |
| 6440 | return strcmp(c1->name, c2->name); |
| 6441 | } |
| 6442 | |
| 6443 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6444 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6445 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6446 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6447 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6448 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6449 | |
| 6450 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6451 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6452 | if (d == NULL) |
| 6453 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6454 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6455 | for (i=0; i < tablesize; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6456 | PyObject *o = PyLong_FromLong(table[i].value); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6457 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6458 | Py_XDECREF(o); |
| 6459 | Py_DECREF(d); |
| 6460 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6461 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6462 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6463 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6464 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6465 | } |
| 6466 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6467 | /* Return -1 on failure, 0 on success. */ |
| 6468 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6469 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6470 | { |
| 6471 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6472 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6473 | sizeof(posix_constants_pathconf) |
| 6474 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6475 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6476 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6477 | #endif |
| 6478 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6479 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6480 | sizeof(posix_constants_confstr) |
| 6481 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6482 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6483 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6484 | #endif |
| 6485 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6486 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6487 | sizeof(posix_constants_sysconf) |
| 6488 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6489 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6490 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6491 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6492 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6493 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6494 | |
| 6495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6496 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6497 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6498 | 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] | 6499 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6500 | |
| 6501 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6502 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6503 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6504 | abort(); |
| 6505 | /*NOTREACHED*/ |
| 6506 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6507 | return NULL; |
| 6508 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6509 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6510 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6511 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6512 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6513 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6514 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6515 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6516 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6517 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6518 | application (if any) its extension is associated.\n\ |
| 6519 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6520 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6521 | \n\ |
| 6522 | startfile returns as soon as the associated application is launched.\n\ |
| 6523 | There is no option to wait for the application to close, and no way\n\ |
| 6524 | to retrieve the application's exit status.\n\ |
| 6525 | \n\ |
| 6526 | The filepath is relative to the current directory. If you want to use\n\ |
| 6527 | 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] | 6528 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6529 | |
| 6530 | static PyObject * |
| 6531 | win32_startfile(PyObject *self, PyObject *args) |
| 6532 | { |
| 6533 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6534 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6535 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6536 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6537 | if (unicode_file_names()) { |
| 6538 | PyObject *unipath, *woperation = NULL; |
| 6539 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6540 | &unipath, &operation)) { |
| 6541 | PyErr_Clear(); |
| 6542 | goto normal; |
| 6543 | } |
| 6544 | |
| 6545 | |
| 6546 | if (operation) { |
| 6547 | woperation = PyUnicode_DecodeASCII(operation, |
| 6548 | strlen(operation), NULL); |
| 6549 | if (!woperation) { |
| 6550 | PyErr_Clear(); |
| 6551 | operation = NULL; |
| 6552 | goto normal; |
| 6553 | } |
| 6554 | } |
| 6555 | |
| 6556 | Py_BEGIN_ALLOW_THREADS |
| 6557 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6558 | PyUnicode_AS_UNICODE(unipath), |
| 6559 | NULL, NULL, SW_SHOWNORMAL); |
| 6560 | Py_END_ALLOW_THREADS |
| 6561 | |
| 6562 | Py_XDECREF(woperation); |
| 6563 | if (rc <= (HINSTANCE)32) { |
| 6564 | PyObject *errval = win32_error_unicode("startfile", |
| 6565 | PyUnicode_AS_UNICODE(unipath)); |
| 6566 | return errval; |
| 6567 | } |
| 6568 | Py_INCREF(Py_None); |
| 6569 | return Py_None; |
| 6570 | } |
| 6571 | #endif |
| 6572 | |
| 6573 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6574 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 6575 | Py_FileSystemDefaultEncoding, &filepath, |
| 6576 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6577 | return NULL; |
| 6578 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6579 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6580 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6581 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6582 | if (rc <= (HINSTANCE)32) { |
| 6583 | PyObject *errval = win32_error("startfile", filepath); |
| 6584 | PyMem_Free(filepath); |
| 6585 | return errval; |
| 6586 | } |
| 6587 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6588 | Py_INCREF(Py_None); |
| 6589 | return Py_None; |
| 6590 | } |
| 6591 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6592 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6593 | #ifdef HAVE_GETLOADAVG |
| 6594 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6595 | "getloadavg() -> (float, float, float)\n\n\ |
| 6596 | Return the number of processes in the system run queue averaged over\n\ |
| 6597 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6598 | was unobtainable"); |
| 6599 | |
| 6600 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6601 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6602 | { |
| 6603 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6604 | if (getloadavg(loadavg, 3)!=3) { |
| 6605 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6606 | return NULL; |
| 6607 | } else |
| 6608 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6609 | } |
| 6610 | #endif |
| 6611 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6612 | #ifdef MS_WINDOWS |
| 6613 | |
| 6614 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6615 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6616 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6617 | |
| 6618 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6619 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6620 | DWORD dwFlags ); |
| 6621 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6622 | BYTE *pbBuffer ); |
| 6623 | |
| 6624 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6625 | /* This handle is never explicitly released. Instead, the operating |
| 6626 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6627 | static HCRYPTPROV hCryptProv = 0; |
| 6628 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6629 | static PyObject* |
| 6630 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6631 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6632 | int howMany; |
| 6633 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6634 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6635 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6636 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6637 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6638 | if (howMany < 0) |
| 6639 | return PyErr_Format(PyExc_ValueError, |
| 6640 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6641 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6642 | if (hCryptProv == 0) { |
| 6643 | HINSTANCE hAdvAPI32 = NULL; |
| 6644 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6645 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6646 | /* Obtain handle to the DLL containing CryptoAPI |
| 6647 | This should not fail */ |
| 6648 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6649 | if(hAdvAPI32 == NULL) |
| 6650 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6651 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6652 | /* Obtain pointers to the CryptoAPI functions |
| 6653 | This will fail on some early versions of Win95 */ |
| 6654 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6655 | hAdvAPI32, |
| 6656 | "CryptAcquireContextA"); |
| 6657 | if (pCryptAcquireContext == NULL) |
| 6658 | return PyErr_Format(PyExc_NotImplementedError, |
| 6659 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6660 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6661 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6662 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6663 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6664 | return PyErr_Format(PyExc_NotImplementedError, |
| 6665 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6666 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6667 | /* Acquire context */ |
| 6668 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6669 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6670 | return win32_error("CryptAcquireContext", NULL); |
| 6671 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6672 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6673 | /* Allocate bytes */ |
Guido van Rossum | 1898084 | 2007-11-21 21:53:11 +0000 | [diff] [blame] | 6674 | result = PyString_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6675 | if (result != NULL) { |
| 6676 | /* Get random data */ |
| 6677 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Guido van Rossum | 1898084 | 2007-11-21 21:53:11 +0000 | [diff] [blame] | 6678 | PyString_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6679 | Py_DECREF(result); |
| 6680 | return win32_error("CryptGenRandom", NULL); |
| 6681 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6682 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6683 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6684 | } |
| 6685 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6686 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6687 | PyDoc_STRVAR(device_encoding__doc__, |
| 6688 | "device_encoding(fd) -> str\n\n\ |
| 6689 | Return a string describing the encoding of the device\n\ |
| 6690 | if the output is a terminal; else return None."); |
| 6691 | |
| 6692 | static PyObject * |
| 6693 | device_encoding(PyObject *self, PyObject *args) |
| 6694 | { |
| 6695 | int fd; |
| 6696 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6697 | return NULL; |
| 6698 | if (!isatty(fd)) { |
| 6699 | Py_INCREF(Py_None); |
| 6700 | return Py_None; |
| 6701 | } |
| 6702 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6703 | if (fd == 0) { |
| 6704 | char buf[100]; |
| 6705 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6706 | return PyUnicode_FromString(buf); |
| 6707 | } |
| 6708 | if (fd == 1 || fd == 2) { |
| 6709 | char buf[100]; |
| 6710 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6711 | return PyUnicode_FromString(buf); |
| 6712 | } |
| 6713 | #elif defined(CODESET) |
| 6714 | { |
| 6715 | char *codeset = nl_langinfo(CODESET); |
| 6716 | if (codeset) |
| 6717 | return PyUnicode_FromString(codeset); |
| 6718 | } |
| 6719 | #endif |
| 6720 | Py_INCREF(Py_None); |
| 6721 | return Py_None; |
| 6722 | } |
| 6723 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6724 | #ifdef __VMS |
| 6725 | /* Use openssl random routine */ |
| 6726 | #include <openssl/rand.h> |
| 6727 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6728 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6729 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6730 | |
| 6731 | static PyObject* |
| 6732 | vms_urandom(PyObject *self, PyObject *args) |
| 6733 | { |
| 6734 | int howMany; |
| 6735 | PyObject* result; |
| 6736 | |
| 6737 | /* Read arguments */ |
| 6738 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6739 | return NULL; |
| 6740 | if (howMany < 0) |
| 6741 | return PyErr_Format(PyExc_ValueError, |
| 6742 | "negative argument not allowed"); |
| 6743 | |
| 6744 | /* Allocate bytes */ |
Guido van Rossum | 1898084 | 2007-11-21 21:53:11 +0000 | [diff] [blame] | 6745 | result = PyString_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6746 | if (result != NULL) { |
| 6747 | /* Get random data */ |
| 6748 | if (RAND_pseudo_bytes((unsigned char*) |
Guido van Rossum | 1898084 | 2007-11-21 21:53:11 +0000 | [diff] [blame] | 6749 | PyString_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6750 | howMany) < 0) { |
| 6751 | Py_DECREF(result); |
| 6752 | return PyErr_Format(PyExc_ValueError, |
| 6753 | "RAND_pseudo_bytes"); |
| 6754 | } |
| 6755 | } |
| 6756 | return result; |
| 6757 | } |
| 6758 | #endif |
| 6759 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6760 | static PyMethodDef posix_methods[] = { |
| 6761 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6762 | #ifdef HAVE_TTYNAME |
| 6763 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6764 | #endif |
| 6765 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6766 | #ifdef HAVE_CHFLAGS |
| 6767 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 6768 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6769 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 6770 | #ifdef HAVE_FCHMOD |
| 6771 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
| 6772 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6773 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6774 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6775 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 6776 | #ifdef HAVE_LCHMOD |
| 6777 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
| 6778 | #endif /* HAVE_LCHMOD */ |
| 6779 | #ifdef HAVE_FCHOWN |
| 6780 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
| 6781 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6782 | #ifdef HAVE_LCHFLAGS |
| 6783 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 6784 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6785 | #ifdef HAVE_LCHOWN |
| 6786 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6787 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6788 | #ifdef HAVE_CHROOT |
| 6789 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6790 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6791 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6792 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6793 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6794 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6795 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6796 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6797 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6798 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6799 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6800 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6801 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6802 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6803 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6804 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6805 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6806 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6807 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6808 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6809 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6810 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6811 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6812 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6813 | {"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] | 6814 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6815 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6816 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6817 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6818 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6819 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6820 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6821 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6822 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6823 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6824 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6825 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6826 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6827 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6828 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6829 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6830 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6831 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6832 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6833 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6834 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6835 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6836 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6837 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6838 | #if defined(PYOS_OS2) |
| 6839 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 6840 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 6841 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6842 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6843 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6844 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6845 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6846 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6847 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6848 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6849 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6850 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6851 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6852 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6853 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6854 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6855 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6856 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6857 | #endif /* HAVE_GETEGID */ |
| 6858 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6859 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6860 | #endif /* HAVE_GETEUID */ |
| 6861 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6862 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6863 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6864 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6865 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6866 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6867 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6868 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6869 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6870 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6871 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6872 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6873 | #endif /* HAVE_GETPPID */ |
| 6874 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6875 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6876 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6877 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6878 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6879 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6880 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6881 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6882 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6883 | #ifdef HAVE_KILLPG |
| 6884 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6885 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6886 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6887 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6888 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 6889 | #ifdef MS_WINDOWS |
| 6890 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 6891 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6892 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6893 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6894 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6895 | #ifdef HAVE_SETEUID |
| 6896 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6897 | #endif /* HAVE_SETEUID */ |
| 6898 | #ifdef HAVE_SETEGID |
| 6899 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6900 | #endif /* HAVE_SETEGID */ |
| 6901 | #ifdef HAVE_SETREUID |
| 6902 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6903 | #endif /* HAVE_SETREUID */ |
| 6904 | #ifdef HAVE_SETREGID |
| 6905 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6906 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6907 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6908 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6909 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6910 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6911 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6912 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6913 | #ifdef HAVE_GETPGID |
| 6914 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 6915 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6916 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6917 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6918 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6919 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6920 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6921 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6922 | #ifdef HAVE_WAIT3 |
| 6923 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 6924 | #endif /* HAVE_WAIT3 */ |
| 6925 | #ifdef HAVE_WAIT4 |
| 6926 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 6927 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6928 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6929 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6930 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6931 | #ifdef HAVE_GETSID |
| 6932 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 6933 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6934 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6935 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6936 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6937 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6938 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6939 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6940 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6941 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6942 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6943 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6944 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6945 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6946 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6947 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6948 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6949 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6950 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6951 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6952 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6953 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6954 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6955 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6956 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6957 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6958 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6959 | #endif |
| 6960 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6961 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6962 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6963 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6964 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 6965 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6966 | #ifdef HAVE_DEVICE_MACROS |
| 6967 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 6968 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 6969 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 6970 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6971 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6972 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6973 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6974 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6975 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6976 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6977 | #ifdef HAVE_UNSETENV |
| 6978 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6979 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6980 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6981 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6982 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6983 | #ifdef HAVE_FCHDIR |
| 6984 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 6985 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6986 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6987 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6988 | #endif |
| 6989 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6990 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6991 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6992 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6993 | #ifdef WCOREDUMP |
| 6994 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 6995 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6996 | #ifdef WIFCONTINUED |
| 6997 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 6998 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6999 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7000 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7001 | #endif /* WIFSTOPPED */ |
| 7002 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7003 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7004 | #endif /* WIFSIGNALED */ |
| 7005 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7006 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7007 | #endif /* WIFEXITED */ |
| 7008 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7009 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7010 | #endif /* WEXITSTATUS */ |
| 7011 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7012 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7013 | #endif /* WTERMSIG */ |
| 7014 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7015 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7016 | #endif /* WSTOPSIG */ |
| 7017 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7018 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7019 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7020 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7021 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7022 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7023 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7024 | #ifdef HAVE_CONFSTR |
| 7025 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7026 | #endif |
| 7027 | #ifdef HAVE_SYSCONF |
| 7028 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7029 | #endif |
| 7030 | #ifdef HAVE_FPATHCONF |
| 7031 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7032 | #endif |
| 7033 | #ifdef HAVE_PATHCONF |
| 7034 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7035 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7036 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7037 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7038 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7039 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7040 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7041 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7042 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7043 | #ifdef MS_WINDOWS |
| 7044 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7045 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7046 | #ifdef __VMS |
| 7047 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 7048 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7049 | {NULL, NULL} /* Sentinel */ |
| 7050 | }; |
| 7051 | |
| 7052 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7053 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7054 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7055 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7056 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7057 | } |
| 7058 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7059 | #if defined(PYOS_OS2) |
| 7060 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7061 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7062 | { |
| 7063 | APIRET rc; |
| 7064 | ULONG values[QSV_MAX+1]; |
| 7065 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7066 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7067 | |
| 7068 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7069 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7070 | Py_END_ALLOW_THREADS |
| 7071 | |
| 7072 | if (rc != NO_ERROR) { |
| 7073 | os2_error(rc); |
| 7074 | return -1; |
| 7075 | } |
| 7076 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7077 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7078 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7079 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7080 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7081 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7082 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7083 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7084 | |
| 7085 | switch (values[QSV_VERSION_MINOR]) { |
| 7086 | case 0: ver = "2.00"; break; |
| 7087 | case 10: ver = "2.10"; break; |
| 7088 | case 11: ver = "2.11"; break; |
| 7089 | case 30: ver = "3.00"; break; |
| 7090 | case 40: ver = "4.00"; break; |
| 7091 | case 50: ver = "5.00"; break; |
| 7092 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7093 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7094 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7095 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7096 | ver = &tmp[0]; |
| 7097 | } |
| 7098 | |
| 7099 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7100 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7101 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7102 | |
| 7103 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7104 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7105 | tmp[1] = ':'; |
| 7106 | tmp[2] = '\0'; |
| 7107 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7108 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7109 | } |
| 7110 | #endif |
| 7111 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7112 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7113 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7114 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7115 | #ifdef F_OK |
| 7116 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7117 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7118 | #ifdef R_OK |
| 7119 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7120 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7121 | #ifdef W_OK |
| 7122 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7123 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7124 | #ifdef X_OK |
| 7125 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7126 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7127 | #ifdef NGROUPS_MAX |
| 7128 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7129 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7130 | #ifdef TMP_MAX |
| 7131 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7132 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7133 | #ifdef WCONTINUED |
| 7134 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7135 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7136 | #ifdef WNOHANG |
| 7137 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7138 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7139 | #ifdef WUNTRACED |
| 7140 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7141 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7142 | #ifdef O_RDONLY |
| 7143 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7144 | #endif |
| 7145 | #ifdef O_WRONLY |
| 7146 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7147 | #endif |
| 7148 | #ifdef O_RDWR |
| 7149 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7150 | #endif |
| 7151 | #ifdef O_NDELAY |
| 7152 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7153 | #endif |
| 7154 | #ifdef O_NONBLOCK |
| 7155 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7156 | #endif |
| 7157 | #ifdef O_APPEND |
| 7158 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7159 | #endif |
| 7160 | #ifdef O_DSYNC |
| 7161 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7162 | #endif |
| 7163 | #ifdef O_RSYNC |
| 7164 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7165 | #endif |
| 7166 | #ifdef O_SYNC |
| 7167 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7168 | #endif |
| 7169 | #ifdef O_NOCTTY |
| 7170 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7171 | #endif |
| 7172 | #ifdef O_CREAT |
| 7173 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7174 | #endif |
| 7175 | #ifdef O_EXCL |
| 7176 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7177 | #endif |
| 7178 | #ifdef O_TRUNC |
| 7179 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7180 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7181 | #ifdef O_BINARY |
| 7182 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7183 | #endif |
| 7184 | #ifdef O_TEXT |
| 7185 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7186 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7187 | #ifdef O_LARGEFILE |
| 7188 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7189 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7190 | #ifdef O_SHLOCK |
| 7191 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7192 | #endif |
| 7193 | #ifdef O_EXLOCK |
| 7194 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7195 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7196 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7197 | /* MS Windows */ |
| 7198 | #ifdef O_NOINHERIT |
| 7199 | /* Don't inherit in child processes. */ |
| 7200 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7201 | #endif |
| 7202 | #ifdef _O_SHORT_LIVED |
| 7203 | /* Optimize for short life (keep in memory). */ |
| 7204 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7205 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7206 | #endif |
| 7207 | #ifdef O_TEMPORARY |
| 7208 | /* Automatically delete when last handle is closed. */ |
| 7209 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7210 | #endif |
| 7211 | #ifdef O_RANDOM |
| 7212 | /* Optimize for random access. */ |
| 7213 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7214 | #endif |
| 7215 | #ifdef O_SEQUENTIAL |
| 7216 | /* Optimize for sequential access. */ |
| 7217 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7218 | #endif |
| 7219 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7220 | /* GNU extensions. */ |
| 7221 | #ifdef O_DIRECT |
| 7222 | /* Direct disk access. */ |
| 7223 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7224 | #endif |
| 7225 | #ifdef O_DIRECTORY |
| 7226 | /* Must be a directory. */ |
| 7227 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7228 | #endif |
| 7229 | #ifdef O_NOFOLLOW |
| 7230 | /* Do not follow links. */ |
| 7231 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7232 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7233 | #ifdef O_NOATIME |
| 7234 | /* Do not update the access time. */ |
| 7235 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
| 7236 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7237 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7238 | /* These come from sysexits.h */ |
| 7239 | #ifdef EX_OK |
| 7240 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7241 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7242 | #ifdef EX_USAGE |
| 7243 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7244 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7245 | #ifdef EX_DATAERR |
| 7246 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7247 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7248 | #ifdef EX_NOINPUT |
| 7249 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7250 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7251 | #ifdef EX_NOUSER |
| 7252 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7253 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7254 | #ifdef EX_NOHOST |
| 7255 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7256 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7257 | #ifdef EX_UNAVAILABLE |
| 7258 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7259 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7260 | #ifdef EX_SOFTWARE |
| 7261 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7262 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7263 | #ifdef EX_OSERR |
| 7264 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7265 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7266 | #ifdef EX_OSFILE |
| 7267 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7268 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7269 | #ifdef EX_CANTCREAT |
| 7270 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7271 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7272 | #ifdef EX_IOERR |
| 7273 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7274 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7275 | #ifdef EX_TEMPFAIL |
| 7276 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7277 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7278 | #ifdef EX_PROTOCOL |
| 7279 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7280 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7281 | #ifdef EX_NOPERM |
| 7282 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7283 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7284 | #ifdef EX_CONFIG |
| 7285 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7286 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7287 | #ifdef EX_NOTFOUND |
| 7288 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7289 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7290 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7291 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7292 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7293 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7294 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7295 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7296 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7297 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7298 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7299 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7300 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7301 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7302 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7303 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7304 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7305 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7306 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7307 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7308 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7309 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7310 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7311 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7312 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7313 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7314 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7315 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7316 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7317 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7318 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7319 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7320 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7321 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7322 | #if defined(PYOS_OS2) |
| 7323 | if (insertvalues(d)) return -1; |
| 7324 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7325 | return 0; |
| 7326 | } |
| 7327 | |
| 7328 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7329 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7330 | #define INITFUNC initnt |
| 7331 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7332 | |
| 7333 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7334 | #define INITFUNC initos2 |
| 7335 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7336 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7337 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7338 | #define INITFUNC initposix |
| 7339 | #define MODNAME "posix" |
| 7340 | #endif |
| 7341 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7342 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7343 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7344 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7345 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7346 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7347 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7348 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7349 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7350 | if (m == NULL) |
| 7351 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7352 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7353 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7354 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7355 | Py_XINCREF(v); |
| 7356 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7357 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7358 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7359 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7360 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7361 | return; |
| 7362 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7363 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7364 | return; |
| 7365 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7366 | Py_INCREF(PyExc_OSError); |
| 7367 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7368 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7369 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7370 | if (posix_putenv_garbage == NULL) |
| 7371 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7372 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7373 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7374 | if (!initialized) { |
| 7375 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7376 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7377 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7378 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7379 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7380 | structseq_new = StatResultType.tp_new; |
| 7381 | StatResultType.tp_new = statresult_new; |
| 7382 | |
| 7383 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7384 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 7385 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7386 | Py_INCREF((PyObject*) &StatResultType); |
| 7387 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7388 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7389 | PyModule_AddObject(m, "statvfs_result", |
| 7390 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7391 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7392 | |
| 7393 | #ifdef __APPLE__ |
| 7394 | /* |
| 7395 | * Step 2 of weak-linking support on Mac OS X. |
| 7396 | * |
| 7397 | * The code below removes functions that are not available on the |
| 7398 | * currently active platform. |
| 7399 | * |
| 7400 | * This block allow one to use a python binary that was build on |
| 7401 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7402 | * OSX 10.4. |
| 7403 | */ |
| 7404 | #ifdef HAVE_FSTATVFS |
| 7405 | if (fstatvfs == NULL) { |
| 7406 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 7407 | return; |
| 7408 | } |
| 7409 | } |
| 7410 | #endif /* HAVE_FSTATVFS */ |
| 7411 | |
| 7412 | #ifdef HAVE_STATVFS |
| 7413 | if (statvfs == NULL) { |
| 7414 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 7415 | return; |
| 7416 | } |
| 7417 | } |
| 7418 | #endif /* HAVE_STATVFS */ |
| 7419 | |
| 7420 | # ifdef HAVE_LCHOWN |
| 7421 | if (lchown == NULL) { |
| 7422 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 7423 | return; |
| 7424 | } |
| 7425 | } |
| 7426 | #endif /* HAVE_LCHOWN */ |
| 7427 | |
| 7428 | |
| 7429 | #endif /* __APPLE__ */ |
| 7430 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7431 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7432 | |
| 7433 | #ifdef __cplusplus |
| 7434 | } |
| 7435 | #endif |