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') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 428 | PyObject *v = PyBytes_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') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 434 | PyObject *v = PyBytes_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. |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 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; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 1909 | long uid, gid; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1910 | int res; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 1911 | if (!PyArg_ParseTuple(args, "etll:chown", |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 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 | { |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 1984 | int bufsize_incr = 1024; |
| 1985 | int bufsize = 0; |
| 1986 | char *tmpbuf = NULL; |
| 1987 | char *res = NULL; |
| 1988 | PyObject *dynamic_return; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1989 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1990 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 1991 | do { |
| 1992 | bufsize = bufsize + bufsize_incr; |
| 1993 | tmpbuf = malloc(bufsize); |
| 1994 | if (tmpbuf == NULL) { |
| 1995 | break; |
| 1996 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1997 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 1998 | res = _getcwd2(tmpbuf, bufsize); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1999 | #else |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 2000 | res = getcwd(tmpbuf, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2001 | #endif |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 2002 | |
| 2003 | if (res == NULL) { |
| 2004 | free(tmpbuf); |
| 2005 | } |
| 2006 | } while ((res == NULL) && (errno == ERANGE)); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2007 | Py_END_ALLOW_THREADS |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 2008 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2009 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2010 | return posix_error(); |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 2011 | |
| 2012 | dynamic_return = PyUnicode_FromString(tmpbuf); |
| 2013 | free(tmpbuf); |
| 2014 | |
| 2015 | return dynamic_return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2016 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2017 | |
| 2018 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 2019 | "getcwdu() -> path\n\n\ |
| 2020 | Return a unicode string representing the current working directory."); |
| 2021 | |
| 2022 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2023 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2024 | { |
| 2025 | char buf[1026]; |
| 2026 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2027 | |
| 2028 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2029 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2030 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2031 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2032 | wchar_t *wbuf2 = wbuf; |
| 2033 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2034 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2035 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2036 | /* If the buffer is large enough, len does not include the |
| 2037 | terminating \0. If the buffer is too small, len includes |
| 2038 | the space needed for the terminator. */ |
| 2039 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2040 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2041 | if (wbuf2) |
| 2042 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2043 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2044 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2045 | if (!wbuf2) { |
| 2046 | PyErr_NoMemory(); |
| 2047 | return NULL; |
| 2048 | } |
| 2049 | if (!len) { |
| 2050 | if (wbuf2 != wbuf) free(wbuf2); |
| 2051 | return win32_error("getcwdu", NULL); |
| 2052 | } |
| 2053 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2054 | if (wbuf2 != wbuf) free(wbuf2); |
| 2055 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2056 | } |
| 2057 | #endif |
| 2058 | |
| 2059 | Py_BEGIN_ALLOW_THREADS |
| 2060 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2061 | res = _getcwd2(buf, sizeof buf); |
| 2062 | #else |
| 2063 | res = getcwd(buf, sizeof buf); |
| 2064 | #endif |
| 2065 | Py_END_ALLOW_THREADS |
| 2066 | if (res == NULL) |
| 2067 | return posix_error(); |
| 2068 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 2069 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2070 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2071 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2072 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2073 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2074 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2075 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2076 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2077 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2078 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2079 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2080 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2081 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2082 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2083 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2084 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2086 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2087 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2088 | Return a list containing the names of the entries in the directory.\n\ |
| 2089 | \n\ |
| 2090 | path: path of directory to list\n\ |
| 2091 | \n\ |
| 2092 | 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] | 2093 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2094 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2095 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2096 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2097 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2098 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2099 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2100 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2101 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2102 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2103 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2104 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2105 | WIN32_FIND_DATA FileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2106 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2107 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2108 | 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] | 2109 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2110 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2111 | /* If on wide-character-capable OS see if argument |
| 2112 | is Unicode and if so use wide API. */ |
| 2113 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2114 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2115 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2116 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2117 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2118 | Py_UNICODE wch; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2119 | /* Overallocate for \\*.*\0 */ |
| 2120 | len = PyUnicode_GET_SIZE(po); |
| 2121 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2122 | if (!wnamebuf) { |
| 2123 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2124 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2125 | } |
| 2126 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2127 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 2128 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2129 | wnamebuf[len++] = L'\\'; |
| 2130 | wcscpy(wnamebuf + len, L"*.*"); |
| 2131 | if ((d = PyList_New(0)) == NULL) { |
| 2132 | free(wnamebuf); |
| 2133 | return NULL; |
| 2134 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2135 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2136 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2137 | int error = GetLastError(); |
| 2138 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2139 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2140 | return d; |
| 2141 | } |
| 2142 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2143 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2144 | free(wnamebuf); |
| 2145 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2146 | } |
| 2147 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2148 | /* Skip over . and .. */ |
| 2149 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2150 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2151 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2152 | if (v == NULL) { |
| 2153 | Py_DECREF(d); |
| 2154 | d = NULL; |
| 2155 | break; |
| 2156 | } |
| 2157 | if (PyList_Append(d, v) != 0) { |
| 2158 | Py_DECREF(v); |
| 2159 | Py_DECREF(d); |
| 2160 | d = NULL; |
| 2161 | break; |
| 2162 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2163 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2164 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2165 | Py_BEGIN_ALLOW_THREADS |
| 2166 | result = FindNextFileW(hFindFile, &wFileData); |
| 2167 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2168 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2169 | it got to the end of the directory. */ |
| 2170 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2171 | Py_DECREF(d); |
| 2172 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2173 | FindClose(hFindFile); |
| 2174 | free(wnamebuf); |
| 2175 | return NULL; |
| 2176 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2177 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2178 | |
| 2179 | if (FindClose(hFindFile) == FALSE) { |
| 2180 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2181 | win32_error_unicode("FindClose", wnamebuf); |
| 2182 | free(wnamebuf); |
| 2183 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2184 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2185 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2186 | return d; |
| 2187 | } |
| 2188 | /* Drop the argument parsing error as narrow strings |
| 2189 | are also valid. */ |
| 2190 | PyErr_Clear(); |
| 2191 | } |
| 2192 | #endif |
| 2193 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2194 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2195 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2196 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2197 | if (len > 0) { |
| 2198 | char ch = namebuf[len-1]; |
| 2199 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2200 | namebuf[len++] = '/'; |
| 2201 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2202 | strcpy(namebuf + len, "*.*"); |
| 2203 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2204 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2205 | return NULL; |
| 2206 | |
| 2207 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2208 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2209 | int error = GetLastError(); |
| 2210 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2211 | return d; |
| 2212 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2213 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2214 | } |
| 2215 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2216 | /* Skip over . and .. */ |
| 2217 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2218 | strcmp(FileData.cFileName, "..") != 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2219 | v = PyBytes_FromString(FileData.cFileName); |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2220 | if (v == NULL) { |
| 2221 | Py_DECREF(d); |
| 2222 | d = NULL; |
| 2223 | break; |
| 2224 | } |
| 2225 | if (PyList_Append(d, v) != 0) { |
| 2226 | Py_DECREF(v); |
| 2227 | Py_DECREF(d); |
| 2228 | d = NULL; |
| 2229 | break; |
| 2230 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2231 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2232 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2233 | Py_BEGIN_ALLOW_THREADS |
| 2234 | result = FindNextFile(hFindFile, &FileData); |
| 2235 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2236 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2237 | it got to the end of the directory. */ |
| 2238 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2239 | Py_DECREF(d); |
| 2240 | win32_error("FindNextFile", namebuf); |
| 2241 | FindClose(hFindFile); |
| 2242 | return NULL; |
| 2243 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2244 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2245 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2246 | if (FindClose(hFindFile) == FALSE) { |
| 2247 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2248 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2249 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2250 | |
| 2251 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2252 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2253 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2254 | |
| 2255 | #ifndef MAX_PATH |
| 2256 | #define MAX_PATH CCHMAXPATH |
| 2257 | #endif |
| 2258 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2259 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2260 | PyObject *d, *v; |
| 2261 | char namebuf[MAX_PATH+5]; |
| 2262 | HDIR hdir = 1; |
| 2263 | ULONG srchcnt = 1; |
| 2264 | FILEFINDBUF3 ep; |
| 2265 | APIRET rc; |
| 2266 | |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 2267 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 2268 | Py_FileSystemDefaultEncoding, &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2269 | return NULL; |
| 2270 | if (len >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2271 | PyMem_Free(name); |
| 2272 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2273 | return NULL; |
| 2274 | } |
| 2275 | strcpy(namebuf, name); |
| 2276 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2277 | if (*pt == ALTSEP) |
| 2278 | *pt = SEP; |
| 2279 | if (namebuf[len-1] != SEP) |
| 2280 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2281 | strcpy(namebuf + len, "*.*"); |
| 2282 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2283 | if ((d = PyList_New(0)) == NULL) { |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2284 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2285 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2286 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2287 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2288 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2289 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2290 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2291 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2292 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2293 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2294 | |
| 2295 | if (rc != NO_ERROR) { |
| 2296 | errno = ENOENT; |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2297 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2300 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2301 | do { |
| 2302 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2303 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2304 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2305 | |
| 2306 | strcpy(namebuf, ep.achName); |
| 2307 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2308 | /* Leave Case of Name Alone -- In Native Form */ |
| 2309 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2310 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2311 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2312 | if (v == NULL) { |
| 2313 | Py_DECREF(d); |
| 2314 | d = NULL; |
| 2315 | break; |
| 2316 | } |
| 2317 | if (PyList_Append(d, v) != 0) { |
| 2318 | Py_DECREF(v); |
| 2319 | Py_DECREF(d); |
| 2320 | d = NULL; |
| 2321 | break; |
| 2322 | } |
| 2323 | Py_DECREF(v); |
| 2324 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2325 | } |
| 2326 | |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2327 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2328 | return d; |
| 2329 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2330 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2331 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2332 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2333 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2334 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2335 | int arg_is_unicode = 1; |
| 2336 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2337 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2338 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2339 | arg_is_unicode = 0; |
| 2340 | PyErr_Clear(); |
| 2341 | } |
| 2342 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2343 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2344 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2345 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2346 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2347 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2348 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2349 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2350 | return NULL; |
| 2351 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2352 | for (;;) { |
| 2353 | Py_BEGIN_ALLOW_THREADS |
| 2354 | ep = readdir(dirp); |
| 2355 | Py_END_ALLOW_THREADS |
| 2356 | if (ep == NULL) |
| 2357 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2358 | if (ep->d_name[0] == '.' && |
| 2359 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2360 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2361 | continue; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2362 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2363 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2364 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2365 | d = NULL; |
| 2366 | break; |
| 2367 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2368 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2369 | PyObject *w; |
| 2370 | |
| 2371 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2372 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2373 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2374 | if (w != NULL) { |
| 2375 | Py_DECREF(v); |
| 2376 | v = w; |
| 2377 | } |
| 2378 | else { |
| 2379 | /* fall back to the original byte string, as |
| 2380 | discussed in patch #683592 */ |
| 2381 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2382 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2383 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2384 | if (PyList_Append(d, v) != 0) { |
| 2385 | Py_DECREF(v); |
| 2386 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2387 | d = NULL; |
| 2388 | break; |
| 2389 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2390 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2391 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2392 | if (errno != 0 && d != NULL) { |
| 2393 | /* readdir() returned NULL and set errno */ |
| 2394 | closedir(dirp); |
| 2395 | Py_DECREF(d); |
| 2396 | return posix_error_with_allocated_filename(name); |
| 2397 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2398 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2399 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2400 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2401 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2402 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2403 | #endif /* which OS */ |
| 2404 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2405 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2406 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2407 | /* A helper function for abspath on win32 */ |
| 2408 | static PyObject * |
| 2409 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2410 | { |
| 2411 | /* assume encoded strings wont more than double no of chars */ |
| 2412 | char inbuf[MAX_PATH*2]; |
| 2413 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2414 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2415 | char outbuf[MAX_PATH*2]; |
| 2416 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2417 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2418 | if (unicode_file_names()) { |
| 2419 | PyUnicodeObject *po; |
| 2420 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2421 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2422 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2423 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2424 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2425 | woutbuf, &wtemp)) |
| 2426 | return win32_error("GetFullPathName", ""); |
| 2427 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2428 | } |
| 2429 | /* Drop the argument parsing error as narrow strings |
| 2430 | are also valid. */ |
| 2431 | PyErr_Clear(); |
| 2432 | } |
| 2433 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2434 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2435 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2436 | &insize)) |
| 2437 | return NULL; |
| 2438 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2439 | outbuf, &temp)) |
| 2440 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2441 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2442 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2443 | Py_FileSystemDefaultEncoding, NULL); |
| 2444 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2445 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2446 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2447 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2448 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2449 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2450 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2451 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2452 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2453 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2454 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2455 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2456 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2457 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2458 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2459 | |
| 2460 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2461 | if (unicode_file_names()) { |
| 2462 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2463 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2464 | Py_BEGIN_ALLOW_THREADS |
| 2465 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2466 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2467 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2468 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2469 | if (!res) |
| 2470 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2471 | Py_INCREF(Py_None); |
| 2472 | return Py_None; |
| 2473 | } |
| 2474 | /* Drop the argument parsing error as narrow strings |
| 2475 | are also valid. */ |
| 2476 | PyErr_Clear(); |
| 2477 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2478 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2479 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2480 | return NULL; |
| 2481 | Py_BEGIN_ALLOW_THREADS |
| 2482 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2483 | it is a simple dereference. */ |
| 2484 | res = CreateDirectoryA(path, NULL); |
| 2485 | Py_END_ALLOW_THREADS |
| 2486 | if (!res) { |
| 2487 | win32_error("mkdir", path); |
| 2488 | PyMem_Free(path); |
| 2489 | return NULL; |
| 2490 | } |
| 2491 | PyMem_Free(path); |
| 2492 | Py_INCREF(Py_None); |
| 2493 | return Py_None; |
| 2494 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2495 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2496 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2497 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2498 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2499 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2500 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2501 | res = mkdir(path); |
| 2502 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2503 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2504 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2505 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2506 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2507 | return posix_error_with_allocated_filename(path); |
| 2508 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2509 | Py_INCREF(Py_None); |
| 2510 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2511 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2514 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2515 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2516 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2517 | #include <sys/resource.h> |
| 2518 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2519 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2520 | |
| 2521 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2522 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2523 | "nice(inc) -> new_priority\n\n\ |
| 2524 | 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] | 2525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2526 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2527 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2528 | { |
| 2529 | int increment, value; |
| 2530 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2531 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2532 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2533 | |
| 2534 | /* There are two flavours of 'nice': one that returns the new |
| 2535 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2536 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2537 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2538 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2539 | If we are of the nice family that returns the new priority, we |
| 2540 | need to clear errno before the call, and check if errno is filled |
| 2541 | before calling posix_error() on a returnvalue of -1, because the |
| 2542 | -1 may be the actual new priority! */ |
| 2543 | |
| 2544 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2545 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2546 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2547 | if (value == 0) |
| 2548 | value = getpriority(PRIO_PROCESS, 0); |
| 2549 | #endif |
| 2550 | if (value == -1 && errno != 0) |
| 2551 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2552 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2553 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2554 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2555 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2557 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2558 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2559 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2560 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2561 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2562 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2563 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2564 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2565 | PyObject *o1, *o2; |
| 2566 | char *p1, *p2; |
| 2567 | BOOL result; |
| 2568 | if (unicode_file_names()) { |
| 2569 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2570 | convert_to_unicode, &o1, |
| 2571 | convert_to_unicode, &o2)) |
| 2572 | PyErr_Clear(); |
| 2573 | else { |
| 2574 | Py_BEGIN_ALLOW_THREADS |
| 2575 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2576 | PyUnicode_AsUnicode(o2)); |
| 2577 | Py_END_ALLOW_THREADS |
| 2578 | Py_DECREF(o1); |
| 2579 | Py_DECREF(o2); |
| 2580 | if (!result) |
| 2581 | return win32_error("rename", NULL); |
| 2582 | Py_INCREF(Py_None); |
| 2583 | return Py_None; |
| 2584 | } |
| 2585 | } |
| 2586 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2587 | return NULL; |
| 2588 | Py_BEGIN_ALLOW_THREADS |
| 2589 | result = MoveFileA(p1, p2); |
| 2590 | Py_END_ALLOW_THREADS |
| 2591 | if (!result) |
| 2592 | return win32_error("rename", NULL); |
| 2593 | Py_INCREF(Py_None); |
| 2594 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2595 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2596 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2597 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2601 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2602 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2603 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2604 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2605 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2606 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2607 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2608 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2609 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2610 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2611 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2612 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2616 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2617 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2618 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2619 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2620 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2621 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2622 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2623 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2624 | 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] | 2625 | #else |
| 2626 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2627 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2630 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2631 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2632 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2633 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2634 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2635 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2636 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2637 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2638 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2639 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2640 | #ifdef MS_WINDOWS |
| 2641 | wchar_t *command; |
| 2642 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2643 | return NULL; |
| 2644 | #else |
| 2645 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2646 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2647 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2648 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2649 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2650 | #ifdef MS_WINDOWS |
| 2651 | sts = _wsystem(command); |
| 2652 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2653 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2654 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2655 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2656 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2657 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2658 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2659 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2661 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2662 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2663 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2664 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2665 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2666 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2667 | { |
| 2668 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2669 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2670 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2671 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2672 | if (i < 0) |
| 2673 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2674 | return PyLong_FromLong((long)i); |
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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2678 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2679 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2680 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2682 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2683 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2684 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2685 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2686 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2687 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2688 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2689 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2690 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2691 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2692 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2693 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2696 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2697 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2698 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2699 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2700 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2701 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2702 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2703 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2704 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2705 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2706 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2707 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2708 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2709 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2710 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2711 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2712 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2713 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2714 | u.sysname, |
| 2715 | u.nodename, |
| 2716 | u.release, |
| 2717 | u.version, |
| 2718 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2719 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2720 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2721 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2722 | static int |
| 2723 | extract_time(PyObject *t, long* sec, long* usec) |
| 2724 | { |
| 2725 | long intval; |
| 2726 | if (PyFloat_Check(t)) { |
| 2727 | double tval = PyFloat_AsDouble(t); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2728 | 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] | 2729 | if (!intobj) |
| 2730 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2731 | intval = PyLong_AsLong(intobj); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2732 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2733 | if (intval == -1 && PyErr_Occurred()) |
| 2734 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2735 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2736 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2737 | if (*usec < 0) |
| 2738 | /* If rounding gave us a negative number, |
| 2739 | truncate. */ |
| 2740 | *usec = 0; |
| 2741 | return 0; |
| 2742 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2743 | intval = PyLong_AsLong(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2744 | if (intval == -1 && PyErr_Occurred()) |
| 2745 | return -1; |
| 2746 | *sec = intval; |
| 2747 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2748 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2749 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2750 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2751 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2752 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2753 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2754 | 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] | 2755 | 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] | 2756 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2757 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2758 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2759 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2760 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2761 | PyObject *arg; |
| 2762 | PyUnicodeObject *obwpath; |
| 2763 | wchar_t *wpath = NULL; |
| 2764 | char *apath = NULL; |
| 2765 | HANDLE hFile; |
| 2766 | long atimesec, mtimesec, ausec, musec; |
| 2767 | FILETIME atime, mtime; |
| 2768 | PyObject *result = NULL; |
| 2769 | |
| 2770 | if (unicode_file_names()) { |
| 2771 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2772 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2773 | Py_BEGIN_ALLOW_THREADS |
| 2774 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2775 | NULL, OPEN_EXISTING, |
| 2776 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2777 | Py_END_ALLOW_THREADS |
| 2778 | if (hFile == INVALID_HANDLE_VALUE) |
| 2779 | return win32_error_unicode("utime", wpath); |
| 2780 | } else |
| 2781 | /* Drop the argument parsing error as narrow strings |
| 2782 | are also valid. */ |
| 2783 | PyErr_Clear(); |
| 2784 | } |
| 2785 | if (!wpath) { |
| 2786 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2787 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2788 | return NULL; |
| 2789 | Py_BEGIN_ALLOW_THREADS |
| 2790 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2791 | NULL, OPEN_EXISTING, |
| 2792 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2793 | Py_END_ALLOW_THREADS |
| 2794 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2795 | win32_error("utime", apath); |
| 2796 | PyMem_Free(apath); |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | PyMem_Free(apath); |
| 2800 | } |
| 2801 | |
| 2802 | if (arg == Py_None) { |
| 2803 | SYSTEMTIME now; |
| 2804 | GetSystemTime(&now); |
| 2805 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2806 | !SystemTimeToFileTime(&now, &atime)) { |
| 2807 | win32_error("utime", NULL); |
| 2808 | goto done; |
| 2809 | } |
| 2810 | } |
| 2811 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2812 | PyErr_SetString(PyExc_TypeError, |
| 2813 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2814 | goto done; |
| 2815 | } |
| 2816 | else { |
| 2817 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2818 | &atimesec, &ausec) == -1) |
| 2819 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2820 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2821 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2822 | &mtimesec, &musec) == -1) |
| 2823 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2824 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2825 | } |
| 2826 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2827 | /* Avoid putting the file name into the error here, |
| 2828 | as that may confuse the user into believing that |
| 2829 | something is wrong with the file, when it also |
| 2830 | could be the time stamp that gives a problem. */ |
| 2831 | win32_error("utime", NULL); |
| 2832 | } |
| 2833 | Py_INCREF(Py_None); |
| 2834 | result = Py_None; |
| 2835 | done: |
| 2836 | CloseHandle(hFile); |
| 2837 | return result; |
| 2838 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2839 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2840 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2841 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2842 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2843 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2844 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2845 | #if defined(HAVE_UTIMES) |
| 2846 | struct timeval buf[2]; |
| 2847 | #define ATIME buf[0].tv_sec |
| 2848 | #define MTIME buf[1].tv_sec |
| 2849 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2850 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2851 | struct utimbuf buf; |
| 2852 | #define ATIME buf.actime |
| 2853 | #define MTIME buf.modtime |
| 2854 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2855 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2856 | time_t buf[2]; |
| 2857 | #define ATIME buf[0] |
| 2858 | #define MTIME buf[1] |
| 2859 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2860 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2861 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2862 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2863 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2864 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2865 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2866 | if (arg == Py_None) { |
| 2867 | /* optional time values not given */ |
| 2868 | Py_BEGIN_ALLOW_THREADS |
| 2869 | res = utime(path, NULL); |
| 2870 | Py_END_ALLOW_THREADS |
| 2871 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2872 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2873 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2874 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2875 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2876 | return NULL; |
| 2877 | } |
| 2878 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2879 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2880 | &atime, &ausec) == -1) { |
| 2881 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2882 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2883 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2884 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2885 | &mtime, &musec) == -1) { |
| 2886 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2887 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2888 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2889 | ATIME = atime; |
| 2890 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2891 | #ifdef HAVE_UTIMES |
| 2892 | buf[0].tv_usec = ausec; |
| 2893 | buf[1].tv_usec = musec; |
| 2894 | Py_BEGIN_ALLOW_THREADS |
| 2895 | res = utimes(path, buf); |
| 2896 | Py_END_ALLOW_THREADS |
| 2897 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2898 | Py_BEGIN_ALLOW_THREADS |
| 2899 | res = utime(path, UTIME_ARG); |
| 2900 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2901 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2902 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2903 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2904 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2905 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2906 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2907 | Py_INCREF(Py_None); |
| 2908 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2909 | #undef UTIME_ARG |
| 2910 | #undef ATIME |
| 2911 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2912 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2915 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2916 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2917 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2918 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2919 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2920 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2921 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2922 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2923 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2924 | { |
| 2925 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2926 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2927 | return NULL; |
| 2928 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2929 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2932 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2933 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2934 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2935 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2936 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2937 | for (i = 0; i < count; i++) |
| 2938 | PyMem_Free(array[i]); |
| 2939 | PyMem_DEL(array); |
| 2940 | } |
| 2941 | #endif |
| 2942 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2943 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2944 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2945 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2946 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2947 | Execute an executable path with arguments, replacing current process.\n\ |
| 2948 | \n\ |
| 2949 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2950 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2951 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2952 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2953 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2954 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2955 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2956 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2957 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2958 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2959 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2960 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2961 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2962 | argv is a list or tuple of strings. */ |
| 2963 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2964 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2965 | Py_FileSystemDefaultEncoding, |
| 2966 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2967 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2968 | if (PyList_Check(argv)) { |
| 2969 | argc = PyList_Size(argv); |
| 2970 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2971 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2972 | else if (PyTuple_Check(argv)) { |
| 2973 | argc = PyTuple_Size(argv); |
| 2974 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2975 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2976 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2977 | 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] | 2978 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2979 | return NULL; |
| 2980 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 2981 | if (argc < 1) { |
| 2982 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 2983 | PyMem_Free(path); |
| 2984 | return NULL; |
| 2985 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2987 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2988 | if (argvlist == NULL) { |
| 2989 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2990 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2991 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2992 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2993 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2994 | Py_FileSystemDefaultEncoding, |
| 2995 | &argvlist[i])) { |
| 2996 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2997 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2998 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2999 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3000 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3001 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3002 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3003 | } |
| 3004 | argvlist[argc] = NULL; |
| 3005 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3006 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3007 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3008 | /* If we get here it's definitely an error */ |
| 3009 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3010 | free_string_array(argvlist, argc); |
| 3011 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3012 | return posix_error(); |
| 3013 | } |
| 3014 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3015 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3016 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3017 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3018 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3019 | \n\ |
| 3020 | path: path of executable file\n\ |
| 3021 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3022 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3023 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3024 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3025 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3026 | { |
| 3027 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3028 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3029 | char **argvlist; |
| 3030 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3031 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3032 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3033 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3034 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3035 | |
| 3036 | /* execve has three arguments: (path, argv, env), where |
| 3037 | argv is a list or tuple of strings and env is a dictionary |
| 3038 | like posix.environ. */ |
| 3039 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3040 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 3041 | Py_FileSystemDefaultEncoding, |
| 3042 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3043 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3044 | if (PyList_Check(argv)) { |
| 3045 | argc = PyList_Size(argv); |
| 3046 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3047 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3048 | else if (PyTuple_Check(argv)) { |
| 3049 | argc = PyTuple_Size(argv); |
| 3050 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3051 | } |
| 3052 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3053 | PyErr_SetString(PyExc_TypeError, |
| 3054 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3055 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3056 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3057 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3058 | PyErr_SetString(PyExc_TypeError, |
| 3059 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3060 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3063 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3064 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3065 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3066 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3067 | } |
| 3068 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3069 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3070 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 3071 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3072 | &argvlist[i])) |
| 3073 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3074 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3075 | goto fail_1; |
| 3076 | } |
| 3077 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3078 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3079 | argvlist[argc] = NULL; |
| 3080 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3081 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3082 | if (i < 0) |
| 3083 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3084 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3085 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3086 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3087 | goto fail_1; |
| 3088 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3089 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3090 | keys = PyMapping_Keys(env); |
| 3091 | vals = PyMapping_Values(env); |
| 3092 | if (!keys || !vals) |
| 3093 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3094 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3095 | PyErr_SetString(PyExc_TypeError, |
| 3096 | "execve(): env.keys() or env.values() is not a list"); |
| 3097 | goto fail_2; |
| 3098 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3099 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3100 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3101 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3102 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3103 | |
| 3104 | key = PyList_GetItem(keys, pos); |
| 3105 | val = PyList_GetItem(vals, pos); |
| 3106 | if (!key || !val) |
| 3107 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3108 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3109 | if (!PyArg_Parse( |
| 3110 | key, |
| 3111 | "s;execve() arg 3 contains a non-string key", |
| 3112 | &k) || |
| 3113 | !PyArg_Parse( |
| 3114 | val, |
| 3115 | "s;execve() arg 3 contains a non-string value", |
| 3116 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3117 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3118 | goto fail_2; |
| 3119 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3120 | |
| 3121 | #if defined(PYOS_OS2) |
| 3122 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3123 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3124 | #endif |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3125 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3126 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3127 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3128 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3129 | goto fail_2; |
| 3130 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3131 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3132 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3133 | #if defined(PYOS_OS2) |
| 3134 | } |
| 3135 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3136 | } |
| 3137 | envlist[envc] = 0; |
| 3138 | |
| 3139 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3140 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3141 | /* If we get here it's definitely an error */ |
| 3142 | |
| 3143 | (void) posix_error(); |
| 3144 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3145 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3146 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3147 | PyMem_DEL(envlist[envc]); |
| 3148 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3149 | fail_1: |
| 3150 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3151 | Py_XDECREF(vals); |
| 3152 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3153 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3154 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3155 | return NULL; |
| 3156 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3157 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3158 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3159 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3160 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3161 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3162 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3163 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3164 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3165 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3166 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3167 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3168 | |
| 3169 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3170 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3171 | { |
| 3172 | char *path; |
| 3173 | PyObject *argv; |
| 3174 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3175 | int mode, i; |
| 3176 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3177 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3178 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3179 | |
| 3180 | /* spawnv has three arguments: (mode, path, argv), where |
| 3181 | argv is a list or tuple of strings. */ |
| 3182 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3183 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3184 | Py_FileSystemDefaultEncoding, |
| 3185 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3186 | return NULL; |
| 3187 | if (PyList_Check(argv)) { |
| 3188 | argc = PyList_Size(argv); |
| 3189 | getitem = PyList_GetItem; |
| 3190 | } |
| 3191 | else if (PyTuple_Check(argv)) { |
| 3192 | argc = PyTuple_Size(argv); |
| 3193 | getitem = PyTuple_GetItem; |
| 3194 | } |
| 3195 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3196 | PyErr_SetString(PyExc_TypeError, |
| 3197 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3198 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3199 | return NULL; |
| 3200 | } |
| 3201 | |
| 3202 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3203 | if (argvlist == NULL) { |
| 3204 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3205 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3206 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3207 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3208 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3209 | Py_FileSystemDefaultEncoding, |
| 3210 | &argvlist[i])) { |
| 3211 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3212 | PyErr_SetString( |
| 3213 | PyExc_TypeError, |
| 3214 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3215 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3216 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3217 | } |
| 3218 | } |
| 3219 | argvlist[argc] = NULL; |
| 3220 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3221 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3222 | Py_BEGIN_ALLOW_THREADS |
| 3223 | spawnval = spawnv(mode, path, argvlist); |
| 3224 | Py_END_ALLOW_THREADS |
| 3225 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3226 | if (mode == _OLD_P_OVERLAY) |
| 3227 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3228 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3229 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3230 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3231 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3232 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3233 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3234 | free_string_array(argvlist, argc); |
| 3235 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3236 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3237 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3238 | return posix_error(); |
| 3239 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3240 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3241 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3242 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3243 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3244 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3248 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3249 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3250 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3251 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3252 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3253 | path: path of executable file\n\ |
| 3254 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3255 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3256 | |
| 3257 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3258 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3259 | { |
| 3260 | char *path; |
| 3261 | PyObject *argv, *env; |
| 3262 | char **argvlist; |
| 3263 | char **envlist; |
| 3264 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3265 | int mode, pos, envc; |
| 3266 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3267 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3268 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3269 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3270 | |
| 3271 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3272 | argv is a list or tuple of strings and env is a dictionary |
| 3273 | like posix.environ. */ |
| 3274 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3275 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3276 | Py_FileSystemDefaultEncoding, |
| 3277 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3278 | return NULL; |
| 3279 | if (PyList_Check(argv)) { |
| 3280 | argc = PyList_Size(argv); |
| 3281 | getitem = PyList_GetItem; |
| 3282 | } |
| 3283 | else if (PyTuple_Check(argv)) { |
| 3284 | argc = PyTuple_Size(argv); |
| 3285 | getitem = PyTuple_GetItem; |
| 3286 | } |
| 3287 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3288 | PyErr_SetString(PyExc_TypeError, |
| 3289 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3290 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3291 | } |
| 3292 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3293 | PyErr_SetString(PyExc_TypeError, |
| 3294 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3295 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | argvlist = PyMem_NEW(char *, argc+1); |
| 3299 | if (argvlist == NULL) { |
| 3300 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3301 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3302 | } |
| 3303 | for (i = 0; i < argc; i++) { |
| 3304 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3305 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3306 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3307 | &argvlist[i])) |
| 3308 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3309 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3310 | goto fail_1; |
| 3311 | } |
| 3312 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3313 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3314 | argvlist[argc] = NULL; |
| 3315 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3316 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3317 | if (i < 0) |
| 3318 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3319 | envlist = PyMem_NEW(char *, i + 1); |
| 3320 | if (envlist == NULL) { |
| 3321 | PyErr_NoMemory(); |
| 3322 | goto fail_1; |
| 3323 | } |
| 3324 | envc = 0; |
| 3325 | keys = PyMapping_Keys(env); |
| 3326 | vals = PyMapping_Values(env); |
| 3327 | if (!keys || !vals) |
| 3328 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3329 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3330 | PyErr_SetString(PyExc_TypeError, |
| 3331 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3332 | goto fail_2; |
| 3333 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3334 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3335 | for (pos = 0; pos < i; pos++) { |
| 3336 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3337 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3338 | |
| 3339 | key = PyList_GetItem(keys, pos); |
| 3340 | val = PyList_GetItem(vals, pos); |
| 3341 | if (!key || !val) |
| 3342 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3343 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3344 | if (!PyArg_Parse( |
| 3345 | key, |
| 3346 | "s;spawnve() arg 3 contains a non-string key", |
| 3347 | &k) || |
| 3348 | !PyArg_Parse( |
| 3349 | val, |
| 3350 | "s;spawnve() arg 3 contains a non-string value", |
| 3351 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3352 | { |
| 3353 | goto fail_2; |
| 3354 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3355 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3356 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3357 | if (p == NULL) { |
| 3358 | PyErr_NoMemory(); |
| 3359 | goto fail_2; |
| 3360 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3361 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3362 | envlist[envc++] = p; |
| 3363 | } |
| 3364 | envlist[envc] = 0; |
| 3365 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3366 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3367 | Py_BEGIN_ALLOW_THREADS |
| 3368 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3369 | Py_END_ALLOW_THREADS |
| 3370 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3371 | if (mode == _OLD_P_OVERLAY) |
| 3372 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3373 | |
| 3374 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3375 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3376 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3377 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3378 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3379 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3380 | (void) posix_error(); |
| 3381 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3382 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3383 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3384 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3385 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3386 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3387 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3388 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3389 | while (--envc >= 0) |
| 3390 | PyMem_DEL(envlist[envc]); |
| 3391 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3392 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3393 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3394 | Py_XDECREF(vals); |
| 3395 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3396 | fail_0: |
| 3397 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3398 | return res; |
| 3399 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3400 | |
| 3401 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3402 | #if defined(PYOS_OS2) |
| 3403 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3404 | "spawnvp(mode, file, args)\n\n\ |
| 3405 | Execute the program 'file' in a new process, using the environment\n\ |
| 3406 | search path to find the file.\n\ |
| 3407 | \n\ |
| 3408 | mode: mode of process creation\n\ |
| 3409 | file: executable file name\n\ |
| 3410 | args: tuple or list of strings"); |
| 3411 | |
| 3412 | static PyObject * |
| 3413 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3414 | { |
| 3415 | char *path; |
| 3416 | PyObject *argv; |
| 3417 | char **argvlist; |
| 3418 | int mode, i, argc; |
| 3419 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3420 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3421 | |
| 3422 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3423 | argv is a list or tuple of strings. */ |
| 3424 | |
| 3425 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3426 | Py_FileSystemDefaultEncoding, |
| 3427 | &path, &argv)) |
| 3428 | return NULL; |
| 3429 | if (PyList_Check(argv)) { |
| 3430 | argc = PyList_Size(argv); |
| 3431 | getitem = PyList_GetItem; |
| 3432 | } |
| 3433 | else if (PyTuple_Check(argv)) { |
| 3434 | argc = PyTuple_Size(argv); |
| 3435 | getitem = PyTuple_GetItem; |
| 3436 | } |
| 3437 | else { |
| 3438 | PyErr_SetString(PyExc_TypeError, |
| 3439 | "spawnvp() arg 2 must be a tuple or list"); |
| 3440 | PyMem_Free(path); |
| 3441 | return NULL; |
| 3442 | } |
| 3443 | |
| 3444 | argvlist = PyMem_NEW(char *, argc+1); |
| 3445 | if (argvlist == NULL) { |
| 3446 | PyMem_Free(path); |
| 3447 | return PyErr_NoMemory(); |
| 3448 | } |
| 3449 | for (i = 0; i < argc; i++) { |
| 3450 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3451 | Py_FileSystemDefaultEncoding, |
| 3452 | &argvlist[i])) { |
| 3453 | free_string_array(argvlist, i); |
| 3454 | PyErr_SetString( |
| 3455 | PyExc_TypeError, |
| 3456 | "spawnvp() arg 2 must contain only strings"); |
| 3457 | PyMem_Free(path); |
| 3458 | return NULL; |
| 3459 | } |
| 3460 | } |
| 3461 | argvlist[argc] = NULL; |
| 3462 | |
| 3463 | Py_BEGIN_ALLOW_THREADS |
| 3464 | #if defined(PYCC_GCC) |
| 3465 | spawnval = spawnvp(mode, path, argvlist); |
| 3466 | #else |
| 3467 | spawnval = _spawnvp(mode, path, argvlist); |
| 3468 | #endif |
| 3469 | Py_END_ALLOW_THREADS |
| 3470 | |
| 3471 | free_string_array(argvlist, argc); |
| 3472 | PyMem_Free(path); |
| 3473 | |
| 3474 | if (spawnval == -1) |
| 3475 | return posix_error(); |
| 3476 | else |
| 3477 | return Py_BuildValue("l", (long) spawnval); |
| 3478 | } |
| 3479 | |
| 3480 | |
| 3481 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3482 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3483 | Execute the program 'file' in a new process, using the environment\n\ |
| 3484 | search path to find the file.\n\ |
| 3485 | \n\ |
| 3486 | mode: mode of process creation\n\ |
| 3487 | file: executable file name\n\ |
| 3488 | args: tuple or list of arguments\n\ |
| 3489 | env: dictionary of strings mapping to strings"); |
| 3490 | |
| 3491 | static PyObject * |
| 3492 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3493 | { |
| 3494 | char *path; |
| 3495 | PyObject *argv, *env; |
| 3496 | char **argvlist; |
| 3497 | char **envlist; |
| 3498 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3499 | int mode, i, pos, argc, envc; |
| 3500 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3501 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3502 | int lastarg = 0; |
| 3503 | |
| 3504 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3505 | argv is a list or tuple of strings and env is a dictionary |
| 3506 | like posix.environ. */ |
| 3507 | |
| 3508 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3509 | Py_FileSystemDefaultEncoding, |
| 3510 | &path, &argv, &env)) |
| 3511 | return NULL; |
| 3512 | if (PyList_Check(argv)) { |
| 3513 | argc = PyList_Size(argv); |
| 3514 | getitem = PyList_GetItem; |
| 3515 | } |
| 3516 | else if (PyTuple_Check(argv)) { |
| 3517 | argc = PyTuple_Size(argv); |
| 3518 | getitem = PyTuple_GetItem; |
| 3519 | } |
| 3520 | else { |
| 3521 | PyErr_SetString(PyExc_TypeError, |
| 3522 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3523 | goto fail_0; |
| 3524 | } |
| 3525 | if (!PyMapping_Check(env)) { |
| 3526 | PyErr_SetString(PyExc_TypeError, |
| 3527 | "spawnvpe() arg 3 must be a mapping object"); |
| 3528 | goto fail_0; |
| 3529 | } |
| 3530 | |
| 3531 | argvlist = PyMem_NEW(char *, argc+1); |
| 3532 | if (argvlist == NULL) { |
| 3533 | PyErr_NoMemory(); |
| 3534 | goto fail_0; |
| 3535 | } |
| 3536 | for (i = 0; i < argc; i++) { |
| 3537 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3538 | "et;spawnvpe() arg 2 must contain only strings", |
| 3539 | Py_FileSystemDefaultEncoding, |
| 3540 | &argvlist[i])) |
| 3541 | { |
| 3542 | lastarg = i; |
| 3543 | goto fail_1; |
| 3544 | } |
| 3545 | } |
| 3546 | lastarg = argc; |
| 3547 | argvlist[argc] = NULL; |
| 3548 | |
| 3549 | i = PyMapping_Size(env); |
| 3550 | if (i < 0) |
| 3551 | goto fail_1; |
| 3552 | envlist = PyMem_NEW(char *, i + 1); |
| 3553 | if (envlist == NULL) { |
| 3554 | PyErr_NoMemory(); |
| 3555 | goto fail_1; |
| 3556 | } |
| 3557 | envc = 0; |
| 3558 | keys = PyMapping_Keys(env); |
| 3559 | vals = PyMapping_Values(env); |
| 3560 | if (!keys || !vals) |
| 3561 | goto fail_2; |
| 3562 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3563 | PyErr_SetString(PyExc_TypeError, |
| 3564 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3565 | goto fail_2; |
| 3566 | } |
| 3567 | |
| 3568 | for (pos = 0; pos < i; pos++) { |
| 3569 | char *p, *k, *v; |
| 3570 | size_t len; |
| 3571 | |
| 3572 | key = PyList_GetItem(keys, pos); |
| 3573 | val = PyList_GetItem(vals, pos); |
| 3574 | if (!key || !val) |
| 3575 | goto fail_2; |
| 3576 | |
| 3577 | if (!PyArg_Parse( |
| 3578 | key, |
| 3579 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3580 | &k) || |
| 3581 | !PyArg_Parse( |
| 3582 | val, |
| 3583 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3584 | &v)) |
| 3585 | { |
| 3586 | goto fail_2; |
| 3587 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3588 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3589 | p = PyMem_NEW(char, len); |
| 3590 | if (p == NULL) { |
| 3591 | PyErr_NoMemory(); |
| 3592 | goto fail_2; |
| 3593 | } |
| 3594 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3595 | envlist[envc++] = p; |
| 3596 | } |
| 3597 | envlist[envc] = 0; |
| 3598 | |
| 3599 | Py_BEGIN_ALLOW_THREADS |
| 3600 | #if defined(PYCC_GCC) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3601 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3602 | #else |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3603 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3604 | #endif |
| 3605 | Py_END_ALLOW_THREADS |
| 3606 | |
| 3607 | if (spawnval == -1) |
| 3608 | (void) posix_error(); |
| 3609 | else |
| 3610 | res = Py_BuildValue("l", (long) spawnval); |
| 3611 | |
| 3612 | fail_2: |
| 3613 | while (--envc >= 0) |
| 3614 | PyMem_DEL(envlist[envc]); |
| 3615 | PyMem_DEL(envlist); |
| 3616 | fail_1: |
| 3617 | free_string_array(argvlist, lastarg); |
| 3618 | Py_XDECREF(vals); |
| 3619 | Py_XDECREF(keys); |
| 3620 | fail_0: |
| 3621 | PyMem_Free(path); |
| 3622 | return res; |
| 3623 | } |
| 3624 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3625 | #endif /* HAVE_SPAWNV */ |
| 3626 | |
| 3627 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3628 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3629 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3630 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3631 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3632 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3633 | 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] | 3634 | |
| 3635 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3636 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3637 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3638 | pid_t pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3639 | if (pid == -1) |
| 3640 | return posix_error(); |
| 3641 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3642 | return PyLong_FromLong(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3643 | } |
| 3644 | #endif |
| 3645 | |
| 3646 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3647 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3648 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3649 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3650 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3651 | 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] | 3652 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3653 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3654 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3655 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3656 | pid_t pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3657 | if (pid == -1) |
| 3658 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3659 | if (pid == 0) |
| 3660 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3661 | return PyLong_FromLong(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3662 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3663 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3664 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3665 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3666 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3667 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3668 | #define DEV_PTY_FILE "/dev/ptc" |
| 3669 | #define HAVE_DEV_PTMX |
| 3670 | #else |
| 3671 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3672 | #endif |
| 3673 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3674 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3675 | #ifdef HAVE_PTY_H |
| 3676 | #include <pty.h> |
| 3677 | #else |
| 3678 | #ifdef HAVE_LIBUTIL_H |
| 3679 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3680 | #endif /* HAVE_LIBUTIL_H */ |
| 3681 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3682 | #ifdef HAVE_STROPTS_H |
| 3683 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3684 | #endif |
| 3685 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3686 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3687 | #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] | 3688 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3689 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3690 | 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] | 3691 | |
| 3692 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3693 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3694 | { |
| 3695 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3696 | #ifndef HAVE_OPENPTY |
| 3697 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3698 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3699 | #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] | 3700 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3701 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3702 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3703 | #endif |
| 3704 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3705 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3706 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3707 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3708 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3709 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3710 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3711 | if (slave_name == NULL) |
| 3712 | return posix_error(); |
| 3713 | |
| 3714 | slave_fd = open(slave_name, O_RDWR); |
| 3715 | if (slave_fd < 0) |
| 3716 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3717 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3718 | 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] | 3719 | if (master_fd < 0) |
| 3720 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3721 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3722 | /* change permission of slave */ |
| 3723 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3724 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3725 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3726 | } |
| 3727 | /* unlock slave */ |
| 3728 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3729 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3730 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3731 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3732 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3733 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3734 | if (slave_name == NULL) |
| 3735 | return posix_error(); |
| 3736 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3737 | if (slave_fd < 0) |
| 3738 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3739 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3740 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3741 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3742 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3743 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3744 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3745 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3746 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3747 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3748 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3749 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3750 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3751 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3752 | |
| 3753 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3754 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3755 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3756 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3757 | 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] | 3758 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3759 | |
| 3760 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3761 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3762 | { |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3763 | int master_fd = -1; |
| 3764 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3765 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3766 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3767 | if (pid == -1) |
| 3768 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3769 | if (pid == 0) |
| 3770 | PyOS_AfterFork(); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3771 | return Py_BuildValue("(li)", pid, master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3772 | } |
| 3773 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3774 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3775 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3776 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3777 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3778 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3779 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3780 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3781 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3782 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3783 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3784 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3785 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3786 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3787 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3788 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3789 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3790 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3791 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3792 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3793 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3794 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3795 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3796 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3797 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3798 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3799 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3800 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3801 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3802 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3803 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3804 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3805 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3806 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3807 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3808 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3809 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3810 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3811 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3812 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3813 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3814 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3815 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3816 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3817 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3818 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3819 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3820 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3821 | return PyLong_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3824 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3825 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3826 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3827 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3828 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3829 | |
| 3830 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3831 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3832 | { |
| 3833 | PyObject *result = NULL; |
| 3834 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3835 | #ifdef NGROUPS_MAX |
| 3836 | #define MAX_GROUPS NGROUPS_MAX |
| 3837 | #else |
| 3838 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3839 | #define MAX_GROUPS 64 |
| 3840 | #endif |
| 3841 | gid_t grouplist[MAX_GROUPS]; |
| 3842 | int n; |
| 3843 | |
| 3844 | n = getgroups(MAX_GROUPS, grouplist); |
| 3845 | if (n < 0) |
| 3846 | posix_error(); |
| 3847 | else { |
| 3848 | result = PyList_New(n); |
| 3849 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3850 | int i; |
| 3851 | for (i = 0; i < n; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3852 | PyObject *o = PyLong_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3853 | if (o == NULL) { |
| 3854 | Py_DECREF(result); |
| 3855 | result = NULL; |
| 3856 | break; |
| 3857 | } |
| 3858 | PyList_SET_ITEM(result, i, o); |
| 3859 | } |
| 3860 | } |
| 3861 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3862 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3863 | return result; |
| 3864 | } |
| 3865 | #endif |
| 3866 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3867 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3868 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3869 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3870 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3871 | |
| 3872 | static PyObject * |
| 3873 | posix_getpgid(PyObject *self, PyObject *args) |
| 3874 | { |
| 3875 | int pid, pgid; |
| 3876 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3877 | return NULL; |
| 3878 | pgid = getpgid(pid); |
| 3879 | if (pgid < 0) |
| 3880 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3881 | return PyLong_FromLong((long)pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3882 | } |
| 3883 | #endif /* HAVE_GETPGID */ |
| 3884 | |
| 3885 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3886 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3887 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3888 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3889 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3890 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3891 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3892 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3893 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3894 | #ifdef GETPGRP_HAVE_ARG |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3895 | return PyLong_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3896 | #else /* GETPGRP_HAVE_ARG */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3897 | return PyLong_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3898 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3899 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3900 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3901 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3902 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3903 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3904 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3905 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3906 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3907 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3908 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3909 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3910 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3911 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3912 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3913 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3914 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3915 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3916 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3917 | Py_INCREF(Py_None); |
| 3918 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3921 | #endif /* HAVE_SETPGRP */ |
| 3922 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3923 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3924 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3925 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3926 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3927 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3928 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3929 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3930 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3931 | return PyLong_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3932 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3933 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3934 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3935 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3936 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3937 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3938 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3939 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3940 | |
| 3941 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3942 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3943 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3944 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3945 | char *name; |
| 3946 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3947 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3948 | errno = 0; |
| 3949 | name = getlogin(); |
| 3950 | if (name == NULL) { |
| 3951 | if (errno) |
| 3952 | posix_error(); |
| 3953 | else |
| 3954 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3955 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3956 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3957 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 3958 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3959 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3960 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3961 | return result; |
| 3962 | } |
| 3963 | #endif |
| 3964 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3965 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3966 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3967 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3968 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3969 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3970 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3971 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3972 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3973 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3974 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3975 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3976 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3977 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3978 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3979 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3980 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3981 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3982 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3983 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3984 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3985 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3986 | pid_t pid; |
| 3987 | int sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3988 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3989 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3990 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3991 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3992 | APIRET rc; |
| 3993 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3994 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3995 | |
| 3996 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3997 | APIRET rc; |
| 3998 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3999 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4000 | |
| 4001 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4002 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4003 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4004 | if (kill(pid, sig) == -1) |
| 4005 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4006 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4007 | Py_INCREF(Py_None); |
| 4008 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4009 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4010 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4011 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4012 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4013 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4014 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4015 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4016 | |
| 4017 | static PyObject * |
| 4018 | posix_killpg(PyObject *self, PyObject *args) |
| 4019 | { |
| 4020 | int pgid, sig; |
| 4021 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 4022 | return NULL; |
| 4023 | if (killpg(pgid, sig) == -1) |
| 4024 | return posix_error(); |
| 4025 | Py_INCREF(Py_None); |
| 4026 | return Py_None; |
| 4027 | } |
| 4028 | #endif |
| 4029 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4030 | #ifdef HAVE_PLOCK |
| 4031 | |
| 4032 | #ifdef HAVE_SYS_LOCK_H |
| 4033 | #include <sys/lock.h> |
| 4034 | #endif |
| 4035 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4036 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4037 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4038 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4039 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4040 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4041 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4042 | { |
| 4043 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4044 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4045 | return NULL; |
| 4046 | if (plock(op) == -1) |
| 4047 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4048 | Py_INCREF(Py_None); |
| 4049 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4050 | } |
| 4051 | #endif |
| 4052 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4053 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4054 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4055 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4056 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4057 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4058 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4059 | Set the current process's user id."); |
| 4060 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4061 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4062 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4063 | { |
| 4064 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4065 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4066 | return NULL; |
| 4067 | if (setuid(uid) < 0) |
| 4068 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4069 | Py_INCREF(Py_None); |
| 4070 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4071 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4072 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4073 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4074 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4075 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4076 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4077 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4078 | Set the current process's effective user id."); |
| 4079 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4080 | static PyObject * |
| 4081 | posix_seteuid (PyObject *self, PyObject *args) |
| 4082 | { |
| 4083 | int euid; |
| 4084 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4085 | return NULL; |
| 4086 | } else if (seteuid(euid) < 0) { |
| 4087 | return posix_error(); |
| 4088 | } else { |
| 4089 | Py_INCREF(Py_None); |
| 4090 | return Py_None; |
| 4091 | } |
| 4092 | } |
| 4093 | #endif /* HAVE_SETEUID */ |
| 4094 | |
| 4095 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4096 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4097 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4098 | Set the current process's effective group id."); |
| 4099 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4100 | static PyObject * |
| 4101 | posix_setegid (PyObject *self, PyObject *args) |
| 4102 | { |
| 4103 | int egid; |
| 4104 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4105 | return NULL; |
| 4106 | } else if (setegid(egid) < 0) { |
| 4107 | return posix_error(); |
| 4108 | } else { |
| 4109 | Py_INCREF(Py_None); |
| 4110 | return Py_None; |
| 4111 | } |
| 4112 | } |
| 4113 | #endif /* HAVE_SETEGID */ |
| 4114 | |
| 4115 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4116 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4117 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4118 | Set the current process's real and effective user ids."); |
| 4119 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4120 | static PyObject * |
| 4121 | posix_setreuid (PyObject *self, PyObject *args) |
| 4122 | { |
| 4123 | int ruid, euid; |
| 4124 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4125 | return NULL; |
| 4126 | } else if (setreuid(ruid, euid) < 0) { |
| 4127 | return posix_error(); |
| 4128 | } else { |
| 4129 | Py_INCREF(Py_None); |
| 4130 | return Py_None; |
| 4131 | } |
| 4132 | } |
| 4133 | #endif /* HAVE_SETREUID */ |
| 4134 | |
| 4135 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4136 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4137 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4138 | Set the current process's real and effective group ids."); |
| 4139 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4140 | static PyObject * |
| 4141 | posix_setregid (PyObject *self, PyObject *args) |
| 4142 | { |
| 4143 | int rgid, egid; |
| 4144 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4145 | return NULL; |
| 4146 | } else if (setregid(rgid, egid) < 0) { |
| 4147 | return posix_error(); |
| 4148 | } else { |
| 4149 | Py_INCREF(Py_None); |
| 4150 | return Py_None; |
| 4151 | } |
| 4152 | } |
| 4153 | #endif /* HAVE_SETREGID */ |
| 4154 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4155 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4156 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4157 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4158 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4159 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4160 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4161 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4162 | { |
| 4163 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4164 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4165 | return NULL; |
| 4166 | if (setgid(gid) < 0) |
| 4167 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4168 | Py_INCREF(Py_None); |
| 4169 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4170 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4171 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4172 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4173 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4174 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4175 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4176 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4177 | |
| 4178 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4179 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4180 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4181 | int i, len; |
| 4182 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4183 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4184 | if (!PySequence_Check(groups)) { |
| 4185 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4186 | return NULL; |
| 4187 | } |
| 4188 | len = PySequence_Size(groups); |
| 4189 | if (len > MAX_GROUPS) { |
| 4190 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4191 | return NULL; |
| 4192 | } |
| 4193 | for(i = 0; i < len; i++) { |
| 4194 | PyObject *elem; |
| 4195 | elem = PySequence_GetItem(groups, i); |
| 4196 | if (!elem) |
| 4197 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4198 | if (!PyLong_Check(elem)) { |
| 4199 | PyErr_SetString(PyExc_TypeError, |
| 4200 | "groups must be integers"); |
| 4201 | Py_DECREF(elem); |
| 4202 | return NULL; |
| 4203 | } else { |
| 4204 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4205 | if (PyErr_Occurred()) { |
| 4206 | PyErr_SetString(PyExc_TypeError, |
| 4207 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4208 | Py_DECREF(elem); |
| 4209 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4210 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4211 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4212 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4213 | if (grouplist[i] != x) { |
| 4214 | PyErr_SetString(PyExc_TypeError, |
| 4215 | "group id too big"); |
| 4216 | Py_DECREF(elem); |
| 4217 | return NULL; |
| 4218 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4219 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4220 | Py_DECREF(elem); |
| 4221 | } |
| 4222 | |
| 4223 | if (setgroups(len, grouplist) < 0) |
| 4224 | return posix_error(); |
| 4225 | Py_INCREF(Py_None); |
| 4226 | return Py_None; |
| 4227 | } |
| 4228 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4229 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4230 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4231 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4232 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4233 | { |
| 4234 | PyObject *result; |
| 4235 | static PyObject *struct_rusage; |
| 4236 | |
| 4237 | if (pid == -1) |
| 4238 | return posix_error(); |
| 4239 | |
| 4240 | if (struct_rusage == NULL) { |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4241 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4242 | if (m == NULL) |
| 4243 | return NULL; |
| 4244 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4245 | Py_DECREF(m); |
| 4246 | if (struct_rusage == NULL) |
| 4247 | return NULL; |
| 4248 | } |
| 4249 | |
| 4250 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4251 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4252 | if (!result) |
| 4253 | return NULL; |
| 4254 | |
| 4255 | #ifndef doubletime |
| 4256 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4257 | #endif |
| 4258 | |
| 4259 | PyStructSequence_SET_ITEM(result, 0, |
| 4260 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4261 | PyStructSequence_SET_ITEM(result, 1, |
| 4262 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4263 | #define SET_INT(result, index, value)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4264 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4265 | SET_INT(result, 2, ru->ru_maxrss); |
| 4266 | SET_INT(result, 3, ru->ru_ixrss); |
| 4267 | SET_INT(result, 4, ru->ru_idrss); |
| 4268 | SET_INT(result, 5, ru->ru_isrss); |
| 4269 | SET_INT(result, 6, ru->ru_minflt); |
| 4270 | SET_INT(result, 7, ru->ru_majflt); |
| 4271 | SET_INT(result, 8, ru->ru_nswap); |
| 4272 | SET_INT(result, 9, ru->ru_inblock); |
| 4273 | SET_INT(result, 10, ru->ru_oublock); |
| 4274 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4275 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4276 | SET_INT(result, 13, ru->ru_nsignals); |
| 4277 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4278 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4279 | #undef SET_INT |
| 4280 | |
| 4281 | if (PyErr_Occurred()) { |
| 4282 | Py_DECREF(result); |
| 4283 | return NULL; |
| 4284 | } |
| 4285 | |
| 4286 | return Py_BuildValue("iiN", pid, status, result); |
| 4287 | } |
| 4288 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4289 | |
| 4290 | #ifdef HAVE_WAIT3 |
| 4291 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4292 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4293 | Wait for completion of a child process."); |
| 4294 | |
| 4295 | static PyObject * |
| 4296 | posix_wait3(PyObject *self, PyObject *args) |
| 4297 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4298 | pid_t pid; |
| 4299 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4300 | struct rusage ru; |
| 4301 | WAIT_TYPE status; |
| 4302 | WAIT_STATUS_INT(status) = 0; |
| 4303 | |
| 4304 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4305 | return NULL; |
| 4306 | |
| 4307 | Py_BEGIN_ALLOW_THREADS |
| 4308 | pid = wait3(&status, options, &ru); |
| 4309 | Py_END_ALLOW_THREADS |
| 4310 | |
| 4311 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4312 | } |
| 4313 | #endif /* HAVE_WAIT3 */ |
| 4314 | |
| 4315 | #ifdef HAVE_WAIT4 |
| 4316 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4317 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4318 | Wait for completion of a given child process."); |
| 4319 | |
| 4320 | static PyObject * |
| 4321 | posix_wait4(PyObject *self, PyObject *args) |
| 4322 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4323 | pid_t pid; |
| 4324 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4325 | struct rusage ru; |
| 4326 | WAIT_TYPE status; |
| 4327 | WAIT_STATUS_INT(status) = 0; |
| 4328 | |
| 4329 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4330 | return NULL; |
| 4331 | |
| 4332 | Py_BEGIN_ALLOW_THREADS |
| 4333 | pid = wait4(pid, &status, options, &ru); |
| 4334 | Py_END_ALLOW_THREADS |
| 4335 | |
| 4336 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4337 | } |
| 4338 | #endif /* HAVE_WAIT4 */ |
| 4339 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4340 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4341 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4342 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4343 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4344 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4345 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4346 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4347 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4348 | pid_t pid; |
| 4349 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4350 | WAIT_TYPE status; |
| 4351 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4352 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4353 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4354 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4355 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4356 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4357 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4358 | if (pid == -1) |
| 4359 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4360 | |
| 4361 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4364 | #elif defined(HAVE_CWAIT) |
| 4365 | |
| 4366 | /* 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] | 4367 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4368 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4369 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4370 | |
| 4371 | static PyObject * |
| 4372 | posix_waitpid(PyObject *self, PyObject *args) |
| 4373 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4374 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4375 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4376 | |
| 4377 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4378 | return NULL; |
| 4379 | Py_BEGIN_ALLOW_THREADS |
| 4380 | pid = _cwait(&status, pid, options); |
| 4381 | Py_END_ALLOW_THREADS |
| 4382 | if (pid == -1) |
| 4383 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4384 | |
| 4385 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4386 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4387 | } |
| 4388 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4389 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4390 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4391 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4392 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4393 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4394 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4395 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4396 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4397 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4398 | pid_t pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4399 | WAIT_TYPE status; |
| 4400 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4401 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4402 | Py_BEGIN_ALLOW_THREADS |
| 4403 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4404 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4405 | if (pid == -1) |
| 4406 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4407 | |
| 4408 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4409 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4410 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4411 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4412 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4413 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4414 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4415 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4416 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4417 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4418 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4419 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4420 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4421 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4422 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4423 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4424 | 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] | 4425 | #else |
| 4426 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4427 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4428 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4431 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4432 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4433 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4434 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4435 | 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] | 4436 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4437 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4438 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4439 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4440 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4441 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4442 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4443 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4444 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4445 | |
| 4446 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 4447 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4448 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4449 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4450 | if (v == NULL) { |
| 4451 | PyMem_Free(path); |
| 4452 | return NULL; |
| 4453 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4454 | |
| 4455 | if (PyUnicode_Check(v)) { |
| 4456 | arg_is_unicode = 1; |
| 4457 | } |
| 4458 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4459 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4460 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4461 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4462 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4463 | if (n < 0) |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4464 | return posix_error_with_allocated_filename(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4465 | |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4466 | PyMem_Free(path); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4467 | v = PyBytes_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4468 | if (arg_is_unicode) { |
| 4469 | PyObject *w; |
| 4470 | |
| 4471 | w = PyUnicode_FromEncodedObject(v, |
| 4472 | Py_FileSystemDefaultEncoding, |
| 4473 | "strict"); |
| 4474 | if (w != NULL) { |
| 4475 | Py_DECREF(v); |
| 4476 | v = w; |
| 4477 | } |
| 4478 | else { |
| 4479 | /* fall back to the original byte string, as |
| 4480 | discussed in patch #683592 */ |
| 4481 | PyErr_Clear(); |
| 4482 | } |
| 4483 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4484 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4485 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4486 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4487 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4488 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4489 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4490 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4491 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4492 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4493 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4494 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4495 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4496 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4497 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4498 | } |
| 4499 | #endif /* HAVE_SYMLINK */ |
| 4500 | |
| 4501 | |
| 4502 | #ifdef HAVE_TIMES |
| 4503 | #ifndef HZ |
| 4504 | #define HZ 60 /* Universal constant :-) */ |
| 4505 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4506 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4507 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4508 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4509 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4510 | { |
| 4511 | ULONG value = 0; |
| 4512 | |
| 4513 | Py_BEGIN_ALLOW_THREADS |
| 4514 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4515 | Py_END_ALLOW_THREADS |
| 4516 | |
| 4517 | return value; |
| 4518 | } |
| 4519 | |
| 4520 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4521 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4522 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4523 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4524 | return Py_BuildValue("ddddd", |
| 4525 | (double)0 /* t.tms_utime / HZ */, |
| 4526 | (double)0 /* t.tms_stime / HZ */, |
| 4527 | (double)0 /* t.tms_cutime / HZ */, |
| 4528 | (double)0 /* t.tms_cstime / HZ */, |
| 4529 | (double)system_uptime() / 1000); |
| 4530 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4531 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4532 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4533 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4534 | { |
| 4535 | struct tms t; |
| 4536 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4537 | errno = 0; |
| 4538 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4539 | if (c == (clock_t) -1) |
| 4540 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4541 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4542 | (double)t.tms_utime / HZ, |
| 4543 | (double)t.tms_stime / HZ, |
| 4544 | (double)t.tms_cutime / HZ, |
| 4545 | (double)t.tms_cstime / HZ, |
| 4546 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4547 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4548 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4549 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4550 | |
| 4551 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4552 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4553 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4554 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4555 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4556 | { |
| 4557 | FILETIME create, exit, kernel, user; |
| 4558 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4559 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4560 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4561 | /* The fields of a FILETIME structure are the hi and lo part |
| 4562 | of a 64-bit value expressed in 100 nanosecond units. |
| 4563 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4564 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4565 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4566 | return Py_BuildValue( |
| 4567 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4568 | (double)(user.dwHighDateTime*429.4967296 + |
| 4569 | user.dwLowDateTime*1e-7), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 4570 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4571 | kernel.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4572 | (double)0, |
| 4573 | (double)0, |
| 4574 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4575 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4576 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4577 | |
| 4578 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4579 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4580 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4581 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4582 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4583 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4584 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4585 | #ifdef HAVE_GETSID |
| 4586 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4587 | "getsid(pid) -> sid\n\n\ |
| 4588 | Call the system call getsid()."); |
| 4589 | |
| 4590 | static PyObject * |
| 4591 | posix_getsid(PyObject *self, PyObject *args) |
| 4592 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4593 | pid_t pid; |
| 4594 | int sid; |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4595 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4596 | return NULL; |
| 4597 | sid = getsid(pid); |
| 4598 | if (sid < 0) |
| 4599 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4600 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4601 | } |
| 4602 | #endif /* HAVE_GETSID */ |
| 4603 | |
| 4604 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4605 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4606 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4607 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4608 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4609 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4610 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4611 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4612 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4613 | if (setsid() < 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_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4619 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4620 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4621 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4622 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4623 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4624 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4625 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4626 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4627 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4628 | pid_t pid; |
| 4629 | int pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4630 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4631 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4632 | if (setpgid(pid, pgrp) < 0) |
| 4633 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4634 | Py_INCREF(Py_None); |
| 4635 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4636 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4637 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4638 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4639 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4640 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4641 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4642 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4643 | 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] | 4644 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4645 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4646 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4647 | { |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4648 | int fd; |
| 4649 | pid_t pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4650 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4651 | return NULL; |
| 4652 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4653 | if (pgid < 0) |
| 4654 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4655 | return PyLong_FromLong((long)pgid); |
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_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4658 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4659 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4660 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4661 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4662 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4663 | 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] | 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_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4667 | { |
| 4668 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4669 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4670 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4671 | if (tcsetpgrp(fd, pgid) < 0) |
| 4672 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4673 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4674 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4675 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4676 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4677 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4678 | /* Functions acting on file descriptors */ |
| 4679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4680 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4681 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4682 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4683 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4684 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4685 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4686 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4687 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4688 | int flag; |
| 4689 | int mode = 0777; |
| 4690 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4691 | |
| 4692 | #ifdef MS_WINDOWS |
| 4693 | if (unicode_file_names()) { |
| 4694 | PyUnicodeObject *po; |
| 4695 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4696 | Py_BEGIN_ALLOW_THREADS |
| 4697 | /* PyUnicode_AS_UNICODE OK without thread |
| 4698 | lock as it is a simple dereference. */ |
| 4699 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4700 | Py_END_ALLOW_THREADS |
| 4701 | if (fd < 0) |
| 4702 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4703 | return PyLong_FromLong((long)fd); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4704 | } |
| 4705 | /* Drop the argument parsing error as narrow strings |
| 4706 | are also valid. */ |
| 4707 | PyErr_Clear(); |
| 4708 | } |
| 4709 | #endif |
| 4710 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4711 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4712 | Py_FileSystemDefaultEncoding, &file, |
| 4713 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4714 | return NULL; |
| 4715 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4716 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4717 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4718 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4719 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4720 | return posix_error_with_allocated_filename(file); |
| 4721 | PyMem_Free(file); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4722 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4726 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4727 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4728 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4729 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4730 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4731 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4732 | { |
| 4733 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4734 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4735 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4736 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4737 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4738 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4739 | if (res < 0) |
| 4740 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4741 | Py_INCREF(Py_None); |
| 4742 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4745 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4746 | PyDoc_STRVAR(posix_closerange__doc__, |
| 4747 | "closerange(fd_low, fd_high)\n\n\ |
| 4748 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 4749 | |
| 4750 | static PyObject * |
| 4751 | posix_closerange(PyObject *self, PyObject *args) |
| 4752 | { |
| 4753 | int fd_from, fd_to, i; |
| 4754 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 4755 | return NULL; |
| 4756 | Py_BEGIN_ALLOW_THREADS |
| 4757 | for (i = fd_from; i < fd_to; i++) |
| 4758 | close(i); |
| 4759 | Py_END_ALLOW_THREADS |
| 4760 | Py_RETURN_NONE; |
| 4761 | } |
| 4762 | |
| 4763 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4764 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4765 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4766 | Return a duplicate of a 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_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4770 | { |
| 4771 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4772 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
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 | fd = dup(fd); |
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 (fd < 0) |
| 4778 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4779 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4782 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4783 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4784 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4785 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4786 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4787 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4788 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4789 | { |
| 4790 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4791 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4792 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4793 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4794 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4795 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4796 | if (res < 0) |
| 4797 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4798 | Py_INCREF(Py_None); |
| 4799 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4802 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4803 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4804 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4805 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4806 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4807 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4808 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4809 | { |
| 4810 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4811 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4812 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4813 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4814 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4815 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4816 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4817 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4818 | return NULL; |
| 4819 | #ifdef SEEK_SET |
| 4820 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4821 | switch (how) { |
| 4822 | case 0: how = SEEK_SET; break; |
| 4823 | case 1: how = SEEK_CUR; break; |
| 4824 | case 2: how = SEEK_END; break; |
| 4825 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4826 | #endif /* SEEK_END */ |
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 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4830 | #else |
| 4831 | pos = PyLong_Check(posobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4832 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4833 | #endif |
| 4834 | if (PyErr_Occurred()) |
| 4835 | return NULL; |
| 4836 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4837 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4838 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4839 | res = _lseeki64(fd, pos, how); |
| 4840 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4841 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4842 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4843 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4844 | if (res < 0) |
| 4845 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4846 | |
| 4847 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4848 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4849 | #else |
| 4850 | return PyLong_FromLongLong(res); |
| 4851 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4854 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4855 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4856 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4857 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4858 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4859 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4860 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4861 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4862 | int fd, size; |
| 4863 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4864 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4865 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4866 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 4867 | if (size < 0) { |
| 4868 | errno = EINVAL; |
| 4869 | return posix_error(); |
| 4870 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4871 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4872 | if (buffer == NULL) |
| 4873 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4874 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4875 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4876 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4877 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4878 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4879 | return posix_error(); |
| 4880 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4881 | if (n != size) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4882 | _PyBytes_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4883 | return buffer; |
| 4884 | } |
| 4885 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4887 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4888 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4889 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4890 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4891 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4892 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4893 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4894 | int fd; |
| 4895 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4896 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4897 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4898 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4899 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4900 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4901 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4902 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4903 | if (size < 0) |
| 4904 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4905 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4908 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4909 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4910 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4911 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4912 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4913 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4914 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4915 | { |
| 4916 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4917 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4918 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4919 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4920 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 4921 | #ifdef __VMS |
| 4922 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 4923 | fsync(fd); |
| 4924 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4925 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4926 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4927 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4928 | if (res != 0) { |
| 4929 | #ifdef MS_WINDOWS |
| 4930 | return win32_error("fstat", NULL); |
| 4931 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4932 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4933 | #endif |
| 4934 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4935 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4936 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4939 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4940 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4941 | 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] | 4942 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4943 | |
| 4944 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4945 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4946 | { |
| 4947 | int fd; |
| 4948 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4949 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4950 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4951 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4952 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4953 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4954 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4955 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4956 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4957 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4958 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4959 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4960 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4961 | #if defined(PYOS_OS2) |
| 4962 | HFILE read, write; |
| 4963 | APIRET rc; |
| 4964 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4965 | Py_BEGIN_ALLOW_THREADS |
| 4966 | rc = DosCreatePipe( &read, &write, 4096); |
| 4967 | Py_END_ALLOW_THREADS |
| 4968 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4969 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4970 | |
| 4971 | return Py_BuildValue("(ii)", read, write); |
| 4972 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4973 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4974 | int fds[2]; |
| 4975 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4976 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4977 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4978 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4979 | if (res != 0) |
| 4980 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4981 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4982 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4983 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4984 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4985 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4986 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4987 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4988 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4989 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4990 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4991 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4992 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4993 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4994 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4995 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4996 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4997 | #endif /* HAVE_PIPE */ |
| 4998 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4999 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5000 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5001 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5002 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5003 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5004 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5005 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5006 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5007 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5008 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5009 | int mode = 0666; |
| 5010 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5011 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5012 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5013 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5014 | res = mkfifo(filename, mode); |
| 5015 | Py_END_ALLOW_THREADS |
| 5016 | if (res < 0) |
| 5017 | return posix_error(); |
| 5018 | Py_INCREF(Py_None); |
| 5019 | return Py_None; |
| 5020 | } |
| 5021 | #endif |
| 5022 | |
| 5023 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5024 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5026 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5027 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5028 | named filename. mode specifies both the permissions to use and the\n\ |
| 5029 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5030 | 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] | 5031 | device defines the newly created device special file (probably using\n\ |
| 5032 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5033 | |
| 5034 | |
| 5035 | static PyObject * |
| 5036 | posix_mknod(PyObject *self, PyObject *args) |
| 5037 | { |
| 5038 | char *filename; |
| 5039 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5040 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5041 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5042 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5043 | return NULL; |
| 5044 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5045 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5046 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5047 | if (res < 0) |
| 5048 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5049 | Py_INCREF(Py_None); |
| 5050 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5051 | } |
| 5052 | #endif |
| 5053 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5054 | #ifdef HAVE_DEVICE_MACROS |
| 5055 | PyDoc_STRVAR(posix_major__doc__, |
| 5056 | "major(device) -> major number\n\ |
| 5057 | Extracts a device major number from a raw device number."); |
| 5058 | |
| 5059 | static PyObject * |
| 5060 | posix_major(PyObject *self, PyObject *args) |
| 5061 | { |
| 5062 | int device; |
| 5063 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5064 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5065 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | PyDoc_STRVAR(posix_minor__doc__, |
| 5069 | "minor(device) -> minor number\n\ |
| 5070 | Extracts a device minor number from a raw device number."); |
| 5071 | |
| 5072 | static PyObject * |
| 5073 | posix_minor(PyObject *self, PyObject *args) |
| 5074 | { |
| 5075 | int device; |
| 5076 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5077 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5078 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
| 5081 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5082 | "makedev(major, minor) -> device number\n\ |
| 5083 | Composes a raw device number from the major and minor device numbers."); |
| 5084 | |
| 5085 | static PyObject * |
| 5086 | posix_makedev(PyObject *self, PyObject *args) |
| 5087 | { |
| 5088 | int major, minor; |
| 5089 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5090 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5091 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5092 | } |
| 5093 | #endif /* device macros */ |
| 5094 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5095 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5096 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5097 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5098 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5099 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5100 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5101 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5102 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5103 | { |
| 5104 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5105 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5106 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5107 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5108 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5109 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5110 | return NULL; |
| 5111 | |
| 5112 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5113 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5114 | #else |
| 5115 | length = PyLong_Check(lenobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5116 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5117 | #endif |
| 5118 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5119 | return NULL; |
| 5120 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5121 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5122 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5123 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5124 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5125 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5126 | return NULL; |
| 5127 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5128 | Py_INCREF(Py_None); |
| 5129 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5130 | } |
| 5131 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5132 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5133 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5134 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5135 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5136 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5137 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5138 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5139 | * get re-set with another call for the same key. */ |
| 5140 | static PyObject *posix_putenv_garbage; |
| 5141 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5142 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5143 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5144 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5145 | #ifdef MS_WINDOWS |
| 5146 | wchar_t *s1, *s2; |
| 5147 | wchar_t *newenv; |
| 5148 | #else |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5149 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5150 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5151 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5152 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5153 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5154 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5155 | if (!PyArg_ParseTuple(args, |
| 5156 | #ifdef MS_WINDOWS |
| 5157 | "uu:putenv", |
| 5158 | #else |
| 5159 | "ss:putenv", |
| 5160 | #endif |
| 5161 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5162 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5163 | |
| 5164 | #if defined(PYOS_OS2) |
| 5165 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5166 | APIRET rc; |
| 5167 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5168 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5169 | if (rc != NO_ERROR) |
| 5170 | return os2_error(rc); |
| 5171 | |
| 5172 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5173 | APIRET rc; |
| 5174 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5175 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5176 | if (rc != NO_ERROR) |
| 5177 | return os2_error(rc); |
| 5178 | } else { |
| 5179 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5180 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5181 | /* len includes space for a trailing \0; the size arg to |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5182 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5183 | #ifdef MS_WINDOWS |
| 5184 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5185 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5186 | #else |
| 5187 | len = strlen(s1) + strlen(s2) + 2; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5188 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
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 | if (newstr == NULL) |
| 5191 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5192 | #ifdef MS_WINDOWS |
| 5193 | newenv = PyUnicode_AsUnicode(newstr); |
| 5194 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5195 | if (_wputenv(newenv)) { |
| 5196 | Py_DECREF(newstr); |
| 5197 | posix_error(); |
| 5198 | return NULL; |
| 5199 | } |
| 5200 | #else |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5201 | newenv = PyBytes_AS_STRING(newstr); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5202 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5203 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5204 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5205 | posix_error(); |
| 5206 | return NULL; |
| 5207 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5208 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5209 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5210 | * this will cause previous value to be collected. This has to |
| 5211 | * happen after the real putenv() call because the old value |
| 5212 | * was still accessible until then. */ |
| 5213 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5214 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5215 | /* really not much we can do; just leak */ |
| 5216 | PyErr_Clear(); |
| 5217 | } |
| 5218 | else { |
| 5219 | Py_DECREF(newstr); |
| 5220 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5221 | |
| 5222 | #if defined(PYOS_OS2) |
| 5223 | } |
| 5224 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5225 | Py_INCREF(Py_None); |
| 5226 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5227 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5228 | #endif /* putenv */ |
| 5229 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5230 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5231 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5232 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5233 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5234 | |
| 5235 | static PyObject * |
| 5236 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5237 | { |
| 5238 | char *s1; |
| 5239 | |
| 5240 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5241 | return NULL; |
| 5242 | |
| 5243 | unsetenv(s1); |
| 5244 | |
| 5245 | /* Remove the key from posix_putenv_garbage; |
| 5246 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5247 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5248 | * old value was still accessible until then. |
| 5249 | */ |
| 5250 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5251 | PyTuple_GET_ITEM(args, 0))) { |
| 5252 | /* really not much we can do; just leak */ |
| 5253 | PyErr_Clear(); |
| 5254 | } |
| 5255 | |
| 5256 | Py_INCREF(Py_None); |
| 5257 | return Py_None; |
| 5258 | } |
| 5259 | #endif /* unsetenv */ |
| 5260 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5261 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5262 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5263 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5264 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5265 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5266 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5267 | { |
| 5268 | int code; |
| 5269 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5270 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5271 | return NULL; |
| 5272 | message = strerror(code); |
| 5273 | if (message == NULL) { |
| 5274 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5275 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5276 | return NULL; |
| 5277 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5278 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5279 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5280 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5281 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5282 | #ifdef HAVE_SYS_WAIT_H |
| 5283 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5284 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5285 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5286 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5287 | 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] | 5288 | |
| 5289 | static PyObject * |
| 5290 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5291 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5292 | WAIT_TYPE status; |
| 5293 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5294 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5295 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5296 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5297 | |
| 5298 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5299 | } |
| 5300 | #endif /* WCOREDUMP */ |
| 5301 | |
| 5302 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5303 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5304 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5305 | 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] | 5306 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5307 | |
| 5308 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5309 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5310 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5311 | WAIT_TYPE status; |
| 5312 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5313 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5314 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5315 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5316 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5317 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5318 | } |
| 5319 | #endif /* WIFCONTINUED */ |
| 5320 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5321 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5322 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5323 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5324 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5325 | |
| 5326 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5327 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5328 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5329 | WAIT_TYPE status; |
| 5330 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5331 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5332 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5333 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5334 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5335 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5336 | } |
| 5337 | #endif /* WIFSTOPPED */ |
| 5338 | |
| 5339 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5340 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5341 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5342 | 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] | 5343 | |
| 5344 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5345 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5346 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5347 | WAIT_TYPE status; |
| 5348 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5349 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5350 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5351 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5352 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5353 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5354 | } |
| 5355 | #endif /* WIFSIGNALED */ |
| 5356 | |
| 5357 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5358 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5359 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5360 | 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] | 5361 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5362 | |
| 5363 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5364 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5365 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5366 | WAIT_TYPE status; |
| 5367 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5368 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5369 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5370 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5371 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5372 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5373 | } |
| 5374 | #endif /* WIFEXITED */ |
| 5375 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5376 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5377 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5378 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5379 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5380 | |
| 5381 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5382 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5383 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5384 | WAIT_TYPE status; |
| 5385 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5386 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5387 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5388 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5389 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5390 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5391 | } |
| 5392 | #endif /* WEXITSTATUS */ |
| 5393 | |
| 5394 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5395 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5396 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5397 | 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] | 5398 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5399 | |
| 5400 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5401 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5402 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5403 | WAIT_TYPE status; |
| 5404 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5405 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5406 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5407 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5408 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5409 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5410 | } |
| 5411 | #endif /* WTERMSIG */ |
| 5412 | |
| 5413 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5414 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5415 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5416 | Return the signal that stopped the process that provided\n\ |
| 5417 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5418 | |
| 5419 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5420 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5421 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5422 | WAIT_TYPE status; |
| 5423 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5424 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5425 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5426 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5427 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5428 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5429 | } |
| 5430 | #endif /* WSTOPSIG */ |
| 5431 | |
| 5432 | #endif /* HAVE_SYS_WAIT_H */ |
| 5433 | |
| 5434 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5435 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5436 | #ifdef _SCO_DS |
| 5437 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5438 | needed definitions in sys/statvfs.h */ |
| 5439 | #define _SVID3 |
| 5440 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5441 | #include <sys/statvfs.h> |
| 5442 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5443 | static PyObject* |
| 5444 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5445 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5446 | if (v == NULL) |
| 5447 | return NULL; |
| 5448 | |
| 5449 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5450 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5451 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5452 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5453 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5454 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5455 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5456 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5457 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 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 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5461 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5462 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5463 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5464 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5465 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5466 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5467 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5468 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5469 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5470 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5471 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5472 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5473 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5474 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5475 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5476 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5477 | #endif |
| 5478 | |
| 5479 | return v; |
| 5480 | } |
| 5481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5482 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5483 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5484 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5485 | |
| 5486 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5487 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5488 | { |
| 5489 | int fd, res; |
| 5490 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5491 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5492 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5493 | return NULL; |
| 5494 | Py_BEGIN_ALLOW_THREADS |
| 5495 | res = fstatvfs(fd, &st); |
| 5496 | Py_END_ALLOW_THREADS |
| 5497 | if (res != 0) |
| 5498 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5499 | |
| 5500 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5501 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5502 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5503 | |
| 5504 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5505 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5506 | #include <sys/statvfs.h> |
| 5507 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5508 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5509 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5510 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5511 | |
| 5512 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5513 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5514 | { |
| 5515 | char *path; |
| 5516 | int res; |
| 5517 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5518 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5519 | return NULL; |
| 5520 | Py_BEGIN_ALLOW_THREADS |
| 5521 | res = statvfs(path, &st); |
| 5522 | Py_END_ALLOW_THREADS |
| 5523 | if (res != 0) |
| 5524 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5525 | |
| 5526 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5527 | } |
| 5528 | #endif /* HAVE_STATVFS */ |
| 5529 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5530 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5531 | * It maps strings representing configuration variable names to |
| 5532 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5533 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5534 | * rarely-used constants. There are three separate tables that use |
| 5535 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5536 | * |
| 5537 | * This code is always included, even if none of the interfaces that |
| 5538 | * need it are included. The #if hackery needed to avoid it would be |
| 5539 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5540 | */ |
| 5541 | struct constdef { |
| 5542 | char *name; |
| 5543 | long value; |
| 5544 | }; |
| 5545 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5546 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5547 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5548 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5549 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5550 | if (PyLong_Check(arg)) { |
| 5551 | *valuep = PyLong_AS_LONG(arg); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5552 | return 1; |
| 5553 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5554 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5555 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5556 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5557 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5558 | size_t hi = tablesize; |
| 5559 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5560 | const char *confname; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5561 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5562 | PyErr_SetString(PyExc_TypeError, |
| 5563 | "configuration names must be strings or integers"); |
| 5564 | return 0; |
| 5565 | } |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5566 | confname = PyUnicode_AsString(arg); |
| 5567 | if (confname == NULL) |
| 5568 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5569 | while (lo < hi) { |
| 5570 | mid = (lo + hi) / 2; |
| 5571 | cmp = strcmp(confname, table[mid].name); |
| 5572 | if (cmp < 0) |
| 5573 | hi = mid; |
| 5574 | else if (cmp > 0) |
| 5575 | lo = mid + 1; |
| 5576 | else { |
| 5577 | *valuep = table[mid].value; |
| 5578 | return 1; |
| 5579 | } |
| 5580 | } |
| 5581 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5582 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5583 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | |
| 5587 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5588 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5589 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5590 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5591 | #endif |
| 5592 | #ifdef _PC_ABI_ASYNC_IO |
| 5593 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5594 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5595 | #ifdef _PC_ASYNC_IO |
| 5596 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5597 | #endif |
| 5598 | #ifdef _PC_CHOWN_RESTRICTED |
| 5599 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5600 | #endif |
| 5601 | #ifdef _PC_FILESIZEBITS |
| 5602 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5603 | #endif |
| 5604 | #ifdef _PC_LAST |
| 5605 | {"PC_LAST", _PC_LAST}, |
| 5606 | #endif |
| 5607 | #ifdef _PC_LINK_MAX |
| 5608 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5609 | #endif |
| 5610 | #ifdef _PC_MAX_CANON |
| 5611 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5612 | #endif |
| 5613 | #ifdef _PC_MAX_INPUT |
| 5614 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5615 | #endif |
| 5616 | #ifdef _PC_NAME_MAX |
| 5617 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5618 | #endif |
| 5619 | #ifdef _PC_NO_TRUNC |
| 5620 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5621 | #endif |
| 5622 | #ifdef _PC_PATH_MAX |
| 5623 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5624 | #endif |
| 5625 | #ifdef _PC_PIPE_BUF |
| 5626 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5627 | #endif |
| 5628 | #ifdef _PC_PRIO_IO |
| 5629 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5630 | #endif |
| 5631 | #ifdef _PC_SOCK_MAXBUF |
| 5632 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5633 | #endif |
| 5634 | #ifdef _PC_SYNC_IO |
| 5635 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5636 | #endif |
| 5637 | #ifdef _PC_VDISABLE |
| 5638 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5639 | #endif |
| 5640 | }; |
| 5641 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5642 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5643 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5644 | { |
| 5645 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5646 | sizeof(posix_constants_pathconf) |
| 5647 | / sizeof(struct constdef)); |
| 5648 | } |
| 5649 | #endif |
| 5650 | |
| 5651 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5652 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5653 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5654 | 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] | 5655 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5656 | |
| 5657 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5658 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5659 | { |
| 5660 | PyObject *result = NULL; |
| 5661 | int name, fd; |
| 5662 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5663 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5664 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5665 | long limit; |
| 5666 | |
| 5667 | errno = 0; |
| 5668 | limit = fpathconf(fd, name); |
| 5669 | if (limit == -1 && errno != 0) |
| 5670 | posix_error(); |
| 5671 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5672 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5673 | } |
| 5674 | return result; |
| 5675 | } |
| 5676 | #endif |
| 5677 | |
| 5678 | |
| 5679 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5680 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5681 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5682 | 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] | 5683 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5684 | |
| 5685 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5686 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5687 | { |
| 5688 | PyObject *result = NULL; |
| 5689 | int name; |
| 5690 | char *path; |
| 5691 | |
| 5692 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5693 | conv_path_confname, &name)) { |
| 5694 | long limit; |
| 5695 | |
| 5696 | errno = 0; |
| 5697 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5698 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5699 | if (errno == EINVAL) |
| 5700 | /* could be a path or name problem */ |
| 5701 | posix_error(); |
| 5702 | else |
| 5703 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5704 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5705 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5706 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5707 | } |
| 5708 | return result; |
| 5709 | } |
| 5710 | #endif |
| 5711 | |
| 5712 | #ifdef HAVE_CONFSTR |
| 5713 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5714 | #ifdef _CS_ARCHITECTURE |
| 5715 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5716 | #endif |
| 5717 | #ifdef _CS_HOSTNAME |
| 5718 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5719 | #endif |
| 5720 | #ifdef _CS_HW_PROVIDER |
| 5721 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5722 | #endif |
| 5723 | #ifdef _CS_HW_SERIAL |
| 5724 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5725 | #endif |
| 5726 | #ifdef _CS_INITTAB_NAME |
| 5727 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5728 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5729 | #ifdef _CS_LFS64_CFLAGS |
| 5730 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5731 | #endif |
| 5732 | #ifdef _CS_LFS64_LDFLAGS |
| 5733 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5734 | #endif |
| 5735 | #ifdef _CS_LFS64_LIBS |
| 5736 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5737 | #endif |
| 5738 | #ifdef _CS_LFS64_LINTFLAGS |
| 5739 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5740 | #endif |
| 5741 | #ifdef _CS_LFS_CFLAGS |
| 5742 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5743 | #endif |
| 5744 | #ifdef _CS_LFS_LDFLAGS |
| 5745 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5746 | #endif |
| 5747 | #ifdef _CS_LFS_LIBS |
| 5748 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5749 | #endif |
| 5750 | #ifdef _CS_LFS_LINTFLAGS |
| 5751 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5752 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5753 | #ifdef _CS_MACHINE |
| 5754 | {"CS_MACHINE", _CS_MACHINE}, |
| 5755 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5756 | #ifdef _CS_PATH |
| 5757 | {"CS_PATH", _CS_PATH}, |
| 5758 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5759 | #ifdef _CS_RELEASE |
| 5760 | {"CS_RELEASE", _CS_RELEASE}, |
| 5761 | #endif |
| 5762 | #ifdef _CS_SRPC_DOMAIN |
| 5763 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5764 | #endif |
| 5765 | #ifdef _CS_SYSNAME |
| 5766 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5767 | #endif |
| 5768 | #ifdef _CS_VERSION |
| 5769 | {"CS_VERSION", _CS_VERSION}, |
| 5770 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5771 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5772 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5773 | #endif |
| 5774 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5775 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5776 | #endif |
| 5777 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5778 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5779 | #endif |
| 5780 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5781 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5782 | #endif |
| 5783 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5784 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5785 | #endif |
| 5786 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5787 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5788 | #endif |
| 5789 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5790 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5791 | #endif |
| 5792 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5793 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5794 | #endif |
| 5795 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5796 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5797 | #endif |
| 5798 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5799 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5800 | #endif |
| 5801 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5802 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5803 | #endif |
| 5804 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5805 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5806 | #endif |
| 5807 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5808 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5809 | #endif |
| 5810 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5811 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5812 | #endif |
| 5813 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5814 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5815 | #endif |
| 5816 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5817 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5818 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5819 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5820 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5821 | #endif |
| 5822 | #ifdef _MIPS_CS_BASE |
| 5823 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5824 | #endif |
| 5825 | #ifdef _MIPS_CS_HOSTID |
| 5826 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5827 | #endif |
| 5828 | #ifdef _MIPS_CS_HW_NAME |
| 5829 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5830 | #endif |
| 5831 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5832 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5833 | #endif |
| 5834 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5835 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5836 | #endif |
| 5837 | #ifdef _MIPS_CS_OSREL_MIN |
| 5838 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5839 | #endif |
| 5840 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5841 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5842 | #endif |
| 5843 | #ifdef _MIPS_CS_OS_NAME |
| 5844 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5845 | #endif |
| 5846 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5847 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5848 | #endif |
| 5849 | #ifdef _MIPS_CS_PROCESSORS |
| 5850 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5851 | #endif |
| 5852 | #ifdef _MIPS_CS_SERIAL |
| 5853 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5854 | #endif |
| 5855 | #ifdef _MIPS_CS_VENDOR |
| 5856 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5857 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5858 | }; |
| 5859 | |
| 5860 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5861 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5862 | { |
| 5863 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5864 | sizeof(posix_constants_confstr) |
| 5865 | / sizeof(struct constdef)); |
| 5866 | } |
| 5867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5868 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5869 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5870 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5871 | |
| 5872 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5873 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5874 | { |
| 5875 | PyObject *result = NULL; |
| 5876 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5877 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5878 | |
| 5879 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5880 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5881 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5882 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5883 | len = confstr(name, buffer, sizeof(buffer)); |
| 5884 | if (len == 0) { |
| 5885 | if (errno) { |
| 5886 | posix_error(); |
| 5887 | } |
| 5888 | else { |
| 5889 | result = Py_None; |
| 5890 | Py_INCREF(Py_None); |
| 5891 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5892 | } |
| 5893 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5894 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5895 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5896 | if (result != NULL) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5897 | confstr(name, PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5898 | } |
| 5899 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5900 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5901 | } |
| 5902 | } |
| 5903 | return result; |
| 5904 | } |
| 5905 | #endif |
| 5906 | |
| 5907 | |
| 5908 | #ifdef HAVE_SYSCONF |
| 5909 | static struct constdef posix_constants_sysconf[] = { |
| 5910 | #ifdef _SC_2_CHAR_TERM |
| 5911 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5912 | #endif |
| 5913 | #ifdef _SC_2_C_BIND |
| 5914 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5915 | #endif |
| 5916 | #ifdef _SC_2_C_DEV |
| 5917 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5918 | #endif |
| 5919 | #ifdef _SC_2_C_VERSION |
| 5920 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5921 | #endif |
| 5922 | #ifdef _SC_2_FORT_DEV |
| 5923 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5924 | #endif |
| 5925 | #ifdef _SC_2_FORT_RUN |
| 5926 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5927 | #endif |
| 5928 | #ifdef _SC_2_LOCALEDEF |
| 5929 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5930 | #endif |
| 5931 | #ifdef _SC_2_SW_DEV |
| 5932 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5933 | #endif |
| 5934 | #ifdef _SC_2_UPE |
| 5935 | {"SC_2_UPE", _SC_2_UPE}, |
| 5936 | #endif |
| 5937 | #ifdef _SC_2_VERSION |
| 5938 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5939 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5940 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5941 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5942 | #endif |
| 5943 | #ifdef _SC_ACL |
| 5944 | {"SC_ACL", _SC_ACL}, |
| 5945 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5946 | #ifdef _SC_AIO_LISTIO_MAX |
| 5947 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5948 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5949 | #ifdef _SC_AIO_MAX |
| 5950 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5951 | #endif |
| 5952 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5953 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5954 | #endif |
| 5955 | #ifdef _SC_ARG_MAX |
| 5956 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5957 | #endif |
| 5958 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5959 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5960 | #endif |
| 5961 | #ifdef _SC_ATEXIT_MAX |
| 5962 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5963 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5964 | #ifdef _SC_AUDIT |
| 5965 | {"SC_AUDIT", _SC_AUDIT}, |
| 5966 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5967 | #ifdef _SC_AVPHYS_PAGES |
| 5968 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5969 | #endif |
| 5970 | #ifdef _SC_BC_BASE_MAX |
| 5971 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5972 | #endif |
| 5973 | #ifdef _SC_BC_DIM_MAX |
| 5974 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5975 | #endif |
| 5976 | #ifdef _SC_BC_SCALE_MAX |
| 5977 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5978 | #endif |
| 5979 | #ifdef _SC_BC_STRING_MAX |
| 5980 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5981 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5982 | #ifdef _SC_CAP |
| 5983 | {"SC_CAP", _SC_CAP}, |
| 5984 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5985 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5986 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5987 | #endif |
| 5988 | #ifdef _SC_CHAR_BIT |
| 5989 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5990 | #endif |
| 5991 | #ifdef _SC_CHAR_MAX |
| 5992 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5993 | #endif |
| 5994 | #ifdef _SC_CHAR_MIN |
| 5995 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5996 | #endif |
| 5997 | #ifdef _SC_CHILD_MAX |
| 5998 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5999 | #endif |
| 6000 | #ifdef _SC_CLK_TCK |
| 6001 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6002 | #endif |
| 6003 | #ifdef _SC_COHER_BLKSZ |
| 6004 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6005 | #endif |
| 6006 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6007 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6008 | #endif |
| 6009 | #ifdef _SC_DCACHE_ASSOC |
| 6010 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6011 | #endif |
| 6012 | #ifdef _SC_DCACHE_BLKSZ |
| 6013 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6014 | #endif |
| 6015 | #ifdef _SC_DCACHE_LINESZ |
| 6016 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6017 | #endif |
| 6018 | #ifdef _SC_DCACHE_SZ |
| 6019 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6020 | #endif |
| 6021 | #ifdef _SC_DCACHE_TBLKSZ |
| 6022 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6023 | #endif |
| 6024 | #ifdef _SC_DELAYTIMER_MAX |
| 6025 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6026 | #endif |
| 6027 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6028 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6029 | #endif |
| 6030 | #ifdef _SC_EXPR_NEST_MAX |
| 6031 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6032 | #endif |
| 6033 | #ifdef _SC_FSYNC |
| 6034 | {"SC_FSYNC", _SC_FSYNC}, |
| 6035 | #endif |
| 6036 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6037 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6038 | #endif |
| 6039 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6040 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6041 | #endif |
| 6042 | #ifdef _SC_ICACHE_ASSOC |
| 6043 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6044 | #endif |
| 6045 | #ifdef _SC_ICACHE_BLKSZ |
| 6046 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6047 | #endif |
| 6048 | #ifdef _SC_ICACHE_LINESZ |
| 6049 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6050 | #endif |
| 6051 | #ifdef _SC_ICACHE_SZ |
| 6052 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6053 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6054 | #ifdef _SC_INF |
| 6055 | {"SC_INF", _SC_INF}, |
| 6056 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6057 | #ifdef _SC_INT_MAX |
| 6058 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6059 | #endif |
| 6060 | #ifdef _SC_INT_MIN |
| 6061 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6062 | #endif |
| 6063 | #ifdef _SC_IOV_MAX |
| 6064 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6065 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6066 | #ifdef _SC_IP_SECOPTS |
| 6067 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6068 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6069 | #ifdef _SC_JOB_CONTROL |
| 6070 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6071 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6072 | #ifdef _SC_KERN_POINTERS |
| 6073 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6074 | #endif |
| 6075 | #ifdef _SC_KERN_SIM |
| 6076 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6077 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6078 | #ifdef _SC_LINE_MAX |
| 6079 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6080 | #endif |
| 6081 | #ifdef _SC_LOGIN_NAME_MAX |
| 6082 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6083 | #endif |
| 6084 | #ifdef _SC_LOGNAME_MAX |
| 6085 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6086 | #endif |
| 6087 | #ifdef _SC_LONG_BIT |
| 6088 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6089 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6090 | #ifdef _SC_MAC |
| 6091 | {"SC_MAC", _SC_MAC}, |
| 6092 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6093 | #ifdef _SC_MAPPED_FILES |
| 6094 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6095 | #endif |
| 6096 | #ifdef _SC_MAXPID |
| 6097 | {"SC_MAXPID", _SC_MAXPID}, |
| 6098 | #endif |
| 6099 | #ifdef _SC_MB_LEN_MAX |
| 6100 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6101 | #endif |
| 6102 | #ifdef _SC_MEMLOCK |
| 6103 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6104 | #endif |
| 6105 | #ifdef _SC_MEMLOCK_RANGE |
| 6106 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6107 | #endif |
| 6108 | #ifdef _SC_MEMORY_PROTECTION |
| 6109 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6110 | #endif |
| 6111 | #ifdef _SC_MESSAGE_PASSING |
| 6112 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6113 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6114 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6115 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6116 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6117 | #ifdef _SC_MQ_OPEN_MAX |
| 6118 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6119 | #endif |
| 6120 | #ifdef _SC_MQ_PRIO_MAX |
| 6121 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6122 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6123 | #ifdef _SC_NACLS_MAX |
| 6124 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6125 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6126 | #ifdef _SC_NGROUPS_MAX |
| 6127 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6128 | #endif |
| 6129 | #ifdef _SC_NL_ARGMAX |
| 6130 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6131 | #endif |
| 6132 | #ifdef _SC_NL_LANGMAX |
| 6133 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6134 | #endif |
| 6135 | #ifdef _SC_NL_MSGMAX |
| 6136 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6137 | #endif |
| 6138 | #ifdef _SC_NL_NMAX |
| 6139 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6140 | #endif |
| 6141 | #ifdef _SC_NL_SETMAX |
| 6142 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6143 | #endif |
| 6144 | #ifdef _SC_NL_TEXTMAX |
| 6145 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6146 | #endif |
| 6147 | #ifdef _SC_NPROCESSORS_CONF |
| 6148 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6149 | #endif |
| 6150 | #ifdef _SC_NPROCESSORS_ONLN |
| 6151 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6152 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6153 | #ifdef _SC_NPROC_CONF |
| 6154 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6155 | #endif |
| 6156 | #ifdef _SC_NPROC_ONLN |
| 6157 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6158 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6159 | #ifdef _SC_NZERO |
| 6160 | {"SC_NZERO", _SC_NZERO}, |
| 6161 | #endif |
| 6162 | #ifdef _SC_OPEN_MAX |
| 6163 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6164 | #endif |
| 6165 | #ifdef _SC_PAGESIZE |
| 6166 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6167 | #endif |
| 6168 | #ifdef _SC_PAGE_SIZE |
| 6169 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6170 | #endif |
| 6171 | #ifdef _SC_PASS_MAX |
| 6172 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6173 | #endif |
| 6174 | #ifdef _SC_PHYS_PAGES |
| 6175 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6176 | #endif |
| 6177 | #ifdef _SC_PII |
| 6178 | {"SC_PII", _SC_PII}, |
| 6179 | #endif |
| 6180 | #ifdef _SC_PII_INTERNET |
| 6181 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6182 | #endif |
| 6183 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6184 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6185 | #endif |
| 6186 | #ifdef _SC_PII_INTERNET_STREAM |
| 6187 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6188 | #endif |
| 6189 | #ifdef _SC_PII_OSI |
| 6190 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6191 | #endif |
| 6192 | #ifdef _SC_PII_OSI_CLTS |
| 6193 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6194 | #endif |
| 6195 | #ifdef _SC_PII_OSI_COTS |
| 6196 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6197 | #endif |
| 6198 | #ifdef _SC_PII_OSI_M |
| 6199 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6200 | #endif |
| 6201 | #ifdef _SC_PII_SOCKET |
| 6202 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6203 | #endif |
| 6204 | #ifdef _SC_PII_XTI |
| 6205 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6206 | #endif |
| 6207 | #ifdef _SC_POLL |
| 6208 | {"SC_POLL", _SC_POLL}, |
| 6209 | #endif |
| 6210 | #ifdef _SC_PRIORITIZED_IO |
| 6211 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6212 | #endif |
| 6213 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6214 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6215 | #endif |
| 6216 | #ifdef _SC_REALTIME_SIGNALS |
| 6217 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6218 | #endif |
| 6219 | #ifdef _SC_RE_DUP_MAX |
| 6220 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6221 | #endif |
| 6222 | #ifdef _SC_RTSIG_MAX |
| 6223 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6224 | #endif |
| 6225 | #ifdef _SC_SAVED_IDS |
| 6226 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6227 | #endif |
| 6228 | #ifdef _SC_SCHAR_MAX |
| 6229 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6230 | #endif |
| 6231 | #ifdef _SC_SCHAR_MIN |
| 6232 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6233 | #endif |
| 6234 | #ifdef _SC_SELECT |
| 6235 | {"SC_SELECT", _SC_SELECT}, |
| 6236 | #endif |
| 6237 | #ifdef _SC_SEMAPHORES |
| 6238 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6239 | #endif |
| 6240 | #ifdef _SC_SEM_NSEMS_MAX |
| 6241 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6242 | #endif |
| 6243 | #ifdef _SC_SEM_VALUE_MAX |
| 6244 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6245 | #endif |
| 6246 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6247 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6248 | #endif |
| 6249 | #ifdef _SC_SHRT_MAX |
| 6250 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6251 | #endif |
| 6252 | #ifdef _SC_SHRT_MIN |
| 6253 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6254 | #endif |
| 6255 | #ifdef _SC_SIGQUEUE_MAX |
| 6256 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6257 | #endif |
| 6258 | #ifdef _SC_SIGRT_MAX |
| 6259 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6260 | #endif |
| 6261 | #ifdef _SC_SIGRT_MIN |
| 6262 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6263 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6264 | #ifdef _SC_SOFTPOWER |
| 6265 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6266 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6267 | #ifdef _SC_SPLIT_CACHE |
| 6268 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6269 | #endif |
| 6270 | #ifdef _SC_SSIZE_MAX |
| 6271 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6272 | #endif |
| 6273 | #ifdef _SC_STACK_PROT |
| 6274 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6275 | #endif |
| 6276 | #ifdef _SC_STREAM_MAX |
| 6277 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6278 | #endif |
| 6279 | #ifdef _SC_SYNCHRONIZED_IO |
| 6280 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6281 | #endif |
| 6282 | #ifdef _SC_THREADS |
| 6283 | {"SC_THREADS", _SC_THREADS}, |
| 6284 | #endif |
| 6285 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6286 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6287 | #endif |
| 6288 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6289 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6290 | #endif |
| 6291 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6292 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6293 | #endif |
| 6294 | #ifdef _SC_THREAD_KEYS_MAX |
| 6295 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6296 | #endif |
| 6297 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6298 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6299 | #endif |
| 6300 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6301 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6302 | #endif |
| 6303 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6304 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6305 | #endif |
| 6306 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6307 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6308 | #endif |
| 6309 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6310 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6311 | #endif |
| 6312 | #ifdef _SC_THREAD_STACK_MIN |
| 6313 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6314 | #endif |
| 6315 | #ifdef _SC_THREAD_THREADS_MAX |
| 6316 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6317 | #endif |
| 6318 | #ifdef _SC_TIMERS |
| 6319 | {"SC_TIMERS", _SC_TIMERS}, |
| 6320 | #endif |
| 6321 | #ifdef _SC_TIMER_MAX |
| 6322 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6323 | #endif |
| 6324 | #ifdef _SC_TTY_NAME_MAX |
| 6325 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6326 | #endif |
| 6327 | #ifdef _SC_TZNAME_MAX |
| 6328 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6329 | #endif |
| 6330 | #ifdef _SC_T_IOV_MAX |
| 6331 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6332 | #endif |
| 6333 | #ifdef _SC_UCHAR_MAX |
| 6334 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6335 | #endif |
| 6336 | #ifdef _SC_UINT_MAX |
| 6337 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6338 | #endif |
| 6339 | #ifdef _SC_UIO_MAXIOV |
| 6340 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6341 | #endif |
| 6342 | #ifdef _SC_ULONG_MAX |
| 6343 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6344 | #endif |
| 6345 | #ifdef _SC_USHRT_MAX |
| 6346 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6347 | #endif |
| 6348 | #ifdef _SC_VERSION |
| 6349 | {"SC_VERSION", _SC_VERSION}, |
| 6350 | #endif |
| 6351 | #ifdef _SC_WORD_BIT |
| 6352 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6353 | #endif |
| 6354 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6355 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6356 | #endif |
| 6357 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6358 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6359 | #endif |
| 6360 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6361 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6362 | #endif |
| 6363 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6364 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6365 | #endif |
| 6366 | #ifdef _SC_XOPEN_CRYPT |
| 6367 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6368 | #endif |
| 6369 | #ifdef _SC_XOPEN_ENH_I18N |
| 6370 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6371 | #endif |
| 6372 | #ifdef _SC_XOPEN_LEGACY |
| 6373 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6374 | #endif |
| 6375 | #ifdef _SC_XOPEN_REALTIME |
| 6376 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6377 | #endif |
| 6378 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6379 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6380 | #endif |
| 6381 | #ifdef _SC_XOPEN_SHM |
| 6382 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6383 | #endif |
| 6384 | #ifdef _SC_XOPEN_UNIX |
| 6385 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6386 | #endif |
| 6387 | #ifdef _SC_XOPEN_VERSION |
| 6388 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6389 | #endif |
| 6390 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6391 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6392 | #endif |
| 6393 | #ifdef _SC_XOPEN_XPG2 |
| 6394 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6395 | #endif |
| 6396 | #ifdef _SC_XOPEN_XPG3 |
| 6397 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6398 | #endif |
| 6399 | #ifdef _SC_XOPEN_XPG4 |
| 6400 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6401 | #endif |
| 6402 | }; |
| 6403 | |
| 6404 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6405 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6406 | { |
| 6407 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6408 | sizeof(posix_constants_sysconf) |
| 6409 | / sizeof(struct constdef)); |
| 6410 | } |
| 6411 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6412 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6413 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6414 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6415 | |
| 6416 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6417 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6418 | { |
| 6419 | PyObject *result = NULL; |
| 6420 | int name; |
| 6421 | |
| 6422 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6423 | int value; |
| 6424 | |
| 6425 | errno = 0; |
| 6426 | value = sysconf(name); |
| 6427 | if (value == -1 && errno != 0) |
| 6428 | posix_error(); |
| 6429 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6430 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6431 | } |
| 6432 | return result; |
| 6433 | } |
| 6434 | #endif |
| 6435 | |
| 6436 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6437 | /* This code is used to ensure that the tables of configuration value names |
| 6438 | * are in sorted order as required by conv_confname(), and also to build the |
| 6439 | * the exported dictionaries that are used to publish information about the |
| 6440 | * names available on the host platform. |
| 6441 | * |
| 6442 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6443 | * when used, even for platforms we're not able to test on. It also makes |
| 6444 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6445 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6446 | |
| 6447 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6448 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6449 | { |
| 6450 | const struct constdef *c1 = |
| 6451 | (const struct constdef *) v1; |
| 6452 | const struct constdef *c2 = |
| 6453 | (const struct constdef *) v2; |
| 6454 | |
| 6455 | return strcmp(c1->name, c2->name); |
| 6456 | } |
| 6457 | |
| 6458 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6459 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6460 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6461 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6462 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6463 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6464 | |
| 6465 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6466 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6467 | if (d == NULL) |
| 6468 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6469 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6470 | for (i=0; i < tablesize; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6471 | PyObject *o = PyLong_FromLong(table[i].value); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6472 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6473 | Py_XDECREF(o); |
| 6474 | Py_DECREF(d); |
| 6475 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6476 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6477 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6478 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6479 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6480 | } |
| 6481 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6482 | /* Return -1 on failure, 0 on success. */ |
| 6483 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6484 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6485 | { |
| 6486 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6487 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6488 | sizeof(posix_constants_pathconf) |
| 6489 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6490 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6491 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6492 | #endif |
| 6493 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6494 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6495 | sizeof(posix_constants_confstr) |
| 6496 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6497 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6498 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6499 | #endif |
| 6500 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6501 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6502 | sizeof(posix_constants_sysconf) |
| 6503 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6504 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6505 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6506 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6507 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6508 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6509 | |
| 6510 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6511 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6512 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6513 | 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] | 6514 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6515 | |
| 6516 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6517 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6518 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6519 | abort(); |
| 6520 | /*NOTREACHED*/ |
| 6521 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6522 | return NULL; |
| 6523 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6524 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6525 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6526 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6527 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6528 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6529 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6530 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6531 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6532 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6533 | application (if any) its extension is associated.\n\ |
| 6534 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6535 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6536 | \n\ |
| 6537 | startfile returns as soon as the associated application is launched.\n\ |
| 6538 | There is no option to wait for the application to close, and no way\n\ |
| 6539 | to retrieve the application's exit status.\n\ |
| 6540 | \n\ |
| 6541 | The filepath is relative to the current directory. If you want to use\n\ |
| 6542 | 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] | 6543 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6544 | |
| 6545 | static PyObject * |
| 6546 | win32_startfile(PyObject *self, PyObject *args) |
| 6547 | { |
| 6548 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6549 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6550 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6551 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6552 | if (unicode_file_names()) { |
| 6553 | PyObject *unipath, *woperation = NULL; |
| 6554 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6555 | &unipath, &operation)) { |
| 6556 | PyErr_Clear(); |
| 6557 | goto normal; |
| 6558 | } |
| 6559 | |
| 6560 | |
| 6561 | if (operation) { |
| 6562 | woperation = PyUnicode_DecodeASCII(operation, |
| 6563 | strlen(operation), NULL); |
| 6564 | if (!woperation) { |
| 6565 | PyErr_Clear(); |
| 6566 | operation = NULL; |
| 6567 | goto normal; |
| 6568 | } |
| 6569 | } |
| 6570 | |
| 6571 | Py_BEGIN_ALLOW_THREADS |
| 6572 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6573 | PyUnicode_AS_UNICODE(unipath), |
| 6574 | NULL, NULL, SW_SHOWNORMAL); |
| 6575 | Py_END_ALLOW_THREADS |
| 6576 | |
| 6577 | Py_XDECREF(woperation); |
| 6578 | if (rc <= (HINSTANCE)32) { |
| 6579 | PyObject *errval = win32_error_unicode("startfile", |
| 6580 | PyUnicode_AS_UNICODE(unipath)); |
| 6581 | return errval; |
| 6582 | } |
| 6583 | Py_INCREF(Py_None); |
| 6584 | return Py_None; |
| 6585 | } |
| 6586 | #endif |
| 6587 | |
| 6588 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6589 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 6590 | Py_FileSystemDefaultEncoding, &filepath, |
| 6591 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6592 | return NULL; |
| 6593 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6594 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6595 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6596 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6597 | if (rc <= (HINSTANCE)32) { |
| 6598 | PyObject *errval = win32_error("startfile", filepath); |
| 6599 | PyMem_Free(filepath); |
| 6600 | return errval; |
| 6601 | } |
| 6602 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6603 | Py_INCREF(Py_None); |
| 6604 | return Py_None; |
| 6605 | } |
| 6606 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6607 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6608 | #ifdef HAVE_GETLOADAVG |
| 6609 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6610 | "getloadavg() -> (float, float, float)\n\n\ |
| 6611 | Return the number of processes in the system run queue averaged over\n\ |
| 6612 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6613 | was unobtainable"); |
| 6614 | |
| 6615 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6616 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6617 | { |
| 6618 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6619 | if (getloadavg(loadavg, 3)!=3) { |
| 6620 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6621 | return NULL; |
| 6622 | } else |
| 6623 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6624 | } |
| 6625 | #endif |
| 6626 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6627 | #ifdef MS_WINDOWS |
| 6628 | |
| 6629 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6630 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6631 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6632 | |
| 6633 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6634 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6635 | DWORD dwFlags ); |
| 6636 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6637 | BYTE *pbBuffer ); |
| 6638 | |
| 6639 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6640 | /* This handle is never explicitly released. Instead, the operating |
| 6641 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6642 | static HCRYPTPROV hCryptProv = 0; |
| 6643 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6644 | static PyObject* |
| 6645 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6646 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6647 | int howMany; |
| 6648 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6649 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6650 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6651 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6652 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6653 | if (howMany < 0) |
| 6654 | return PyErr_Format(PyExc_ValueError, |
| 6655 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6656 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6657 | if (hCryptProv == 0) { |
| 6658 | HINSTANCE hAdvAPI32 = NULL; |
| 6659 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
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 | /* Obtain handle to the DLL containing CryptoAPI |
| 6662 | This should not fail */ |
| 6663 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6664 | if(hAdvAPI32 == NULL) |
| 6665 | return win32_error("GetModuleHandle", NULL); |
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 | /* Obtain pointers to the CryptoAPI functions |
| 6668 | This will fail on some early versions of Win95 */ |
| 6669 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6670 | hAdvAPI32, |
| 6671 | "CryptAcquireContextA"); |
| 6672 | if (pCryptAcquireContext == NULL) |
| 6673 | return PyErr_Format(PyExc_NotImplementedError, |
| 6674 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6675 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6676 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6677 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6678 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6679 | return PyErr_Format(PyExc_NotImplementedError, |
| 6680 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6681 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6682 | /* Acquire context */ |
| 6683 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6684 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6685 | return win32_error("CryptAcquireContext", NULL); |
| 6686 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6687 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6688 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6689 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6690 | if (result != NULL) { |
| 6691 | /* Get random data */ |
| 6692 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6693 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6694 | Py_DECREF(result); |
| 6695 | return win32_error("CryptGenRandom", NULL); |
| 6696 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6697 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6698 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6699 | } |
| 6700 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6701 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6702 | PyDoc_STRVAR(device_encoding__doc__, |
| 6703 | "device_encoding(fd) -> str\n\n\ |
| 6704 | Return a string describing the encoding of the device\n\ |
| 6705 | if the output is a terminal; else return None."); |
| 6706 | |
| 6707 | static PyObject * |
| 6708 | device_encoding(PyObject *self, PyObject *args) |
| 6709 | { |
| 6710 | int fd; |
| 6711 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6712 | return NULL; |
| 6713 | if (!isatty(fd)) { |
| 6714 | Py_INCREF(Py_None); |
| 6715 | return Py_None; |
| 6716 | } |
| 6717 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6718 | if (fd == 0) { |
| 6719 | char buf[100]; |
| 6720 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6721 | return PyUnicode_FromString(buf); |
| 6722 | } |
| 6723 | if (fd == 1 || fd == 2) { |
| 6724 | char buf[100]; |
| 6725 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6726 | return PyUnicode_FromString(buf); |
| 6727 | } |
| 6728 | #elif defined(CODESET) |
| 6729 | { |
| 6730 | char *codeset = nl_langinfo(CODESET); |
| 6731 | if (codeset) |
| 6732 | return PyUnicode_FromString(codeset); |
| 6733 | } |
| 6734 | #endif |
| 6735 | Py_INCREF(Py_None); |
| 6736 | return Py_None; |
| 6737 | } |
| 6738 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6739 | #ifdef __VMS |
| 6740 | /* Use openssl random routine */ |
| 6741 | #include <openssl/rand.h> |
| 6742 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6743 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6744 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6745 | |
| 6746 | static PyObject* |
| 6747 | vms_urandom(PyObject *self, PyObject *args) |
| 6748 | { |
| 6749 | int howMany; |
| 6750 | PyObject* result; |
| 6751 | |
| 6752 | /* Read arguments */ |
| 6753 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6754 | return NULL; |
| 6755 | if (howMany < 0) |
| 6756 | return PyErr_Format(PyExc_ValueError, |
| 6757 | "negative argument not allowed"); |
| 6758 | |
| 6759 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6760 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6761 | if (result != NULL) { |
| 6762 | /* Get random data */ |
| 6763 | if (RAND_pseudo_bytes((unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6764 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6765 | howMany) < 0) { |
| 6766 | Py_DECREF(result); |
| 6767 | return PyErr_Format(PyExc_ValueError, |
| 6768 | "RAND_pseudo_bytes"); |
| 6769 | } |
| 6770 | } |
| 6771 | return result; |
| 6772 | } |
| 6773 | #endif |
| 6774 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6775 | static PyMethodDef posix_methods[] = { |
| 6776 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6777 | #ifdef HAVE_TTYNAME |
| 6778 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6779 | #endif |
| 6780 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6781 | #ifdef HAVE_CHFLAGS |
| 6782 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 6783 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6784 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 6785 | #ifdef HAVE_FCHMOD |
| 6786 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
| 6787 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6788 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6789 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6790 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 6791 | #ifdef HAVE_LCHMOD |
| 6792 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
| 6793 | #endif /* HAVE_LCHMOD */ |
| 6794 | #ifdef HAVE_FCHOWN |
| 6795 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
| 6796 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6797 | #ifdef HAVE_LCHFLAGS |
| 6798 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 6799 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6800 | #ifdef HAVE_LCHOWN |
| 6801 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6802 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6803 | #ifdef HAVE_CHROOT |
| 6804 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6805 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6806 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6807 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6808 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6809 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6810 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6811 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6812 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6813 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6814 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6815 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6816 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6817 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6818 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6819 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6820 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6821 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6822 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6823 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6824 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6825 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6826 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6827 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6828 | {"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] | 6829 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6830 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6831 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6832 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6833 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6834 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6835 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6836 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6837 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6838 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6839 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6840 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6841 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6842 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6843 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6844 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6845 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6846 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6847 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6848 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6849 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6850 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6851 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6852 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6853 | #if defined(PYOS_OS2) |
| 6854 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 6855 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 6856 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6857 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6858 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6859 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6860 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6861 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6862 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6863 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6864 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6865 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6866 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6867 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6868 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6869 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6870 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6871 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6872 | #endif /* HAVE_GETEGID */ |
| 6873 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6874 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6875 | #endif /* HAVE_GETEUID */ |
| 6876 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6877 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6878 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6879 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6880 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6881 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6882 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6883 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6884 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6885 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6886 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6887 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6888 | #endif /* HAVE_GETPPID */ |
| 6889 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6890 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6891 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6892 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6893 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6894 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6895 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6896 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6897 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6898 | #ifdef HAVE_KILLPG |
| 6899 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6900 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6901 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6902 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6903 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 6904 | #ifdef MS_WINDOWS |
| 6905 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 6906 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6907 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6908 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6909 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6910 | #ifdef HAVE_SETEUID |
| 6911 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6912 | #endif /* HAVE_SETEUID */ |
| 6913 | #ifdef HAVE_SETEGID |
| 6914 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6915 | #endif /* HAVE_SETEGID */ |
| 6916 | #ifdef HAVE_SETREUID |
| 6917 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6918 | #endif /* HAVE_SETREUID */ |
| 6919 | #ifdef HAVE_SETREGID |
| 6920 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6921 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6922 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6923 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6924 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6925 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6926 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6927 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6928 | #ifdef HAVE_GETPGID |
| 6929 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 6930 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6931 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6932 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6933 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6934 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6935 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6936 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6937 | #ifdef HAVE_WAIT3 |
| 6938 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 6939 | #endif /* HAVE_WAIT3 */ |
| 6940 | #ifdef HAVE_WAIT4 |
| 6941 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 6942 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6943 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6944 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6945 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6946 | #ifdef HAVE_GETSID |
| 6947 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 6948 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6949 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6950 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6951 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6952 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6953 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6954 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6955 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6956 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6957 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6958 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6959 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6960 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6961 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6962 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6963 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6964 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6965 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6966 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6967 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6968 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6969 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6970 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6971 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6972 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6973 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6974 | #endif |
| 6975 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6976 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6977 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6978 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6979 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 6980 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6981 | #ifdef HAVE_DEVICE_MACROS |
| 6982 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 6983 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 6984 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 6985 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6986 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6987 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6988 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6989 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6990 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6991 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6992 | #ifdef HAVE_UNSETENV |
| 6993 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6994 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6995 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6996 | #ifdef HAVE_FCHDIR |
| 6997 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 6998 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6999 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7000 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7001 | #endif |
| 7002 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7003 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7004 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7005 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7006 | #ifdef WCOREDUMP |
| 7007 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7008 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7009 | #ifdef WIFCONTINUED |
| 7010 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7011 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7012 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7013 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7014 | #endif /* WIFSTOPPED */ |
| 7015 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7016 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7017 | #endif /* WIFSIGNALED */ |
| 7018 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7019 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7020 | #endif /* WIFEXITED */ |
| 7021 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7022 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7023 | #endif /* WEXITSTATUS */ |
| 7024 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7025 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7026 | #endif /* WTERMSIG */ |
| 7027 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7028 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7029 | #endif /* WSTOPSIG */ |
| 7030 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7031 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7032 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7033 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7034 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7035 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7036 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7037 | #ifdef HAVE_CONFSTR |
| 7038 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7039 | #endif |
| 7040 | #ifdef HAVE_SYSCONF |
| 7041 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7042 | #endif |
| 7043 | #ifdef HAVE_FPATHCONF |
| 7044 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7045 | #endif |
| 7046 | #ifdef HAVE_PATHCONF |
| 7047 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7048 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7049 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7050 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7051 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7052 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7053 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7054 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7055 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7056 | #ifdef MS_WINDOWS |
| 7057 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7058 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7059 | #ifdef __VMS |
| 7060 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 7061 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7062 | {NULL, NULL} /* Sentinel */ |
| 7063 | }; |
| 7064 | |
| 7065 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7066 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7067 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7068 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7069 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7072 | #if defined(PYOS_OS2) |
| 7073 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7074 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7075 | { |
| 7076 | APIRET rc; |
| 7077 | ULONG values[QSV_MAX+1]; |
| 7078 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7079 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7080 | |
| 7081 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7082 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7083 | Py_END_ALLOW_THREADS |
| 7084 | |
| 7085 | if (rc != NO_ERROR) { |
| 7086 | os2_error(rc); |
| 7087 | return -1; |
| 7088 | } |
| 7089 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7090 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7091 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7092 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7093 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7094 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7095 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7096 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7097 | |
| 7098 | switch (values[QSV_VERSION_MINOR]) { |
| 7099 | case 0: ver = "2.00"; break; |
| 7100 | case 10: ver = "2.10"; break; |
| 7101 | case 11: ver = "2.11"; break; |
| 7102 | case 30: ver = "3.00"; break; |
| 7103 | case 40: ver = "4.00"; break; |
| 7104 | case 50: ver = "5.00"; break; |
| 7105 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7106 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7107 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7108 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7109 | ver = &tmp[0]; |
| 7110 | } |
| 7111 | |
| 7112 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7113 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7114 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7115 | |
| 7116 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7117 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7118 | tmp[1] = ':'; |
| 7119 | tmp[2] = '\0'; |
| 7120 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7121 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7122 | } |
| 7123 | #endif |
| 7124 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7125 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7126 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7127 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7128 | #ifdef F_OK |
| 7129 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7130 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7131 | #ifdef R_OK |
| 7132 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7133 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7134 | #ifdef W_OK |
| 7135 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7136 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7137 | #ifdef X_OK |
| 7138 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7139 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7140 | #ifdef NGROUPS_MAX |
| 7141 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7142 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7143 | #ifdef TMP_MAX |
| 7144 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7145 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7146 | #ifdef WCONTINUED |
| 7147 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7148 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7149 | #ifdef WNOHANG |
| 7150 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7151 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7152 | #ifdef WUNTRACED |
| 7153 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7154 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7155 | #ifdef O_RDONLY |
| 7156 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7157 | #endif |
| 7158 | #ifdef O_WRONLY |
| 7159 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7160 | #endif |
| 7161 | #ifdef O_RDWR |
| 7162 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7163 | #endif |
| 7164 | #ifdef O_NDELAY |
| 7165 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7166 | #endif |
| 7167 | #ifdef O_NONBLOCK |
| 7168 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7169 | #endif |
| 7170 | #ifdef O_APPEND |
| 7171 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7172 | #endif |
| 7173 | #ifdef O_DSYNC |
| 7174 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7175 | #endif |
| 7176 | #ifdef O_RSYNC |
| 7177 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7178 | #endif |
| 7179 | #ifdef O_SYNC |
| 7180 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7181 | #endif |
| 7182 | #ifdef O_NOCTTY |
| 7183 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7184 | #endif |
| 7185 | #ifdef O_CREAT |
| 7186 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7187 | #endif |
| 7188 | #ifdef O_EXCL |
| 7189 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7190 | #endif |
| 7191 | #ifdef O_TRUNC |
| 7192 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7193 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7194 | #ifdef O_BINARY |
| 7195 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7196 | #endif |
| 7197 | #ifdef O_TEXT |
| 7198 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7199 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7200 | #ifdef O_LARGEFILE |
| 7201 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7202 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7203 | #ifdef O_SHLOCK |
| 7204 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7205 | #endif |
| 7206 | #ifdef O_EXLOCK |
| 7207 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7208 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7209 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7210 | /* MS Windows */ |
| 7211 | #ifdef O_NOINHERIT |
| 7212 | /* Don't inherit in child processes. */ |
| 7213 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7214 | #endif |
| 7215 | #ifdef _O_SHORT_LIVED |
| 7216 | /* Optimize for short life (keep in memory). */ |
| 7217 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7218 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7219 | #endif |
| 7220 | #ifdef O_TEMPORARY |
| 7221 | /* Automatically delete when last handle is closed. */ |
| 7222 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7223 | #endif |
| 7224 | #ifdef O_RANDOM |
| 7225 | /* Optimize for random access. */ |
| 7226 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7227 | #endif |
| 7228 | #ifdef O_SEQUENTIAL |
| 7229 | /* Optimize for sequential access. */ |
| 7230 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7231 | #endif |
| 7232 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7233 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7234 | #ifdef O_ASYNC |
| 7235 | /* Send a SIGIO signal whenever input or output |
| 7236 | becomes available on file descriptor */ |
| 7237 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
| 7238 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7239 | #ifdef O_DIRECT |
| 7240 | /* Direct disk access. */ |
| 7241 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7242 | #endif |
| 7243 | #ifdef O_DIRECTORY |
| 7244 | /* Must be a directory. */ |
| 7245 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7246 | #endif |
| 7247 | #ifdef O_NOFOLLOW |
| 7248 | /* Do not follow links. */ |
| 7249 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7250 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7251 | #ifdef O_NOATIME |
| 7252 | /* Do not update the access time. */ |
| 7253 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
| 7254 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7255 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7256 | /* These come from sysexits.h */ |
| 7257 | #ifdef EX_OK |
| 7258 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7259 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7260 | #ifdef EX_USAGE |
| 7261 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7262 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7263 | #ifdef EX_DATAERR |
| 7264 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7265 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7266 | #ifdef EX_NOINPUT |
| 7267 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7268 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7269 | #ifdef EX_NOUSER |
| 7270 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7271 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7272 | #ifdef EX_NOHOST |
| 7273 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7274 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7275 | #ifdef EX_UNAVAILABLE |
| 7276 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7277 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7278 | #ifdef EX_SOFTWARE |
| 7279 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7280 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7281 | #ifdef EX_OSERR |
| 7282 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7283 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7284 | #ifdef EX_OSFILE |
| 7285 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7286 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7287 | #ifdef EX_CANTCREAT |
| 7288 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7289 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7290 | #ifdef EX_IOERR |
| 7291 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7292 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7293 | #ifdef EX_TEMPFAIL |
| 7294 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7295 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7296 | #ifdef EX_PROTOCOL |
| 7297 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7298 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7299 | #ifdef EX_NOPERM |
| 7300 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7301 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7302 | #ifdef EX_CONFIG |
| 7303 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7304 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7305 | #ifdef EX_NOTFOUND |
| 7306 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7307 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7308 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7309 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7310 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7311 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7312 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7313 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7314 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7315 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7316 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7317 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7318 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7319 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7320 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7321 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7322 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7323 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7324 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7325 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7326 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7327 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7328 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7329 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7330 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7331 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7332 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7333 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7334 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7335 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7336 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7337 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7338 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7339 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7340 | #if defined(PYOS_OS2) |
| 7341 | if (insertvalues(d)) return -1; |
| 7342 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7343 | return 0; |
| 7344 | } |
| 7345 | |
| 7346 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7347 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7348 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7349 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7350 | |
| 7351 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7352 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7353 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7354 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7355 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7356 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7357 | #define MODNAME "posix" |
| 7358 | #endif |
| 7359 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7360 | static struct PyModuleDef posixmodule = { |
| 7361 | PyModuleDef_HEAD_INIT, |
| 7362 | MODNAME, |
| 7363 | posix__doc__, |
| 7364 | -1, |
| 7365 | posix_methods, |
| 7366 | NULL, |
| 7367 | NULL, |
| 7368 | NULL, |
| 7369 | NULL |
| 7370 | }; |
| 7371 | |
| 7372 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7373 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7374 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7375 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7376 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7377 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7378 | m = PyModule_Create(&posixmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7379 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7380 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7381 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7382 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7383 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7384 | Py_XINCREF(v); |
| 7385 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7386 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7387 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7388 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7389 | if (all_ins(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7390 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7391 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7392 | if (setup_confname_tables(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7393 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7394 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7395 | Py_INCREF(PyExc_OSError); |
| 7396 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7397 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7398 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7399 | if (posix_putenv_garbage == NULL) |
| 7400 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7401 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7402 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7403 | if (!initialized) { |
| 7404 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7405 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7406 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7407 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7408 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7409 | structseq_new = StatResultType.tp_new; |
| 7410 | StatResultType.tp_new = statresult_new; |
| 7411 | |
| 7412 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7413 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 7414 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7415 | Py_INCREF((PyObject*) &StatResultType); |
| 7416 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7417 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7418 | PyModule_AddObject(m, "statvfs_result", |
| 7419 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7420 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7421 | |
| 7422 | #ifdef __APPLE__ |
| 7423 | /* |
| 7424 | * Step 2 of weak-linking support on Mac OS X. |
| 7425 | * |
| 7426 | * The code below removes functions that are not available on the |
| 7427 | * currently active platform. |
| 7428 | * |
| 7429 | * This block allow one to use a python binary that was build on |
| 7430 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7431 | * OSX 10.4. |
| 7432 | */ |
| 7433 | #ifdef HAVE_FSTATVFS |
| 7434 | if (fstatvfs == NULL) { |
| 7435 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7436 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7437 | } |
| 7438 | } |
| 7439 | #endif /* HAVE_FSTATVFS */ |
| 7440 | |
| 7441 | #ifdef HAVE_STATVFS |
| 7442 | if (statvfs == NULL) { |
| 7443 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7444 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7445 | } |
| 7446 | } |
| 7447 | #endif /* HAVE_STATVFS */ |
| 7448 | |
| 7449 | # ifdef HAVE_LCHOWN |
| 7450 | if (lchown == NULL) { |
| 7451 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7452 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7453 | } |
| 7454 | } |
| 7455 | #endif /* HAVE_LCHOWN */ |
| 7456 | |
| 7457 | |
| 7458 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7459 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7460 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7461 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7462 | |
| 7463 | #ifdef __cplusplus |
| 7464 | } |
| 7465 | #endif |