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 |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 189 | extern int chown(const char *, uid_t, gid_t); |
| 190 | extern char *getcwd(char *, int); |
| 191 | extern char *strerror(int); |
| 192 | extern int link(const char *, const char *); |
| 193 | extern int rename(const char *, const char *); |
| 194 | extern int stat(const char *, struct stat *); |
| 195 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 197 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 198 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 201 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 204 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | #ifdef HAVE_UTIME_H |
| 207 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 208 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 210 | #ifdef HAVE_SYS_UTIME_H |
| 211 | #include <sys/utime.h> |
| 212 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 213 | #endif /* HAVE_SYS_UTIME_H */ |
| 214 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #ifdef HAVE_SYS_TIMES_H |
| 216 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 217 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | |
| 219 | #ifdef HAVE_SYS_PARAM_H |
| 220 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 221 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
| 223 | #ifdef HAVE_SYS_UTSNAME_H |
| 224 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 225 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 229 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 230 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 231 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #include <direct.h> |
| 233 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 234 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 236 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 237 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 238 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 240 | #endif |
| 241 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 243 | #endif |
| 244 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 249 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 250 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 255 | #endif |
| 256 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 258 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 259 | #include "osdefs.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 261 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 262 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 264 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 266 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 268 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 269 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 270 | #define MAXPATHLEN PATH_MAX |
| 271 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 272 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 273 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 274 | #endif /* MAXPATHLEN */ |
| 275 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 276 | #ifdef UNION_WAIT |
| 277 | /* Emulate some macros on systems that have a union instead of macros */ |
| 278 | |
| 279 | #ifndef WIFEXITED |
| 280 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 281 | #endif |
| 282 | |
| 283 | #ifndef WEXITSTATUS |
| 284 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 285 | #endif |
| 286 | |
| 287 | #ifndef WTERMSIG |
| 288 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 289 | #endif |
| 290 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 291 | #define WAIT_TYPE union wait |
| 292 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 293 | |
| 294 | #else /* !UNION_WAIT */ |
| 295 | #define WAIT_TYPE int |
| 296 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 297 | #endif /* UNION_WAIT */ |
| 298 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 299 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 300 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 301 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 302 | #define USE_CTERMID_R |
| 303 | #endif |
| 304 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 305 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 306 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 307 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 308 | # define STAT win32_stat |
| 309 | # define FSTAT win32_fstat |
| 310 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 311 | #else |
| 312 | # define STAT stat |
| 313 | # define FSTAT fstat |
| 314 | # define STRUCT_STAT struct stat |
| 315 | #endif |
| 316 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 317 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 318 | #include <sys/mkdev.h> |
| 319 | #else |
| 320 | #if defined(MAJOR_IN_SYSMACROS) |
| 321 | #include <sys/sysmacros.h> |
| 322 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 323 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 324 | #include <sys/mkdev.h> |
| 325 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 326 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 329 | #ifdef WITH_NEXT_FRAMEWORK |
| 330 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 331 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 332 | */ |
| 333 | #include <crt_externs.h> |
| 334 | static char **environ; |
| 335 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 337 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 339 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 340 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 342 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 344 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | if (d == NULL) |
| 346 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 347 | #ifdef WITH_NEXT_FRAMEWORK |
| 348 | if (environ == NULL) |
| 349 | environ = *_NSGetEnviron(); |
| 350 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 351 | if (environ == NULL) |
| 352 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 353 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 355 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 356 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | char *p = strchr(*e, '='); |
| 358 | if (p == NULL) |
| 359 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 360 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 361 | if (k == NULL) { |
| 362 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 363 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 364 | } |
| 365 | v = PyString_FromString(p+1); |
| 366 | if (v == NULL) { |
| 367 | PyErr_Clear(); |
| 368 | Py_DECREF(k); |
| 369 | continue; |
| 370 | } |
| 371 | if (PyDict_GetItem(d, k) == NULL) { |
| 372 | if (PyDict_SetItem(d, k, v) != 0) |
| 373 | PyErr_Clear(); |
| 374 | } |
| 375 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 376 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 378 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 379 | { |
| 380 | APIRET rc; |
| 381 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 382 | |
| 383 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 384 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 385 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 386 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 387 | Py_DECREF(v); |
| 388 | } |
| 389 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 390 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 391 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 392 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 393 | Py_DECREF(v); |
| 394 | } |
| 395 | } |
| 396 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 397 | return d; |
| 398 | } |
| 399 | |
| 400 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 401 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 402 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 403 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 404 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 405 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 406 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 407 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 408 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 409 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 410 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 411 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 414 | #ifdef Py_WIN_WIDE_FILENAMES |
| 415 | static PyObject * |
| 416 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 417 | { |
| 418 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 419 | } |
| 420 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 421 | |
| 422 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 423 | static PyObject * |
| 424 | posix_error_with_allocated_filename(char* name) |
| 425 | { |
| 426 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 427 | PyMem_Free(name); |
| 428 | return rc; |
| 429 | } |
| 430 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 431 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 432 | static PyObject * |
| 433 | win32_error(char* function, char* filename) |
| 434 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 435 | /* XXX We should pass the function name along in the future. |
| 436 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 437 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 438 | Windows error object, which is non-trivial. |
| 439 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 440 | errno = GetLastError(); |
| 441 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 442 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 443 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 444 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 445 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 446 | |
| 447 | #ifdef Py_WIN_WIDE_FILENAMES |
| 448 | static PyObject * |
| 449 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 450 | { |
| 451 | /* XXX - see win32_error for comments on 'function' */ |
| 452 | errno = GetLastError(); |
| 453 | if (filename) |
| 454 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 455 | else |
| 456 | return PyErr_SetFromWindowsErr(errno); |
| 457 | } |
| 458 | |
| 459 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 460 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* Function suitable for O& conversion */ |
| 464 | static int |
| 465 | convert_to_unicode(PyObject *arg, void* _param) |
| 466 | { |
| 467 | PyObject **param = (PyObject**)_param; |
| 468 | if (PyUnicode_CheckExact(arg)) { |
| 469 | Py_INCREF(arg); |
| 470 | *param = arg; |
| 471 | } |
| 472 | else if (PyUnicode_Check(arg)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 473 | /* For a Unicode subtype that's not a Unicode object, |
| 474 | return a true Unicode object with the same data. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 475 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), |
| 476 | PyUnicode_GET_SIZE(arg)); |
| 477 | return *param != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 478 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 479 | else |
| 480 | *param = PyUnicode_FromEncodedObject(arg, |
| 481 | Py_FileSystemDefaultEncoding, |
| 482 | "strict"); |
| 483 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 487 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 488 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 490 | #if defined(PYOS_OS2) |
| 491 | /********************************************************************** |
| 492 | * Helper Function to Trim and Format OS/2 Messages |
| 493 | **********************************************************************/ |
| 494 | static void |
| 495 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 496 | { |
| 497 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 498 | |
| 499 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 500 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 501 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 502 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 503 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 504 | } |
| 505 | |
| 506 | /* Add Optional Reason Text */ |
| 507 | if (reason) { |
| 508 | strcat(msgbuf, " : "); |
| 509 | strcat(msgbuf, reason); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /********************************************************************** |
| 514 | * Decode an OS/2 Operating System Error Code |
| 515 | * |
| 516 | * A convenience function to lookup an OS/2 error code and return a |
| 517 | * text message we can use to raise a Python exception. |
| 518 | * |
| 519 | * Notes: |
| 520 | * The messages for errors returned from the OS/2 kernel reside in |
| 521 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 522 | * |
| 523 | **********************************************************************/ |
| 524 | static char * |
| 525 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 526 | { |
| 527 | APIRET rc; |
| 528 | ULONG msglen; |
| 529 | |
| 530 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 531 | Py_BEGIN_ALLOW_THREADS |
| 532 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 533 | errorcode, "oso001.msg", &msglen); |
| 534 | Py_END_ALLOW_THREADS |
| 535 | |
| 536 | if (rc == NO_ERROR) |
| 537 | os2_formatmsg(msgbuf, msglen, reason); |
| 538 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 539 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 540 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 541 | |
| 542 | return msgbuf; |
| 543 | } |
| 544 | |
| 545 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 546 | errors are not in a global variable e.g. 'errno' nor are |
| 547 | they congruent with posix error numbers. */ |
| 548 | |
| 549 | static PyObject * os2_error(int code) |
| 550 | { |
| 551 | char text[1024]; |
| 552 | PyObject *v; |
| 553 | |
| 554 | os2_strerror(text, sizeof(text), code, ""); |
| 555 | |
| 556 | v = Py_BuildValue("(is)", code, text); |
| 557 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 558 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 559 | Py_DECREF(v); |
| 560 | } |
| 561 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 562 | } |
| 563 | |
| 564 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 565 | |
| 566 | /* POSIX generic methods */ |
| 567 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 568 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 569 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 570 | { |
| 571 | int fd; |
| 572 | int res; |
| 573 | fd = PyObject_AsFileDescriptor(fdobj); |
| 574 | if (fd < 0) |
| 575 | return NULL; |
| 576 | Py_BEGIN_ALLOW_THREADS |
| 577 | res = (*func)(fd); |
| 578 | Py_END_ALLOW_THREADS |
| 579 | if (res < 0) |
| 580 | return posix_error(); |
| 581 | Py_INCREF(Py_None); |
| 582 | return Py_None; |
| 583 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 584 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 585 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 586 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 587 | unicode_file_names(void) |
| 588 | { |
| 589 | static int canusewide = -1; |
| 590 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 591 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 592 | the Windows NT family. */ |
| 593 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 594 | } |
| 595 | return canusewide; |
| 596 | } |
| 597 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 598 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 599 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 600 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 601 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 602 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 603 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 604 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 605 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 606 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 607 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 608 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 609 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 610 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 611 | return posix_error_with_allocated_filename(path1); |
| 612 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 613 | Py_INCREF(Py_None); |
| 614 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 617 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 618 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 619 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 620 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 621 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 622 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 623 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 624 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 625 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 626 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 627 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 628 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 629 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 630 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 631 | PyMem_Free(path1); |
| 632 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 633 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 634 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 635 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 636 | Py_INCREF(Py_None); |
| 637 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 640 | #ifdef Py_WIN_WIDE_FILENAMES |
| 641 | static PyObject* |
| 642 | win32_1str(PyObject* args, char* func, |
| 643 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 644 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 645 | { |
| 646 | PyObject *uni; |
| 647 | char *ansi; |
| 648 | BOOL result; |
| 649 | if (unicode_file_names()) { |
| 650 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 651 | PyErr_Clear(); |
| 652 | else { |
| 653 | Py_BEGIN_ALLOW_THREADS |
| 654 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 655 | Py_END_ALLOW_THREADS |
| 656 | if (!result) |
| 657 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 658 | Py_INCREF(Py_None); |
| 659 | return Py_None; |
| 660 | } |
| 661 | } |
| 662 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 663 | return NULL; |
| 664 | Py_BEGIN_ALLOW_THREADS |
| 665 | result = funcA(ansi); |
| 666 | Py_END_ALLOW_THREADS |
| 667 | if (!result) |
| 668 | return win32_error(func, ansi); |
| 669 | Py_INCREF(Py_None); |
| 670 | return Py_None; |
| 671 | |
| 672 | } |
| 673 | |
| 674 | /* This is a reimplementation of the C library's chdir function, |
| 675 | but one that produces Win32 errors instead of DOS error codes. |
| 676 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 677 | it also needs to set "magic" environment variables indicating |
| 678 | the per-drive current directory, which are of the form =<drive>: */ |
| 679 | BOOL __stdcall |
| 680 | win32_chdir(LPCSTR path) |
| 681 | { |
| 682 | char new_path[MAX_PATH+1]; |
| 683 | int result; |
| 684 | char env[4] = "=x:"; |
| 685 | |
| 686 | if(!SetCurrentDirectoryA(path)) |
| 687 | return FALSE; |
| 688 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 689 | if (!result) |
| 690 | return FALSE; |
| 691 | /* In the ANSI API, there should not be any paths longer |
| 692 | than MAX_PATH. */ |
| 693 | assert(result <= MAX_PATH+1); |
| 694 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 695 | strncmp(new_path, "//", 2) == 0) |
| 696 | /* UNC path, nothing to do. */ |
| 697 | return TRUE; |
| 698 | env[1] = new_path[0]; |
| 699 | return SetEnvironmentVariableA(env, new_path); |
| 700 | } |
| 701 | |
| 702 | /* The Unicode version differs from the ANSI version |
| 703 | since the current directory might exceed MAX_PATH characters */ |
| 704 | BOOL __stdcall |
| 705 | win32_wchdir(LPCWSTR path) |
| 706 | { |
| 707 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 708 | int result; |
| 709 | wchar_t env[4] = L"=x:"; |
| 710 | |
| 711 | if(!SetCurrentDirectoryW(path)) |
| 712 | return FALSE; |
| 713 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 714 | if (!result) |
| 715 | return FALSE; |
| 716 | if (result > MAX_PATH+1) { |
| 717 | new_path = malloc(result); |
| 718 | if (!new_path) { |
| 719 | SetLastError(ERROR_OUTOFMEMORY); |
| 720 | return FALSE; |
| 721 | } |
| 722 | } |
| 723 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 724 | wcsncmp(new_path, L"//", 2) == 0) |
| 725 | /* UNC path, nothing to do. */ |
| 726 | return TRUE; |
| 727 | env[1] = new_path[0]; |
| 728 | result = SetEnvironmentVariableW(env, new_path); |
| 729 | if (new_path != _new_path) |
| 730 | free(new_path); |
| 731 | return result; |
| 732 | } |
| 733 | #endif |
| 734 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 735 | #ifdef MS_WINDOWS |
| 736 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 737 | - time stamps are restricted to second resolution |
| 738 | - file modification times suffer from forth-and-back conversions between |
| 739 | UTC and local time |
| 740 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 741 | */ |
| 742 | #define HAVE_STAT_NSEC 1 |
| 743 | |
| 744 | struct win32_stat{ |
| 745 | int st_dev; |
| 746 | __int64 st_ino; |
| 747 | unsigned short st_mode; |
| 748 | int st_nlink; |
| 749 | int st_uid; |
| 750 | int st_gid; |
| 751 | int st_rdev; |
| 752 | __int64 st_size; |
| 753 | int st_atime; |
| 754 | int st_atime_nsec; |
| 755 | int st_mtime; |
| 756 | int st_mtime_nsec; |
| 757 | int st_ctime; |
| 758 | int st_ctime_nsec; |
| 759 | }; |
| 760 | |
| 761 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 762 | |
| 763 | static void |
| 764 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 765 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 766 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 767 | /* Cannot simply cast and dereference in_ptr, |
| 768 | since it might not be aligned properly */ |
| 769 | __int64 in; |
| 770 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 771 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 772 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 773 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 774 | } |
| 775 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 776 | static void |
| 777 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 778 | { |
| 779 | /* XXX endianness */ |
| 780 | __int64 out; |
| 781 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 782 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 783 | memcpy(out_ptr, &out, sizeof(out)); |
| 784 | } |
| 785 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 786 | /* Below, we *know* that ugo+r is 0444 */ |
| 787 | #if _S_IREAD != 0400 |
| 788 | #error Unsupported C library |
| 789 | #endif |
| 790 | static int |
| 791 | attributes_to_mode(DWORD attr) |
| 792 | { |
| 793 | int m = 0; |
| 794 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 795 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 796 | else |
| 797 | m |= _S_IFREG; |
| 798 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 799 | m |= 0444; |
| 800 | else |
| 801 | m |= 0666; |
| 802 | return m; |
| 803 | } |
| 804 | |
| 805 | static int |
| 806 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 807 | { |
| 808 | memset(result, 0, sizeof(*result)); |
| 809 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 810 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 811 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 812 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 813 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 818 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 819 | static int checked = 0; |
| 820 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 821 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 822 | static void |
| 823 | check_gfax() |
| 824 | { |
| 825 | HINSTANCE hKernel32; |
| 826 | if (checked) |
| 827 | return; |
| 828 | checked = 1; |
| 829 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 830 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 831 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 832 | } |
| 833 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 834 | static BOOL |
| 835 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 836 | { |
| 837 | HANDLE hFindFile; |
| 838 | WIN32_FIND_DATAA FileData; |
| 839 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 840 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 841 | return FALSE; |
| 842 | FindClose(hFindFile); |
| 843 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 844 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 845 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 846 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 847 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 848 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 849 | return TRUE; |
| 850 | } |
| 851 | |
| 852 | static BOOL |
| 853 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 854 | { |
| 855 | HANDLE hFindFile; |
| 856 | WIN32_FIND_DATAW FileData; |
| 857 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 858 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 859 | return FALSE; |
| 860 | FindClose(hFindFile); |
| 861 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 862 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 863 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 864 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 865 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 866 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 867 | return TRUE; |
| 868 | } |
| 869 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 870 | static BOOL WINAPI |
| 871 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 872 | GET_FILEEX_INFO_LEVELS level, |
| 873 | LPVOID pv) |
| 874 | { |
| 875 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 876 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 877 | /* First try to use the system's implementation, if that is |
| 878 | available and either succeeds to gives an error other than |
| 879 | that it isn't implemented. */ |
| 880 | check_gfax(); |
| 881 | if (gfaxa) { |
| 882 | result = gfaxa(pszFile, level, pv); |
| 883 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 884 | return result; |
| 885 | } |
| 886 | /* It's either not present, or not implemented. |
| 887 | Emulate using FindFirstFile. */ |
| 888 | if (level != GetFileExInfoStandard) { |
| 889 | SetLastError(ERROR_INVALID_PARAMETER); |
| 890 | return FALSE; |
| 891 | } |
| 892 | /* Use GetFileAttributes to validate that the file name |
| 893 | does not contain wildcards (which FindFirstFile would |
| 894 | accept). */ |
| 895 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 896 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 897 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | static BOOL WINAPI |
| 901 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 902 | GET_FILEEX_INFO_LEVELS level, |
| 903 | LPVOID pv) |
| 904 | { |
| 905 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 906 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 907 | /* First try to use the system's implementation, if that is |
| 908 | available and either succeeds to gives an error other than |
| 909 | that it isn't implemented. */ |
| 910 | check_gfax(); |
| 911 | if (gfaxa) { |
| 912 | result = gfaxw(pszFile, level, pv); |
| 913 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 914 | return result; |
| 915 | } |
| 916 | /* It's either not present, or not implemented. |
| 917 | Emulate using FindFirstFile. */ |
| 918 | if (level != GetFileExInfoStandard) { |
| 919 | SetLastError(ERROR_INVALID_PARAMETER); |
| 920 | return FALSE; |
| 921 | } |
| 922 | /* Use GetFileAttributes to validate that the file name |
| 923 | does not contain wildcards (which FindFirstFile would |
| 924 | accept). */ |
| 925 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 926 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 927 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 930 | static int |
| 931 | win32_stat(const char* path, struct win32_stat *result) |
| 932 | { |
| 933 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 934 | int code; |
| 935 | char *dot; |
| 936 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 937 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 938 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 939 | /* Protocol violation: we explicitly clear errno, instead of |
| 940 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 941 | errno = 0; |
| 942 | return -1; |
| 943 | } else { |
| 944 | /* Could not get attributes on open file. Fall back to |
| 945 | reading the directory. */ |
| 946 | if (!attributes_from_dir(path, &info)) { |
| 947 | /* Very strange. This should not fail now */ |
| 948 | errno = 0; |
| 949 | return -1; |
| 950 | } |
| 951 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 952 | } |
| 953 | code = attribute_data_to_stat(&info, result); |
| 954 | if (code != 0) |
| 955 | return code; |
| 956 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 957 | dot = strrchr(path, '.'); |
| 958 | if (dot) { |
| 959 | if (stricmp(dot, ".bat") == 0 || |
| 960 | stricmp(dot, ".cmd") == 0 || |
| 961 | stricmp(dot, ".exe") == 0 || |
| 962 | stricmp(dot, ".com") == 0) |
| 963 | result->st_mode |= 0111; |
| 964 | } |
| 965 | return code; |
| 966 | } |
| 967 | |
| 968 | static int |
| 969 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 970 | { |
| 971 | int code; |
| 972 | const wchar_t *dot; |
| 973 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 974 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 975 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 976 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 977 | /* Protocol violation: we explicitly clear errno, instead of |
| 978 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 979 | errno = 0; |
| 980 | return -1; |
| 981 | } else { |
| 982 | /* Could not get attributes on open file. Fall back to |
| 983 | reading the directory. */ |
| 984 | if (!attributes_from_dir_w(path, &info)) { |
| 985 | /* Very strange. This should not fail now */ |
| 986 | errno = 0; |
| 987 | return -1; |
| 988 | } |
| 989 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 990 | } |
| 991 | code = attribute_data_to_stat(&info, result); |
| 992 | if (code < 0) |
| 993 | return code; |
| 994 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 995 | dot = wcsrchr(path, '.'); |
| 996 | if (dot) { |
| 997 | if (_wcsicmp(dot, L".bat") == 0 || |
| 998 | _wcsicmp(dot, L".cmd") == 0 || |
| 999 | _wcsicmp(dot, L".exe") == 0 || |
| 1000 | _wcsicmp(dot, L".com") == 0) |
| 1001 | result->st_mode |= 0111; |
| 1002 | } |
| 1003 | return code; |
| 1004 | } |
| 1005 | |
| 1006 | static int |
| 1007 | win32_fstat(int file_number, struct win32_stat *result) |
| 1008 | { |
| 1009 | BY_HANDLE_FILE_INFORMATION info; |
| 1010 | HANDLE h; |
| 1011 | int type; |
| 1012 | |
| 1013 | h = (HANDLE)_get_osfhandle(file_number); |
| 1014 | |
| 1015 | /* Protocol violation: we explicitly clear errno, instead of |
| 1016 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1017 | errno = 0; |
| 1018 | |
| 1019 | if (h == INVALID_HANDLE_VALUE) { |
| 1020 | /* This is really a C library error (invalid file handle). |
| 1021 | We set the Win32 error to the closes one matching. */ |
| 1022 | SetLastError(ERROR_INVALID_HANDLE); |
| 1023 | return -1; |
| 1024 | } |
| 1025 | memset(result, 0, sizeof(*result)); |
| 1026 | |
| 1027 | type = GetFileType(h); |
| 1028 | if (type == FILE_TYPE_UNKNOWN) { |
| 1029 | DWORD error = GetLastError(); |
| 1030 | if (error != 0) { |
| 1031 | return -1; |
| 1032 | } |
| 1033 | /* else: valid but unknown file */ |
| 1034 | } |
| 1035 | |
| 1036 | if (type != FILE_TYPE_DISK) { |
| 1037 | if (type == FILE_TYPE_CHAR) |
| 1038 | result->st_mode = _S_IFCHR; |
| 1039 | else if (type == FILE_TYPE_PIPE) |
| 1040 | result->st_mode = _S_IFIFO; |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | if (!GetFileInformationByHandle(h, &info)) { |
| 1045 | return -1; |
| 1046 | } |
| 1047 | |
| 1048 | /* similar to stat() */ |
| 1049 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1050 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1051 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1052 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1053 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1054 | /* specific to fstat() */ |
| 1055 | result->st_nlink = info.nNumberOfLinks; |
| 1056 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | #endif /* MS_WINDOWS */ |
| 1061 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1062 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1063 | "stat_result: Result from stat or lstat.\n\n\ |
| 1064 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1065 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1066 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1067 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1068 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1069 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1070 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1071 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1072 | |
| 1073 | static PyStructSequence_Field stat_result_fields[] = { |
| 1074 | {"st_mode", "protection bits"}, |
| 1075 | {"st_ino", "inode"}, |
| 1076 | {"st_dev", "device"}, |
| 1077 | {"st_nlink", "number of hard links"}, |
| 1078 | {"st_uid", "user ID of owner"}, |
| 1079 | {"st_gid", "group ID of owner"}, |
| 1080 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1081 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1082 | {NULL, "integer time of last access"}, |
| 1083 | {NULL, "integer time of last modification"}, |
| 1084 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1085 | {"st_atime", "time of last access"}, |
| 1086 | {"st_mtime", "time of last modification"}, |
| 1087 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1088 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1089 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1090 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1091 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1092 | {"st_blocks", "number of blocks allocated"}, |
| 1093 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1094 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1095 | {"st_rdev", "device type (if inode device)"}, |
| 1096 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1097 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1098 | {"st_flags", "user defined flags for file"}, |
| 1099 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1100 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1101 | {"st_gen", "generation number"}, |
| 1102 | #endif |
| 1103 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1104 | {"st_birthtime", "time of creation"}, |
| 1105 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1106 | {0} |
| 1107 | }; |
| 1108 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1109 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1110 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1111 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1112 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1113 | #endif |
| 1114 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1115 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1116 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1117 | #else |
| 1118 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1119 | #endif |
| 1120 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1121 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1122 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1123 | #else |
| 1124 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1125 | #endif |
| 1126 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1127 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1128 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1129 | #else |
| 1130 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1131 | #endif |
| 1132 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1133 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1134 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1135 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1136 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1137 | #endif |
| 1138 | |
| 1139 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1140 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1141 | #else |
| 1142 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1143 | #endif |
| 1144 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1145 | static PyStructSequence_Desc stat_result_desc = { |
| 1146 | "stat_result", /* name */ |
| 1147 | stat_result__doc__, /* doc */ |
| 1148 | stat_result_fields, |
| 1149 | 10 |
| 1150 | }; |
| 1151 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1152 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1153 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1154 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1155 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1156 | 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] | 1157 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1158 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1159 | |
| 1160 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1161 | {"f_bsize", }, |
| 1162 | {"f_frsize", }, |
| 1163 | {"f_blocks", }, |
| 1164 | {"f_bfree", }, |
| 1165 | {"f_bavail", }, |
| 1166 | {"f_files", }, |
| 1167 | {"f_ffree", }, |
| 1168 | {"f_favail", }, |
| 1169 | {"f_flag", }, |
| 1170 | {"f_namemax",}, |
| 1171 | {0} |
| 1172 | }; |
| 1173 | |
| 1174 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1175 | "statvfs_result", /* name */ |
| 1176 | statvfs_result__doc__, /* doc */ |
| 1177 | statvfs_result_fields, |
| 1178 | 10 |
| 1179 | }; |
| 1180 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1181 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1182 | static PyTypeObject StatResultType; |
| 1183 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1184 | static newfunc structseq_new; |
| 1185 | |
| 1186 | static PyObject * |
| 1187 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1188 | { |
| 1189 | PyStructSequence *result; |
| 1190 | int i; |
| 1191 | |
| 1192 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1193 | if (!result) |
| 1194 | return NULL; |
| 1195 | /* If we have been initialized from a tuple, |
| 1196 | st_?time might be set to None. Initialize it |
| 1197 | from the int slots. */ |
| 1198 | for (i = 7; i <= 9; i++) { |
| 1199 | if (result->ob_item[i+3] == Py_None) { |
| 1200 | Py_DECREF(Py_None); |
| 1201 | Py_INCREF(result->ob_item[i]); |
| 1202 | result->ob_item[i+3] = result->ob_item[i]; |
| 1203 | } |
| 1204 | } |
| 1205 | return (PyObject*)result; |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1211 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1212 | |
| 1213 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1214 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1215 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1216 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1217 | future calls return ints. \n\ |
| 1218 | If newval is omitted, return the current setting.\n"); |
| 1219 | |
| 1220 | static PyObject* |
| 1221 | stat_float_times(PyObject* self, PyObject *args) |
| 1222 | { |
| 1223 | int newval = -1; |
| 1224 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1225 | return NULL; |
| 1226 | if (newval == -1) |
| 1227 | /* Return old value */ |
| 1228 | return PyBool_FromLong(_stat_float_times); |
| 1229 | _stat_float_times = newval; |
| 1230 | Py_INCREF(Py_None); |
| 1231 | return Py_None; |
| 1232 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1233 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1234 | static void |
| 1235 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1236 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1237 | PyObject *fval,*ival; |
| 1238 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1239 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1240 | #else |
| 1241 | ival = PyInt_FromLong((long)sec); |
| 1242 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1243 | if (!ival) |
| 1244 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1245 | if (_stat_float_times) { |
| 1246 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1247 | } else { |
| 1248 | fval = ival; |
| 1249 | Py_INCREF(fval); |
| 1250 | } |
| 1251 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1252 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1255 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1256 | (used by posix_stat() and posix_fstat()) */ |
| 1257 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1258 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1259 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1260 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1261 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1262 | if (v == NULL) |
| 1263 | return NULL; |
| 1264 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1265 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1266 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1267 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1268 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1269 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1270 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1271 | #endif |
| 1272 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1273 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1274 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1275 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1276 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1277 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1278 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1279 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1280 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1281 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1282 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1283 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1284 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1285 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1286 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1287 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1288 | #if defined(HAVE_STAT_TV_NSEC) |
| 1289 | ansec = st->st_atim.tv_nsec; |
| 1290 | mnsec = st->st_mtim.tv_nsec; |
| 1291 | cnsec = st->st_ctim.tv_nsec; |
| 1292 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1293 | ansec = st->st_atimespec.tv_nsec; |
| 1294 | mnsec = st->st_mtimespec.tv_nsec; |
| 1295 | cnsec = st->st_ctimespec.tv_nsec; |
| 1296 | #elif defined(HAVE_STAT_NSEC) |
| 1297 | ansec = st->st_atime_nsec; |
| 1298 | mnsec = st->st_mtime_nsec; |
| 1299 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1300 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1301 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1302 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1303 | fill_time(v, 7, st->st_atime, ansec); |
| 1304 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1305 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1306 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1307 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1308 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1309 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1310 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1311 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1312 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1313 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1314 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1315 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1316 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1317 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1318 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1319 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1320 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1321 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1322 | #endif |
| 1323 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1324 | { |
| 1325 | PyObject *val; |
| 1326 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1327 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1328 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1329 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1330 | #else |
| 1331 | bnsec = 0; |
| 1332 | #endif |
| 1333 | if (_stat_float_times) { |
| 1334 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1335 | } else { |
| 1336 | val = PyInt_FromLong((long)bsec); |
| 1337 | } |
| 1338 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1339 | val); |
| 1340 | } |
| 1341 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1342 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1343 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1344 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1345 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1346 | |
| 1347 | if (PyErr_Occurred()) { |
| 1348 | Py_DECREF(v); |
| 1349 | return NULL; |
| 1350 | } |
| 1351 | |
| 1352 | return v; |
| 1353 | } |
| 1354 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1355 | #ifdef MS_WINDOWS |
| 1356 | |
| 1357 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1358 | where / can be used in place of \ and the trailing slash is optional. |
| 1359 | Both SERVER and SHARE must have at least one character. |
| 1360 | */ |
| 1361 | |
| 1362 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1363 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1364 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1365 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1366 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1367 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1368 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1369 | IsUNCRootA(char *path, int pathlen) |
| 1370 | { |
| 1371 | #define ISSLASH ISSLASHA |
| 1372 | |
| 1373 | int i, share; |
| 1374 | |
| 1375 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1376 | /* minimum UNCRoot is \\x\y */ |
| 1377 | return FALSE; |
| 1378 | for (i = 2; i < pathlen ; i++) |
| 1379 | if (ISSLASH(path[i])) break; |
| 1380 | if (i == 2 || i == pathlen) |
| 1381 | /* do not allow \\\SHARE or \\SERVER */ |
| 1382 | return FALSE; |
| 1383 | share = i+1; |
| 1384 | for (i = share; i < pathlen; i++) |
| 1385 | if (ISSLASH(path[i])) break; |
| 1386 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1387 | |
| 1388 | #undef ISSLASH |
| 1389 | } |
| 1390 | |
| 1391 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1392 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1393 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1394 | { |
| 1395 | #define ISSLASH ISSLASHW |
| 1396 | |
| 1397 | int i, share; |
| 1398 | |
| 1399 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1400 | /* minimum UNCRoot is \\x\y */ |
| 1401 | return FALSE; |
| 1402 | for (i = 2; i < pathlen ; i++) |
| 1403 | if (ISSLASH(path[i])) break; |
| 1404 | if (i == 2 || i == pathlen) |
| 1405 | /* do not allow \\\SHARE or \\SERVER */ |
| 1406 | return FALSE; |
| 1407 | share = i+1; |
| 1408 | for (i = share; i < pathlen; i++) |
| 1409 | if (ISSLASH(path[i])) break; |
| 1410 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1411 | |
| 1412 | #undef ISSLASH |
| 1413 | } |
| 1414 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1415 | #endif /* MS_WINDOWS */ |
| 1416 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1417 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1418 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1419 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1420 | #ifdef __VMS |
| 1421 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1422 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1423 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1424 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1425 | char *wformat, |
| 1426 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1427 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1428 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1429 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1430 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1431 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1432 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1433 | |
| 1434 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1435 | /* If on wide-character-capable OS see if argument |
| 1436 | is Unicode and if so use wide API. */ |
| 1437 | if (unicode_file_names()) { |
| 1438 | PyUnicodeObject *po; |
| 1439 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1440 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1441 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1442 | Py_BEGIN_ALLOW_THREADS |
| 1443 | /* PyUnicode_AS_UNICODE result OK without |
| 1444 | thread lock as it is a simple dereference. */ |
| 1445 | res = wstatfunc(wpath, &st); |
| 1446 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1447 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1448 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1449 | return win32_error_unicode("stat", wpath); |
| 1450 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1451 | } |
| 1452 | /* Drop the argument parsing error as narrow strings |
| 1453 | are also valid. */ |
| 1454 | PyErr_Clear(); |
| 1455 | } |
| 1456 | #endif |
| 1457 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1458 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1459 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1460 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1461 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1462 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1463 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1464 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1465 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1466 | |
| 1467 | if (res != 0) { |
| 1468 | #ifdef MS_WINDOWS |
| 1469 | result = win32_error("stat", pathfree); |
| 1470 | #else |
| 1471 | result = posix_error_with_filename(pathfree); |
| 1472 | #endif |
| 1473 | } |
| 1474 | else |
| 1475 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1476 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1477 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1478 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1481 | /* POSIX methods */ |
| 1482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1483 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1484 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1485 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1486 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1487 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1488 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1489 | 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] | 1490 | |
| 1491 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1492 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1493 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1494 | char *path; |
| 1495 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1496 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1497 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1498 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1499 | if (unicode_file_names()) { |
| 1500 | PyUnicodeObject *po; |
| 1501 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1502 | Py_BEGIN_ALLOW_THREADS |
| 1503 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1504 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1505 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1506 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1507 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1508 | } |
| 1509 | /* Drop the argument parsing error as narrow strings |
| 1510 | are also valid. */ |
| 1511 | PyErr_Clear(); |
| 1512 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1513 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1514 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1515 | return 0; |
| 1516 | Py_BEGIN_ALLOW_THREADS |
| 1517 | attr = GetFileAttributesA(path); |
| 1518 | Py_END_ALLOW_THREADS |
| 1519 | PyMem_Free(path); |
| 1520 | finish: |
| 1521 | if (attr == 0xFFFFFFFF) |
| 1522 | /* File does not exist, or cannot read attributes */ |
| 1523 | return PyBool_FromLong(0); |
| 1524 | /* Access is possible if either write access wasn't requested, or |
| 1525 | the file isn't read-only. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1526 | return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1527 | #else |
| 1528 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1529 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1530 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1531 | return NULL; |
| 1532 | Py_BEGIN_ALLOW_THREADS |
| 1533 | res = access(path, mode); |
| 1534 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1535 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1536 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1537 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1540 | #ifndef F_OK |
| 1541 | #define F_OK 0 |
| 1542 | #endif |
| 1543 | #ifndef R_OK |
| 1544 | #define R_OK 4 |
| 1545 | #endif |
| 1546 | #ifndef W_OK |
| 1547 | #define W_OK 2 |
| 1548 | #endif |
| 1549 | #ifndef X_OK |
| 1550 | #define X_OK 1 |
| 1551 | #endif |
| 1552 | |
| 1553 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1554 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1555 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1556 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1557 | |
| 1558 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1559 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1560 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1561 | int id; |
| 1562 | char *ret; |
| 1563 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1564 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1565 | return NULL; |
| 1566 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1567 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1568 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1569 | if (id == 0) { |
| 1570 | ret = ttyname(); |
| 1571 | } |
| 1572 | else { |
| 1573 | ret = NULL; |
| 1574 | } |
| 1575 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1576 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1577 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1578 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1579 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1580 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1581 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1582 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1583 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1584 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1585 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1586 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1587 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1588 | |
| 1589 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1590 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1591 | { |
| 1592 | char *ret; |
| 1593 | char buffer[L_ctermid]; |
| 1594 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1595 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1596 | ret = ctermid_r(buffer); |
| 1597 | #else |
| 1598 | ret = ctermid(buffer); |
| 1599 | #endif |
| 1600 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1601 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1602 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1603 | } |
| 1604 | #endif |
| 1605 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1606 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1607 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1608 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1609 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1610 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1611 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1612 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1613 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1614 | 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] | 1615 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1616 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1617 | #elif defined(__VMS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1618 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1619 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1620 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1621 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1624 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1625 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1626 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1627 | 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] | 1628 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1629 | |
| 1630 | static PyObject * |
| 1631 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1632 | { |
| 1633 | return posix_fildes(fdobj, fchdir); |
| 1634 | } |
| 1635 | #endif /* HAVE_FCHDIR */ |
| 1636 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1637 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1638 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1639 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1640 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1641 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1642 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1643 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1644 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1645 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1646 | int i; |
| 1647 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1648 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1649 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1650 | if (unicode_file_names()) { |
| 1651 | PyUnicodeObject *po; |
| 1652 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1653 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1654 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1655 | if (attr != 0xFFFFFFFF) { |
| 1656 | if (i & _S_IWRITE) |
| 1657 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1658 | else |
| 1659 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1660 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1661 | } |
| 1662 | else |
| 1663 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1664 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1665 | if (!res) |
| 1666 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1667 | PyUnicode_AS_UNICODE(po)); |
| 1668 | Py_INCREF(Py_None); |
| 1669 | return Py_None; |
| 1670 | } |
| 1671 | /* Drop the argument parsing error as narrow strings |
| 1672 | are also valid. */ |
| 1673 | PyErr_Clear(); |
| 1674 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1675 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1676 | &path, &i)) |
| 1677 | return NULL; |
| 1678 | Py_BEGIN_ALLOW_THREADS |
| 1679 | attr = GetFileAttributesA(path); |
| 1680 | if (attr != 0xFFFFFFFF) { |
| 1681 | if (i & _S_IWRITE) |
| 1682 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1683 | else |
| 1684 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1685 | res = SetFileAttributesA(path, attr); |
| 1686 | } |
| 1687 | else |
| 1688 | res = 0; |
| 1689 | Py_END_ALLOW_THREADS |
| 1690 | if (!res) { |
| 1691 | win32_error("chmod", path); |
| 1692 | PyMem_Free(path); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | PyMem_Free(path); |
| 1696 | Py_INCREF(Py_None); |
| 1697 | return Py_None; |
| 1698 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1699 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1700 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1701 | return NULL; |
| 1702 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1703 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1704 | Py_END_ALLOW_THREADS |
| 1705 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1706 | return posix_error_with_allocated_filename(path); |
| 1707 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1708 | Py_INCREF(Py_None); |
| 1709 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1710 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1713 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1714 | #ifdef HAVE_CHFLAGS |
| 1715 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1716 | "chflags(path, flags)\n\n\ |
| 1717 | Set file flags."); |
| 1718 | |
| 1719 | static PyObject * |
| 1720 | posix_chflags(PyObject *self, PyObject *args) |
| 1721 | { |
| 1722 | char *path; |
| 1723 | unsigned long flags; |
| 1724 | int res; |
| 1725 | if (!PyArg_ParseTuple(args, "etk:chflags", |
| 1726 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1727 | return NULL; |
| 1728 | Py_BEGIN_ALLOW_THREADS |
| 1729 | res = chflags(path, flags); |
| 1730 | Py_END_ALLOW_THREADS |
| 1731 | if (res < 0) |
| 1732 | return posix_error_with_allocated_filename(path); |
| 1733 | PyMem_Free(path); |
| 1734 | Py_INCREF(Py_None); |
| 1735 | return Py_None; |
| 1736 | } |
| 1737 | #endif /* HAVE_CHFLAGS */ |
| 1738 | |
| 1739 | #ifdef HAVE_LCHFLAGS |
| 1740 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1741 | "lchflags(path, flags)\n\n\ |
| 1742 | Set file flags.\n\ |
| 1743 | This function will not follow symbolic links."); |
| 1744 | |
| 1745 | static PyObject * |
| 1746 | posix_lchflags(PyObject *self, PyObject *args) |
| 1747 | { |
| 1748 | char *path; |
| 1749 | unsigned long flags; |
| 1750 | int res; |
| 1751 | if (!PyArg_ParseTuple(args, "etk:lchflags", |
| 1752 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1753 | return NULL; |
| 1754 | Py_BEGIN_ALLOW_THREADS |
| 1755 | res = lchflags(path, flags); |
| 1756 | Py_END_ALLOW_THREADS |
| 1757 | if (res < 0) |
| 1758 | return posix_error_with_allocated_filename(path); |
| 1759 | PyMem_Free(path); |
| 1760 | Py_INCREF(Py_None); |
| 1761 | return Py_None; |
| 1762 | } |
| 1763 | #endif /* HAVE_LCHFLAGS */ |
| 1764 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1765 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1766 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1767 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1768 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1769 | |
| 1770 | static PyObject * |
| 1771 | posix_chroot(PyObject *self, PyObject *args) |
| 1772 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1773 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1774 | } |
| 1775 | #endif |
| 1776 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1777 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1778 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1779 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1780 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1781 | |
| 1782 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1783 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1784 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1785 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1786 | } |
| 1787 | #endif /* HAVE_FSYNC */ |
| 1788 | |
| 1789 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1790 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1791 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1792 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1793 | #endif |
| 1794 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1795 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1796 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1797 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1798 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1799 | |
| 1800 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1801 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1802 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1803 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1804 | } |
| 1805 | #endif /* HAVE_FDATASYNC */ |
| 1806 | |
| 1807 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1808 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1809 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1810 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1811 | 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] | 1812 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1813 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1814 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1815 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1816 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1817 | int uid, gid; |
| 1818 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1819 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1820 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1821 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1822 | return NULL; |
| 1823 | Py_BEGIN_ALLOW_THREADS |
| 1824 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1825 | Py_END_ALLOW_THREADS |
| 1826 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1827 | return posix_error_with_allocated_filename(path); |
| 1828 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1829 | Py_INCREF(Py_None); |
| 1830 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1831 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1832 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1833 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1834 | #ifdef HAVE_LCHOWN |
| 1835 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1836 | "lchown(path, uid, gid)\n\n\ |
| 1837 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1838 | This function will not follow symbolic links."); |
| 1839 | |
| 1840 | static PyObject * |
| 1841 | posix_lchown(PyObject *self, PyObject *args) |
| 1842 | { |
| 1843 | char *path = NULL; |
| 1844 | int uid, gid; |
| 1845 | int res; |
| 1846 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1847 | Py_FileSystemDefaultEncoding, &path, |
| 1848 | &uid, &gid)) |
| 1849 | return NULL; |
| 1850 | Py_BEGIN_ALLOW_THREADS |
| 1851 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1852 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1853 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1854 | return posix_error_with_allocated_filename(path); |
| 1855 | PyMem_Free(path); |
| 1856 | Py_INCREF(Py_None); |
| 1857 | return Py_None; |
| 1858 | } |
| 1859 | #endif /* HAVE_LCHOWN */ |
| 1860 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1861 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1862 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1863 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1864 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1865 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1866 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1867 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1868 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1869 | { |
| 1870 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1871 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1872 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1873 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1874 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1875 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1876 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1877 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1878 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1879 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1880 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1881 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1882 | return PyUnicode_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1883 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1884 | |
| 1885 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1886 | "getcwdu() -> path\n\n\ |
| 1887 | Return a unicode string representing the current working directory."); |
| 1888 | |
| 1889 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1890 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1891 | { |
| 1892 | char buf[1026]; |
| 1893 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1894 | |
| 1895 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1896 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1897 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1898 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1899 | wchar_t *wbuf2 = wbuf; |
| 1900 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1901 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1902 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 1903 | /* If the buffer is large enough, len does not include the |
| 1904 | terminating \0. If the buffer is too small, len includes |
| 1905 | the space needed for the terminator. */ |
| 1906 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 1907 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 1908 | if (wbuf2) |
| 1909 | len = GetCurrentDirectoryW(len, wbuf2); |
| 1910 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1911 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1912 | if (!wbuf2) { |
| 1913 | PyErr_NoMemory(); |
| 1914 | return NULL; |
| 1915 | } |
| 1916 | if (!len) { |
| 1917 | if (wbuf2 != wbuf) free(wbuf2); |
| 1918 | return win32_error("getcwdu", NULL); |
| 1919 | } |
| 1920 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 1921 | if (wbuf2 != wbuf) free(wbuf2); |
| 1922 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1923 | } |
| 1924 | #endif |
| 1925 | |
| 1926 | Py_BEGIN_ALLOW_THREADS |
| 1927 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1928 | res = _getcwd2(buf, sizeof buf); |
| 1929 | #else |
| 1930 | res = getcwd(buf, sizeof buf); |
| 1931 | #endif |
| 1932 | Py_END_ALLOW_THREADS |
| 1933 | if (res == NULL) |
| 1934 | return posix_error(); |
| 1935 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1936 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1937 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1938 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1939 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1940 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1941 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1942 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1943 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1944 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1945 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1946 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1947 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1948 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1949 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1950 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1951 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1953 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1954 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1955 | Return a list containing the names of the entries in the directory.\n\ |
| 1956 | \n\ |
| 1957 | path: path of directory to list\n\ |
| 1958 | \n\ |
| 1959 | 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] | 1960 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1961 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1962 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1963 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1964 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1965 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1966 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1967 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1968 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1969 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1970 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1971 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1972 | WIN32_FIND_DATA FileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1973 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1974 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1975 | 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] | 1976 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1977 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1978 | /* If on wide-character-capable OS see if argument |
| 1979 | is Unicode and if so use wide API. */ |
| 1980 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1981 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1982 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1983 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1984 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1985 | Py_UNICODE wch; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1986 | /* Overallocate for \\*.*\0 */ |
| 1987 | len = PyUnicode_GET_SIZE(po); |
| 1988 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 1989 | if (!wnamebuf) { |
| 1990 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1991 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1992 | } |
| 1993 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 1994 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 1995 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1996 | wnamebuf[len++] = L'\\'; |
| 1997 | wcscpy(wnamebuf + len, L"*.*"); |
| 1998 | if ((d = PyList_New(0)) == NULL) { |
| 1999 | free(wnamebuf); |
| 2000 | return NULL; |
| 2001 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2002 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2003 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2004 | int error = GetLastError(); |
| 2005 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2006 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2007 | return d; |
| 2008 | } |
| 2009 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2010 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2011 | free(wnamebuf); |
| 2012 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2013 | } |
| 2014 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2015 | /* Skip over . and .. */ |
| 2016 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2017 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2018 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2019 | if (v == NULL) { |
| 2020 | Py_DECREF(d); |
| 2021 | d = NULL; |
| 2022 | break; |
| 2023 | } |
| 2024 | if (PyList_Append(d, v) != 0) { |
| 2025 | Py_DECREF(v); |
| 2026 | Py_DECREF(d); |
| 2027 | d = NULL; |
| 2028 | break; |
| 2029 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2030 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2031 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2032 | Py_BEGIN_ALLOW_THREADS |
| 2033 | result = FindNextFileW(hFindFile, &wFileData); |
| 2034 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2035 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2036 | it got to the end of the directory. */ |
| 2037 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2038 | Py_DECREF(d); |
| 2039 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2040 | FindClose(hFindFile); |
| 2041 | free(wnamebuf); |
| 2042 | return NULL; |
| 2043 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2044 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2045 | |
| 2046 | if (FindClose(hFindFile) == FALSE) { |
| 2047 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2048 | win32_error_unicode("FindClose", wnamebuf); |
| 2049 | free(wnamebuf); |
| 2050 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2051 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2052 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2053 | return d; |
| 2054 | } |
| 2055 | /* Drop the argument parsing error as narrow strings |
| 2056 | are also valid. */ |
| 2057 | PyErr_Clear(); |
| 2058 | } |
| 2059 | #endif |
| 2060 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2061 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2062 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2063 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2064 | if (len > 0) { |
| 2065 | char ch = namebuf[len-1]; |
| 2066 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2067 | namebuf[len++] = '/'; |
| 2068 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2069 | strcpy(namebuf + len, "*.*"); |
| 2070 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2071 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2072 | return NULL; |
| 2073 | |
| 2074 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2075 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2076 | int error = GetLastError(); |
| 2077 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2078 | return d; |
| 2079 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2080 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2081 | } |
| 2082 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2083 | /* Skip over . and .. */ |
| 2084 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2085 | strcmp(FileData.cFileName, "..") != 0) { |
| 2086 | v = PyString_FromString(FileData.cFileName); |
| 2087 | if (v == NULL) { |
| 2088 | Py_DECREF(d); |
| 2089 | d = NULL; |
| 2090 | break; |
| 2091 | } |
| 2092 | if (PyList_Append(d, v) != 0) { |
| 2093 | Py_DECREF(v); |
| 2094 | Py_DECREF(d); |
| 2095 | d = NULL; |
| 2096 | break; |
| 2097 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2098 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2099 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2100 | Py_BEGIN_ALLOW_THREADS |
| 2101 | result = FindNextFile(hFindFile, &FileData); |
| 2102 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2103 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2104 | it got to the end of the directory. */ |
| 2105 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2106 | Py_DECREF(d); |
| 2107 | win32_error("FindNextFile", namebuf); |
| 2108 | FindClose(hFindFile); |
| 2109 | return NULL; |
| 2110 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2111 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2112 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2113 | if (FindClose(hFindFile) == FALSE) { |
| 2114 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2115 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2116 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2117 | |
| 2118 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2119 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2120 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2121 | |
| 2122 | #ifndef MAX_PATH |
| 2123 | #define MAX_PATH CCHMAXPATH |
| 2124 | #endif |
| 2125 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2126 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2127 | PyObject *d, *v; |
| 2128 | char namebuf[MAX_PATH+5]; |
| 2129 | HDIR hdir = 1; |
| 2130 | ULONG srchcnt = 1; |
| 2131 | FILEFINDBUF3 ep; |
| 2132 | APIRET rc; |
| 2133 | |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 2134 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 2135 | Py_FileSystemDefaultEncoding, &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2136 | return NULL; |
| 2137 | if (len >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2138 | PyMem_Free(name); |
| 2139 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2140 | return NULL; |
| 2141 | } |
| 2142 | strcpy(namebuf, name); |
| 2143 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2144 | if (*pt == ALTSEP) |
| 2145 | *pt = SEP; |
| 2146 | if (namebuf[len-1] != SEP) |
| 2147 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2148 | strcpy(namebuf + len, "*.*"); |
| 2149 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2150 | if ((d = PyList_New(0)) == NULL) { |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2151 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2152 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2153 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2154 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2155 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2156 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2157 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2158 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2159 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2160 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2161 | |
| 2162 | if (rc != NO_ERROR) { |
| 2163 | errno = ENOENT; |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2164 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2167 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2168 | do { |
| 2169 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2170 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2171 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2172 | |
| 2173 | strcpy(namebuf, ep.achName); |
| 2174 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2175 | /* Leave Case of Name Alone -- In Native Form */ |
| 2176 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2177 | |
| 2178 | v = PyString_FromString(namebuf); |
| 2179 | if (v == NULL) { |
| 2180 | Py_DECREF(d); |
| 2181 | d = NULL; |
| 2182 | break; |
| 2183 | } |
| 2184 | if (PyList_Append(d, v) != 0) { |
| 2185 | Py_DECREF(v); |
| 2186 | Py_DECREF(d); |
| 2187 | d = NULL; |
| 2188 | break; |
| 2189 | } |
| 2190 | Py_DECREF(v); |
| 2191 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2192 | } |
| 2193 | |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2194 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2195 | return d; |
| 2196 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2197 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2198 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2199 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2200 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2201 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2202 | int arg_is_unicode = 1; |
| 2203 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2204 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2205 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2206 | arg_is_unicode = 0; |
| 2207 | PyErr_Clear(); |
| 2208 | } |
| 2209 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2210 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2211 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2212 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2213 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2214 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2215 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2216 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2217 | return NULL; |
| 2218 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2219 | for (;;) { |
| 2220 | Py_BEGIN_ALLOW_THREADS |
| 2221 | ep = readdir(dirp); |
| 2222 | Py_END_ALLOW_THREADS |
| 2223 | if (ep == NULL) |
| 2224 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2225 | if (ep->d_name[0] == '.' && |
| 2226 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2227 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2228 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2229 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2230 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2231 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2232 | d = NULL; |
| 2233 | break; |
| 2234 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2235 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2236 | PyObject *w; |
| 2237 | |
| 2238 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2239 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2240 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2241 | if (w != NULL) { |
| 2242 | Py_DECREF(v); |
| 2243 | v = w; |
| 2244 | } |
| 2245 | else { |
| 2246 | /* fall back to the original byte string, as |
| 2247 | discussed in patch #683592 */ |
| 2248 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2249 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2250 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2251 | if (PyList_Append(d, v) != 0) { |
| 2252 | Py_DECREF(v); |
| 2253 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2254 | d = NULL; |
| 2255 | break; |
| 2256 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2257 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2258 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2259 | if (errno != 0 && d != NULL) { |
| 2260 | /* readdir() returned NULL and set errno */ |
| 2261 | closedir(dirp); |
| 2262 | Py_DECREF(d); |
| 2263 | return posix_error_with_allocated_filename(name); |
| 2264 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2265 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2266 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2267 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2268 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2269 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2270 | #endif /* which OS */ |
| 2271 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2272 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2273 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2274 | /* A helper function for abspath on win32 */ |
| 2275 | static PyObject * |
| 2276 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2277 | { |
| 2278 | /* assume encoded strings wont more than double no of chars */ |
| 2279 | char inbuf[MAX_PATH*2]; |
| 2280 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2281 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2282 | char outbuf[MAX_PATH*2]; |
| 2283 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2284 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2285 | if (unicode_file_names()) { |
| 2286 | PyUnicodeObject *po; |
| 2287 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2288 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2289 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2290 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2291 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2292 | woutbuf, &wtemp)) |
| 2293 | return win32_error("GetFullPathName", ""); |
| 2294 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2295 | } |
| 2296 | /* Drop the argument parsing error as narrow strings |
| 2297 | are also valid. */ |
| 2298 | PyErr_Clear(); |
| 2299 | } |
| 2300 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2301 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2302 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2303 | &insize)) |
| 2304 | return NULL; |
| 2305 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2306 | outbuf, &temp)) |
| 2307 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2308 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2309 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2310 | Py_FileSystemDefaultEncoding, NULL); |
| 2311 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2312 | return PyString_FromString(outbuf); |
| 2313 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2314 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2315 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2316 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2317 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2318 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2319 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2320 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2321 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2322 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2323 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2324 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2325 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2326 | |
| 2327 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2328 | if (unicode_file_names()) { |
| 2329 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2330 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2331 | Py_BEGIN_ALLOW_THREADS |
| 2332 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2333 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2334 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2335 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2336 | if (!res) |
| 2337 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2338 | Py_INCREF(Py_None); |
| 2339 | return Py_None; |
| 2340 | } |
| 2341 | /* Drop the argument parsing error as narrow strings |
| 2342 | are also valid. */ |
| 2343 | PyErr_Clear(); |
| 2344 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2345 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2346 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2347 | return NULL; |
| 2348 | Py_BEGIN_ALLOW_THREADS |
| 2349 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2350 | it is a simple dereference. */ |
| 2351 | res = CreateDirectoryA(path, NULL); |
| 2352 | Py_END_ALLOW_THREADS |
| 2353 | if (!res) { |
| 2354 | win32_error("mkdir", path); |
| 2355 | PyMem_Free(path); |
| 2356 | return NULL; |
| 2357 | } |
| 2358 | PyMem_Free(path); |
| 2359 | Py_INCREF(Py_None); |
| 2360 | return Py_None; |
| 2361 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2362 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2363 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2364 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2365 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2366 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2367 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2368 | res = mkdir(path); |
| 2369 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2370 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2371 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2372 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2373 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2374 | return posix_error_with_allocated_filename(path); |
| 2375 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2376 | Py_INCREF(Py_None); |
| 2377 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2378 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2381 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2382 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2383 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2384 | #include <sys/resource.h> |
| 2385 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2386 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2387 | |
| 2388 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2389 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2390 | "nice(inc) -> new_priority\n\n\ |
| 2391 | 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] | 2392 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2393 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2394 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2395 | { |
| 2396 | int increment, value; |
| 2397 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2398 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2399 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2400 | |
| 2401 | /* There are two flavours of 'nice': one that returns the new |
| 2402 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2403 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2404 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2405 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2406 | If we are of the nice family that returns the new priority, we |
| 2407 | need to clear errno before the call, and check if errno is filled |
| 2408 | before calling posix_error() on a returnvalue of -1, because the |
| 2409 | -1 may be the actual new priority! */ |
| 2410 | |
| 2411 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2412 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2413 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2414 | if (value == 0) |
| 2415 | value = getpriority(PRIO_PROCESS, 0); |
| 2416 | #endif |
| 2417 | if (value == -1 && errno != 0) |
| 2418 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2419 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2420 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2421 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2422 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2423 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2424 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2425 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2426 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2427 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2428 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2429 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2430 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2431 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2432 | PyObject *o1, *o2; |
| 2433 | char *p1, *p2; |
| 2434 | BOOL result; |
| 2435 | if (unicode_file_names()) { |
| 2436 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2437 | convert_to_unicode, &o1, |
| 2438 | convert_to_unicode, &o2)) |
| 2439 | PyErr_Clear(); |
| 2440 | else { |
| 2441 | Py_BEGIN_ALLOW_THREADS |
| 2442 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2443 | PyUnicode_AsUnicode(o2)); |
| 2444 | Py_END_ALLOW_THREADS |
| 2445 | Py_DECREF(o1); |
| 2446 | Py_DECREF(o2); |
| 2447 | if (!result) |
| 2448 | return win32_error("rename", NULL); |
| 2449 | Py_INCREF(Py_None); |
| 2450 | return Py_None; |
| 2451 | } |
| 2452 | } |
| 2453 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2454 | return NULL; |
| 2455 | Py_BEGIN_ALLOW_THREADS |
| 2456 | result = MoveFileA(p1, p2); |
| 2457 | Py_END_ALLOW_THREADS |
| 2458 | if (!result) |
| 2459 | return win32_error("rename", NULL); |
| 2460 | Py_INCREF(Py_None); |
| 2461 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2462 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2463 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2464 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2467 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2468 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2469 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2470 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2471 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2472 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2473 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2474 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2475 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2476 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2477 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2478 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2479 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2483 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2484 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2485 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2486 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2487 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2488 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2489 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2490 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2491 | 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] | 2492 | #else |
| 2493 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2494 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2497 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2498 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2499 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2500 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2501 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2502 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2503 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2504 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2505 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2506 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2507 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2508 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2509 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2510 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2511 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2512 | Py_END_ALLOW_THREADS |
| 2513 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2514 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2515 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2516 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2518 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2519 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2520 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2521 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2522 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2523 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2524 | { |
| 2525 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2526 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2527 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2528 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2529 | if (i < 0) |
| 2530 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2531 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2534 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2535 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2536 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2537 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2539 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2540 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2541 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2542 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2543 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2544 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2545 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2546 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2547 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2548 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2549 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2550 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2553 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2554 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2555 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2556 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2557 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2558 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2559 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2560 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2561 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2562 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2563 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2564 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2565 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2566 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2567 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2568 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2569 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2570 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2571 | u.sysname, |
| 2572 | u.nodename, |
| 2573 | u.release, |
| 2574 | u.version, |
| 2575 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2576 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2577 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2578 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2579 | static int |
| 2580 | extract_time(PyObject *t, long* sec, long* usec) |
| 2581 | { |
| 2582 | long intval; |
| 2583 | if (PyFloat_Check(t)) { |
| 2584 | double tval = PyFloat_AsDouble(t); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2585 | 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] | 2586 | if (!intobj) |
| 2587 | return -1; |
| 2588 | intval = PyInt_AsLong(intobj); |
| 2589 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2590 | if (intval == -1 && PyErr_Occurred()) |
| 2591 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2592 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2593 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2594 | if (*usec < 0) |
| 2595 | /* If rounding gave us a negative number, |
| 2596 | truncate. */ |
| 2597 | *usec = 0; |
| 2598 | return 0; |
| 2599 | } |
| 2600 | intval = PyInt_AsLong(t); |
| 2601 | if (intval == -1 && PyErr_Occurred()) |
| 2602 | return -1; |
| 2603 | *sec = intval; |
| 2604 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2605 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2606 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2608 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2609 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2610 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2611 | 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] | 2612 | 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] | 2613 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2614 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2615 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2616 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2617 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2618 | PyObject *arg; |
| 2619 | PyUnicodeObject *obwpath; |
| 2620 | wchar_t *wpath = NULL; |
| 2621 | char *apath = NULL; |
| 2622 | HANDLE hFile; |
| 2623 | long atimesec, mtimesec, ausec, musec; |
| 2624 | FILETIME atime, mtime; |
| 2625 | PyObject *result = NULL; |
| 2626 | |
| 2627 | if (unicode_file_names()) { |
| 2628 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2629 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2630 | Py_BEGIN_ALLOW_THREADS |
| 2631 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2632 | NULL, OPEN_EXISTING, |
| 2633 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2634 | Py_END_ALLOW_THREADS |
| 2635 | if (hFile == INVALID_HANDLE_VALUE) |
| 2636 | return win32_error_unicode("utime", wpath); |
| 2637 | } else |
| 2638 | /* Drop the argument parsing error as narrow strings |
| 2639 | are also valid. */ |
| 2640 | PyErr_Clear(); |
| 2641 | } |
| 2642 | if (!wpath) { |
| 2643 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2644 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2645 | return NULL; |
| 2646 | Py_BEGIN_ALLOW_THREADS |
| 2647 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2648 | NULL, OPEN_EXISTING, |
| 2649 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2650 | Py_END_ALLOW_THREADS |
| 2651 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2652 | win32_error("utime", apath); |
| 2653 | PyMem_Free(apath); |
| 2654 | return NULL; |
| 2655 | } |
| 2656 | PyMem_Free(apath); |
| 2657 | } |
| 2658 | |
| 2659 | if (arg == Py_None) { |
| 2660 | SYSTEMTIME now; |
| 2661 | GetSystemTime(&now); |
| 2662 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2663 | !SystemTimeToFileTime(&now, &atime)) { |
| 2664 | win32_error("utime", NULL); |
| 2665 | goto done; |
| 2666 | } |
| 2667 | } |
| 2668 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2669 | PyErr_SetString(PyExc_TypeError, |
| 2670 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2671 | goto done; |
| 2672 | } |
| 2673 | else { |
| 2674 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2675 | &atimesec, &ausec) == -1) |
| 2676 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2677 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2678 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2679 | &mtimesec, &musec) == -1) |
| 2680 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2681 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2682 | } |
| 2683 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2684 | /* Avoid putting the file name into the error here, |
| 2685 | as that may confuse the user into believing that |
| 2686 | something is wrong with the file, when it also |
| 2687 | could be the time stamp that gives a problem. */ |
| 2688 | win32_error("utime", NULL); |
| 2689 | } |
| 2690 | Py_INCREF(Py_None); |
| 2691 | result = Py_None; |
| 2692 | done: |
| 2693 | CloseHandle(hFile); |
| 2694 | return result; |
| 2695 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2696 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2697 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2698 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2699 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2700 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2701 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2702 | #if defined(HAVE_UTIMES) |
| 2703 | struct timeval buf[2]; |
| 2704 | #define ATIME buf[0].tv_sec |
| 2705 | #define MTIME buf[1].tv_sec |
| 2706 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2707 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2708 | struct utimbuf buf; |
| 2709 | #define ATIME buf.actime |
| 2710 | #define MTIME buf.modtime |
| 2711 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2712 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2713 | time_t buf[2]; |
| 2714 | #define ATIME buf[0] |
| 2715 | #define MTIME buf[1] |
| 2716 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2717 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2718 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2719 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2720 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2721 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2722 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2723 | if (arg == Py_None) { |
| 2724 | /* optional time values not given */ |
| 2725 | Py_BEGIN_ALLOW_THREADS |
| 2726 | res = utime(path, NULL); |
| 2727 | Py_END_ALLOW_THREADS |
| 2728 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2729 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2730 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2731 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2732 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2733 | return NULL; |
| 2734 | } |
| 2735 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2736 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2737 | &atime, &ausec) == -1) { |
| 2738 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2739 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2740 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2741 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2742 | &mtime, &musec) == -1) { |
| 2743 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2744 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2745 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2746 | ATIME = atime; |
| 2747 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2748 | #ifdef HAVE_UTIMES |
| 2749 | buf[0].tv_usec = ausec; |
| 2750 | buf[1].tv_usec = musec; |
| 2751 | Py_BEGIN_ALLOW_THREADS |
| 2752 | res = utimes(path, buf); |
| 2753 | Py_END_ALLOW_THREADS |
| 2754 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2755 | Py_BEGIN_ALLOW_THREADS |
| 2756 | res = utime(path, UTIME_ARG); |
| 2757 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2758 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2759 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2760 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2761 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2762 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2763 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2764 | Py_INCREF(Py_None); |
| 2765 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2766 | #undef UTIME_ARG |
| 2767 | #undef ATIME |
| 2768 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2769 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2772 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2773 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2775 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2776 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2777 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2778 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2779 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2780 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2781 | { |
| 2782 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2783 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2784 | return NULL; |
| 2785 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2786 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2789 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2790 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2791 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2792 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2793 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2794 | for (i = 0; i < count; i++) |
| 2795 | PyMem_Free(array[i]); |
| 2796 | PyMem_DEL(array); |
| 2797 | } |
| 2798 | #endif |
| 2799 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2800 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2801 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2802 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2803 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2804 | Execute an executable path with arguments, replacing current process.\n\ |
| 2805 | \n\ |
| 2806 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2807 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2808 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2809 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2810 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2811 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2812 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2813 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2814 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2815 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2816 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2817 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2818 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2819 | argv is a list or tuple of strings. */ |
| 2820 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2821 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2822 | Py_FileSystemDefaultEncoding, |
| 2823 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2824 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2825 | if (PyList_Check(argv)) { |
| 2826 | argc = PyList_Size(argv); |
| 2827 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2828 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2829 | else if (PyTuple_Check(argv)) { |
| 2830 | argc = PyTuple_Size(argv); |
| 2831 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2832 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2833 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2834 | 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] | 2835 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2836 | return NULL; |
| 2837 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 2838 | if (argc < 1) { |
| 2839 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 2840 | PyMem_Free(path); |
| 2841 | return NULL; |
| 2842 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2843 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2844 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2845 | if (argvlist == NULL) { |
| 2846 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2847 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2848 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2849 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2850 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2851 | Py_FileSystemDefaultEncoding, |
| 2852 | &argvlist[i])) { |
| 2853 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2854 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2855 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2856 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2857 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2858 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2859 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2860 | } |
| 2861 | argvlist[argc] = NULL; |
| 2862 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2863 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2864 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2865 | /* If we get here it's definitely an error */ |
| 2866 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2867 | free_string_array(argvlist, argc); |
| 2868 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2869 | return posix_error(); |
| 2870 | } |
| 2871 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2872 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2873 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2874 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2875 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2876 | \n\ |
| 2877 | path: path of executable file\n\ |
| 2878 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2879 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2880 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2881 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2882 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2883 | { |
| 2884 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2885 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2886 | char **argvlist; |
| 2887 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2888 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2889 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2890 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2891 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2892 | |
| 2893 | /* execve has three arguments: (path, argv, env), where |
| 2894 | argv is a list or tuple of strings and env is a dictionary |
| 2895 | like posix.environ. */ |
| 2896 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2897 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2898 | Py_FileSystemDefaultEncoding, |
| 2899 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2900 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2901 | if (PyList_Check(argv)) { |
| 2902 | argc = PyList_Size(argv); |
| 2903 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2904 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2905 | else if (PyTuple_Check(argv)) { |
| 2906 | argc = PyTuple_Size(argv); |
| 2907 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2908 | } |
| 2909 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2910 | PyErr_SetString(PyExc_TypeError, |
| 2911 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2912 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2913 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2914 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2915 | PyErr_SetString(PyExc_TypeError, |
| 2916 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2917 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2920 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2921 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2922 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2923 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2924 | } |
| 2925 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2926 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2927 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2928 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2929 | &argvlist[i])) |
| 2930 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2931 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2932 | goto fail_1; |
| 2933 | } |
| 2934 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2935 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2936 | argvlist[argc] = NULL; |
| 2937 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2938 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2939 | if (i < 0) |
| 2940 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2941 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2942 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2943 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2944 | goto fail_1; |
| 2945 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2946 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2947 | keys = PyMapping_Keys(env); |
| 2948 | vals = PyMapping_Values(env); |
| 2949 | if (!keys || !vals) |
| 2950 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2951 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2952 | PyErr_SetString(PyExc_TypeError, |
| 2953 | "execve(): env.keys() or env.values() is not a list"); |
| 2954 | goto fail_2; |
| 2955 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2956 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2957 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2958 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2959 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2960 | |
| 2961 | key = PyList_GetItem(keys, pos); |
| 2962 | val = PyList_GetItem(vals, pos); |
| 2963 | if (!key || !val) |
| 2964 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2965 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2966 | if (!PyArg_Parse( |
| 2967 | key, |
| 2968 | "s;execve() arg 3 contains a non-string key", |
| 2969 | &k) || |
| 2970 | !PyArg_Parse( |
| 2971 | val, |
| 2972 | "s;execve() arg 3 contains a non-string value", |
| 2973 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2974 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2975 | goto fail_2; |
| 2976 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2977 | |
| 2978 | #if defined(PYOS_OS2) |
| 2979 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2980 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2981 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2982 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2983 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2984 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2985 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2986 | goto fail_2; |
| 2987 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2988 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2989 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2990 | #if defined(PYOS_OS2) |
| 2991 | } |
| 2992 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2993 | } |
| 2994 | envlist[envc] = 0; |
| 2995 | |
| 2996 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2997 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2998 | /* If we get here it's definitely an error */ |
| 2999 | |
| 3000 | (void) posix_error(); |
| 3001 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3002 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3003 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3004 | PyMem_DEL(envlist[envc]); |
| 3005 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3006 | fail_1: |
| 3007 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3008 | Py_XDECREF(vals); |
| 3009 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3010 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3011 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3012 | return NULL; |
| 3013 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3014 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3015 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3016 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3017 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3018 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3019 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3020 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3021 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3022 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3023 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3024 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3025 | |
| 3026 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3027 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3028 | { |
| 3029 | char *path; |
| 3030 | PyObject *argv; |
| 3031 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3032 | int mode, i; |
| 3033 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3034 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3035 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3036 | |
| 3037 | /* spawnv has three arguments: (mode, path, argv), where |
| 3038 | argv is a list or tuple of strings. */ |
| 3039 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3040 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3041 | Py_FileSystemDefaultEncoding, |
| 3042 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3043 | return NULL; |
| 3044 | if (PyList_Check(argv)) { |
| 3045 | argc = PyList_Size(argv); |
| 3046 | getitem = PyList_GetItem; |
| 3047 | } |
| 3048 | else if (PyTuple_Check(argv)) { |
| 3049 | argc = PyTuple_Size(argv); |
| 3050 | getitem = PyTuple_GetItem; |
| 3051 | } |
| 3052 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3053 | PyErr_SetString(PyExc_TypeError, |
| 3054 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3055 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3056 | return NULL; |
| 3057 | } |
| 3058 | |
| 3059 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3060 | if (argvlist == NULL) { |
| 3061 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3062 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3063 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3064 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3065 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3066 | Py_FileSystemDefaultEncoding, |
| 3067 | &argvlist[i])) { |
| 3068 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3069 | PyErr_SetString( |
| 3070 | PyExc_TypeError, |
| 3071 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3072 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3073 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | argvlist[argc] = NULL; |
| 3077 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3078 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3079 | Py_BEGIN_ALLOW_THREADS |
| 3080 | spawnval = spawnv(mode, path, argvlist); |
| 3081 | Py_END_ALLOW_THREADS |
| 3082 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3083 | if (mode == _OLD_P_OVERLAY) |
| 3084 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3085 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3086 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3087 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3088 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3089 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3090 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3091 | free_string_array(argvlist, argc); |
| 3092 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3093 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3094 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3095 | return posix_error(); |
| 3096 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3097 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3098 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3099 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3100 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3101 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3105 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3106 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3107 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3108 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3109 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3110 | path: path of executable file\n\ |
| 3111 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3112 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3113 | |
| 3114 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3115 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3116 | { |
| 3117 | char *path; |
| 3118 | PyObject *argv, *env; |
| 3119 | char **argvlist; |
| 3120 | char **envlist; |
| 3121 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3122 | int mode, pos, envc; |
| 3123 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3124 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3125 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3126 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3127 | |
| 3128 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3129 | argv is a list or tuple of strings and env is a dictionary |
| 3130 | like posix.environ. */ |
| 3131 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3132 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3133 | Py_FileSystemDefaultEncoding, |
| 3134 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3135 | return NULL; |
| 3136 | if (PyList_Check(argv)) { |
| 3137 | argc = PyList_Size(argv); |
| 3138 | getitem = PyList_GetItem; |
| 3139 | } |
| 3140 | else if (PyTuple_Check(argv)) { |
| 3141 | argc = PyTuple_Size(argv); |
| 3142 | getitem = PyTuple_GetItem; |
| 3143 | } |
| 3144 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3145 | PyErr_SetString(PyExc_TypeError, |
| 3146 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3147 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3148 | } |
| 3149 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3150 | PyErr_SetString(PyExc_TypeError, |
| 3151 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3152 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | argvlist = PyMem_NEW(char *, argc+1); |
| 3156 | if (argvlist == NULL) { |
| 3157 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3158 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3159 | } |
| 3160 | for (i = 0; i < argc; i++) { |
| 3161 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3162 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3163 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3164 | &argvlist[i])) |
| 3165 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3166 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3167 | goto fail_1; |
| 3168 | } |
| 3169 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3170 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3171 | argvlist[argc] = NULL; |
| 3172 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3173 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3174 | if (i < 0) |
| 3175 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3176 | envlist = PyMem_NEW(char *, i + 1); |
| 3177 | if (envlist == NULL) { |
| 3178 | PyErr_NoMemory(); |
| 3179 | goto fail_1; |
| 3180 | } |
| 3181 | envc = 0; |
| 3182 | keys = PyMapping_Keys(env); |
| 3183 | vals = PyMapping_Values(env); |
| 3184 | if (!keys || !vals) |
| 3185 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3186 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3187 | PyErr_SetString(PyExc_TypeError, |
| 3188 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3189 | goto fail_2; |
| 3190 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3191 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3192 | for (pos = 0; pos < i; pos++) { |
| 3193 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3194 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3195 | |
| 3196 | key = PyList_GetItem(keys, pos); |
| 3197 | val = PyList_GetItem(vals, pos); |
| 3198 | if (!key || !val) |
| 3199 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3200 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3201 | if (!PyArg_Parse( |
| 3202 | key, |
| 3203 | "s;spawnve() arg 3 contains a non-string key", |
| 3204 | &k) || |
| 3205 | !PyArg_Parse( |
| 3206 | val, |
| 3207 | "s;spawnve() arg 3 contains a non-string value", |
| 3208 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3209 | { |
| 3210 | goto fail_2; |
| 3211 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3212 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3213 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3214 | if (p == NULL) { |
| 3215 | PyErr_NoMemory(); |
| 3216 | goto fail_2; |
| 3217 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3218 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3219 | envlist[envc++] = p; |
| 3220 | } |
| 3221 | envlist[envc] = 0; |
| 3222 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3223 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3224 | Py_BEGIN_ALLOW_THREADS |
| 3225 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3226 | Py_END_ALLOW_THREADS |
| 3227 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3228 | if (mode == _OLD_P_OVERLAY) |
| 3229 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3230 | |
| 3231 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3232 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3233 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3234 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3235 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3236 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3237 | (void) posix_error(); |
| 3238 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3239 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3240 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3241 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3242 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3243 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3244 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3245 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3246 | while (--envc >= 0) |
| 3247 | PyMem_DEL(envlist[envc]); |
| 3248 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3249 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3250 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3251 | Py_XDECREF(vals); |
| 3252 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3253 | fail_0: |
| 3254 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3255 | return res; |
| 3256 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3257 | |
| 3258 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3259 | #if defined(PYOS_OS2) |
| 3260 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3261 | "spawnvp(mode, file, args)\n\n\ |
| 3262 | Execute the program 'file' in a new process, using the environment\n\ |
| 3263 | search path to find the file.\n\ |
| 3264 | \n\ |
| 3265 | mode: mode of process creation\n\ |
| 3266 | file: executable file name\n\ |
| 3267 | args: tuple or list of strings"); |
| 3268 | |
| 3269 | static PyObject * |
| 3270 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3271 | { |
| 3272 | char *path; |
| 3273 | PyObject *argv; |
| 3274 | char **argvlist; |
| 3275 | int mode, i, argc; |
| 3276 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3277 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3278 | |
| 3279 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3280 | argv is a list or tuple of strings. */ |
| 3281 | |
| 3282 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3283 | Py_FileSystemDefaultEncoding, |
| 3284 | &path, &argv)) |
| 3285 | return NULL; |
| 3286 | if (PyList_Check(argv)) { |
| 3287 | argc = PyList_Size(argv); |
| 3288 | getitem = PyList_GetItem; |
| 3289 | } |
| 3290 | else if (PyTuple_Check(argv)) { |
| 3291 | argc = PyTuple_Size(argv); |
| 3292 | getitem = PyTuple_GetItem; |
| 3293 | } |
| 3294 | else { |
| 3295 | PyErr_SetString(PyExc_TypeError, |
| 3296 | "spawnvp() arg 2 must be a tuple or list"); |
| 3297 | PyMem_Free(path); |
| 3298 | return NULL; |
| 3299 | } |
| 3300 | |
| 3301 | argvlist = PyMem_NEW(char *, argc+1); |
| 3302 | if (argvlist == NULL) { |
| 3303 | PyMem_Free(path); |
| 3304 | return PyErr_NoMemory(); |
| 3305 | } |
| 3306 | for (i = 0; i < argc; i++) { |
| 3307 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3308 | Py_FileSystemDefaultEncoding, |
| 3309 | &argvlist[i])) { |
| 3310 | free_string_array(argvlist, i); |
| 3311 | PyErr_SetString( |
| 3312 | PyExc_TypeError, |
| 3313 | "spawnvp() arg 2 must contain only strings"); |
| 3314 | PyMem_Free(path); |
| 3315 | return NULL; |
| 3316 | } |
| 3317 | } |
| 3318 | argvlist[argc] = NULL; |
| 3319 | |
| 3320 | Py_BEGIN_ALLOW_THREADS |
| 3321 | #if defined(PYCC_GCC) |
| 3322 | spawnval = spawnvp(mode, path, argvlist); |
| 3323 | #else |
| 3324 | spawnval = _spawnvp(mode, path, argvlist); |
| 3325 | #endif |
| 3326 | Py_END_ALLOW_THREADS |
| 3327 | |
| 3328 | free_string_array(argvlist, argc); |
| 3329 | PyMem_Free(path); |
| 3330 | |
| 3331 | if (spawnval == -1) |
| 3332 | return posix_error(); |
| 3333 | else |
| 3334 | return Py_BuildValue("l", (long) spawnval); |
| 3335 | } |
| 3336 | |
| 3337 | |
| 3338 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3339 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3340 | Execute the program 'file' in a new process, using the environment\n\ |
| 3341 | search path to find the file.\n\ |
| 3342 | \n\ |
| 3343 | mode: mode of process creation\n\ |
| 3344 | file: executable file name\n\ |
| 3345 | args: tuple or list of arguments\n\ |
| 3346 | env: dictionary of strings mapping to strings"); |
| 3347 | |
| 3348 | static PyObject * |
| 3349 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3350 | { |
| 3351 | char *path; |
| 3352 | PyObject *argv, *env; |
| 3353 | char **argvlist; |
| 3354 | char **envlist; |
| 3355 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3356 | int mode, i, pos, argc, envc; |
| 3357 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3358 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3359 | int lastarg = 0; |
| 3360 | |
| 3361 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3362 | argv is a list or tuple of strings and env is a dictionary |
| 3363 | like posix.environ. */ |
| 3364 | |
| 3365 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3366 | Py_FileSystemDefaultEncoding, |
| 3367 | &path, &argv, &env)) |
| 3368 | return NULL; |
| 3369 | if (PyList_Check(argv)) { |
| 3370 | argc = PyList_Size(argv); |
| 3371 | getitem = PyList_GetItem; |
| 3372 | } |
| 3373 | else if (PyTuple_Check(argv)) { |
| 3374 | argc = PyTuple_Size(argv); |
| 3375 | getitem = PyTuple_GetItem; |
| 3376 | } |
| 3377 | else { |
| 3378 | PyErr_SetString(PyExc_TypeError, |
| 3379 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3380 | goto fail_0; |
| 3381 | } |
| 3382 | if (!PyMapping_Check(env)) { |
| 3383 | PyErr_SetString(PyExc_TypeError, |
| 3384 | "spawnvpe() arg 3 must be a mapping object"); |
| 3385 | goto fail_0; |
| 3386 | } |
| 3387 | |
| 3388 | argvlist = PyMem_NEW(char *, argc+1); |
| 3389 | if (argvlist == NULL) { |
| 3390 | PyErr_NoMemory(); |
| 3391 | goto fail_0; |
| 3392 | } |
| 3393 | for (i = 0; i < argc; i++) { |
| 3394 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3395 | "et;spawnvpe() arg 2 must contain only strings", |
| 3396 | Py_FileSystemDefaultEncoding, |
| 3397 | &argvlist[i])) |
| 3398 | { |
| 3399 | lastarg = i; |
| 3400 | goto fail_1; |
| 3401 | } |
| 3402 | } |
| 3403 | lastarg = argc; |
| 3404 | argvlist[argc] = NULL; |
| 3405 | |
| 3406 | i = PyMapping_Size(env); |
| 3407 | if (i < 0) |
| 3408 | goto fail_1; |
| 3409 | envlist = PyMem_NEW(char *, i + 1); |
| 3410 | if (envlist == NULL) { |
| 3411 | PyErr_NoMemory(); |
| 3412 | goto fail_1; |
| 3413 | } |
| 3414 | envc = 0; |
| 3415 | keys = PyMapping_Keys(env); |
| 3416 | vals = PyMapping_Values(env); |
| 3417 | if (!keys || !vals) |
| 3418 | goto fail_2; |
| 3419 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3420 | PyErr_SetString(PyExc_TypeError, |
| 3421 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3422 | goto fail_2; |
| 3423 | } |
| 3424 | |
| 3425 | for (pos = 0; pos < i; pos++) { |
| 3426 | char *p, *k, *v; |
| 3427 | size_t len; |
| 3428 | |
| 3429 | key = PyList_GetItem(keys, pos); |
| 3430 | val = PyList_GetItem(vals, pos); |
| 3431 | if (!key || !val) |
| 3432 | goto fail_2; |
| 3433 | |
| 3434 | if (!PyArg_Parse( |
| 3435 | key, |
| 3436 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3437 | &k) || |
| 3438 | !PyArg_Parse( |
| 3439 | val, |
| 3440 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3441 | &v)) |
| 3442 | { |
| 3443 | goto fail_2; |
| 3444 | } |
| 3445 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3446 | p = PyMem_NEW(char, len); |
| 3447 | if (p == NULL) { |
| 3448 | PyErr_NoMemory(); |
| 3449 | goto fail_2; |
| 3450 | } |
| 3451 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3452 | envlist[envc++] = p; |
| 3453 | } |
| 3454 | envlist[envc] = 0; |
| 3455 | |
| 3456 | Py_BEGIN_ALLOW_THREADS |
| 3457 | #if defined(PYCC_GCC) |
| 3458 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3459 | #else |
| 3460 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3461 | #endif |
| 3462 | Py_END_ALLOW_THREADS |
| 3463 | |
| 3464 | if (spawnval == -1) |
| 3465 | (void) posix_error(); |
| 3466 | else |
| 3467 | res = Py_BuildValue("l", (long) spawnval); |
| 3468 | |
| 3469 | fail_2: |
| 3470 | while (--envc >= 0) |
| 3471 | PyMem_DEL(envlist[envc]); |
| 3472 | PyMem_DEL(envlist); |
| 3473 | fail_1: |
| 3474 | free_string_array(argvlist, lastarg); |
| 3475 | Py_XDECREF(vals); |
| 3476 | Py_XDECREF(keys); |
| 3477 | fail_0: |
| 3478 | PyMem_Free(path); |
| 3479 | return res; |
| 3480 | } |
| 3481 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3482 | #endif /* HAVE_SPAWNV */ |
| 3483 | |
| 3484 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3485 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3486 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3487 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3488 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3489 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3490 | 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] | 3491 | |
| 3492 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3493 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3494 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3495 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3496 | if (pid == -1) |
| 3497 | return posix_error(); |
| 3498 | PyOS_AfterFork(); |
| 3499 | return PyInt_FromLong((long)pid); |
| 3500 | } |
| 3501 | #endif |
| 3502 | |
| 3503 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3504 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3505 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3506 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3507 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3508 | 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] | 3509 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3510 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3511 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3512 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3513 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3514 | if (pid == -1) |
| 3515 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3516 | if (pid == 0) |
| 3517 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3518 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3519 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3520 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3521 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3522 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3523 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3524 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3525 | #define DEV_PTY_FILE "/dev/ptc" |
| 3526 | #define HAVE_DEV_PTMX |
| 3527 | #else |
| 3528 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3529 | #endif |
| 3530 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3531 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3532 | #ifdef HAVE_PTY_H |
| 3533 | #include <pty.h> |
| 3534 | #else |
| 3535 | #ifdef HAVE_LIBUTIL_H |
| 3536 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3537 | #endif /* HAVE_LIBUTIL_H */ |
| 3538 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3539 | #ifdef HAVE_STROPTS_H |
| 3540 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3541 | #endif |
| 3542 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3543 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3544 | #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] | 3545 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3546 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3547 | 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] | 3548 | |
| 3549 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3550 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3551 | { |
| 3552 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3553 | #ifndef HAVE_OPENPTY |
| 3554 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3555 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3556 | #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] | 3557 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3558 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3559 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3560 | #endif |
| 3561 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3562 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3563 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3564 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3565 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3566 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3567 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3568 | if (slave_name == NULL) |
| 3569 | return posix_error(); |
| 3570 | |
| 3571 | slave_fd = open(slave_name, O_RDWR); |
| 3572 | if (slave_fd < 0) |
| 3573 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3574 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3575 | 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] | 3576 | if (master_fd < 0) |
| 3577 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3578 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3579 | /* change permission of slave */ |
| 3580 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3581 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3582 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3583 | } |
| 3584 | /* unlock slave */ |
| 3585 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3586 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3587 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3588 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3589 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3590 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3591 | if (slave_name == NULL) |
| 3592 | return posix_error(); |
| 3593 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3594 | if (slave_fd < 0) |
| 3595 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3596 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3597 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3598 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3599 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3600 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3601 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3602 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3603 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3604 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3605 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3606 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3607 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3608 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3609 | |
| 3610 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3611 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3612 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3613 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3614 | 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] | 3615 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3616 | |
| 3617 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3618 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3619 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3620 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3621 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3622 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3623 | if (pid == -1) |
| 3624 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3625 | if (pid == 0) |
| 3626 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3627 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3628 | } |
| 3629 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3630 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3631 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3632 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3633 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3634 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3635 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3636 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3637 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3638 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3639 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3640 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3641 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3642 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3643 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3644 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3645 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3646 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3647 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3648 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3649 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3650 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3651 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3652 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3653 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3654 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3655 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3656 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3657 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3658 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3659 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3660 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3661 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3662 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3663 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3664 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3665 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3666 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3667 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3668 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3669 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3670 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3671 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3672 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3673 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3674 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3675 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3676 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3677 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3680 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3681 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3682 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3683 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3684 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3685 | |
| 3686 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3687 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3688 | { |
| 3689 | PyObject *result = NULL; |
| 3690 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3691 | #ifdef NGROUPS_MAX |
| 3692 | #define MAX_GROUPS NGROUPS_MAX |
| 3693 | #else |
| 3694 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3695 | #define MAX_GROUPS 64 |
| 3696 | #endif |
| 3697 | gid_t grouplist[MAX_GROUPS]; |
| 3698 | int n; |
| 3699 | |
| 3700 | n = getgroups(MAX_GROUPS, grouplist); |
| 3701 | if (n < 0) |
| 3702 | posix_error(); |
| 3703 | else { |
| 3704 | result = PyList_New(n); |
| 3705 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3706 | int i; |
| 3707 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3708 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3709 | if (o == NULL) { |
| 3710 | Py_DECREF(result); |
| 3711 | result = NULL; |
| 3712 | break; |
| 3713 | } |
| 3714 | PyList_SET_ITEM(result, i, o); |
| 3715 | } |
| 3716 | } |
| 3717 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3718 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3719 | return result; |
| 3720 | } |
| 3721 | #endif |
| 3722 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3723 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3724 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3725 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3726 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3727 | |
| 3728 | static PyObject * |
| 3729 | posix_getpgid(PyObject *self, PyObject *args) |
| 3730 | { |
| 3731 | int pid, pgid; |
| 3732 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3733 | return NULL; |
| 3734 | pgid = getpgid(pid); |
| 3735 | if (pgid < 0) |
| 3736 | return posix_error(); |
| 3737 | return PyInt_FromLong((long)pgid); |
| 3738 | } |
| 3739 | #endif /* HAVE_GETPGID */ |
| 3740 | |
| 3741 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3742 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3743 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3744 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3745 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3746 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3747 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3748 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3749 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3750 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3751 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3752 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3753 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3754 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3755 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3756 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3757 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3758 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3759 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3760 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3761 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3762 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3763 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3764 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3765 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3766 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3767 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3768 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3769 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3770 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3771 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3772 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3773 | Py_INCREF(Py_None); |
| 3774 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3777 | #endif /* HAVE_SETPGRP */ |
| 3778 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3779 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3780 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3781 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3782 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3783 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3784 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3785 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3786 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3787 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3788 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3789 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3790 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3791 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3792 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3793 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3794 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3795 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3796 | |
| 3797 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3798 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3799 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3800 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3801 | char *name; |
| 3802 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3803 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3804 | errno = 0; |
| 3805 | name = getlogin(); |
| 3806 | if (name == NULL) { |
| 3807 | if (errno) |
| 3808 | posix_error(); |
| 3809 | else |
| 3810 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3811 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3812 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3813 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 3814 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3815 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3816 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3817 | return result; |
| 3818 | } |
| 3819 | #endif |
| 3820 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3821 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3822 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3823 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3824 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3825 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3826 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3827 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3828 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3829 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3830 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3831 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3832 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3833 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3834 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3835 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3836 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3837 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3838 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3839 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3840 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3841 | { |
| 3842 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3843 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3844 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3845 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3846 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3847 | APIRET rc; |
| 3848 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3849 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3850 | |
| 3851 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3852 | APIRET rc; |
| 3853 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3854 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3855 | |
| 3856 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3857 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3858 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3859 | if (kill(pid, sig) == -1) |
| 3860 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3861 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3862 | Py_INCREF(Py_None); |
| 3863 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3864 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3865 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3866 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3867 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3868 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3869 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3870 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3871 | |
| 3872 | static PyObject * |
| 3873 | posix_killpg(PyObject *self, PyObject *args) |
| 3874 | { |
| 3875 | int pgid, sig; |
| 3876 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3877 | return NULL; |
| 3878 | if (killpg(pgid, sig) == -1) |
| 3879 | return posix_error(); |
| 3880 | Py_INCREF(Py_None); |
| 3881 | return Py_None; |
| 3882 | } |
| 3883 | #endif |
| 3884 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3885 | #ifdef HAVE_PLOCK |
| 3886 | |
| 3887 | #ifdef HAVE_SYS_LOCK_H |
| 3888 | #include <sys/lock.h> |
| 3889 | #endif |
| 3890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3891 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3892 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3893 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3894 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3895 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3896 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3897 | { |
| 3898 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3899 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3900 | return NULL; |
| 3901 | if (plock(op) == -1) |
| 3902 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3903 | Py_INCREF(Py_None); |
| 3904 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3905 | } |
| 3906 | #endif |
| 3907 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3908 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3909 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3910 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3911 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3912 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3913 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3914 | Set the current process's user id."); |
| 3915 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3916 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3917 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3918 | { |
| 3919 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3920 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3921 | return NULL; |
| 3922 | if (setuid(uid) < 0) |
| 3923 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3924 | Py_INCREF(Py_None); |
| 3925 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3926 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3927 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3928 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3929 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3930 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3931 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3932 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3933 | Set the current process's effective user id."); |
| 3934 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3935 | static PyObject * |
| 3936 | posix_seteuid (PyObject *self, PyObject *args) |
| 3937 | { |
| 3938 | int euid; |
| 3939 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3940 | return NULL; |
| 3941 | } else if (seteuid(euid) < 0) { |
| 3942 | return posix_error(); |
| 3943 | } else { |
| 3944 | Py_INCREF(Py_None); |
| 3945 | return Py_None; |
| 3946 | } |
| 3947 | } |
| 3948 | #endif /* HAVE_SETEUID */ |
| 3949 | |
| 3950 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3951 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3952 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3953 | Set the current process's effective group id."); |
| 3954 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3955 | static PyObject * |
| 3956 | posix_setegid (PyObject *self, PyObject *args) |
| 3957 | { |
| 3958 | int egid; |
| 3959 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3960 | return NULL; |
| 3961 | } else if (setegid(egid) < 0) { |
| 3962 | return posix_error(); |
| 3963 | } else { |
| 3964 | Py_INCREF(Py_None); |
| 3965 | return Py_None; |
| 3966 | } |
| 3967 | } |
| 3968 | #endif /* HAVE_SETEGID */ |
| 3969 | |
| 3970 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3971 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 3972 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3973 | Set the current process's real and effective user ids."); |
| 3974 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3975 | static PyObject * |
| 3976 | posix_setreuid (PyObject *self, PyObject *args) |
| 3977 | { |
| 3978 | int ruid, euid; |
| 3979 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3980 | return NULL; |
| 3981 | } else if (setreuid(ruid, euid) < 0) { |
| 3982 | return posix_error(); |
| 3983 | } else { |
| 3984 | Py_INCREF(Py_None); |
| 3985 | return Py_None; |
| 3986 | } |
| 3987 | } |
| 3988 | #endif /* HAVE_SETREUID */ |
| 3989 | |
| 3990 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3991 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 3992 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3993 | Set the current process's real and effective group ids."); |
| 3994 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3995 | static PyObject * |
| 3996 | posix_setregid (PyObject *self, PyObject *args) |
| 3997 | { |
| 3998 | int rgid, egid; |
| 3999 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4000 | return NULL; |
| 4001 | } else if (setregid(rgid, egid) < 0) { |
| 4002 | return posix_error(); |
| 4003 | } else { |
| 4004 | Py_INCREF(Py_None); |
| 4005 | return Py_None; |
| 4006 | } |
| 4007 | } |
| 4008 | #endif /* HAVE_SETREGID */ |
| 4009 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4010 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4011 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4012 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4013 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4014 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4015 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4016 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4017 | { |
| 4018 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4019 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4020 | return NULL; |
| 4021 | if (setgid(gid) < 0) |
| 4022 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4023 | Py_INCREF(Py_None); |
| 4024 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4025 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4026 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4027 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4028 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4029 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4030 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4031 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4032 | |
| 4033 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4034 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4035 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4036 | int i, len; |
| 4037 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4038 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4039 | if (!PySequence_Check(groups)) { |
| 4040 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4041 | return NULL; |
| 4042 | } |
| 4043 | len = PySequence_Size(groups); |
| 4044 | if (len > MAX_GROUPS) { |
| 4045 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4046 | return NULL; |
| 4047 | } |
| 4048 | for(i = 0; i < len; i++) { |
| 4049 | PyObject *elem; |
| 4050 | elem = PySequence_GetItem(groups, i); |
| 4051 | if (!elem) |
| 4052 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4053 | if (!PyLong_Check(elem)) { |
| 4054 | PyErr_SetString(PyExc_TypeError, |
| 4055 | "groups must be integers"); |
| 4056 | Py_DECREF(elem); |
| 4057 | return NULL; |
| 4058 | } else { |
| 4059 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4060 | if (PyErr_Occurred()) { |
| 4061 | PyErr_SetString(PyExc_TypeError, |
| 4062 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4063 | Py_DECREF(elem); |
| 4064 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4065 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4066 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4067 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4068 | if (grouplist[i] != x) { |
| 4069 | PyErr_SetString(PyExc_TypeError, |
| 4070 | "group id too big"); |
| 4071 | Py_DECREF(elem); |
| 4072 | return NULL; |
| 4073 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4074 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4075 | Py_DECREF(elem); |
| 4076 | } |
| 4077 | |
| 4078 | if (setgroups(len, grouplist) < 0) |
| 4079 | return posix_error(); |
| 4080 | Py_INCREF(Py_None); |
| 4081 | return Py_None; |
| 4082 | } |
| 4083 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4084 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4085 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4086 | static PyObject * |
| 4087 | wait_helper(int pid, int status, struct rusage *ru) |
| 4088 | { |
| 4089 | PyObject *result; |
| 4090 | static PyObject *struct_rusage; |
| 4091 | |
| 4092 | if (pid == -1) |
| 4093 | return posix_error(); |
| 4094 | |
| 4095 | if (struct_rusage == NULL) { |
| 4096 | PyObject *m = PyImport_ImportModule("resource"); |
| 4097 | if (m == NULL) |
| 4098 | return NULL; |
| 4099 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4100 | Py_DECREF(m); |
| 4101 | if (struct_rusage == NULL) |
| 4102 | return NULL; |
| 4103 | } |
| 4104 | |
| 4105 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4106 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4107 | if (!result) |
| 4108 | return NULL; |
| 4109 | |
| 4110 | #ifndef doubletime |
| 4111 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4112 | #endif |
| 4113 | |
| 4114 | PyStructSequence_SET_ITEM(result, 0, |
| 4115 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4116 | PyStructSequence_SET_ITEM(result, 1, |
| 4117 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4118 | #define SET_INT(result, index, value)\ |
| 4119 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 4120 | SET_INT(result, 2, ru->ru_maxrss); |
| 4121 | SET_INT(result, 3, ru->ru_ixrss); |
| 4122 | SET_INT(result, 4, ru->ru_idrss); |
| 4123 | SET_INT(result, 5, ru->ru_isrss); |
| 4124 | SET_INT(result, 6, ru->ru_minflt); |
| 4125 | SET_INT(result, 7, ru->ru_majflt); |
| 4126 | SET_INT(result, 8, ru->ru_nswap); |
| 4127 | SET_INT(result, 9, ru->ru_inblock); |
| 4128 | SET_INT(result, 10, ru->ru_oublock); |
| 4129 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4130 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4131 | SET_INT(result, 13, ru->ru_nsignals); |
| 4132 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4133 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4134 | #undef SET_INT |
| 4135 | |
| 4136 | if (PyErr_Occurred()) { |
| 4137 | Py_DECREF(result); |
| 4138 | return NULL; |
| 4139 | } |
| 4140 | |
| 4141 | return Py_BuildValue("iiN", pid, status, result); |
| 4142 | } |
| 4143 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4144 | |
| 4145 | #ifdef HAVE_WAIT3 |
| 4146 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4147 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4148 | Wait for completion of a child process."); |
| 4149 | |
| 4150 | static PyObject * |
| 4151 | posix_wait3(PyObject *self, PyObject *args) |
| 4152 | { |
| 4153 | int pid, options; |
| 4154 | struct rusage ru; |
| 4155 | WAIT_TYPE status; |
| 4156 | WAIT_STATUS_INT(status) = 0; |
| 4157 | |
| 4158 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4159 | return NULL; |
| 4160 | |
| 4161 | Py_BEGIN_ALLOW_THREADS |
| 4162 | pid = wait3(&status, options, &ru); |
| 4163 | Py_END_ALLOW_THREADS |
| 4164 | |
| 4165 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4166 | } |
| 4167 | #endif /* HAVE_WAIT3 */ |
| 4168 | |
| 4169 | #ifdef HAVE_WAIT4 |
| 4170 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4171 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4172 | Wait for completion of a given child process."); |
| 4173 | |
| 4174 | static PyObject * |
| 4175 | posix_wait4(PyObject *self, PyObject *args) |
| 4176 | { |
| 4177 | int pid, options; |
| 4178 | struct rusage ru; |
| 4179 | WAIT_TYPE status; |
| 4180 | WAIT_STATUS_INT(status) = 0; |
| 4181 | |
| 4182 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4183 | return NULL; |
| 4184 | |
| 4185 | Py_BEGIN_ALLOW_THREADS |
| 4186 | pid = wait4(pid, &status, options, &ru); |
| 4187 | Py_END_ALLOW_THREADS |
| 4188 | |
| 4189 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4190 | } |
| 4191 | #endif /* HAVE_WAIT4 */ |
| 4192 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4193 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4194 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4195 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4196 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4197 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4198 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4199 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4200 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4201 | int pid, options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4202 | WAIT_TYPE status; |
| 4203 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4204 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4205 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4206 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4207 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4208 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4209 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4210 | if (pid == -1) |
| 4211 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4212 | |
| 4213 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4216 | #elif defined(HAVE_CWAIT) |
| 4217 | |
| 4218 | /* 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] | 4219 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4220 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4221 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4222 | |
| 4223 | static PyObject * |
| 4224 | posix_waitpid(PyObject *self, PyObject *args) |
| 4225 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4226 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4227 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4228 | |
| 4229 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4230 | return NULL; |
| 4231 | Py_BEGIN_ALLOW_THREADS |
| 4232 | pid = _cwait(&status, pid, options); |
| 4233 | Py_END_ALLOW_THREADS |
| 4234 | if (pid == -1) |
| 4235 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4236 | |
| 4237 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4238 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4239 | } |
| 4240 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4241 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4242 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4243 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4244 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4245 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4246 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4247 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4248 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4249 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4250 | int pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4251 | WAIT_TYPE status; |
| 4252 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4253 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4254 | Py_BEGIN_ALLOW_THREADS |
| 4255 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4256 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4257 | if (pid == -1) |
| 4258 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4259 | |
| 4260 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4261 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4262 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4263 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4265 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4266 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4267 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4268 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4269 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4270 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4271 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4272 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4273 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4274 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4275 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4276 | 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] | 4277 | #else |
| 4278 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4279 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4280 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4283 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4284 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4285 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4286 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4287 | 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] | 4288 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4289 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4290 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4291 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4292 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4293 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4294 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4295 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4296 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4297 | |
| 4298 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 4299 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4300 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4301 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4302 | if (v == NULL) { |
| 4303 | PyMem_Free(path); |
| 4304 | return NULL; |
| 4305 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4306 | |
| 4307 | if (PyUnicode_Check(v)) { |
| 4308 | arg_is_unicode = 1; |
| 4309 | } |
| 4310 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4311 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4312 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4313 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4314 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4315 | if (n < 0) |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4316 | return posix_error_with_allocated_filename(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4317 | |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4318 | PyMem_Free(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4319 | v = PyString_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4320 | if (arg_is_unicode) { |
| 4321 | PyObject *w; |
| 4322 | |
| 4323 | w = PyUnicode_FromEncodedObject(v, |
| 4324 | Py_FileSystemDefaultEncoding, |
| 4325 | "strict"); |
| 4326 | if (w != NULL) { |
| 4327 | Py_DECREF(v); |
| 4328 | v = w; |
| 4329 | } |
| 4330 | else { |
| 4331 | /* fall back to the original byte string, as |
| 4332 | discussed in patch #683592 */ |
| 4333 | PyErr_Clear(); |
| 4334 | } |
| 4335 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4336 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4337 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4338 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4339 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4340 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4341 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4342 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4343 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4344 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4345 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4346 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4347 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4348 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4349 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4350 | } |
| 4351 | #endif /* HAVE_SYMLINK */ |
| 4352 | |
| 4353 | |
| 4354 | #ifdef HAVE_TIMES |
| 4355 | #ifndef HZ |
| 4356 | #define HZ 60 /* Universal constant :-) */ |
| 4357 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4358 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4359 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4360 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4361 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4362 | { |
| 4363 | ULONG value = 0; |
| 4364 | |
| 4365 | Py_BEGIN_ALLOW_THREADS |
| 4366 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4367 | Py_END_ALLOW_THREADS |
| 4368 | |
| 4369 | return value; |
| 4370 | } |
| 4371 | |
| 4372 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4373 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4374 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4375 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4376 | return Py_BuildValue("ddddd", |
| 4377 | (double)0 /* t.tms_utime / HZ */, |
| 4378 | (double)0 /* t.tms_stime / HZ */, |
| 4379 | (double)0 /* t.tms_cutime / HZ */, |
| 4380 | (double)0 /* t.tms_cstime / HZ */, |
| 4381 | (double)system_uptime() / 1000); |
| 4382 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4383 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4384 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4385 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4386 | { |
| 4387 | struct tms t; |
| 4388 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4389 | errno = 0; |
| 4390 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4391 | if (c == (clock_t) -1) |
| 4392 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4393 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4394 | (double)t.tms_utime / HZ, |
| 4395 | (double)t.tms_stime / HZ, |
| 4396 | (double)t.tms_cutime / HZ, |
| 4397 | (double)t.tms_cstime / HZ, |
| 4398 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4399 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4400 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4401 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4402 | |
| 4403 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4404 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4405 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4406 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4407 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4408 | { |
| 4409 | FILETIME create, exit, kernel, user; |
| 4410 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4411 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4412 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4413 | /* The fields of a FILETIME structure are the hi and lo part |
| 4414 | of a 64-bit value expressed in 100 nanosecond units. |
| 4415 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4416 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4417 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4418 | return Py_BuildValue( |
| 4419 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4420 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4421 | kernel.dwLowDateTime*1e-7), |
| 4422 | (double)(user.dwHighDateTime*429.4967296 + |
| 4423 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4424 | (double)0, |
| 4425 | (double)0, |
| 4426 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4427 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4428 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4429 | |
| 4430 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4431 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4432 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4433 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4434 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4435 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4436 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4437 | #ifdef HAVE_GETSID |
| 4438 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4439 | "getsid(pid) -> sid\n\n\ |
| 4440 | Call the system call getsid()."); |
| 4441 | |
| 4442 | static PyObject * |
| 4443 | posix_getsid(PyObject *self, PyObject *args) |
| 4444 | { |
| 4445 | int pid, sid; |
| 4446 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4447 | return NULL; |
| 4448 | sid = getsid(pid); |
| 4449 | if (sid < 0) |
| 4450 | return posix_error(); |
| 4451 | return PyInt_FromLong((long)sid); |
| 4452 | } |
| 4453 | #endif /* HAVE_GETSID */ |
| 4454 | |
| 4455 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4456 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4457 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4458 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4459 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4460 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4461 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4462 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4463 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4464 | if (setsid() < 0) |
| 4465 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4466 | Py_INCREF(Py_None); |
| 4467 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4468 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4469 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4470 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4471 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4472 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4473 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4474 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4475 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4476 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4477 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4478 | { |
| 4479 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4480 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4481 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4482 | if (setpgid(pid, pgrp) < 0) |
| 4483 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4484 | Py_INCREF(Py_None); |
| 4485 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4486 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4487 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4488 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4489 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4490 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4491 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4492 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4493 | 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] | 4494 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4495 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4496 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4497 | { |
| 4498 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4499 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4500 | return NULL; |
| 4501 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4502 | if (pgid < 0) |
| 4503 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4504 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4505 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4506 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4507 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4508 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4509 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4510 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4511 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4512 | 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] | 4513 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4514 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4515 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4516 | { |
| 4517 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4518 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4519 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4520 | if (tcsetpgrp(fd, pgid) < 0) |
| 4521 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4522 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4523 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4524 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4525 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4526 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4527 | /* Functions acting on file descriptors */ |
| 4528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4529 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4530 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4531 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4532 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4533 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4534 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4535 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4536 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4537 | int flag; |
| 4538 | int mode = 0777; |
| 4539 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4540 | |
| 4541 | #ifdef MS_WINDOWS |
| 4542 | if (unicode_file_names()) { |
| 4543 | PyUnicodeObject *po; |
| 4544 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4545 | Py_BEGIN_ALLOW_THREADS |
| 4546 | /* PyUnicode_AS_UNICODE OK without thread |
| 4547 | lock as it is a simple dereference. */ |
| 4548 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4549 | Py_END_ALLOW_THREADS |
| 4550 | if (fd < 0) |
| 4551 | return posix_error(); |
| 4552 | return PyInt_FromLong((long)fd); |
| 4553 | } |
| 4554 | /* Drop the argument parsing error as narrow strings |
| 4555 | are also valid. */ |
| 4556 | PyErr_Clear(); |
| 4557 | } |
| 4558 | #endif |
| 4559 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4560 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4561 | Py_FileSystemDefaultEncoding, &file, |
| 4562 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4563 | return NULL; |
| 4564 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4565 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4566 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4567 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4568 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4569 | return posix_error_with_allocated_filename(file); |
| 4570 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4571 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4574 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4575 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4576 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4577 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4578 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4579 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4580 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4581 | { |
| 4582 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4583 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4584 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4585 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4586 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4587 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4588 | if (res < 0) |
| 4589 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4590 | Py_INCREF(Py_None); |
| 4591 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4595 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4596 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4597 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4598 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4599 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4600 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4601 | { |
| 4602 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4603 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4604 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4605 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4606 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4607 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4608 | if (fd < 0) |
| 4609 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4610 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4613 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4614 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4615 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4616 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4617 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4618 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4619 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4620 | { |
| 4621 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4622 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4623 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4624 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4625 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4626 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4627 | if (res < 0) |
| 4628 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4629 | Py_INCREF(Py_None); |
| 4630 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4631 | } |
| 4632 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4633 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4634 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4635 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4636 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4637 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4638 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4639 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4640 | { |
| 4641 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4642 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4643 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4644 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4645 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4646 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4647 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4648 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4649 | return NULL; |
| 4650 | #ifdef SEEK_SET |
| 4651 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4652 | switch (how) { |
| 4653 | case 0: how = SEEK_SET; break; |
| 4654 | case 1: how = SEEK_CUR; break; |
| 4655 | case 2: how = SEEK_END; break; |
| 4656 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4657 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4658 | |
| 4659 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4660 | pos = PyInt_AsLong(posobj); |
| 4661 | #else |
| 4662 | pos = PyLong_Check(posobj) ? |
| 4663 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4664 | #endif |
| 4665 | if (PyErr_Occurred()) |
| 4666 | return NULL; |
| 4667 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4668 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4669 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4670 | res = _lseeki64(fd, pos, how); |
| 4671 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4672 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4673 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4674 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4675 | if (res < 0) |
| 4676 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4677 | |
| 4678 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4679 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4680 | #else |
| 4681 | return PyLong_FromLongLong(res); |
| 4682 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4686 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4687 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4688 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4689 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4690 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4691 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4692 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4693 | int fd, size; |
| 4694 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4695 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4696 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4697 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 4698 | if (size < 0) { |
| 4699 | errno = EINVAL; |
| 4700 | return posix_error(); |
| 4701 | } |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4702 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4703 | if (buffer == NULL) |
| 4704 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4705 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4706 | n = read(fd, PyBytes_AsString(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4707 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4708 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4709 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4710 | return posix_error(); |
| 4711 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4712 | if (n != size) |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4713 | PyBytes_Resize(buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4714 | return buffer; |
| 4715 | } |
| 4716 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4717 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4718 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4719 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4720 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4721 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4722 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4723 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4724 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4725 | int fd; |
| 4726 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4727 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4728 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4729 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4730 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4731 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4732 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4733 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4734 | if (size < 0) |
| 4735 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4736 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4739 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4740 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4741 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4742 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4743 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4744 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4745 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4746 | { |
| 4747 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4748 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4749 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4750 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4751 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 4752 | #ifdef __VMS |
| 4753 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 4754 | fsync(fd); |
| 4755 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4756 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4757 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4758 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4759 | if (res != 0) { |
| 4760 | #ifdef MS_WINDOWS |
| 4761 | return win32_error("fstat", NULL); |
| 4762 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4763 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4764 | #endif |
| 4765 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4766 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4767 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4770 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4771 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4772 | 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] | 4773 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4774 | |
| 4775 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4776 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4777 | { |
| 4778 | int fd; |
| 4779 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4780 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4781 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4782 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4783 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4784 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4785 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4786 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4787 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4788 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4789 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4790 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4791 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4792 | #if defined(PYOS_OS2) |
| 4793 | HFILE read, write; |
| 4794 | APIRET rc; |
| 4795 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4796 | Py_BEGIN_ALLOW_THREADS |
| 4797 | rc = DosCreatePipe( &read, &write, 4096); |
| 4798 | Py_END_ALLOW_THREADS |
| 4799 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4800 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4801 | |
| 4802 | return Py_BuildValue("(ii)", read, write); |
| 4803 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4804 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4805 | int fds[2]; |
| 4806 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4807 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4808 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4809 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4810 | if (res != 0) |
| 4811 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4812 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4813 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4814 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4815 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4816 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4817 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4818 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4819 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4820 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4821 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4822 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4823 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4824 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4825 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4826 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4827 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4828 | #endif /* HAVE_PIPE */ |
| 4829 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4830 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4831 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4832 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4833 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4834 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4835 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4836 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4837 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4838 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4839 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4840 | int mode = 0666; |
| 4841 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4842 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4843 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4844 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4845 | res = mkfifo(filename, mode); |
| 4846 | Py_END_ALLOW_THREADS |
| 4847 | if (res < 0) |
| 4848 | return posix_error(); |
| 4849 | Py_INCREF(Py_None); |
| 4850 | return Py_None; |
| 4851 | } |
| 4852 | #endif |
| 4853 | |
| 4854 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 4855 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4856 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4857 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4858 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 4859 | named filename. mode specifies both the permissions to use and the\n\ |
| 4860 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 4861 | 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] | 4862 | device defines the newly created device special file (probably using\n\ |
| 4863 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4864 | |
| 4865 | |
| 4866 | static PyObject * |
| 4867 | posix_mknod(PyObject *self, PyObject *args) |
| 4868 | { |
| 4869 | char *filename; |
| 4870 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4871 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4872 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 4873 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4874 | return NULL; |
| 4875 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4876 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4877 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4878 | if (res < 0) |
| 4879 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4880 | Py_INCREF(Py_None); |
| 4881 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4882 | } |
| 4883 | #endif |
| 4884 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4885 | #ifdef HAVE_DEVICE_MACROS |
| 4886 | PyDoc_STRVAR(posix_major__doc__, |
| 4887 | "major(device) -> major number\n\ |
| 4888 | Extracts a device major number from a raw device number."); |
| 4889 | |
| 4890 | static PyObject * |
| 4891 | posix_major(PyObject *self, PyObject *args) |
| 4892 | { |
| 4893 | int device; |
| 4894 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 4895 | return NULL; |
| 4896 | return PyInt_FromLong((long)major(device)); |
| 4897 | } |
| 4898 | |
| 4899 | PyDoc_STRVAR(posix_minor__doc__, |
| 4900 | "minor(device) -> minor number\n\ |
| 4901 | Extracts a device minor number from a raw device number."); |
| 4902 | |
| 4903 | static PyObject * |
| 4904 | posix_minor(PyObject *self, PyObject *args) |
| 4905 | { |
| 4906 | int device; |
| 4907 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 4908 | return NULL; |
| 4909 | return PyInt_FromLong((long)minor(device)); |
| 4910 | } |
| 4911 | |
| 4912 | PyDoc_STRVAR(posix_makedev__doc__, |
| 4913 | "makedev(major, minor) -> device number\n\ |
| 4914 | Composes a raw device number from the major and minor device numbers."); |
| 4915 | |
| 4916 | static PyObject * |
| 4917 | posix_makedev(PyObject *self, PyObject *args) |
| 4918 | { |
| 4919 | int major, minor; |
| 4920 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 4921 | return NULL; |
| 4922 | return PyInt_FromLong((long)makedev(major, minor)); |
| 4923 | } |
| 4924 | #endif /* device macros */ |
| 4925 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4926 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4927 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4928 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4929 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4930 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4931 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4932 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4933 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4934 | { |
| 4935 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4936 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4937 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4938 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4939 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4940 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4941 | return NULL; |
| 4942 | |
| 4943 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4944 | length = PyInt_AsLong(lenobj); |
| 4945 | #else |
| 4946 | length = PyLong_Check(lenobj) ? |
| 4947 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 4948 | #endif |
| 4949 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4950 | return NULL; |
| 4951 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4952 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4953 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4954 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4955 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4956 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4957 | return NULL; |
| 4958 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4959 | Py_INCREF(Py_None); |
| 4960 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4961 | } |
| 4962 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4963 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4964 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4965 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4966 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4967 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4968 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4969 | /* Save putenv() parameters as values here, so we can collect them when they |
| 4970 | * get re-set with another call for the same key. */ |
| 4971 | static PyObject *posix_putenv_garbage; |
| 4972 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4973 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4974 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4975 | { |
| 4976 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4977 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4978 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4979 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4980 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4981 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4982 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4983 | |
| 4984 | #if defined(PYOS_OS2) |
| 4985 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 4986 | APIRET rc; |
| 4987 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4988 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 4989 | if (rc != NO_ERROR) |
| 4990 | return os2_error(rc); |
| 4991 | |
| 4992 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 4993 | APIRET rc; |
| 4994 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4995 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 4996 | if (rc != NO_ERROR) |
| 4997 | return os2_error(rc); |
| 4998 | } else { |
| 4999 | #endif |
| 5000 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5001 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5002 | len = strlen(s1) + strlen(s2) + 2; |
| 5003 | /* len includes space for a trailing \0; the size arg to |
| 5004 | PyString_FromStringAndSize does not count that */ |
| 5005 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5006 | if (newstr == NULL) |
| 5007 | return PyErr_NoMemory(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5008 | newenv = PyString_AS_STRING(newstr); |
| 5009 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5010 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5011 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5012 | posix_error(); |
| 5013 | return NULL; |
| 5014 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5015 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5016 | * this will cause previous value to be collected. This has to |
| 5017 | * happen after the real putenv() call because the old value |
| 5018 | * was still accessible until then. */ |
| 5019 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5020 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5021 | /* really not much we can do; just leak */ |
| 5022 | PyErr_Clear(); |
| 5023 | } |
| 5024 | else { |
| 5025 | Py_DECREF(newstr); |
| 5026 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5027 | |
| 5028 | #if defined(PYOS_OS2) |
| 5029 | } |
| 5030 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5031 | Py_INCREF(Py_None); |
| 5032 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5033 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5034 | #endif /* putenv */ |
| 5035 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5036 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5037 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5038 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5039 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5040 | |
| 5041 | static PyObject * |
| 5042 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5043 | { |
| 5044 | char *s1; |
| 5045 | |
| 5046 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5047 | return NULL; |
| 5048 | |
| 5049 | unsetenv(s1); |
| 5050 | |
| 5051 | /* Remove the key from posix_putenv_garbage; |
| 5052 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5053 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5054 | * old value was still accessible until then. |
| 5055 | */ |
| 5056 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5057 | PyTuple_GET_ITEM(args, 0))) { |
| 5058 | /* really not much we can do; just leak */ |
| 5059 | PyErr_Clear(); |
| 5060 | } |
| 5061 | |
| 5062 | Py_INCREF(Py_None); |
| 5063 | return Py_None; |
| 5064 | } |
| 5065 | #endif /* unsetenv */ |
| 5066 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5067 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5068 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5069 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5070 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5071 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5072 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5073 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5074 | { |
| 5075 | int code; |
| 5076 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5077 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5078 | return NULL; |
| 5079 | message = strerror(code); |
| 5080 | if (message == NULL) { |
| 5081 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5082 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5083 | return NULL; |
| 5084 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5085 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5086 | } |
| 5087 | #endif /* strerror */ |
| 5088 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5089 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5090 | #ifdef HAVE_SYS_WAIT_H |
| 5091 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5092 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5093 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5094 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5095 | 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] | 5096 | |
| 5097 | static PyObject * |
| 5098 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5099 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5100 | WAIT_TYPE status; |
| 5101 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5102 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5103 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5104 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5105 | |
| 5106 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5107 | } |
| 5108 | #endif /* WCOREDUMP */ |
| 5109 | |
| 5110 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5111 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5112 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5113 | 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] | 5114 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5115 | |
| 5116 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5117 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5118 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5119 | WAIT_TYPE status; |
| 5120 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5121 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5122 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5123 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5124 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5125 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5126 | } |
| 5127 | #endif /* WIFCONTINUED */ |
| 5128 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5129 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5130 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5131 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5132 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5133 | |
| 5134 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5135 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5136 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5137 | WAIT_TYPE status; |
| 5138 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5139 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5140 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5141 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5142 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5143 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5144 | } |
| 5145 | #endif /* WIFSTOPPED */ |
| 5146 | |
| 5147 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5148 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5149 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5150 | 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] | 5151 | |
| 5152 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5153 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5154 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5155 | WAIT_TYPE status; |
| 5156 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5157 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5158 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5159 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5160 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5161 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5162 | } |
| 5163 | #endif /* WIFSIGNALED */ |
| 5164 | |
| 5165 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5166 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5167 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5168 | 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] | 5169 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5170 | |
| 5171 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5172 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5173 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5174 | WAIT_TYPE status; |
| 5175 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5176 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5177 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5178 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5179 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5180 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5181 | } |
| 5182 | #endif /* WIFEXITED */ |
| 5183 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5184 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5185 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5186 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5187 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5188 | |
| 5189 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5190 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5191 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5192 | WAIT_TYPE status; |
| 5193 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5194 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5195 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5196 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5197 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5198 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5199 | } |
| 5200 | #endif /* WEXITSTATUS */ |
| 5201 | |
| 5202 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5203 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5204 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5205 | 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] | 5206 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5207 | |
| 5208 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5209 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5210 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5211 | WAIT_TYPE status; |
| 5212 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5213 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5214 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5215 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5216 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5217 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5218 | } |
| 5219 | #endif /* WTERMSIG */ |
| 5220 | |
| 5221 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5222 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5223 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5224 | Return the signal that stopped the process that provided\n\ |
| 5225 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5226 | |
| 5227 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5228 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5229 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5230 | WAIT_TYPE status; |
| 5231 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5232 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5233 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5234 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5235 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5236 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5237 | } |
| 5238 | #endif /* WSTOPSIG */ |
| 5239 | |
| 5240 | #endif /* HAVE_SYS_WAIT_H */ |
| 5241 | |
| 5242 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5243 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5244 | #ifdef _SCO_DS |
| 5245 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5246 | needed definitions in sys/statvfs.h */ |
| 5247 | #define _SVID3 |
| 5248 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5249 | #include <sys/statvfs.h> |
| 5250 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5251 | static PyObject* |
| 5252 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5253 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5254 | if (v == NULL) |
| 5255 | return NULL; |
| 5256 | |
| 5257 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5258 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5259 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5260 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5261 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5262 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5263 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5264 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5265 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5266 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5267 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5268 | #else |
| 5269 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5270 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5271 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5272 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5273 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5274 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5275 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5276 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5277 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5278 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5279 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5280 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5281 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5282 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5283 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5284 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5285 | #endif |
| 5286 | |
| 5287 | return v; |
| 5288 | } |
| 5289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5290 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5291 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5292 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5293 | |
| 5294 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5295 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5296 | { |
| 5297 | int fd, res; |
| 5298 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5299 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5300 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5301 | return NULL; |
| 5302 | Py_BEGIN_ALLOW_THREADS |
| 5303 | res = fstatvfs(fd, &st); |
| 5304 | Py_END_ALLOW_THREADS |
| 5305 | if (res != 0) |
| 5306 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5307 | |
| 5308 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5309 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5310 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5311 | |
| 5312 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5313 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5314 | #include <sys/statvfs.h> |
| 5315 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5316 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5317 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5318 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5319 | |
| 5320 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5321 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5322 | { |
| 5323 | char *path; |
| 5324 | int res; |
| 5325 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5326 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5327 | return NULL; |
| 5328 | Py_BEGIN_ALLOW_THREADS |
| 5329 | res = statvfs(path, &st); |
| 5330 | Py_END_ALLOW_THREADS |
| 5331 | if (res != 0) |
| 5332 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5333 | |
| 5334 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5335 | } |
| 5336 | #endif /* HAVE_STATVFS */ |
| 5337 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5338 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5339 | * It maps strings representing configuration variable names to |
| 5340 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5341 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5342 | * rarely-used constants. There are three separate tables that use |
| 5343 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5344 | * |
| 5345 | * This code is always included, even if none of the interfaces that |
| 5346 | * need it are included. The #if hackery needed to avoid it would be |
| 5347 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5348 | */ |
| 5349 | struct constdef { |
| 5350 | char *name; |
| 5351 | long value; |
| 5352 | }; |
| 5353 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5354 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5355 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5356 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5357 | { |
| 5358 | if (PyInt_Check(arg)) { |
| 5359 | *valuep = PyInt_AS_LONG(arg); |
| 5360 | return 1; |
| 5361 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5362 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5363 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5364 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5365 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5366 | size_t hi = tablesize; |
| 5367 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5368 | const char *confname; |
| 5369 | Py_ssize_t namelen; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5370 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5371 | PyErr_SetString(PyExc_TypeError, |
| 5372 | "configuration names must be strings or integers"); |
| 5373 | return 0; |
| 5374 | } |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5375 | confname = PyUnicode_AsString(arg); |
| 5376 | if (confname == NULL) |
| 5377 | return 0; |
| 5378 | namelen = strlen(confname); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5379 | while (lo < hi) { |
| 5380 | mid = (lo + hi) / 2; |
| 5381 | cmp = strcmp(confname, table[mid].name); |
| 5382 | if (cmp < 0) |
| 5383 | hi = mid; |
| 5384 | else if (cmp > 0) |
| 5385 | lo = mid + 1; |
| 5386 | else { |
| 5387 | *valuep = table[mid].value; |
| 5388 | return 1; |
| 5389 | } |
| 5390 | } |
| 5391 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5392 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5393 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
| 5396 | |
| 5397 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5398 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5399 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5400 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5401 | #endif |
| 5402 | #ifdef _PC_ABI_ASYNC_IO |
| 5403 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5404 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5405 | #ifdef _PC_ASYNC_IO |
| 5406 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5407 | #endif |
| 5408 | #ifdef _PC_CHOWN_RESTRICTED |
| 5409 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5410 | #endif |
| 5411 | #ifdef _PC_FILESIZEBITS |
| 5412 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5413 | #endif |
| 5414 | #ifdef _PC_LAST |
| 5415 | {"PC_LAST", _PC_LAST}, |
| 5416 | #endif |
| 5417 | #ifdef _PC_LINK_MAX |
| 5418 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5419 | #endif |
| 5420 | #ifdef _PC_MAX_CANON |
| 5421 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5422 | #endif |
| 5423 | #ifdef _PC_MAX_INPUT |
| 5424 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5425 | #endif |
| 5426 | #ifdef _PC_NAME_MAX |
| 5427 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5428 | #endif |
| 5429 | #ifdef _PC_NO_TRUNC |
| 5430 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5431 | #endif |
| 5432 | #ifdef _PC_PATH_MAX |
| 5433 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5434 | #endif |
| 5435 | #ifdef _PC_PIPE_BUF |
| 5436 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5437 | #endif |
| 5438 | #ifdef _PC_PRIO_IO |
| 5439 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5440 | #endif |
| 5441 | #ifdef _PC_SOCK_MAXBUF |
| 5442 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5443 | #endif |
| 5444 | #ifdef _PC_SYNC_IO |
| 5445 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5446 | #endif |
| 5447 | #ifdef _PC_VDISABLE |
| 5448 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5449 | #endif |
| 5450 | }; |
| 5451 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5452 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5453 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5454 | { |
| 5455 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5456 | sizeof(posix_constants_pathconf) |
| 5457 | / sizeof(struct constdef)); |
| 5458 | } |
| 5459 | #endif |
| 5460 | |
| 5461 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5462 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5463 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5464 | 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] | 5465 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5466 | |
| 5467 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5468 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5469 | { |
| 5470 | PyObject *result = NULL; |
| 5471 | int name, fd; |
| 5472 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5473 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5474 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5475 | long limit; |
| 5476 | |
| 5477 | errno = 0; |
| 5478 | limit = fpathconf(fd, name); |
| 5479 | if (limit == -1 && errno != 0) |
| 5480 | posix_error(); |
| 5481 | else |
| 5482 | result = PyInt_FromLong(limit); |
| 5483 | } |
| 5484 | return result; |
| 5485 | } |
| 5486 | #endif |
| 5487 | |
| 5488 | |
| 5489 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5490 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5491 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5492 | 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] | 5493 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5494 | |
| 5495 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5496 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5497 | { |
| 5498 | PyObject *result = NULL; |
| 5499 | int name; |
| 5500 | char *path; |
| 5501 | |
| 5502 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5503 | conv_path_confname, &name)) { |
| 5504 | long limit; |
| 5505 | |
| 5506 | errno = 0; |
| 5507 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5508 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5509 | if (errno == EINVAL) |
| 5510 | /* could be a path or name problem */ |
| 5511 | posix_error(); |
| 5512 | else |
| 5513 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5514 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5515 | else |
| 5516 | result = PyInt_FromLong(limit); |
| 5517 | } |
| 5518 | return result; |
| 5519 | } |
| 5520 | #endif |
| 5521 | |
| 5522 | #ifdef HAVE_CONFSTR |
| 5523 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5524 | #ifdef _CS_ARCHITECTURE |
| 5525 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5526 | #endif |
| 5527 | #ifdef _CS_HOSTNAME |
| 5528 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5529 | #endif |
| 5530 | #ifdef _CS_HW_PROVIDER |
| 5531 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5532 | #endif |
| 5533 | #ifdef _CS_HW_SERIAL |
| 5534 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5535 | #endif |
| 5536 | #ifdef _CS_INITTAB_NAME |
| 5537 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5538 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5539 | #ifdef _CS_LFS64_CFLAGS |
| 5540 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5541 | #endif |
| 5542 | #ifdef _CS_LFS64_LDFLAGS |
| 5543 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5544 | #endif |
| 5545 | #ifdef _CS_LFS64_LIBS |
| 5546 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5547 | #endif |
| 5548 | #ifdef _CS_LFS64_LINTFLAGS |
| 5549 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5550 | #endif |
| 5551 | #ifdef _CS_LFS_CFLAGS |
| 5552 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5553 | #endif |
| 5554 | #ifdef _CS_LFS_LDFLAGS |
| 5555 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5556 | #endif |
| 5557 | #ifdef _CS_LFS_LIBS |
| 5558 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5559 | #endif |
| 5560 | #ifdef _CS_LFS_LINTFLAGS |
| 5561 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5562 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5563 | #ifdef _CS_MACHINE |
| 5564 | {"CS_MACHINE", _CS_MACHINE}, |
| 5565 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5566 | #ifdef _CS_PATH |
| 5567 | {"CS_PATH", _CS_PATH}, |
| 5568 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5569 | #ifdef _CS_RELEASE |
| 5570 | {"CS_RELEASE", _CS_RELEASE}, |
| 5571 | #endif |
| 5572 | #ifdef _CS_SRPC_DOMAIN |
| 5573 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5574 | #endif |
| 5575 | #ifdef _CS_SYSNAME |
| 5576 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5577 | #endif |
| 5578 | #ifdef _CS_VERSION |
| 5579 | {"CS_VERSION", _CS_VERSION}, |
| 5580 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5581 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5582 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5583 | #endif |
| 5584 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5585 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5586 | #endif |
| 5587 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5588 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5589 | #endif |
| 5590 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5591 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5592 | #endif |
| 5593 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5594 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5595 | #endif |
| 5596 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5597 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5598 | #endif |
| 5599 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5600 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5601 | #endif |
| 5602 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5603 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5604 | #endif |
| 5605 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5606 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5607 | #endif |
| 5608 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5609 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5610 | #endif |
| 5611 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5612 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5613 | #endif |
| 5614 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5615 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5616 | #endif |
| 5617 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5618 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5619 | #endif |
| 5620 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5621 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5622 | #endif |
| 5623 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5624 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5625 | #endif |
| 5626 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5627 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5628 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5629 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5630 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5631 | #endif |
| 5632 | #ifdef _MIPS_CS_BASE |
| 5633 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5634 | #endif |
| 5635 | #ifdef _MIPS_CS_HOSTID |
| 5636 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5637 | #endif |
| 5638 | #ifdef _MIPS_CS_HW_NAME |
| 5639 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5640 | #endif |
| 5641 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5642 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5643 | #endif |
| 5644 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5645 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5646 | #endif |
| 5647 | #ifdef _MIPS_CS_OSREL_MIN |
| 5648 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5649 | #endif |
| 5650 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5651 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5652 | #endif |
| 5653 | #ifdef _MIPS_CS_OS_NAME |
| 5654 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5655 | #endif |
| 5656 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5657 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5658 | #endif |
| 5659 | #ifdef _MIPS_CS_PROCESSORS |
| 5660 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5661 | #endif |
| 5662 | #ifdef _MIPS_CS_SERIAL |
| 5663 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5664 | #endif |
| 5665 | #ifdef _MIPS_CS_VENDOR |
| 5666 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5667 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5668 | }; |
| 5669 | |
| 5670 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5671 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5672 | { |
| 5673 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5674 | sizeof(posix_constants_confstr) |
| 5675 | / sizeof(struct constdef)); |
| 5676 | } |
| 5677 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5678 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5679 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5680 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5681 | |
| 5682 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5683 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5684 | { |
| 5685 | PyObject *result = NULL; |
| 5686 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5687 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5688 | |
| 5689 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5690 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5691 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5692 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5693 | len = confstr(name, buffer, sizeof(buffer)); |
| 5694 | if (len == 0) { |
| 5695 | if (errno) { |
| 5696 | posix_error(); |
| 5697 | } |
| 5698 | else { |
| 5699 | result = Py_None; |
| 5700 | Py_INCREF(Py_None); |
| 5701 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5702 | } |
| 5703 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5704 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5705 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5706 | if (result != NULL) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5707 | confstr(name, PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5708 | } |
| 5709 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5710 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5711 | } |
| 5712 | } |
| 5713 | return result; |
| 5714 | } |
| 5715 | #endif |
| 5716 | |
| 5717 | |
| 5718 | #ifdef HAVE_SYSCONF |
| 5719 | static struct constdef posix_constants_sysconf[] = { |
| 5720 | #ifdef _SC_2_CHAR_TERM |
| 5721 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5722 | #endif |
| 5723 | #ifdef _SC_2_C_BIND |
| 5724 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5725 | #endif |
| 5726 | #ifdef _SC_2_C_DEV |
| 5727 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5728 | #endif |
| 5729 | #ifdef _SC_2_C_VERSION |
| 5730 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5731 | #endif |
| 5732 | #ifdef _SC_2_FORT_DEV |
| 5733 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5734 | #endif |
| 5735 | #ifdef _SC_2_FORT_RUN |
| 5736 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5737 | #endif |
| 5738 | #ifdef _SC_2_LOCALEDEF |
| 5739 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5740 | #endif |
| 5741 | #ifdef _SC_2_SW_DEV |
| 5742 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5743 | #endif |
| 5744 | #ifdef _SC_2_UPE |
| 5745 | {"SC_2_UPE", _SC_2_UPE}, |
| 5746 | #endif |
| 5747 | #ifdef _SC_2_VERSION |
| 5748 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5749 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5750 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5751 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5752 | #endif |
| 5753 | #ifdef _SC_ACL |
| 5754 | {"SC_ACL", _SC_ACL}, |
| 5755 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5756 | #ifdef _SC_AIO_LISTIO_MAX |
| 5757 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5758 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5759 | #ifdef _SC_AIO_MAX |
| 5760 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5761 | #endif |
| 5762 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5763 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5764 | #endif |
| 5765 | #ifdef _SC_ARG_MAX |
| 5766 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5767 | #endif |
| 5768 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5769 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5770 | #endif |
| 5771 | #ifdef _SC_ATEXIT_MAX |
| 5772 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5773 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5774 | #ifdef _SC_AUDIT |
| 5775 | {"SC_AUDIT", _SC_AUDIT}, |
| 5776 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5777 | #ifdef _SC_AVPHYS_PAGES |
| 5778 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5779 | #endif |
| 5780 | #ifdef _SC_BC_BASE_MAX |
| 5781 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5782 | #endif |
| 5783 | #ifdef _SC_BC_DIM_MAX |
| 5784 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5785 | #endif |
| 5786 | #ifdef _SC_BC_SCALE_MAX |
| 5787 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5788 | #endif |
| 5789 | #ifdef _SC_BC_STRING_MAX |
| 5790 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5791 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5792 | #ifdef _SC_CAP |
| 5793 | {"SC_CAP", _SC_CAP}, |
| 5794 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5795 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5796 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5797 | #endif |
| 5798 | #ifdef _SC_CHAR_BIT |
| 5799 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5800 | #endif |
| 5801 | #ifdef _SC_CHAR_MAX |
| 5802 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5803 | #endif |
| 5804 | #ifdef _SC_CHAR_MIN |
| 5805 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5806 | #endif |
| 5807 | #ifdef _SC_CHILD_MAX |
| 5808 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5809 | #endif |
| 5810 | #ifdef _SC_CLK_TCK |
| 5811 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 5812 | #endif |
| 5813 | #ifdef _SC_COHER_BLKSZ |
| 5814 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 5815 | #endif |
| 5816 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 5817 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 5818 | #endif |
| 5819 | #ifdef _SC_DCACHE_ASSOC |
| 5820 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5821 | #endif |
| 5822 | #ifdef _SC_DCACHE_BLKSZ |
| 5823 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5824 | #endif |
| 5825 | #ifdef _SC_DCACHE_LINESZ |
| 5826 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 5827 | #endif |
| 5828 | #ifdef _SC_DCACHE_SZ |
| 5829 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5830 | #endif |
| 5831 | #ifdef _SC_DCACHE_TBLKSZ |
| 5832 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5833 | #endif |
| 5834 | #ifdef _SC_DELAYTIMER_MAX |
| 5835 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5836 | #endif |
| 5837 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5838 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5839 | #endif |
| 5840 | #ifdef _SC_EXPR_NEST_MAX |
| 5841 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5842 | #endif |
| 5843 | #ifdef _SC_FSYNC |
| 5844 | {"SC_FSYNC", _SC_FSYNC}, |
| 5845 | #endif |
| 5846 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5847 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5848 | #endif |
| 5849 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5850 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5851 | #endif |
| 5852 | #ifdef _SC_ICACHE_ASSOC |
| 5853 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5854 | #endif |
| 5855 | #ifdef _SC_ICACHE_BLKSZ |
| 5856 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5857 | #endif |
| 5858 | #ifdef _SC_ICACHE_LINESZ |
| 5859 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5860 | #endif |
| 5861 | #ifdef _SC_ICACHE_SZ |
| 5862 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5863 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5864 | #ifdef _SC_INF |
| 5865 | {"SC_INF", _SC_INF}, |
| 5866 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5867 | #ifdef _SC_INT_MAX |
| 5868 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5869 | #endif |
| 5870 | #ifdef _SC_INT_MIN |
| 5871 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5872 | #endif |
| 5873 | #ifdef _SC_IOV_MAX |
| 5874 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5875 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5876 | #ifdef _SC_IP_SECOPTS |
| 5877 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5878 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5879 | #ifdef _SC_JOB_CONTROL |
| 5880 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5881 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5882 | #ifdef _SC_KERN_POINTERS |
| 5883 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5884 | #endif |
| 5885 | #ifdef _SC_KERN_SIM |
| 5886 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5887 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5888 | #ifdef _SC_LINE_MAX |
| 5889 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5890 | #endif |
| 5891 | #ifdef _SC_LOGIN_NAME_MAX |
| 5892 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5893 | #endif |
| 5894 | #ifdef _SC_LOGNAME_MAX |
| 5895 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5896 | #endif |
| 5897 | #ifdef _SC_LONG_BIT |
| 5898 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5899 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5900 | #ifdef _SC_MAC |
| 5901 | {"SC_MAC", _SC_MAC}, |
| 5902 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5903 | #ifdef _SC_MAPPED_FILES |
| 5904 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5905 | #endif |
| 5906 | #ifdef _SC_MAXPID |
| 5907 | {"SC_MAXPID", _SC_MAXPID}, |
| 5908 | #endif |
| 5909 | #ifdef _SC_MB_LEN_MAX |
| 5910 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5911 | #endif |
| 5912 | #ifdef _SC_MEMLOCK |
| 5913 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5914 | #endif |
| 5915 | #ifdef _SC_MEMLOCK_RANGE |
| 5916 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5917 | #endif |
| 5918 | #ifdef _SC_MEMORY_PROTECTION |
| 5919 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5920 | #endif |
| 5921 | #ifdef _SC_MESSAGE_PASSING |
| 5922 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5923 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5924 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5925 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 5926 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5927 | #ifdef _SC_MQ_OPEN_MAX |
| 5928 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 5929 | #endif |
| 5930 | #ifdef _SC_MQ_PRIO_MAX |
| 5931 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 5932 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5933 | #ifdef _SC_NACLS_MAX |
| 5934 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 5935 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5936 | #ifdef _SC_NGROUPS_MAX |
| 5937 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 5938 | #endif |
| 5939 | #ifdef _SC_NL_ARGMAX |
| 5940 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 5941 | #endif |
| 5942 | #ifdef _SC_NL_LANGMAX |
| 5943 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 5944 | #endif |
| 5945 | #ifdef _SC_NL_MSGMAX |
| 5946 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 5947 | #endif |
| 5948 | #ifdef _SC_NL_NMAX |
| 5949 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 5950 | #endif |
| 5951 | #ifdef _SC_NL_SETMAX |
| 5952 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 5953 | #endif |
| 5954 | #ifdef _SC_NL_TEXTMAX |
| 5955 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 5956 | #endif |
| 5957 | #ifdef _SC_NPROCESSORS_CONF |
| 5958 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 5959 | #endif |
| 5960 | #ifdef _SC_NPROCESSORS_ONLN |
| 5961 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 5962 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5963 | #ifdef _SC_NPROC_CONF |
| 5964 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 5965 | #endif |
| 5966 | #ifdef _SC_NPROC_ONLN |
| 5967 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 5968 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5969 | #ifdef _SC_NZERO |
| 5970 | {"SC_NZERO", _SC_NZERO}, |
| 5971 | #endif |
| 5972 | #ifdef _SC_OPEN_MAX |
| 5973 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 5974 | #endif |
| 5975 | #ifdef _SC_PAGESIZE |
| 5976 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 5977 | #endif |
| 5978 | #ifdef _SC_PAGE_SIZE |
| 5979 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 5980 | #endif |
| 5981 | #ifdef _SC_PASS_MAX |
| 5982 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 5983 | #endif |
| 5984 | #ifdef _SC_PHYS_PAGES |
| 5985 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 5986 | #endif |
| 5987 | #ifdef _SC_PII |
| 5988 | {"SC_PII", _SC_PII}, |
| 5989 | #endif |
| 5990 | #ifdef _SC_PII_INTERNET |
| 5991 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 5992 | #endif |
| 5993 | #ifdef _SC_PII_INTERNET_DGRAM |
| 5994 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 5995 | #endif |
| 5996 | #ifdef _SC_PII_INTERNET_STREAM |
| 5997 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 5998 | #endif |
| 5999 | #ifdef _SC_PII_OSI |
| 6000 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6001 | #endif |
| 6002 | #ifdef _SC_PII_OSI_CLTS |
| 6003 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6004 | #endif |
| 6005 | #ifdef _SC_PII_OSI_COTS |
| 6006 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6007 | #endif |
| 6008 | #ifdef _SC_PII_OSI_M |
| 6009 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6010 | #endif |
| 6011 | #ifdef _SC_PII_SOCKET |
| 6012 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6013 | #endif |
| 6014 | #ifdef _SC_PII_XTI |
| 6015 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6016 | #endif |
| 6017 | #ifdef _SC_POLL |
| 6018 | {"SC_POLL", _SC_POLL}, |
| 6019 | #endif |
| 6020 | #ifdef _SC_PRIORITIZED_IO |
| 6021 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6022 | #endif |
| 6023 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6024 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6025 | #endif |
| 6026 | #ifdef _SC_REALTIME_SIGNALS |
| 6027 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6028 | #endif |
| 6029 | #ifdef _SC_RE_DUP_MAX |
| 6030 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6031 | #endif |
| 6032 | #ifdef _SC_RTSIG_MAX |
| 6033 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6034 | #endif |
| 6035 | #ifdef _SC_SAVED_IDS |
| 6036 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6037 | #endif |
| 6038 | #ifdef _SC_SCHAR_MAX |
| 6039 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6040 | #endif |
| 6041 | #ifdef _SC_SCHAR_MIN |
| 6042 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6043 | #endif |
| 6044 | #ifdef _SC_SELECT |
| 6045 | {"SC_SELECT", _SC_SELECT}, |
| 6046 | #endif |
| 6047 | #ifdef _SC_SEMAPHORES |
| 6048 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6049 | #endif |
| 6050 | #ifdef _SC_SEM_NSEMS_MAX |
| 6051 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6052 | #endif |
| 6053 | #ifdef _SC_SEM_VALUE_MAX |
| 6054 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6055 | #endif |
| 6056 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6057 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6058 | #endif |
| 6059 | #ifdef _SC_SHRT_MAX |
| 6060 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6061 | #endif |
| 6062 | #ifdef _SC_SHRT_MIN |
| 6063 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6064 | #endif |
| 6065 | #ifdef _SC_SIGQUEUE_MAX |
| 6066 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6067 | #endif |
| 6068 | #ifdef _SC_SIGRT_MAX |
| 6069 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6070 | #endif |
| 6071 | #ifdef _SC_SIGRT_MIN |
| 6072 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6073 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6074 | #ifdef _SC_SOFTPOWER |
| 6075 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6076 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6077 | #ifdef _SC_SPLIT_CACHE |
| 6078 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6079 | #endif |
| 6080 | #ifdef _SC_SSIZE_MAX |
| 6081 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6082 | #endif |
| 6083 | #ifdef _SC_STACK_PROT |
| 6084 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6085 | #endif |
| 6086 | #ifdef _SC_STREAM_MAX |
| 6087 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6088 | #endif |
| 6089 | #ifdef _SC_SYNCHRONIZED_IO |
| 6090 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6091 | #endif |
| 6092 | #ifdef _SC_THREADS |
| 6093 | {"SC_THREADS", _SC_THREADS}, |
| 6094 | #endif |
| 6095 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6096 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6097 | #endif |
| 6098 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6099 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6100 | #endif |
| 6101 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6102 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6103 | #endif |
| 6104 | #ifdef _SC_THREAD_KEYS_MAX |
| 6105 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6106 | #endif |
| 6107 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6108 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6109 | #endif |
| 6110 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6111 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6112 | #endif |
| 6113 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6114 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6115 | #endif |
| 6116 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6117 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6118 | #endif |
| 6119 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6120 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6121 | #endif |
| 6122 | #ifdef _SC_THREAD_STACK_MIN |
| 6123 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6124 | #endif |
| 6125 | #ifdef _SC_THREAD_THREADS_MAX |
| 6126 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6127 | #endif |
| 6128 | #ifdef _SC_TIMERS |
| 6129 | {"SC_TIMERS", _SC_TIMERS}, |
| 6130 | #endif |
| 6131 | #ifdef _SC_TIMER_MAX |
| 6132 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6133 | #endif |
| 6134 | #ifdef _SC_TTY_NAME_MAX |
| 6135 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6136 | #endif |
| 6137 | #ifdef _SC_TZNAME_MAX |
| 6138 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6139 | #endif |
| 6140 | #ifdef _SC_T_IOV_MAX |
| 6141 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6142 | #endif |
| 6143 | #ifdef _SC_UCHAR_MAX |
| 6144 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6145 | #endif |
| 6146 | #ifdef _SC_UINT_MAX |
| 6147 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6148 | #endif |
| 6149 | #ifdef _SC_UIO_MAXIOV |
| 6150 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6151 | #endif |
| 6152 | #ifdef _SC_ULONG_MAX |
| 6153 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6154 | #endif |
| 6155 | #ifdef _SC_USHRT_MAX |
| 6156 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6157 | #endif |
| 6158 | #ifdef _SC_VERSION |
| 6159 | {"SC_VERSION", _SC_VERSION}, |
| 6160 | #endif |
| 6161 | #ifdef _SC_WORD_BIT |
| 6162 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6163 | #endif |
| 6164 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6165 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6166 | #endif |
| 6167 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6168 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6169 | #endif |
| 6170 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6171 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6172 | #endif |
| 6173 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6174 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6175 | #endif |
| 6176 | #ifdef _SC_XOPEN_CRYPT |
| 6177 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6178 | #endif |
| 6179 | #ifdef _SC_XOPEN_ENH_I18N |
| 6180 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6181 | #endif |
| 6182 | #ifdef _SC_XOPEN_LEGACY |
| 6183 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6184 | #endif |
| 6185 | #ifdef _SC_XOPEN_REALTIME |
| 6186 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6187 | #endif |
| 6188 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6189 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6190 | #endif |
| 6191 | #ifdef _SC_XOPEN_SHM |
| 6192 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6193 | #endif |
| 6194 | #ifdef _SC_XOPEN_UNIX |
| 6195 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6196 | #endif |
| 6197 | #ifdef _SC_XOPEN_VERSION |
| 6198 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6199 | #endif |
| 6200 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6201 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6202 | #endif |
| 6203 | #ifdef _SC_XOPEN_XPG2 |
| 6204 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6205 | #endif |
| 6206 | #ifdef _SC_XOPEN_XPG3 |
| 6207 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6208 | #endif |
| 6209 | #ifdef _SC_XOPEN_XPG4 |
| 6210 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6211 | #endif |
| 6212 | }; |
| 6213 | |
| 6214 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6215 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6216 | { |
| 6217 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6218 | sizeof(posix_constants_sysconf) |
| 6219 | / sizeof(struct constdef)); |
| 6220 | } |
| 6221 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6222 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6223 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6224 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6225 | |
| 6226 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6227 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6228 | { |
| 6229 | PyObject *result = NULL; |
| 6230 | int name; |
| 6231 | |
| 6232 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6233 | int value; |
| 6234 | |
| 6235 | errno = 0; |
| 6236 | value = sysconf(name); |
| 6237 | if (value == -1 && errno != 0) |
| 6238 | posix_error(); |
| 6239 | else |
| 6240 | result = PyInt_FromLong(value); |
| 6241 | } |
| 6242 | return result; |
| 6243 | } |
| 6244 | #endif |
| 6245 | |
| 6246 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6247 | /* This code is used to ensure that the tables of configuration value names |
| 6248 | * are in sorted order as required by conv_confname(), and also to build the |
| 6249 | * the exported dictionaries that are used to publish information about the |
| 6250 | * names available on the host platform. |
| 6251 | * |
| 6252 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6253 | * when used, even for platforms we're not able to test on. It also makes |
| 6254 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6255 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6256 | |
| 6257 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6258 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6259 | { |
| 6260 | const struct constdef *c1 = |
| 6261 | (const struct constdef *) v1; |
| 6262 | const struct constdef *c2 = |
| 6263 | (const struct constdef *) v2; |
| 6264 | |
| 6265 | return strcmp(c1->name, c2->name); |
| 6266 | } |
| 6267 | |
| 6268 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6269 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6270 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6271 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6272 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6273 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6274 | |
| 6275 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6276 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6277 | if (d == NULL) |
| 6278 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6279 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6280 | for (i=0; i < tablesize; ++i) { |
| 6281 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6282 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6283 | Py_XDECREF(o); |
| 6284 | Py_DECREF(d); |
| 6285 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6286 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6287 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6288 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6289 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6290 | } |
| 6291 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6292 | /* Return -1 on failure, 0 on success. */ |
| 6293 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6294 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6295 | { |
| 6296 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6297 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6298 | sizeof(posix_constants_pathconf) |
| 6299 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6300 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6301 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6302 | #endif |
| 6303 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6304 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6305 | sizeof(posix_constants_confstr) |
| 6306 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6307 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6308 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6309 | #endif |
| 6310 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6311 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6312 | sizeof(posix_constants_sysconf) |
| 6313 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6314 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6315 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6316 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6317 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6318 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6319 | |
| 6320 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6321 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6322 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6323 | 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] | 6324 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6325 | |
| 6326 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6327 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6328 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6329 | abort(); |
| 6330 | /*NOTREACHED*/ |
| 6331 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6332 | return NULL; |
| 6333 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6334 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6335 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6336 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6337 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6338 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6339 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6340 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6341 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6342 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6343 | application (if any) its extension is associated.\n\ |
| 6344 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6345 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6346 | \n\ |
| 6347 | startfile returns as soon as the associated application is launched.\n\ |
| 6348 | There is no option to wait for the application to close, and no way\n\ |
| 6349 | to retrieve the application's exit status.\n\ |
| 6350 | \n\ |
| 6351 | The filepath is relative to the current directory. If you want to use\n\ |
| 6352 | 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] | 6353 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6354 | |
| 6355 | static PyObject * |
| 6356 | win32_startfile(PyObject *self, PyObject *args) |
| 6357 | { |
| 6358 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6359 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6360 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6361 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6362 | if (unicode_file_names()) { |
| 6363 | PyObject *unipath, *woperation = NULL; |
| 6364 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6365 | &unipath, &operation)) { |
| 6366 | PyErr_Clear(); |
| 6367 | goto normal; |
| 6368 | } |
| 6369 | |
| 6370 | |
| 6371 | if (operation) { |
| 6372 | woperation = PyUnicode_DecodeASCII(operation, |
| 6373 | strlen(operation), NULL); |
| 6374 | if (!woperation) { |
| 6375 | PyErr_Clear(); |
| 6376 | operation = NULL; |
| 6377 | goto normal; |
| 6378 | } |
| 6379 | } |
| 6380 | |
| 6381 | Py_BEGIN_ALLOW_THREADS |
| 6382 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6383 | PyUnicode_AS_UNICODE(unipath), |
| 6384 | NULL, NULL, SW_SHOWNORMAL); |
| 6385 | Py_END_ALLOW_THREADS |
| 6386 | |
| 6387 | Py_XDECREF(woperation); |
| 6388 | if (rc <= (HINSTANCE)32) { |
| 6389 | PyObject *errval = win32_error_unicode("startfile", |
| 6390 | PyUnicode_AS_UNICODE(unipath)); |
| 6391 | return errval; |
| 6392 | } |
| 6393 | Py_INCREF(Py_None); |
| 6394 | return Py_None; |
| 6395 | } |
| 6396 | #endif |
| 6397 | |
| 6398 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6399 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 6400 | Py_FileSystemDefaultEncoding, &filepath, |
| 6401 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6402 | return NULL; |
| 6403 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6404 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6405 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6406 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6407 | if (rc <= (HINSTANCE)32) { |
| 6408 | PyObject *errval = win32_error("startfile", filepath); |
| 6409 | PyMem_Free(filepath); |
| 6410 | return errval; |
| 6411 | } |
| 6412 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6413 | Py_INCREF(Py_None); |
| 6414 | return Py_None; |
| 6415 | } |
| 6416 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6417 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6418 | #ifdef HAVE_GETLOADAVG |
| 6419 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6420 | "getloadavg() -> (float, float, float)\n\n\ |
| 6421 | Return the number of processes in the system run queue averaged over\n\ |
| 6422 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6423 | was unobtainable"); |
| 6424 | |
| 6425 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6426 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6427 | { |
| 6428 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6429 | if (getloadavg(loadavg, 3)!=3) { |
| 6430 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6431 | return NULL; |
| 6432 | } else |
| 6433 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6434 | } |
| 6435 | #endif |
| 6436 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6437 | #ifdef MS_WINDOWS |
| 6438 | |
| 6439 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6440 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6441 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6442 | |
| 6443 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6444 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6445 | DWORD dwFlags ); |
| 6446 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6447 | BYTE *pbBuffer ); |
| 6448 | |
| 6449 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6450 | /* This handle is never explicitly released. Instead, the operating |
| 6451 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6452 | static HCRYPTPROV hCryptProv = 0; |
| 6453 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6454 | static PyObject* |
| 6455 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6456 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6457 | int howMany; |
| 6458 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6459 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6460 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6461 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6462 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6463 | if (howMany < 0) |
| 6464 | return PyErr_Format(PyExc_ValueError, |
| 6465 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6466 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6467 | if (hCryptProv == 0) { |
| 6468 | HINSTANCE hAdvAPI32 = NULL; |
| 6469 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6470 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6471 | /* Obtain handle to the DLL containing CryptoAPI |
| 6472 | This should not fail */ |
| 6473 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6474 | if(hAdvAPI32 == NULL) |
| 6475 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6476 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6477 | /* Obtain pointers to the CryptoAPI functions |
| 6478 | This will fail on some early versions of Win95 */ |
| 6479 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6480 | hAdvAPI32, |
| 6481 | "CryptAcquireContextA"); |
| 6482 | if (pCryptAcquireContext == NULL) |
| 6483 | return PyErr_Format(PyExc_NotImplementedError, |
| 6484 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6485 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6486 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6487 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6488 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6489 | return PyErr_Format(PyExc_NotImplementedError, |
| 6490 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6491 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6492 | /* Acquire context */ |
| 6493 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6494 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6495 | return win32_error("CryptAcquireContext", NULL); |
| 6496 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6497 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6498 | /* Allocate bytes */ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6499 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6500 | if (result != NULL) { |
| 6501 | /* Get random data */ |
| 6502 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6503 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6504 | Py_DECREF(result); |
| 6505 | return win32_error("CryptGenRandom", NULL); |
| 6506 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6507 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6508 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6509 | } |
| 6510 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6511 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6512 | PyDoc_STRVAR(device_encoding__doc__, |
| 6513 | "device_encoding(fd) -> str\n\n\ |
| 6514 | Return a string describing the encoding of the device\n\ |
| 6515 | if the output is a terminal; else return None."); |
| 6516 | |
| 6517 | static PyObject * |
| 6518 | device_encoding(PyObject *self, PyObject *args) |
| 6519 | { |
| 6520 | int fd; |
| 6521 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6522 | return NULL; |
| 6523 | if (!isatty(fd)) { |
| 6524 | Py_INCREF(Py_None); |
| 6525 | return Py_None; |
| 6526 | } |
| 6527 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6528 | if (fd == 0) { |
| 6529 | char buf[100]; |
| 6530 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6531 | return PyUnicode_FromString(buf); |
| 6532 | } |
| 6533 | if (fd == 1 || fd == 2) { |
| 6534 | char buf[100]; |
| 6535 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6536 | return PyUnicode_FromString(buf); |
| 6537 | } |
| 6538 | #elif defined(CODESET) |
| 6539 | { |
| 6540 | char *codeset = nl_langinfo(CODESET); |
| 6541 | if (codeset) |
| 6542 | return PyUnicode_FromString(codeset); |
| 6543 | } |
| 6544 | #endif |
| 6545 | Py_INCREF(Py_None); |
| 6546 | return Py_None; |
| 6547 | } |
| 6548 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6549 | #ifdef __VMS |
| 6550 | /* Use openssl random routine */ |
| 6551 | #include <openssl/rand.h> |
| 6552 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6553 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6554 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6555 | |
| 6556 | static PyObject* |
| 6557 | vms_urandom(PyObject *self, PyObject *args) |
| 6558 | { |
| 6559 | int howMany; |
| 6560 | PyObject* result; |
| 6561 | |
| 6562 | /* Read arguments */ |
| 6563 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6564 | return NULL; |
| 6565 | if (howMany < 0) |
| 6566 | return PyErr_Format(PyExc_ValueError, |
| 6567 | "negative argument not allowed"); |
| 6568 | |
| 6569 | /* Allocate bytes */ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6570 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6571 | if (result != NULL) { |
| 6572 | /* Get random data */ |
| 6573 | if (RAND_pseudo_bytes((unsigned char*) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6574 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6575 | howMany) < 0) { |
| 6576 | Py_DECREF(result); |
| 6577 | return PyErr_Format(PyExc_ValueError, |
| 6578 | "RAND_pseudo_bytes"); |
| 6579 | } |
| 6580 | } |
| 6581 | return result; |
| 6582 | } |
| 6583 | #endif |
| 6584 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6585 | static PyMethodDef posix_methods[] = { |
| 6586 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6587 | #ifdef HAVE_TTYNAME |
| 6588 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6589 | #endif |
| 6590 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6591 | #ifdef HAVE_CHFLAGS |
| 6592 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 6593 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6594 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6595 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6596 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6597 | #endif /* HAVE_CHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6598 | #ifdef HAVE_LCHFLAGS |
| 6599 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 6600 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6601 | #ifdef HAVE_LCHOWN |
| 6602 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6603 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6604 | #ifdef HAVE_CHROOT |
| 6605 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6606 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6607 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6608 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6609 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6610 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6611 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6612 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6613 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6614 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6615 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6616 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6617 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6618 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6619 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6620 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6621 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6622 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6623 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6624 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6625 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6626 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6627 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6628 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6629 | {"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] | 6630 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6631 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6632 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6633 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6634 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6635 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6636 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6637 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6638 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6639 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6640 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6641 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6642 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6643 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6644 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6645 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6646 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6647 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6648 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6649 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6650 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6651 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6652 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6653 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6654 | #if defined(PYOS_OS2) |
| 6655 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 6656 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 6657 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6658 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6659 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6660 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6661 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6662 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6663 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6664 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6665 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6666 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6667 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6668 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6669 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6670 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6671 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6672 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6673 | #endif /* HAVE_GETEGID */ |
| 6674 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6675 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6676 | #endif /* HAVE_GETEUID */ |
| 6677 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6678 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6679 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6680 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6681 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6682 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6683 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6684 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6685 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6686 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6687 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6688 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6689 | #endif /* HAVE_GETPPID */ |
| 6690 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6691 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6692 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6693 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6694 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6695 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6696 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6697 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6698 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6699 | #ifdef HAVE_KILLPG |
| 6700 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6701 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6702 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6703 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6704 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 6705 | #ifdef MS_WINDOWS |
| 6706 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 6707 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6708 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6709 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6710 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6711 | #ifdef HAVE_SETEUID |
| 6712 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6713 | #endif /* HAVE_SETEUID */ |
| 6714 | #ifdef HAVE_SETEGID |
| 6715 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6716 | #endif /* HAVE_SETEGID */ |
| 6717 | #ifdef HAVE_SETREUID |
| 6718 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6719 | #endif /* HAVE_SETREUID */ |
| 6720 | #ifdef HAVE_SETREGID |
| 6721 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6722 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6723 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6724 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6725 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6726 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6727 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6728 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6729 | #ifdef HAVE_GETPGID |
| 6730 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 6731 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6732 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6733 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6734 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6735 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6736 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6737 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6738 | #ifdef HAVE_WAIT3 |
| 6739 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 6740 | #endif /* HAVE_WAIT3 */ |
| 6741 | #ifdef HAVE_WAIT4 |
| 6742 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 6743 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6744 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6745 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6746 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6747 | #ifdef HAVE_GETSID |
| 6748 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 6749 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6750 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6751 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6752 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6753 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6754 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6755 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6756 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6757 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6758 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6759 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6760 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6761 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6762 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6763 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6764 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6765 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6766 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6767 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6768 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6769 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6770 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6771 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6772 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6773 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6774 | #endif |
| 6775 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6776 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6777 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6778 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6779 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 6780 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6781 | #ifdef HAVE_DEVICE_MACROS |
| 6782 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 6783 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 6784 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 6785 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6786 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6787 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6788 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6789 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6790 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6791 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6792 | #ifdef HAVE_UNSETENV |
| 6793 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6794 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6795 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6796 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6797 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6798 | #ifdef HAVE_FCHDIR |
| 6799 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 6800 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6801 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6802 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6803 | #endif |
| 6804 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6805 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6806 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6807 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6808 | #ifdef WCOREDUMP |
| 6809 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 6810 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6811 | #ifdef WIFCONTINUED |
| 6812 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 6813 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6814 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6815 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6816 | #endif /* WIFSTOPPED */ |
| 6817 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6818 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6819 | #endif /* WIFSIGNALED */ |
| 6820 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6821 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6822 | #endif /* WIFEXITED */ |
| 6823 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6824 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6825 | #endif /* WEXITSTATUS */ |
| 6826 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6827 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6828 | #endif /* WTERMSIG */ |
| 6829 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6830 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6831 | #endif /* WSTOPSIG */ |
| 6832 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6833 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6834 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6835 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6836 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6837 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6838 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6839 | #ifdef HAVE_CONFSTR |
| 6840 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 6841 | #endif |
| 6842 | #ifdef HAVE_SYSCONF |
| 6843 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 6844 | #endif |
| 6845 | #ifdef HAVE_FPATHCONF |
| 6846 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 6847 | #endif |
| 6848 | #ifdef HAVE_PATHCONF |
| 6849 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 6850 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6851 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6852 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 6853 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 6854 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6855 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6856 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6857 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6858 | #ifdef MS_WINDOWS |
| 6859 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 6860 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6861 | #ifdef __VMS |
| 6862 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 6863 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6864 | {NULL, NULL} /* Sentinel */ |
| 6865 | }; |
| 6866 | |
| 6867 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6868 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6869 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6870 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6871 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6872 | } |
| 6873 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6874 | #if defined(PYOS_OS2) |
| 6875 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6876 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6877 | { |
| 6878 | APIRET rc; |
| 6879 | ULONG values[QSV_MAX+1]; |
| 6880 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 6881 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6882 | |
| 6883 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 6884 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6885 | Py_END_ALLOW_THREADS |
| 6886 | |
| 6887 | if (rc != NO_ERROR) { |
| 6888 | os2_error(rc); |
| 6889 | return -1; |
| 6890 | } |
| 6891 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6892 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 6893 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 6894 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 6895 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 6896 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 6897 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 6898 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6899 | |
| 6900 | switch (values[QSV_VERSION_MINOR]) { |
| 6901 | case 0: ver = "2.00"; break; |
| 6902 | case 10: ver = "2.10"; break; |
| 6903 | case 11: ver = "2.11"; break; |
| 6904 | case 30: ver = "3.00"; break; |
| 6905 | case 40: ver = "4.00"; break; |
| 6906 | case 50: ver = "5.00"; break; |
| 6907 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 6908 | PyOS_snprintf(tmp, sizeof(tmp), |
| 6909 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 6910 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6911 | ver = &tmp[0]; |
| 6912 | } |
| 6913 | |
| 6914 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6915 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6916 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6917 | |
| 6918 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 6919 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 6920 | tmp[1] = ':'; |
| 6921 | tmp[2] = '\0'; |
| 6922 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6923 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6924 | } |
| 6925 | #endif |
| 6926 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6927 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6928 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6929 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6930 | #ifdef F_OK |
| 6931 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6932 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6933 | #ifdef R_OK |
| 6934 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6935 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6936 | #ifdef W_OK |
| 6937 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6938 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6939 | #ifdef X_OK |
| 6940 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6941 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6942 | #ifdef NGROUPS_MAX |
| 6943 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 6944 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6945 | #ifdef TMP_MAX |
| 6946 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 6947 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6948 | #ifdef WCONTINUED |
| 6949 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 6950 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6951 | #ifdef WNOHANG |
| 6952 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6953 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6954 | #ifdef WUNTRACED |
| 6955 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 6956 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6957 | #ifdef O_RDONLY |
| 6958 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 6959 | #endif |
| 6960 | #ifdef O_WRONLY |
| 6961 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 6962 | #endif |
| 6963 | #ifdef O_RDWR |
| 6964 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 6965 | #endif |
| 6966 | #ifdef O_NDELAY |
| 6967 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 6968 | #endif |
| 6969 | #ifdef O_NONBLOCK |
| 6970 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 6971 | #endif |
| 6972 | #ifdef O_APPEND |
| 6973 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 6974 | #endif |
| 6975 | #ifdef O_DSYNC |
| 6976 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 6977 | #endif |
| 6978 | #ifdef O_RSYNC |
| 6979 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 6980 | #endif |
| 6981 | #ifdef O_SYNC |
| 6982 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 6983 | #endif |
| 6984 | #ifdef O_NOCTTY |
| 6985 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 6986 | #endif |
| 6987 | #ifdef O_CREAT |
| 6988 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 6989 | #endif |
| 6990 | #ifdef O_EXCL |
| 6991 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 6992 | #endif |
| 6993 | #ifdef O_TRUNC |
| 6994 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 6995 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 6996 | #ifdef O_BINARY |
| 6997 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 6998 | #endif |
| 6999 | #ifdef O_TEXT |
| 7000 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7001 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7002 | #ifdef O_LARGEFILE |
| 7003 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7004 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7005 | #ifdef O_SHLOCK |
| 7006 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7007 | #endif |
| 7008 | #ifdef O_EXLOCK |
| 7009 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7010 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7011 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7012 | /* MS Windows */ |
| 7013 | #ifdef O_NOINHERIT |
| 7014 | /* Don't inherit in child processes. */ |
| 7015 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7016 | #endif |
| 7017 | #ifdef _O_SHORT_LIVED |
| 7018 | /* Optimize for short life (keep in memory). */ |
| 7019 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7020 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7021 | #endif |
| 7022 | #ifdef O_TEMPORARY |
| 7023 | /* Automatically delete when last handle is closed. */ |
| 7024 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7025 | #endif |
| 7026 | #ifdef O_RANDOM |
| 7027 | /* Optimize for random access. */ |
| 7028 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7029 | #endif |
| 7030 | #ifdef O_SEQUENTIAL |
| 7031 | /* Optimize for sequential access. */ |
| 7032 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7033 | #endif |
| 7034 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7035 | /* GNU extensions. */ |
| 7036 | #ifdef O_DIRECT |
| 7037 | /* Direct disk access. */ |
| 7038 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7039 | #endif |
| 7040 | #ifdef O_DIRECTORY |
| 7041 | /* Must be a directory. */ |
| 7042 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7043 | #endif |
| 7044 | #ifdef O_NOFOLLOW |
| 7045 | /* Do not follow links. */ |
| 7046 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7047 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7048 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7049 | /* These come from sysexits.h */ |
| 7050 | #ifdef EX_OK |
| 7051 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7052 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7053 | #ifdef EX_USAGE |
| 7054 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7055 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7056 | #ifdef EX_DATAERR |
| 7057 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7058 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7059 | #ifdef EX_NOINPUT |
| 7060 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7061 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7062 | #ifdef EX_NOUSER |
| 7063 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7064 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7065 | #ifdef EX_NOHOST |
| 7066 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7067 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7068 | #ifdef EX_UNAVAILABLE |
| 7069 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7070 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7071 | #ifdef EX_SOFTWARE |
| 7072 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7073 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7074 | #ifdef EX_OSERR |
| 7075 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7076 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7077 | #ifdef EX_OSFILE |
| 7078 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7079 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7080 | #ifdef EX_CANTCREAT |
| 7081 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7082 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7083 | #ifdef EX_IOERR |
| 7084 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7085 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7086 | #ifdef EX_TEMPFAIL |
| 7087 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7088 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7089 | #ifdef EX_PROTOCOL |
| 7090 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7091 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7092 | #ifdef EX_NOPERM |
| 7093 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7094 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7095 | #ifdef EX_CONFIG |
| 7096 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7097 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7098 | #ifdef EX_NOTFOUND |
| 7099 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7100 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7101 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7102 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7103 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7104 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7105 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7106 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7107 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7108 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7109 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7110 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7111 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7112 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7113 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7114 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7115 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7116 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7117 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7118 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7119 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7120 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7121 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7122 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7123 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7124 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7125 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7126 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7127 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7128 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7129 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7130 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7131 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7132 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7133 | #if defined(PYOS_OS2) |
| 7134 | if (insertvalues(d)) return -1; |
| 7135 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7136 | return 0; |
| 7137 | } |
| 7138 | |
| 7139 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7140 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7141 | #define INITFUNC initnt |
| 7142 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7143 | |
| 7144 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7145 | #define INITFUNC initos2 |
| 7146 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7147 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7148 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7149 | #define INITFUNC initposix |
| 7150 | #define MODNAME "posix" |
| 7151 | #endif |
| 7152 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7153 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7154 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7155 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7156 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7157 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7158 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7159 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7160 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7161 | if (m == NULL) |
| 7162 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7163 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7164 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7165 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7166 | Py_XINCREF(v); |
| 7167 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7168 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7169 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7170 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7171 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7172 | return; |
| 7173 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7174 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7175 | return; |
| 7176 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7177 | Py_INCREF(PyExc_OSError); |
| 7178 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7179 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7180 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7181 | if (posix_putenv_garbage == NULL) |
| 7182 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7183 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7184 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7185 | if (!initialized) { |
| 7186 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7187 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7188 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7189 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7190 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7191 | structseq_new = StatResultType.tp_new; |
| 7192 | StatResultType.tp_new = statresult_new; |
| 7193 | |
| 7194 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7195 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 7196 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7197 | Py_INCREF((PyObject*) &StatResultType); |
| 7198 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7199 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7200 | PyModule_AddObject(m, "statvfs_result", |
| 7201 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7202 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7203 | |
| 7204 | #ifdef __APPLE__ |
| 7205 | /* |
| 7206 | * Step 2 of weak-linking support on Mac OS X. |
| 7207 | * |
| 7208 | * The code below removes functions that are not available on the |
| 7209 | * currently active platform. |
| 7210 | * |
| 7211 | * This block allow one to use a python binary that was build on |
| 7212 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7213 | * OSX 10.4. |
| 7214 | */ |
| 7215 | #ifdef HAVE_FSTATVFS |
| 7216 | if (fstatvfs == NULL) { |
| 7217 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 7218 | return; |
| 7219 | } |
| 7220 | } |
| 7221 | #endif /* HAVE_FSTATVFS */ |
| 7222 | |
| 7223 | #ifdef HAVE_STATVFS |
| 7224 | if (statvfs == NULL) { |
| 7225 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 7226 | return; |
| 7227 | } |
| 7228 | } |
| 7229 | #endif /* HAVE_STATVFS */ |
| 7230 | |
| 7231 | # ifdef HAVE_LCHOWN |
| 7232 | if (lchown == NULL) { |
| 7233 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 7234 | return; |
| 7235 | } |
| 7236 | } |
| 7237 | #endif /* HAVE_LCHOWN */ |
| 7238 | |
| 7239 | |
| 7240 | #endif /* __APPLE__ */ |
| 7241 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7242 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7243 | |
| 7244 | #ifdef __cplusplus |
| 7245 | } |
| 7246 | #endif |