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 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 95 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 96 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 97 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 98 | #include <process.h> |
| 99 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 100 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 101 | #define HAVE_GETCWD 1 |
| 102 | #define HAVE_OPENDIR 1 |
| 103 | #define HAVE_SYSTEM 1 |
| 104 | #if defined(__OS2__) |
| 105 | #define HAVE_EXECV 1 |
| 106 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 107 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 108 | #include <process.h> |
| 109 | #else |
| 110 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 111 | #define HAVE_EXECV 1 |
| 112 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 113 | #define HAVE_OPENDIR 1 |
| 114 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 115 | #define HAVE_SYSTEM 1 |
| 116 | #define HAVE_WAIT 1 |
| 117 | #else |
| 118 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 119 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 120 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 121 | #define HAVE_EXECV 1 |
| 122 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 123 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 124 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 125 | #define HAVE_FSYNC 1 |
| 126 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 127 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 128 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 129 | /* 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] | 130 | #else /* all other compilers */ |
| 131 | /* Unix functions that the configure script doesn't check for */ |
| 132 | #define HAVE_EXECV 1 |
| 133 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 134 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 135 | #define HAVE_FORK1 1 |
| 136 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #define HAVE_GETCWD 1 |
| 138 | #define HAVE_GETEGID 1 |
| 139 | #define HAVE_GETEUID 1 |
| 140 | #define HAVE_GETGID 1 |
| 141 | #define HAVE_GETPPID 1 |
| 142 | #define HAVE_GETUID 1 |
| 143 | #define HAVE_KILL 1 |
| 144 | #define HAVE_OPENDIR 1 |
| 145 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #define HAVE_SYSTEM 1 |
| 147 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 148 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 149 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #endif /* _MSC_VER */ |
| 151 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 152 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 153 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 156 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 157 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 158 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 159 | (default) */ |
| 160 | extern char *ctermid_r(char *); |
| 161 | #endif |
| 162 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 163 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 164 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 165 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 166 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 167 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 168 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 169 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 170 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 171 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 172 | #endif |
| 173 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int chdir(char *); |
| 175 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 177 | extern int chdir(const char *); |
| 178 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 179 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 180 | #ifdef __BORLANDC__ |
| 181 | extern int chmod(const char *, int); |
| 182 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 183 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 184 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 185 | extern int chown(const char *, uid_t, gid_t); |
| 186 | extern char *getcwd(char *, int); |
| 187 | extern char *strerror(int); |
| 188 | extern int link(const char *, const char *); |
| 189 | extern int rename(const char *, const char *); |
| 190 | extern int stat(const char *, struct stat *); |
| 191 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 192 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 193 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 194 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 196 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 197 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 200 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_UTIME_H |
| 203 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 204 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 206 | #ifdef HAVE_SYS_UTIME_H |
| 207 | #include <sys/utime.h> |
| 208 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 209 | #endif /* HAVE_SYS_UTIME_H */ |
| 210 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | #ifdef HAVE_SYS_TIMES_H |
| 212 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 213 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 214 | |
| 215 | #ifdef HAVE_SYS_PARAM_H |
| 216 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 217 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | |
| 219 | #ifdef HAVE_SYS_UTSNAME_H |
| 220 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 221 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 223 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 225 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 226 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 227 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 228 | #include <direct.h> |
| 229 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 230 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 232 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 233 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 234 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 236 | #endif |
| 237 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 239 | #endif |
| 240 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #endif |
| 243 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 245 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 246 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 248 | #endif |
| 249 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 251 | #endif |
| 252 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 254 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 255 | #include "osdefs.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 257 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 258 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 260 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 261 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 262 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 264 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 265 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 266 | #define MAXPATHLEN PATH_MAX |
| 267 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 268 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 269 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 270 | #endif /* MAXPATHLEN */ |
| 271 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 272 | #ifdef UNION_WAIT |
| 273 | /* Emulate some macros on systems that have a union instead of macros */ |
| 274 | |
| 275 | #ifndef WIFEXITED |
| 276 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 277 | #endif |
| 278 | |
| 279 | #ifndef WEXITSTATUS |
| 280 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 281 | #endif |
| 282 | |
| 283 | #ifndef WTERMSIG |
| 284 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 285 | #endif |
| 286 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | #define WAIT_TYPE union wait |
| 288 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 289 | |
| 290 | #else /* !UNION_WAIT */ |
| 291 | #define WAIT_TYPE int |
| 292 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 293 | #endif /* UNION_WAIT */ |
| 294 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 295 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 296 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 297 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 298 | #define USE_CTERMID_R |
| 299 | #endif |
| 300 | |
| 301 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 302 | #define USE_TMPNAM_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); |
| 386 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 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); |
| 392 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 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(); |
| 1580 | return PyString_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(); |
| 1602 | return PyString_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(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1882 | return PyString_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 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2134 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2135 | return NULL; |
| 2136 | if (len >= MAX_PATH) { |
| 2137 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2138 | return NULL; |
| 2139 | } |
| 2140 | strcpy(namebuf, name); |
| 2141 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2142 | if (*pt == ALTSEP) |
| 2143 | *pt = SEP; |
| 2144 | if (namebuf[len-1] != SEP) |
| 2145 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2146 | strcpy(namebuf + len, "*.*"); |
| 2147 | |
| 2148 | if ((d = PyList_New(0)) == NULL) |
| 2149 | return NULL; |
| 2150 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2151 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2152 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2153 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2154 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2155 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2156 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2157 | |
| 2158 | if (rc != NO_ERROR) { |
| 2159 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 2160 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2163 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2164 | do { |
| 2165 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2166 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2167 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2168 | |
| 2169 | strcpy(namebuf, ep.achName); |
| 2170 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2171 | /* Leave Case of Name Alone -- In Native Form */ |
| 2172 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2173 | |
| 2174 | v = PyString_FromString(namebuf); |
| 2175 | if (v == NULL) { |
| 2176 | Py_DECREF(d); |
| 2177 | d = NULL; |
| 2178 | break; |
| 2179 | } |
| 2180 | if (PyList_Append(d, v) != 0) { |
| 2181 | Py_DECREF(v); |
| 2182 | Py_DECREF(d); |
| 2183 | d = NULL; |
| 2184 | break; |
| 2185 | } |
| 2186 | Py_DECREF(v); |
| 2187 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2188 | } |
| 2189 | |
| 2190 | return d; |
| 2191 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2192 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2193 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2194 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2195 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2196 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2197 | int arg_is_unicode = 1; |
| 2198 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2199 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2200 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2201 | arg_is_unicode = 0; |
| 2202 | PyErr_Clear(); |
| 2203 | } |
| 2204 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2205 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2206 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2207 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2208 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2209 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2210 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2211 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2212 | return NULL; |
| 2213 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2214 | for (;;) { |
| 2215 | Py_BEGIN_ALLOW_THREADS |
| 2216 | ep = readdir(dirp); |
| 2217 | Py_END_ALLOW_THREADS |
| 2218 | if (ep == NULL) |
| 2219 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2220 | if (ep->d_name[0] == '.' && |
| 2221 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2222 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2223 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2224 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2225 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2226 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2227 | d = NULL; |
| 2228 | break; |
| 2229 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2230 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2231 | PyObject *w; |
| 2232 | |
| 2233 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2234 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2235 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2236 | if (w != NULL) { |
| 2237 | Py_DECREF(v); |
| 2238 | v = w; |
| 2239 | } |
| 2240 | else { |
| 2241 | /* fall back to the original byte string, as |
| 2242 | discussed in patch #683592 */ |
| 2243 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2244 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2245 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2246 | if (PyList_Append(d, v) != 0) { |
| 2247 | Py_DECREF(v); |
| 2248 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2249 | d = NULL; |
| 2250 | break; |
| 2251 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2252 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2253 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2254 | if (errno != 0 && d != NULL) { |
| 2255 | /* readdir() returned NULL and set errno */ |
| 2256 | closedir(dirp); |
| 2257 | Py_DECREF(d); |
| 2258 | return posix_error_with_allocated_filename(name); |
| 2259 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2260 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2261 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2262 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2263 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2264 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2265 | #endif /* which OS */ |
| 2266 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2267 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2268 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2269 | /* A helper function for abspath on win32 */ |
| 2270 | static PyObject * |
| 2271 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2272 | { |
| 2273 | /* assume encoded strings wont more than double no of chars */ |
| 2274 | char inbuf[MAX_PATH*2]; |
| 2275 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2276 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2277 | char outbuf[MAX_PATH*2]; |
| 2278 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2279 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2280 | if (unicode_file_names()) { |
| 2281 | PyUnicodeObject *po; |
| 2282 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2283 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2284 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2285 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2286 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2287 | woutbuf, &wtemp)) |
| 2288 | return win32_error("GetFullPathName", ""); |
| 2289 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2290 | } |
| 2291 | /* Drop the argument parsing error as narrow strings |
| 2292 | are also valid. */ |
| 2293 | PyErr_Clear(); |
| 2294 | } |
| 2295 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2296 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2297 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2298 | &insize)) |
| 2299 | return NULL; |
| 2300 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2301 | outbuf, &temp)) |
| 2302 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2303 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2304 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2305 | Py_FileSystemDefaultEncoding, NULL); |
| 2306 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2307 | return PyString_FromString(outbuf); |
| 2308 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2309 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2310 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2311 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2312 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2313 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2314 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2315 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2316 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2317 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2318 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2319 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2320 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2321 | |
| 2322 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2323 | if (unicode_file_names()) { |
| 2324 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2325 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2326 | Py_BEGIN_ALLOW_THREADS |
| 2327 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2328 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2329 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2330 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2331 | if (!res) |
| 2332 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2333 | Py_INCREF(Py_None); |
| 2334 | return Py_None; |
| 2335 | } |
| 2336 | /* Drop the argument parsing error as narrow strings |
| 2337 | are also valid. */ |
| 2338 | PyErr_Clear(); |
| 2339 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2340 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2341 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2342 | return NULL; |
| 2343 | Py_BEGIN_ALLOW_THREADS |
| 2344 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2345 | it is a simple dereference. */ |
| 2346 | res = CreateDirectoryA(path, NULL); |
| 2347 | Py_END_ALLOW_THREADS |
| 2348 | if (!res) { |
| 2349 | win32_error("mkdir", path); |
| 2350 | PyMem_Free(path); |
| 2351 | return NULL; |
| 2352 | } |
| 2353 | PyMem_Free(path); |
| 2354 | Py_INCREF(Py_None); |
| 2355 | return Py_None; |
| 2356 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2357 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2358 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2359 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2360 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2361 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2362 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2363 | res = mkdir(path); |
| 2364 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2365 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2366 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2367 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2368 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2369 | return posix_error_with_allocated_filename(path); |
| 2370 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2371 | Py_INCREF(Py_None); |
| 2372 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2373 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2376 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2377 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2378 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2379 | #include <sys/resource.h> |
| 2380 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2381 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2382 | |
| 2383 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2384 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2385 | "nice(inc) -> new_priority\n\n\ |
| 2386 | 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] | 2387 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2388 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2389 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2390 | { |
| 2391 | int increment, value; |
| 2392 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2393 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2394 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2395 | |
| 2396 | /* There are two flavours of 'nice': one that returns the new |
| 2397 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2398 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2399 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2400 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2401 | If we are of the nice family that returns the new priority, we |
| 2402 | need to clear errno before the call, and check if errno is filled |
| 2403 | before calling posix_error() on a returnvalue of -1, because the |
| 2404 | -1 may be the actual new priority! */ |
| 2405 | |
| 2406 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2407 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2408 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2409 | if (value == 0) |
| 2410 | value = getpriority(PRIO_PROCESS, 0); |
| 2411 | #endif |
| 2412 | if (value == -1 && errno != 0) |
| 2413 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2414 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2415 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2416 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2417 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2418 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2419 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2420 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2421 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2422 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2423 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2424 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2425 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2426 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2427 | PyObject *o1, *o2; |
| 2428 | char *p1, *p2; |
| 2429 | BOOL result; |
| 2430 | if (unicode_file_names()) { |
| 2431 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2432 | convert_to_unicode, &o1, |
| 2433 | convert_to_unicode, &o2)) |
| 2434 | PyErr_Clear(); |
| 2435 | else { |
| 2436 | Py_BEGIN_ALLOW_THREADS |
| 2437 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2438 | PyUnicode_AsUnicode(o2)); |
| 2439 | Py_END_ALLOW_THREADS |
| 2440 | Py_DECREF(o1); |
| 2441 | Py_DECREF(o2); |
| 2442 | if (!result) |
| 2443 | return win32_error("rename", NULL); |
| 2444 | Py_INCREF(Py_None); |
| 2445 | return Py_None; |
| 2446 | } |
| 2447 | } |
| 2448 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2449 | return NULL; |
| 2450 | Py_BEGIN_ALLOW_THREADS |
| 2451 | result = MoveFileA(p1, p2); |
| 2452 | Py_END_ALLOW_THREADS |
| 2453 | if (!result) |
| 2454 | return win32_error("rename", NULL); |
| 2455 | Py_INCREF(Py_None); |
| 2456 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2457 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2458 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2459 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2462 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2463 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2464 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2465 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2466 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2467 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2468 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2469 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2470 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2471 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2472 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2473 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2474 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2478 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2479 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2480 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2481 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2482 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2483 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2484 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2485 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2486 | 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] | 2487 | #else |
| 2488 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2489 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2492 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2493 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2494 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2495 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2496 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2497 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2498 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2499 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2500 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2501 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2502 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2503 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2504 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2505 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2506 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2507 | Py_END_ALLOW_THREADS |
| 2508 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2509 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2510 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2511 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2512 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2513 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2514 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2515 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2516 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2517 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2518 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2519 | { |
| 2520 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2521 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2522 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2523 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2524 | if (i < 0) |
| 2525 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2526 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2529 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2530 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2531 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2532 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2534 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2535 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2536 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2537 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2538 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2539 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2540 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2541 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2542 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2543 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2544 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2545 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2548 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2549 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2550 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2551 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2552 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2553 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2554 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2555 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2556 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2557 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2558 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2559 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2560 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2561 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2562 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2563 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2564 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2565 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2566 | u.sysname, |
| 2567 | u.nodename, |
| 2568 | u.release, |
| 2569 | u.version, |
| 2570 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2571 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2572 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2573 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2574 | static int |
| 2575 | extract_time(PyObject *t, long* sec, long* usec) |
| 2576 | { |
| 2577 | long intval; |
| 2578 | if (PyFloat_Check(t)) { |
| 2579 | double tval = PyFloat_AsDouble(t); |
| 2580 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2581 | if (!intobj) |
| 2582 | return -1; |
| 2583 | intval = PyInt_AsLong(intobj); |
| 2584 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2585 | if (intval == -1 && PyErr_Occurred()) |
| 2586 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2587 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2588 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2589 | if (*usec < 0) |
| 2590 | /* If rounding gave us a negative number, |
| 2591 | truncate. */ |
| 2592 | *usec = 0; |
| 2593 | return 0; |
| 2594 | } |
| 2595 | intval = PyInt_AsLong(t); |
| 2596 | if (intval == -1 && PyErr_Occurred()) |
| 2597 | return -1; |
| 2598 | *sec = intval; |
| 2599 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2600 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2601 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2603 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2604 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2605 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2606 | 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] | 2607 | 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] | 2608 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2609 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2610 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2611 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2612 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2613 | PyObject *arg; |
| 2614 | PyUnicodeObject *obwpath; |
| 2615 | wchar_t *wpath = NULL; |
| 2616 | char *apath = NULL; |
| 2617 | HANDLE hFile; |
| 2618 | long atimesec, mtimesec, ausec, musec; |
| 2619 | FILETIME atime, mtime; |
| 2620 | PyObject *result = NULL; |
| 2621 | |
| 2622 | if (unicode_file_names()) { |
| 2623 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2624 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2625 | Py_BEGIN_ALLOW_THREADS |
| 2626 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2627 | NULL, OPEN_EXISTING, |
| 2628 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2629 | Py_END_ALLOW_THREADS |
| 2630 | if (hFile == INVALID_HANDLE_VALUE) |
| 2631 | return win32_error_unicode("utime", wpath); |
| 2632 | } else |
| 2633 | /* Drop the argument parsing error as narrow strings |
| 2634 | are also valid. */ |
| 2635 | PyErr_Clear(); |
| 2636 | } |
| 2637 | if (!wpath) { |
| 2638 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2639 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2640 | return NULL; |
| 2641 | Py_BEGIN_ALLOW_THREADS |
| 2642 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2643 | NULL, OPEN_EXISTING, |
| 2644 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2645 | Py_END_ALLOW_THREADS |
| 2646 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2647 | win32_error("utime", apath); |
| 2648 | PyMem_Free(apath); |
| 2649 | return NULL; |
| 2650 | } |
| 2651 | PyMem_Free(apath); |
| 2652 | } |
| 2653 | |
| 2654 | if (arg == Py_None) { |
| 2655 | SYSTEMTIME now; |
| 2656 | GetSystemTime(&now); |
| 2657 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2658 | !SystemTimeToFileTime(&now, &atime)) { |
| 2659 | win32_error("utime", NULL); |
| 2660 | goto done; |
| 2661 | } |
| 2662 | } |
| 2663 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2664 | PyErr_SetString(PyExc_TypeError, |
| 2665 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2666 | goto done; |
| 2667 | } |
| 2668 | else { |
| 2669 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2670 | &atimesec, &ausec) == -1) |
| 2671 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2672 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2673 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2674 | &mtimesec, &musec) == -1) |
| 2675 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2676 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2677 | } |
| 2678 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2679 | /* Avoid putting the file name into the error here, |
| 2680 | as that may confuse the user into believing that |
| 2681 | something is wrong with the file, when it also |
| 2682 | could be the time stamp that gives a problem. */ |
| 2683 | win32_error("utime", NULL); |
| 2684 | } |
| 2685 | Py_INCREF(Py_None); |
| 2686 | result = Py_None; |
| 2687 | done: |
| 2688 | CloseHandle(hFile); |
| 2689 | return result; |
| 2690 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2691 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2692 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2693 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2694 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2695 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2696 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2697 | #if defined(HAVE_UTIMES) |
| 2698 | struct timeval buf[2]; |
| 2699 | #define ATIME buf[0].tv_sec |
| 2700 | #define MTIME buf[1].tv_sec |
| 2701 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2702 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2703 | struct utimbuf buf; |
| 2704 | #define ATIME buf.actime |
| 2705 | #define MTIME buf.modtime |
| 2706 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2707 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2708 | time_t buf[2]; |
| 2709 | #define ATIME buf[0] |
| 2710 | #define MTIME buf[1] |
| 2711 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2712 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2713 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2714 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2715 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2716 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2717 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2718 | if (arg == Py_None) { |
| 2719 | /* optional time values not given */ |
| 2720 | Py_BEGIN_ALLOW_THREADS |
| 2721 | res = utime(path, NULL); |
| 2722 | Py_END_ALLOW_THREADS |
| 2723 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2724 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2725 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2726 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2727 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2728 | return NULL; |
| 2729 | } |
| 2730 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2731 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2732 | &atime, &ausec) == -1) { |
| 2733 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2734 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2735 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2736 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2737 | &mtime, &musec) == -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 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2741 | ATIME = atime; |
| 2742 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2743 | #ifdef HAVE_UTIMES |
| 2744 | buf[0].tv_usec = ausec; |
| 2745 | buf[1].tv_usec = musec; |
| 2746 | Py_BEGIN_ALLOW_THREADS |
| 2747 | res = utimes(path, buf); |
| 2748 | Py_END_ALLOW_THREADS |
| 2749 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2750 | Py_BEGIN_ALLOW_THREADS |
| 2751 | res = utime(path, UTIME_ARG); |
| 2752 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2753 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2754 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2755 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2756 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2757 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2758 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2759 | Py_INCREF(Py_None); |
| 2760 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2761 | #undef UTIME_ARG |
| 2762 | #undef ATIME |
| 2763 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2764 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2767 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2768 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2769 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2770 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2771 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2772 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2773 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2774 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2775 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2776 | { |
| 2777 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2778 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2779 | return NULL; |
| 2780 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2781 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2784 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2785 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2786 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2787 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2788 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2789 | for (i = 0; i < count; i++) |
| 2790 | PyMem_Free(array[i]); |
| 2791 | PyMem_DEL(array); |
| 2792 | } |
| 2793 | #endif |
| 2794 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2795 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2796 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2797 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2798 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2799 | Execute an executable path with arguments, replacing current process.\n\ |
| 2800 | \n\ |
| 2801 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2802 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2803 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2804 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2805 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2806 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2807 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2808 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2809 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2810 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2811 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2812 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2813 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2814 | argv is a list or tuple of strings. */ |
| 2815 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2816 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2817 | Py_FileSystemDefaultEncoding, |
| 2818 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2819 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2820 | if (PyList_Check(argv)) { |
| 2821 | argc = PyList_Size(argv); |
| 2822 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2823 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2824 | else if (PyTuple_Check(argv)) { |
| 2825 | argc = PyTuple_Size(argv); |
| 2826 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2827 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2828 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2829 | 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] | 2830 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2831 | return NULL; |
| 2832 | } |
| 2833 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2834 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2835 | if (argvlist == NULL) { |
| 2836 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2837 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2838 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2839 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2840 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2841 | Py_FileSystemDefaultEncoding, |
| 2842 | &argvlist[i])) { |
| 2843 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2844 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2845 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2846 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2847 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2848 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2849 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2850 | } |
| 2851 | argvlist[argc] = NULL; |
| 2852 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2853 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2854 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2855 | /* If we get here it's definitely an error */ |
| 2856 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2857 | free_string_array(argvlist, argc); |
| 2858 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2859 | return posix_error(); |
| 2860 | } |
| 2861 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2863 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2864 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2865 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2866 | \n\ |
| 2867 | path: path of executable file\n\ |
| 2868 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2869 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2870 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2871 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2872 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2873 | { |
| 2874 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2875 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2876 | char **argvlist; |
| 2877 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2878 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2879 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2880 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2881 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2882 | |
| 2883 | /* execve has three arguments: (path, argv, env), where |
| 2884 | argv is a list or tuple of strings and env is a dictionary |
| 2885 | like posix.environ. */ |
| 2886 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2887 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2888 | Py_FileSystemDefaultEncoding, |
| 2889 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2890 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2891 | if (PyList_Check(argv)) { |
| 2892 | argc = PyList_Size(argv); |
| 2893 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2894 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2895 | else if (PyTuple_Check(argv)) { |
| 2896 | argc = PyTuple_Size(argv); |
| 2897 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2898 | } |
| 2899 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2900 | PyErr_SetString(PyExc_TypeError, |
| 2901 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2902 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2903 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2904 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2905 | PyErr_SetString(PyExc_TypeError, |
| 2906 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2907 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2910 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2911 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2912 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2913 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2914 | } |
| 2915 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2916 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2917 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2918 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2919 | &argvlist[i])) |
| 2920 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2921 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2922 | goto fail_1; |
| 2923 | } |
| 2924 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2925 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2926 | argvlist[argc] = NULL; |
| 2927 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2928 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2929 | if (i < 0) |
| 2930 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2931 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2932 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2933 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2934 | goto fail_1; |
| 2935 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2936 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2937 | keys = PyMapping_Keys(env); |
| 2938 | vals = PyMapping_Values(env); |
| 2939 | if (!keys || !vals) |
| 2940 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2941 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2942 | PyErr_SetString(PyExc_TypeError, |
| 2943 | "execve(): env.keys() or env.values() is not a list"); |
| 2944 | goto fail_2; |
| 2945 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2946 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2947 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2948 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2949 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2950 | |
| 2951 | key = PyList_GetItem(keys, pos); |
| 2952 | val = PyList_GetItem(vals, pos); |
| 2953 | if (!key || !val) |
| 2954 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2955 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2956 | if (!PyArg_Parse( |
| 2957 | key, |
| 2958 | "s;execve() arg 3 contains a non-string key", |
| 2959 | &k) || |
| 2960 | !PyArg_Parse( |
| 2961 | val, |
| 2962 | "s;execve() arg 3 contains a non-string value", |
| 2963 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2964 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2965 | goto fail_2; |
| 2966 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2967 | |
| 2968 | #if defined(PYOS_OS2) |
| 2969 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2970 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2971 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2972 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2973 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2974 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2975 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2976 | goto fail_2; |
| 2977 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2978 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2979 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2980 | #if defined(PYOS_OS2) |
| 2981 | } |
| 2982 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2983 | } |
| 2984 | envlist[envc] = 0; |
| 2985 | |
| 2986 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2987 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2988 | /* If we get here it's definitely an error */ |
| 2989 | |
| 2990 | (void) posix_error(); |
| 2991 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2992 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2993 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2994 | PyMem_DEL(envlist[envc]); |
| 2995 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2996 | fail_1: |
| 2997 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2998 | Py_XDECREF(vals); |
| 2999 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3000 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3001 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3002 | return NULL; |
| 3003 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3004 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3005 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3006 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3007 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3008 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3009 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3010 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3011 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3012 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3013 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3014 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3015 | |
| 3016 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3017 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3018 | { |
| 3019 | char *path; |
| 3020 | PyObject *argv; |
| 3021 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3022 | int mode, i; |
| 3023 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3024 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3025 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3026 | |
| 3027 | /* spawnv has three arguments: (mode, path, argv), where |
| 3028 | argv is a list or tuple of strings. */ |
| 3029 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3030 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3031 | Py_FileSystemDefaultEncoding, |
| 3032 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3033 | return NULL; |
| 3034 | if (PyList_Check(argv)) { |
| 3035 | argc = PyList_Size(argv); |
| 3036 | getitem = PyList_GetItem; |
| 3037 | } |
| 3038 | else if (PyTuple_Check(argv)) { |
| 3039 | argc = PyTuple_Size(argv); |
| 3040 | getitem = PyTuple_GetItem; |
| 3041 | } |
| 3042 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3043 | PyErr_SetString(PyExc_TypeError, |
| 3044 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3045 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3046 | return NULL; |
| 3047 | } |
| 3048 | |
| 3049 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3050 | if (argvlist == NULL) { |
| 3051 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3052 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3053 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3054 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3055 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3056 | Py_FileSystemDefaultEncoding, |
| 3057 | &argvlist[i])) { |
| 3058 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3059 | PyErr_SetString( |
| 3060 | PyExc_TypeError, |
| 3061 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3062 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3063 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | argvlist[argc] = NULL; |
| 3067 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3068 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3069 | Py_BEGIN_ALLOW_THREADS |
| 3070 | spawnval = spawnv(mode, path, argvlist); |
| 3071 | Py_END_ALLOW_THREADS |
| 3072 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3073 | if (mode == _OLD_P_OVERLAY) |
| 3074 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3075 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3076 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3077 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3078 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3079 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3080 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3081 | free_string_array(argvlist, argc); |
| 3082 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3083 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3084 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3085 | return posix_error(); |
| 3086 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3087 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3088 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3089 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3090 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3091 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
| 3094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3095 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3096 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3097 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3098 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3099 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3100 | path: path of executable file\n\ |
| 3101 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3102 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3103 | |
| 3104 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3105 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3106 | { |
| 3107 | char *path; |
| 3108 | PyObject *argv, *env; |
| 3109 | char **argvlist; |
| 3110 | char **envlist; |
| 3111 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3112 | int mode, pos, envc; |
| 3113 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3114 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3115 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3116 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3117 | |
| 3118 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3119 | argv is a list or tuple of strings and env is a dictionary |
| 3120 | like posix.environ. */ |
| 3121 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3122 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3123 | Py_FileSystemDefaultEncoding, |
| 3124 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3125 | return NULL; |
| 3126 | if (PyList_Check(argv)) { |
| 3127 | argc = PyList_Size(argv); |
| 3128 | getitem = PyList_GetItem; |
| 3129 | } |
| 3130 | else if (PyTuple_Check(argv)) { |
| 3131 | argc = PyTuple_Size(argv); |
| 3132 | getitem = PyTuple_GetItem; |
| 3133 | } |
| 3134 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3135 | PyErr_SetString(PyExc_TypeError, |
| 3136 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3137 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3138 | } |
| 3139 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3140 | PyErr_SetString(PyExc_TypeError, |
| 3141 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3142 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | argvlist = PyMem_NEW(char *, argc+1); |
| 3146 | if (argvlist == NULL) { |
| 3147 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3148 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3149 | } |
| 3150 | for (i = 0; i < argc; i++) { |
| 3151 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3152 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3153 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3154 | &argvlist[i])) |
| 3155 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3156 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3157 | goto fail_1; |
| 3158 | } |
| 3159 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3160 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3161 | argvlist[argc] = NULL; |
| 3162 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3163 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3164 | if (i < 0) |
| 3165 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3166 | envlist = PyMem_NEW(char *, i + 1); |
| 3167 | if (envlist == NULL) { |
| 3168 | PyErr_NoMemory(); |
| 3169 | goto fail_1; |
| 3170 | } |
| 3171 | envc = 0; |
| 3172 | keys = PyMapping_Keys(env); |
| 3173 | vals = PyMapping_Values(env); |
| 3174 | if (!keys || !vals) |
| 3175 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3176 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3177 | PyErr_SetString(PyExc_TypeError, |
| 3178 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3179 | goto fail_2; |
| 3180 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3181 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3182 | for (pos = 0; pos < i; pos++) { |
| 3183 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3184 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3185 | |
| 3186 | key = PyList_GetItem(keys, pos); |
| 3187 | val = PyList_GetItem(vals, pos); |
| 3188 | if (!key || !val) |
| 3189 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3190 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3191 | if (!PyArg_Parse( |
| 3192 | key, |
| 3193 | "s;spawnve() arg 3 contains a non-string key", |
| 3194 | &k) || |
| 3195 | !PyArg_Parse( |
| 3196 | val, |
| 3197 | "s;spawnve() arg 3 contains a non-string value", |
| 3198 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3199 | { |
| 3200 | goto fail_2; |
| 3201 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3202 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3203 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3204 | if (p == NULL) { |
| 3205 | PyErr_NoMemory(); |
| 3206 | goto fail_2; |
| 3207 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3208 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3209 | envlist[envc++] = p; |
| 3210 | } |
| 3211 | envlist[envc] = 0; |
| 3212 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3213 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3214 | Py_BEGIN_ALLOW_THREADS |
| 3215 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3216 | Py_END_ALLOW_THREADS |
| 3217 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3218 | if (mode == _OLD_P_OVERLAY) |
| 3219 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3220 | |
| 3221 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3222 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3223 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3224 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3225 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3226 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3227 | (void) posix_error(); |
| 3228 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3229 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3230 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3231 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3232 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3233 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3234 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3235 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3236 | while (--envc >= 0) |
| 3237 | PyMem_DEL(envlist[envc]); |
| 3238 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3239 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3240 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3241 | Py_XDECREF(vals); |
| 3242 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3243 | fail_0: |
| 3244 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3245 | return res; |
| 3246 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3247 | |
| 3248 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3249 | #if defined(PYOS_OS2) |
| 3250 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3251 | "spawnvp(mode, file, args)\n\n\ |
| 3252 | Execute the program 'file' in a new process, using the environment\n\ |
| 3253 | search path to find the file.\n\ |
| 3254 | \n\ |
| 3255 | mode: mode of process creation\n\ |
| 3256 | file: executable file name\n\ |
| 3257 | args: tuple or list of strings"); |
| 3258 | |
| 3259 | static PyObject * |
| 3260 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3261 | { |
| 3262 | char *path; |
| 3263 | PyObject *argv; |
| 3264 | char **argvlist; |
| 3265 | int mode, i, argc; |
| 3266 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3267 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3268 | |
| 3269 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3270 | argv is a list or tuple of strings. */ |
| 3271 | |
| 3272 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3273 | Py_FileSystemDefaultEncoding, |
| 3274 | &path, &argv)) |
| 3275 | return NULL; |
| 3276 | if (PyList_Check(argv)) { |
| 3277 | argc = PyList_Size(argv); |
| 3278 | getitem = PyList_GetItem; |
| 3279 | } |
| 3280 | else if (PyTuple_Check(argv)) { |
| 3281 | argc = PyTuple_Size(argv); |
| 3282 | getitem = PyTuple_GetItem; |
| 3283 | } |
| 3284 | else { |
| 3285 | PyErr_SetString(PyExc_TypeError, |
| 3286 | "spawnvp() arg 2 must be a tuple or list"); |
| 3287 | PyMem_Free(path); |
| 3288 | return NULL; |
| 3289 | } |
| 3290 | |
| 3291 | argvlist = PyMem_NEW(char *, argc+1); |
| 3292 | if (argvlist == NULL) { |
| 3293 | PyMem_Free(path); |
| 3294 | return PyErr_NoMemory(); |
| 3295 | } |
| 3296 | for (i = 0; i < argc; i++) { |
| 3297 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3298 | Py_FileSystemDefaultEncoding, |
| 3299 | &argvlist[i])) { |
| 3300 | free_string_array(argvlist, i); |
| 3301 | PyErr_SetString( |
| 3302 | PyExc_TypeError, |
| 3303 | "spawnvp() arg 2 must contain only strings"); |
| 3304 | PyMem_Free(path); |
| 3305 | return NULL; |
| 3306 | } |
| 3307 | } |
| 3308 | argvlist[argc] = NULL; |
| 3309 | |
| 3310 | Py_BEGIN_ALLOW_THREADS |
| 3311 | #if defined(PYCC_GCC) |
| 3312 | spawnval = spawnvp(mode, path, argvlist); |
| 3313 | #else |
| 3314 | spawnval = _spawnvp(mode, path, argvlist); |
| 3315 | #endif |
| 3316 | Py_END_ALLOW_THREADS |
| 3317 | |
| 3318 | free_string_array(argvlist, argc); |
| 3319 | PyMem_Free(path); |
| 3320 | |
| 3321 | if (spawnval == -1) |
| 3322 | return posix_error(); |
| 3323 | else |
| 3324 | return Py_BuildValue("l", (long) spawnval); |
| 3325 | } |
| 3326 | |
| 3327 | |
| 3328 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3329 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3330 | Execute the program 'file' in a new process, using the environment\n\ |
| 3331 | search path to find the file.\n\ |
| 3332 | \n\ |
| 3333 | mode: mode of process creation\n\ |
| 3334 | file: executable file name\n\ |
| 3335 | args: tuple or list of arguments\n\ |
| 3336 | env: dictionary of strings mapping to strings"); |
| 3337 | |
| 3338 | static PyObject * |
| 3339 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3340 | { |
| 3341 | char *path; |
| 3342 | PyObject *argv, *env; |
| 3343 | char **argvlist; |
| 3344 | char **envlist; |
| 3345 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3346 | int mode, i, pos, argc, envc; |
| 3347 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3348 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3349 | int lastarg = 0; |
| 3350 | |
| 3351 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3352 | argv is a list or tuple of strings and env is a dictionary |
| 3353 | like posix.environ. */ |
| 3354 | |
| 3355 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3356 | Py_FileSystemDefaultEncoding, |
| 3357 | &path, &argv, &env)) |
| 3358 | return NULL; |
| 3359 | if (PyList_Check(argv)) { |
| 3360 | argc = PyList_Size(argv); |
| 3361 | getitem = PyList_GetItem; |
| 3362 | } |
| 3363 | else if (PyTuple_Check(argv)) { |
| 3364 | argc = PyTuple_Size(argv); |
| 3365 | getitem = PyTuple_GetItem; |
| 3366 | } |
| 3367 | else { |
| 3368 | PyErr_SetString(PyExc_TypeError, |
| 3369 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3370 | goto fail_0; |
| 3371 | } |
| 3372 | if (!PyMapping_Check(env)) { |
| 3373 | PyErr_SetString(PyExc_TypeError, |
| 3374 | "spawnvpe() arg 3 must be a mapping object"); |
| 3375 | goto fail_0; |
| 3376 | } |
| 3377 | |
| 3378 | argvlist = PyMem_NEW(char *, argc+1); |
| 3379 | if (argvlist == NULL) { |
| 3380 | PyErr_NoMemory(); |
| 3381 | goto fail_0; |
| 3382 | } |
| 3383 | for (i = 0; i < argc; i++) { |
| 3384 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3385 | "et;spawnvpe() arg 2 must contain only strings", |
| 3386 | Py_FileSystemDefaultEncoding, |
| 3387 | &argvlist[i])) |
| 3388 | { |
| 3389 | lastarg = i; |
| 3390 | goto fail_1; |
| 3391 | } |
| 3392 | } |
| 3393 | lastarg = argc; |
| 3394 | argvlist[argc] = NULL; |
| 3395 | |
| 3396 | i = PyMapping_Size(env); |
| 3397 | if (i < 0) |
| 3398 | goto fail_1; |
| 3399 | envlist = PyMem_NEW(char *, i + 1); |
| 3400 | if (envlist == NULL) { |
| 3401 | PyErr_NoMemory(); |
| 3402 | goto fail_1; |
| 3403 | } |
| 3404 | envc = 0; |
| 3405 | keys = PyMapping_Keys(env); |
| 3406 | vals = PyMapping_Values(env); |
| 3407 | if (!keys || !vals) |
| 3408 | goto fail_2; |
| 3409 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3410 | PyErr_SetString(PyExc_TypeError, |
| 3411 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3412 | goto fail_2; |
| 3413 | } |
| 3414 | |
| 3415 | for (pos = 0; pos < i; pos++) { |
| 3416 | char *p, *k, *v; |
| 3417 | size_t len; |
| 3418 | |
| 3419 | key = PyList_GetItem(keys, pos); |
| 3420 | val = PyList_GetItem(vals, pos); |
| 3421 | if (!key || !val) |
| 3422 | goto fail_2; |
| 3423 | |
| 3424 | if (!PyArg_Parse( |
| 3425 | key, |
| 3426 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3427 | &k) || |
| 3428 | !PyArg_Parse( |
| 3429 | val, |
| 3430 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3431 | &v)) |
| 3432 | { |
| 3433 | goto fail_2; |
| 3434 | } |
| 3435 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3436 | p = PyMem_NEW(char, len); |
| 3437 | if (p == NULL) { |
| 3438 | PyErr_NoMemory(); |
| 3439 | goto fail_2; |
| 3440 | } |
| 3441 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3442 | envlist[envc++] = p; |
| 3443 | } |
| 3444 | envlist[envc] = 0; |
| 3445 | |
| 3446 | Py_BEGIN_ALLOW_THREADS |
| 3447 | #if defined(PYCC_GCC) |
| 3448 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3449 | #else |
| 3450 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3451 | #endif |
| 3452 | Py_END_ALLOW_THREADS |
| 3453 | |
| 3454 | if (spawnval == -1) |
| 3455 | (void) posix_error(); |
| 3456 | else |
| 3457 | res = Py_BuildValue("l", (long) spawnval); |
| 3458 | |
| 3459 | fail_2: |
| 3460 | while (--envc >= 0) |
| 3461 | PyMem_DEL(envlist[envc]); |
| 3462 | PyMem_DEL(envlist); |
| 3463 | fail_1: |
| 3464 | free_string_array(argvlist, lastarg); |
| 3465 | Py_XDECREF(vals); |
| 3466 | Py_XDECREF(keys); |
| 3467 | fail_0: |
| 3468 | PyMem_Free(path); |
| 3469 | return res; |
| 3470 | } |
| 3471 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3472 | #endif /* HAVE_SPAWNV */ |
| 3473 | |
| 3474 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3475 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3476 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3477 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3478 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3479 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3480 | 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] | 3481 | |
| 3482 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3483 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3484 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3485 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3486 | if (pid == -1) |
| 3487 | return posix_error(); |
| 3488 | PyOS_AfterFork(); |
| 3489 | return PyInt_FromLong((long)pid); |
| 3490 | } |
| 3491 | #endif |
| 3492 | |
| 3493 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3494 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3495 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3496 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3497 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3498 | 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] | 3499 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3500 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3501 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3502 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3503 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3504 | if (pid == -1) |
| 3505 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3506 | if (pid == 0) |
| 3507 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3508 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3509 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3510 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3511 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3512 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3513 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3514 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3515 | #define DEV_PTY_FILE "/dev/ptc" |
| 3516 | #define HAVE_DEV_PTMX |
| 3517 | #else |
| 3518 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3519 | #endif |
| 3520 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3521 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3522 | #ifdef HAVE_PTY_H |
| 3523 | #include <pty.h> |
| 3524 | #else |
| 3525 | #ifdef HAVE_LIBUTIL_H |
| 3526 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3527 | #endif /* HAVE_LIBUTIL_H */ |
| 3528 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3529 | #ifdef HAVE_STROPTS_H |
| 3530 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3531 | #endif |
| 3532 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3533 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3534 | #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] | 3535 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3536 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3537 | 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] | 3538 | |
| 3539 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3540 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3541 | { |
| 3542 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3543 | #ifndef HAVE_OPENPTY |
| 3544 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3545 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3546 | #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] | 3547 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3548 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3549 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3550 | #endif |
| 3551 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3552 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3553 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3554 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3555 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3556 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3557 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3558 | if (slave_name == NULL) |
| 3559 | return posix_error(); |
| 3560 | |
| 3561 | slave_fd = open(slave_name, O_RDWR); |
| 3562 | if (slave_fd < 0) |
| 3563 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3564 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3565 | 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] | 3566 | if (master_fd < 0) |
| 3567 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3568 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3569 | /* change permission of slave */ |
| 3570 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3571 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3572 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3573 | } |
| 3574 | /* unlock slave */ |
| 3575 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3576 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3577 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3578 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3579 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3580 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3581 | if (slave_name == NULL) |
| 3582 | return posix_error(); |
| 3583 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3584 | if (slave_fd < 0) |
| 3585 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3586 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3587 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3588 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3589 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3590 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3591 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3592 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3593 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3594 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3595 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3596 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3597 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3598 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3599 | |
| 3600 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3601 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3602 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3603 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3604 | 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] | 3605 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3606 | |
| 3607 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3608 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3609 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3610 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3611 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3612 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3613 | if (pid == -1) |
| 3614 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3615 | if (pid == 0) |
| 3616 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3617 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3618 | } |
| 3619 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3620 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3621 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3622 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3623 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3624 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3625 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3626 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3627 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3628 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3629 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3630 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3631 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3632 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3633 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3634 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3635 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3636 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3637 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3638 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3639 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3640 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3641 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3642 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3643 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3644 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3645 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3646 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3647 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3648 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3649 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3650 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3651 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3652 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3653 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3654 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3655 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3656 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3657 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3658 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3659 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3660 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3661 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3662 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3663 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3664 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3665 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3666 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3667 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3670 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3671 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3672 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3673 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3674 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3675 | |
| 3676 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3677 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3678 | { |
| 3679 | PyObject *result = NULL; |
| 3680 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3681 | #ifdef NGROUPS_MAX |
| 3682 | #define MAX_GROUPS NGROUPS_MAX |
| 3683 | #else |
| 3684 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3685 | #define MAX_GROUPS 64 |
| 3686 | #endif |
| 3687 | gid_t grouplist[MAX_GROUPS]; |
| 3688 | int n; |
| 3689 | |
| 3690 | n = getgroups(MAX_GROUPS, grouplist); |
| 3691 | if (n < 0) |
| 3692 | posix_error(); |
| 3693 | else { |
| 3694 | result = PyList_New(n); |
| 3695 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3696 | int i; |
| 3697 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3698 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3699 | if (o == NULL) { |
| 3700 | Py_DECREF(result); |
| 3701 | result = NULL; |
| 3702 | break; |
| 3703 | } |
| 3704 | PyList_SET_ITEM(result, i, o); |
| 3705 | } |
| 3706 | } |
| 3707 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3708 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3709 | return result; |
| 3710 | } |
| 3711 | #endif |
| 3712 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3713 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3714 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3715 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3716 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3717 | |
| 3718 | static PyObject * |
| 3719 | posix_getpgid(PyObject *self, PyObject *args) |
| 3720 | { |
| 3721 | int pid, pgid; |
| 3722 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3723 | return NULL; |
| 3724 | pgid = getpgid(pid); |
| 3725 | if (pgid < 0) |
| 3726 | return posix_error(); |
| 3727 | return PyInt_FromLong((long)pgid); |
| 3728 | } |
| 3729 | #endif /* HAVE_GETPGID */ |
| 3730 | |
| 3731 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3732 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3733 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3734 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3735 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3736 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3737 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3738 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3739 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3740 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3741 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3742 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3743 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3744 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3745 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3746 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3747 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3748 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3749 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3750 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3751 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3752 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3753 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3754 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3755 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3756 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3757 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3758 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3759 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3760 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3761 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3762 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3763 | Py_INCREF(Py_None); |
| 3764 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3767 | #endif /* HAVE_SETPGRP */ |
| 3768 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3769 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3770 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3771 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3772 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3773 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3774 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3775 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3776 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3777 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3778 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3779 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3780 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3781 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3782 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3783 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3784 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3785 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3786 | |
| 3787 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3788 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3789 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3790 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3791 | char *name; |
| 3792 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3793 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3794 | errno = 0; |
| 3795 | name = getlogin(); |
| 3796 | if (name == NULL) { |
| 3797 | if (errno) |
| 3798 | posix_error(); |
| 3799 | else |
| 3800 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3801 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3802 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3803 | else |
| 3804 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3805 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3806 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3807 | return result; |
| 3808 | } |
| 3809 | #endif |
| 3810 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3811 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3812 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3813 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3814 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3815 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3816 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3817 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3818 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3819 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3820 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3821 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3823 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3824 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3825 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3826 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3827 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3828 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3829 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3830 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3831 | { |
| 3832 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3833 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3834 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3835 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3836 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3837 | APIRET rc; |
| 3838 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3839 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3840 | |
| 3841 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3842 | APIRET rc; |
| 3843 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3844 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3845 | |
| 3846 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3847 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3848 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3849 | if (kill(pid, sig) == -1) |
| 3850 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3851 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3852 | Py_INCREF(Py_None); |
| 3853 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3854 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3855 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3856 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3857 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3858 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3859 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3860 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3861 | |
| 3862 | static PyObject * |
| 3863 | posix_killpg(PyObject *self, PyObject *args) |
| 3864 | { |
| 3865 | int pgid, sig; |
| 3866 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3867 | return NULL; |
| 3868 | if (killpg(pgid, sig) == -1) |
| 3869 | return posix_error(); |
| 3870 | Py_INCREF(Py_None); |
| 3871 | return Py_None; |
| 3872 | } |
| 3873 | #endif |
| 3874 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3875 | #ifdef HAVE_PLOCK |
| 3876 | |
| 3877 | #ifdef HAVE_SYS_LOCK_H |
| 3878 | #include <sys/lock.h> |
| 3879 | #endif |
| 3880 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3881 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3882 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3883 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3884 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3885 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3886 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3887 | { |
| 3888 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3889 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3890 | return NULL; |
| 3891 | if (plock(op) == -1) |
| 3892 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3893 | Py_INCREF(Py_None); |
| 3894 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3895 | } |
| 3896 | #endif |
| 3897 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3898 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3899 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3900 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3901 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3902 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3903 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3904 | Set the current process's user id."); |
| 3905 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3906 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3907 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3908 | { |
| 3909 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3910 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3911 | return NULL; |
| 3912 | if (setuid(uid) < 0) |
| 3913 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3914 | Py_INCREF(Py_None); |
| 3915 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3916 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3917 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3918 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3919 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3920 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3921 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3922 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3923 | Set the current process's effective user id."); |
| 3924 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3925 | static PyObject * |
| 3926 | posix_seteuid (PyObject *self, PyObject *args) |
| 3927 | { |
| 3928 | int euid; |
| 3929 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3930 | return NULL; |
| 3931 | } else if (seteuid(euid) < 0) { |
| 3932 | return posix_error(); |
| 3933 | } else { |
| 3934 | Py_INCREF(Py_None); |
| 3935 | return Py_None; |
| 3936 | } |
| 3937 | } |
| 3938 | #endif /* HAVE_SETEUID */ |
| 3939 | |
| 3940 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3941 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3942 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3943 | Set the current process's effective group id."); |
| 3944 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3945 | static PyObject * |
| 3946 | posix_setegid (PyObject *self, PyObject *args) |
| 3947 | { |
| 3948 | int egid; |
| 3949 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3950 | return NULL; |
| 3951 | } else if (setegid(egid) < 0) { |
| 3952 | return posix_error(); |
| 3953 | } else { |
| 3954 | Py_INCREF(Py_None); |
| 3955 | return Py_None; |
| 3956 | } |
| 3957 | } |
| 3958 | #endif /* HAVE_SETEGID */ |
| 3959 | |
| 3960 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3961 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 3962 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3963 | Set the current process's real and effective user ids."); |
| 3964 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3965 | static PyObject * |
| 3966 | posix_setreuid (PyObject *self, PyObject *args) |
| 3967 | { |
| 3968 | int ruid, euid; |
| 3969 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3970 | return NULL; |
| 3971 | } else if (setreuid(ruid, euid) < 0) { |
| 3972 | return posix_error(); |
| 3973 | } else { |
| 3974 | Py_INCREF(Py_None); |
| 3975 | return Py_None; |
| 3976 | } |
| 3977 | } |
| 3978 | #endif /* HAVE_SETREUID */ |
| 3979 | |
| 3980 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3981 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 3982 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3983 | Set the current process's real and effective group ids."); |
| 3984 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3985 | static PyObject * |
| 3986 | posix_setregid (PyObject *self, PyObject *args) |
| 3987 | { |
| 3988 | int rgid, egid; |
| 3989 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 3990 | return NULL; |
| 3991 | } else if (setregid(rgid, egid) < 0) { |
| 3992 | return posix_error(); |
| 3993 | } else { |
| 3994 | Py_INCREF(Py_None); |
| 3995 | return Py_None; |
| 3996 | } |
| 3997 | } |
| 3998 | #endif /* HAVE_SETREGID */ |
| 3999 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4000 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4001 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4002 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4003 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4004 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4005 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4006 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4007 | { |
| 4008 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4009 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4010 | return NULL; |
| 4011 | if (setgid(gid) < 0) |
| 4012 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4013 | Py_INCREF(Py_None); |
| 4014 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4015 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4016 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4017 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4018 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4019 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4020 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4021 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4022 | |
| 4023 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4024 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4025 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4026 | int i, len; |
| 4027 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4028 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4029 | if (!PySequence_Check(groups)) { |
| 4030 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4031 | return NULL; |
| 4032 | } |
| 4033 | len = PySequence_Size(groups); |
| 4034 | if (len > MAX_GROUPS) { |
| 4035 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4036 | return NULL; |
| 4037 | } |
| 4038 | for(i = 0; i < len; i++) { |
| 4039 | PyObject *elem; |
| 4040 | elem = PySequence_GetItem(groups, i); |
| 4041 | if (!elem) |
| 4042 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4043 | if (!PyLong_Check(elem)) { |
| 4044 | PyErr_SetString(PyExc_TypeError, |
| 4045 | "groups must be integers"); |
| 4046 | Py_DECREF(elem); |
| 4047 | return NULL; |
| 4048 | } else { |
| 4049 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4050 | if (PyErr_Occurred()) { |
| 4051 | PyErr_SetString(PyExc_TypeError, |
| 4052 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4053 | Py_DECREF(elem); |
| 4054 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4055 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4056 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4057 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4058 | if (grouplist[i] != x) { |
| 4059 | PyErr_SetString(PyExc_TypeError, |
| 4060 | "group id too big"); |
| 4061 | Py_DECREF(elem); |
| 4062 | return NULL; |
| 4063 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4064 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4065 | Py_DECREF(elem); |
| 4066 | } |
| 4067 | |
| 4068 | if (setgroups(len, grouplist) < 0) |
| 4069 | return posix_error(); |
| 4070 | Py_INCREF(Py_None); |
| 4071 | return Py_None; |
| 4072 | } |
| 4073 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4074 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4075 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4076 | static PyObject * |
| 4077 | wait_helper(int pid, int status, struct rusage *ru) |
| 4078 | { |
| 4079 | PyObject *result; |
| 4080 | static PyObject *struct_rusage; |
| 4081 | |
| 4082 | if (pid == -1) |
| 4083 | return posix_error(); |
| 4084 | |
| 4085 | if (struct_rusage == NULL) { |
| 4086 | PyObject *m = PyImport_ImportModule("resource"); |
| 4087 | if (m == NULL) |
| 4088 | return NULL; |
| 4089 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4090 | Py_DECREF(m); |
| 4091 | if (struct_rusage == NULL) |
| 4092 | return NULL; |
| 4093 | } |
| 4094 | |
| 4095 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4096 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4097 | if (!result) |
| 4098 | return NULL; |
| 4099 | |
| 4100 | #ifndef doubletime |
| 4101 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4102 | #endif |
| 4103 | |
| 4104 | PyStructSequence_SET_ITEM(result, 0, |
| 4105 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4106 | PyStructSequence_SET_ITEM(result, 1, |
| 4107 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4108 | #define SET_INT(result, index, value)\ |
| 4109 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 4110 | SET_INT(result, 2, ru->ru_maxrss); |
| 4111 | SET_INT(result, 3, ru->ru_ixrss); |
| 4112 | SET_INT(result, 4, ru->ru_idrss); |
| 4113 | SET_INT(result, 5, ru->ru_isrss); |
| 4114 | SET_INT(result, 6, ru->ru_minflt); |
| 4115 | SET_INT(result, 7, ru->ru_majflt); |
| 4116 | SET_INT(result, 8, ru->ru_nswap); |
| 4117 | SET_INT(result, 9, ru->ru_inblock); |
| 4118 | SET_INT(result, 10, ru->ru_oublock); |
| 4119 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4120 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4121 | SET_INT(result, 13, ru->ru_nsignals); |
| 4122 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4123 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4124 | #undef SET_INT |
| 4125 | |
| 4126 | if (PyErr_Occurred()) { |
| 4127 | Py_DECREF(result); |
| 4128 | return NULL; |
| 4129 | } |
| 4130 | |
| 4131 | return Py_BuildValue("iiN", pid, status, result); |
| 4132 | } |
| 4133 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4134 | |
| 4135 | #ifdef HAVE_WAIT3 |
| 4136 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4137 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4138 | Wait for completion of a child process."); |
| 4139 | |
| 4140 | static PyObject * |
| 4141 | posix_wait3(PyObject *self, PyObject *args) |
| 4142 | { |
| 4143 | int pid, options; |
| 4144 | struct rusage ru; |
| 4145 | WAIT_TYPE status; |
| 4146 | WAIT_STATUS_INT(status) = 0; |
| 4147 | |
| 4148 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4149 | return NULL; |
| 4150 | |
| 4151 | Py_BEGIN_ALLOW_THREADS |
| 4152 | pid = wait3(&status, options, &ru); |
| 4153 | Py_END_ALLOW_THREADS |
| 4154 | |
| 4155 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4156 | } |
| 4157 | #endif /* HAVE_WAIT3 */ |
| 4158 | |
| 4159 | #ifdef HAVE_WAIT4 |
| 4160 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4161 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4162 | Wait for completion of a given child process."); |
| 4163 | |
| 4164 | static PyObject * |
| 4165 | posix_wait4(PyObject *self, PyObject *args) |
| 4166 | { |
| 4167 | int pid, options; |
| 4168 | struct rusage ru; |
| 4169 | WAIT_TYPE status; |
| 4170 | WAIT_STATUS_INT(status) = 0; |
| 4171 | |
| 4172 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4173 | return NULL; |
| 4174 | |
| 4175 | Py_BEGIN_ALLOW_THREADS |
| 4176 | pid = wait4(pid, &status, options, &ru); |
| 4177 | Py_END_ALLOW_THREADS |
| 4178 | |
| 4179 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4180 | } |
| 4181 | #endif /* HAVE_WAIT4 */ |
| 4182 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4183 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4184 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4185 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4186 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4187 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4188 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4189 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4190 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4191 | int pid, options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4192 | WAIT_TYPE status; |
| 4193 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4194 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4195 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4196 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4197 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4198 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4199 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4200 | if (pid == -1) |
| 4201 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4202 | |
| 4203 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4206 | #elif defined(HAVE_CWAIT) |
| 4207 | |
| 4208 | /* 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] | 4209 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4210 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4211 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4212 | |
| 4213 | static PyObject * |
| 4214 | posix_waitpid(PyObject *self, PyObject *args) |
| 4215 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4216 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4217 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4218 | |
| 4219 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4220 | return NULL; |
| 4221 | Py_BEGIN_ALLOW_THREADS |
| 4222 | pid = _cwait(&status, pid, options); |
| 4223 | Py_END_ALLOW_THREADS |
| 4224 | if (pid == -1) |
| 4225 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4226 | |
| 4227 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4228 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4229 | } |
| 4230 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4231 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4232 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4233 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4234 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4235 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4236 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4237 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4238 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4239 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4240 | int pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4241 | WAIT_TYPE status; |
| 4242 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4243 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4244 | Py_BEGIN_ALLOW_THREADS |
| 4245 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4246 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4247 | if (pid == -1) |
| 4248 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4249 | |
| 4250 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4251 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4252 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4253 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4254 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4255 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4256 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4257 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4258 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4259 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4260 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4261 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4262 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4263 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4264 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4265 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4266 | 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] | 4267 | #else |
| 4268 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4269 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4270 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4273 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4274 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4275 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4276 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4277 | 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] | 4278 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4279 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4280 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4281 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4282 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4283 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4284 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4285 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4286 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4287 | |
| 4288 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 4289 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4290 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4291 | v = PySequence_GetItem(args, 0); |
| 4292 | if (v == NULL) return NULL; |
| 4293 | |
| 4294 | if (PyUnicode_Check(v)) { |
| 4295 | arg_is_unicode = 1; |
| 4296 | } |
| 4297 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4298 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4299 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4300 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4301 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4302 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4303 | return posix_error_with_filename(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4304 | |
| 4305 | v = PyString_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4306 | if (arg_is_unicode) { |
| 4307 | PyObject *w; |
| 4308 | |
| 4309 | w = PyUnicode_FromEncodedObject(v, |
| 4310 | Py_FileSystemDefaultEncoding, |
| 4311 | "strict"); |
| 4312 | if (w != NULL) { |
| 4313 | Py_DECREF(v); |
| 4314 | v = w; |
| 4315 | } |
| 4316 | else { |
| 4317 | /* fall back to the original byte string, as |
| 4318 | discussed in patch #683592 */ |
| 4319 | PyErr_Clear(); |
| 4320 | } |
| 4321 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4322 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4323 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4324 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4325 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4326 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4327 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4328 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4329 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4330 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4331 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4332 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4333 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4334 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4335 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4336 | } |
| 4337 | #endif /* HAVE_SYMLINK */ |
| 4338 | |
| 4339 | |
| 4340 | #ifdef HAVE_TIMES |
| 4341 | #ifndef HZ |
| 4342 | #define HZ 60 /* Universal constant :-) */ |
| 4343 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4344 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4345 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4346 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4347 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4348 | { |
| 4349 | ULONG value = 0; |
| 4350 | |
| 4351 | Py_BEGIN_ALLOW_THREADS |
| 4352 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4353 | Py_END_ALLOW_THREADS |
| 4354 | |
| 4355 | return value; |
| 4356 | } |
| 4357 | |
| 4358 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4359 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4360 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4361 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4362 | return Py_BuildValue("ddddd", |
| 4363 | (double)0 /* t.tms_utime / HZ */, |
| 4364 | (double)0 /* t.tms_stime / HZ */, |
| 4365 | (double)0 /* t.tms_cutime / HZ */, |
| 4366 | (double)0 /* t.tms_cstime / HZ */, |
| 4367 | (double)system_uptime() / 1000); |
| 4368 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4369 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4370 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4371 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4372 | { |
| 4373 | struct tms t; |
| 4374 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4375 | errno = 0; |
| 4376 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4377 | if (c == (clock_t) -1) |
| 4378 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4379 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4380 | (double)t.tms_utime / HZ, |
| 4381 | (double)t.tms_stime / HZ, |
| 4382 | (double)t.tms_cutime / HZ, |
| 4383 | (double)t.tms_cstime / HZ, |
| 4384 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4385 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4386 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4387 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4388 | |
| 4389 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4390 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4391 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4392 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4393 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4394 | { |
| 4395 | FILETIME create, exit, kernel, user; |
| 4396 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4397 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4398 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4399 | /* The fields of a FILETIME structure are the hi and lo part |
| 4400 | of a 64-bit value expressed in 100 nanosecond units. |
| 4401 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4402 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4403 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4404 | return Py_BuildValue( |
| 4405 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4406 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4407 | kernel.dwLowDateTime*1e-7), |
| 4408 | (double)(user.dwHighDateTime*429.4967296 + |
| 4409 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4410 | (double)0, |
| 4411 | (double)0, |
| 4412 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4413 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4414 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4415 | |
| 4416 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4417 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4418 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4419 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4420 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4421 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4422 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4423 | #ifdef HAVE_GETSID |
| 4424 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4425 | "getsid(pid) -> sid\n\n\ |
| 4426 | Call the system call getsid()."); |
| 4427 | |
| 4428 | static PyObject * |
| 4429 | posix_getsid(PyObject *self, PyObject *args) |
| 4430 | { |
| 4431 | int pid, sid; |
| 4432 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4433 | return NULL; |
| 4434 | sid = getsid(pid); |
| 4435 | if (sid < 0) |
| 4436 | return posix_error(); |
| 4437 | return PyInt_FromLong((long)sid); |
| 4438 | } |
| 4439 | #endif /* HAVE_GETSID */ |
| 4440 | |
| 4441 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4442 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4443 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4444 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4445 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4446 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4447 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4448 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4449 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4450 | if (setsid() < 0) |
| 4451 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4452 | Py_INCREF(Py_None); |
| 4453 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4454 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4455 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4456 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4457 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4458 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4459 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4460 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4461 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4462 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4463 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4464 | { |
| 4465 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4466 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4467 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4468 | if (setpgid(pid, pgrp) < 0) |
| 4469 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4470 | Py_INCREF(Py_None); |
| 4471 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4472 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4473 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4474 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4475 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4476 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4477 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4478 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4479 | 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] | 4480 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4481 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4482 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4483 | { |
| 4484 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4485 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4486 | return NULL; |
| 4487 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4488 | if (pgid < 0) |
| 4489 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4490 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4491 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4492 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4493 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4494 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4495 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4496 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4497 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4498 | 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] | 4499 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4500 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4501 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4502 | { |
| 4503 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4504 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4505 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4506 | if (tcsetpgrp(fd, pgid) < 0) |
| 4507 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4508 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4509 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4510 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4511 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4512 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4513 | /* Functions acting on file descriptors */ |
| 4514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4515 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4516 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4517 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4518 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4519 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4520 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4521 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4522 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4523 | int flag; |
| 4524 | int mode = 0777; |
| 4525 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4526 | |
| 4527 | #ifdef MS_WINDOWS |
| 4528 | if (unicode_file_names()) { |
| 4529 | PyUnicodeObject *po; |
| 4530 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4531 | Py_BEGIN_ALLOW_THREADS |
| 4532 | /* PyUnicode_AS_UNICODE OK without thread |
| 4533 | lock as it is a simple dereference. */ |
| 4534 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4535 | Py_END_ALLOW_THREADS |
| 4536 | if (fd < 0) |
| 4537 | return posix_error(); |
| 4538 | return PyInt_FromLong((long)fd); |
| 4539 | } |
| 4540 | /* Drop the argument parsing error as narrow strings |
| 4541 | are also valid. */ |
| 4542 | PyErr_Clear(); |
| 4543 | } |
| 4544 | #endif |
| 4545 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4546 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4547 | Py_FileSystemDefaultEncoding, &file, |
| 4548 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4549 | return NULL; |
| 4550 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4551 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4552 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4553 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4554 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4555 | return posix_error_with_allocated_filename(file); |
| 4556 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4557 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4560 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4561 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4562 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4563 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4564 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4565 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4566 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4567 | { |
| 4568 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4569 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4570 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4571 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4572 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4573 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4574 | if (res < 0) |
| 4575 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4576 | Py_INCREF(Py_None); |
| 4577 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4581 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4582 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4583 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4584 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4585 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4586 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4587 | { |
| 4588 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4589 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4590 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4591 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4592 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4593 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4594 | if (fd < 0) |
| 4595 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4596 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4599 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4600 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4601 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4602 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4603 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4604 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4605 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4606 | { |
| 4607 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4608 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4609 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4610 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4611 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4612 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4613 | if (res < 0) |
| 4614 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4615 | Py_INCREF(Py_None); |
| 4616 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4620 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4621 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4622 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4623 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4624 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4625 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4626 | { |
| 4627 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4628 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4629 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4630 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4631 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4632 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4633 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4634 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4635 | return NULL; |
| 4636 | #ifdef SEEK_SET |
| 4637 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4638 | switch (how) { |
| 4639 | case 0: how = SEEK_SET; break; |
| 4640 | case 1: how = SEEK_CUR; break; |
| 4641 | case 2: how = SEEK_END; break; |
| 4642 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4643 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4644 | |
| 4645 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4646 | pos = PyInt_AsLong(posobj); |
| 4647 | #else |
| 4648 | pos = PyLong_Check(posobj) ? |
| 4649 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4650 | #endif |
| 4651 | if (PyErr_Occurred()) |
| 4652 | return NULL; |
| 4653 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4654 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4655 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4656 | res = _lseeki64(fd, pos, how); |
| 4657 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4658 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4659 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4660 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4661 | if (res < 0) |
| 4662 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4663 | |
| 4664 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4665 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4666 | #else |
| 4667 | return PyLong_FromLongLong(res); |
| 4668 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4672 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4673 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4674 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4675 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4676 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4677 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4678 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4679 | int fd, size; |
| 4680 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4681 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4682 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4683 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 4684 | if (size < 0) { |
| 4685 | errno = EINVAL; |
| 4686 | return posix_error(); |
| 4687 | } |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4688 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4689 | if (buffer == NULL) |
| 4690 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4691 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4692 | n = read(fd, PyBytes_AsString(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4693 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4694 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4695 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4696 | return posix_error(); |
| 4697 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4698 | if (n != size) |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4699 | PyBytes_Resize(buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4700 | return buffer; |
| 4701 | } |
| 4702 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4703 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4704 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4705 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4706 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4707 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4708 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4709 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4710 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4711 | int fd; |
| 4712 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4713 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4714 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4715 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4716 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4717 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4718 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4719 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4720 | if (size < 0) |
| 4721 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4722 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4726 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4727 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4728 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4729 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4730 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4731 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4732 | { |
| 4733 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4734 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4735 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4736 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4737 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 4738 | #ifdef __VMS |
| 4739 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 4740 | fsync(fd); |
| 4741 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4742 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4743 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4744 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4745 | if (res != 0) { |
| 4746 | #ifdef MS_WINDOWS |
| 4747 | return win32_error("fstat", NULL); |
| 4748 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4749 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4750 | #endif |
| 4751 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4752 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4753 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4756 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4757 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4758 | 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] | 4759 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4760 | |
| 4761 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4762 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4763 | { |
| 4764 | int fd; |
| 4765 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4766 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4767 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4768 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4769 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4770 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4771 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4772 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4773 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4774 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4775 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4776 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4777 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4778 | #if defined(PYOS_OS2) |
| 4779 | HFILE read, write; |
| 4780 | APIRET rc; |
| 4781 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4782 | Py_BEGIN_ALLOW_THREADS |
| 4783 | rc = DosCreatePipe( &read, &write, 4096); |
| 4784 | Py_END_ALLOW_THREADS |
| 4785 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4786 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4787 | |
| 4788 | return Py_BuildValue("(ii)", read, write); |
| 4789 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4790 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4791 | int fds[2]; |
| 4792 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4793 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4794 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4795 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4796 | if (res != 0) |
| 4797 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4798 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4799 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4800 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4801 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4802 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4803 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4804 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4805 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4806 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4807 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4808 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4809 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4810 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4811 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4812 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4813 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4814 | #endif /* HAVE_PIPE */ |
| 4815 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4816 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4817 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4818 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4819 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4820 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4821 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4822 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4823 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4824 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4825 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4826 | int mode = 0666; |
| 4827 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4828 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4829 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4830 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4831 | res = mkfifo(filename, mode); |
| 4832 | Py_END_ALLOW_THREADS |
| 4833 | if (res < 0) |
| 4834 | return posix_error(); |
| 4835 | Py_INCREF(Py_None); |
| 4836 | return Py_None; |
| 4837 | } |
| 4838 | #endif |
| 4839 | |
| 4840 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 4841 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4842 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4843 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4844 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 4845 | named filename. mode specifies both the permissions to use and the\n\ |
| 4846 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 4847 | 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] | 4848 | device defines the newly created device special file (probably using\n\ |
| 4849 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4850 | |
| 4851 | |
| 4852 | static PyObject * |
| 4853 | posix_mknod(PyObject *self, PyObject *args) |
| 4854 | { |
| 4855 | char *filename; |
| 4856 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4857 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4858 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 4859 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4860 | return NULL; |
| 4861 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4862 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4863 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4864 | if (res < 0) |
| 4865 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4866 | Py_INCREF(Py_None); |
| 4867 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4868 | } |
| 4869 | #endif |
| 4870 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4871 | #ifdef HAVE_DEVICE_MACROS |
| 4872 | PyDoc_STRVAR(posix_major__doc__, |
| 4873 | "major(device) -> major number\n\ |
| 4874 | Extracts a device major number from a raw device number."); |
| 4875 | |
| 4876 | static PyObject * |
| 4877 | posix_major(PyObject *self, PyObject *args) |
| 4878 | { |
| 4879 | int device; |
| 4880 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 4881 | return NULL; |
| 4882 | return PyInt_FromLong((long)major(device)); |
| 4883 | } |
| 4884 | |
| 4885 | PyDoc_STRVAR(posix_minor__doc__, |
| 4886 | "minor(device) -> minor number\n\ |
| 4887 | Extracts a device minor number from a raw device number."); |
| 4888 | |
| 4889 | static PyObject * |
| 4890 | posix_minor(PyObject *self, PyObject *args) |
| 4891 | { |
| 4892 | int device; |
| 4893 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 4894 | return NULL; |
| 4895 | return PyInt_FromLong((long)minor(device)); |
| 4896 | } |
| 4897 | |
| 4898 | PyDoc_STRVAR(posix_makedev__doc__, |
| 4899 | "makedev(major, minor) -> device number\n\ |
| 4900 | Composes a raw device number from the major and minor device numbers."); |
| 4901 | |
| 4902 | static PyObject * |
| 4903 | posix_makedev(PyObject *self, PyObject *args) |
| 4904 | { |
| 4905 | int major, minor; |
| 4906 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 4907 | return NULL; |
| 4908 | return PyInt_FromLong((long)makedev(major, minor)); |
| 4909 | } |
| 4910 | #endif /* device macros */ |
| 4911 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4912 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4913 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4914 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4915 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4916 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4917 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4918 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4919 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4920 | { |
| 4921 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4922 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4923 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4924 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4925 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4926 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4927 | return NULL; |
| 4928 | |
| 4929 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4930 | length = PyInt_AsLong(lenobj); |
| 4931 | #else |
| 4932 | length = PyLong_Check(lenobj) ? |
| 4933 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 4934 | #endif |
| 4935 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4936 | return NULL; |
| 4937 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4938 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4939 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4940 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4941 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4942 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4943 | return NULL; |
| 4944 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4945 | Py_INCREF(Py_None); |
| 4946 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4947 | } |
| 4948 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4949 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4950 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4951 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4952 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4953 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4954 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4955 | /* Save putenv() parameters as values here, so we can collect them when they |
| 4956 | * get re-set with another call for the same key. */ |
| 4957 | static PyObject *posix_putenv_garbage; |
| 4958 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4959 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4960 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4961 | { |
| 4962 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4963 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4964 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4965 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4966 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4967 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4968 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4969 | |
| 4970 | #if defined(PYOS_OS2) |
| 4971 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 4972 | APIRET rc; |
| 4973 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4974 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 4975 | if (rc != NO_ERROR) |
| 4976 | return os2_error(rc); |
| 4977 | |
| 4978 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 4979 | APIRET rc; |
| 4980 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4981 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 4982 | if (rc != NO_ERROR) |
| 4983 | return os2_error(rc); |
| 4984 | } else { |
| 4985 | #endif |
| 4986 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4987 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4988 | len = strlen(s1) + strlen(s2) + 2; |
| 4989 | /* len includes space for a trailing \0; the size arg to |
| 4990 | PyString_FromStringAndSize does not count that */ |
| 4991 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4992 | if (newstr == NULL) |
| 4993 | return PyErr_NoMemory(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4994 | newenv = PyString_AS_STRING(newstr); |
| 4995 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 4996 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 4997 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4998 | posix_error(); |
| 4999 | return NULL; |
| 5000 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5001 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5002 | * this will cause previous value to be collected. This has to |
| 5003 | * happen after the real putenv() call because the old value |
| 5004 | * was still accessible until then. */ |
| 5005 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5006 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5007 | /* really not much we can do; just leak */ |
| 5008 | PyErr_Clear(); |
| 5009 | } |
| 5010 | else { |
| 5011 | Py_DECREF(newstr); |
| 5012 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5013 | |
| 5014 | #if defined(PYOS_OS2) |
| 5015 | } |
| 5016 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5017 | Py_INCREF(Py_None); |
| 5018 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5019 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5020 | #endif /* putenv */ |
| 5021 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5022 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5023 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5024 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5026 | |
| 5027 | static PyObject * |
| 5028 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5029 | { |
| 5030 | char *s1; |
| 5031 | |
| 5032 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5033 | return NULL; |
| 5034 | |
| 5035 | unsetenv(s1); |
| 5036 | |
| 5037 | /* Remove the key from posix_putenv_garbage; |
| 5038 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5039 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5040 | * old value was still accessible until then. |
| 5041 | */ |
| 5042 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5043 | PyTuple_GET_ITEM(args, 0))) { |
| 5044 | /* really not much we can do; just leak */ |
| 5045 | PyErr_Clear(); |
| 5046 | } |
| 5047 | |
| 5048 | Py_INCREF(Py_None); |
| 5049 | return Py_None; |
| 5050 | } |
| 5051 | #endif /* unsetenv */ |
| 5052 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5053 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5054 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5055 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5056 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5057 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5058 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5059 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5060 | { |
| 5061 | int code; |
| 5062 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5063 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5064 | return NULL; |
| 5065 | message = strerror(code); |
| 5066 | if (message == NULL) { |
| 5067 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5068 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5069 | return NULL; |
| 5070 | } |
| 5071 | return PyString_FromString(message); |
| 5072 | } |
| 5073 | #endif /* strerror */ |
| 5074 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5075 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5076 | #ifdef HAVE_SYS_WAIT_H |
| 5077 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5078 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5079 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5080 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5081 | 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] | 5082 | |
| 5083 | static PyObject * |
| 5084 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5085 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5086 | WAIT_TYPE status; |
| 5087 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5088 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5089 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5090 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5091 | |
| 5092 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5093 | } |
| 5094 | #endif /* WCOREDUMP */ |
| 5095 | |
| 5096 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5097 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5098 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5099 | 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] | 5100 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5101 | |
| 5102 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5103 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5104 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5105 | WAIT_TYPE status; |
| 5106 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5107 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5108 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5109 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5110 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5111 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5112 | } |
| 5113 | #endif /* WIFCONTINUED */ |
| 5114 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5115 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5116 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5117 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5118 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5119 | |
| 5120 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5121 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5122 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5123 | WAIT_TYPE status; |
| 5124 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5125 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5126 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5127 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5128 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5129 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5130 | } |
| 5131 | #endif /* WIFSTOPPED */ |
| 5132 | |
| 5133 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5134 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5135 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5136 | 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] | 5137 | |
| 5138 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5139 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5140 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5141 | WAIT_TYPE status; |
| 5142 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5143 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5144 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5145 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5146 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5147 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5148 | } |
| 5149 | #endif /* WIFSIGNALED */ |
| 5150 | |
| 5151 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5152 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5153 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5154 | 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] | 5155 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5156 | |
| 5157 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5158 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5159 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5160 | WAIT_TYPE status; |
| 5161 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5162 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5163 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5164 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5165 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5166 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5167 | } |
| 5168 | #endif /* WIFEXITED */ |
| 5169 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5170 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5171 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5172 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5173 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5174 | |
| 5175 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5176 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5177 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5178 | WAIT_TYPE status; |
| 5179 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5180 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5181 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5182 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5183 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5184 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5185 | } |
| 5186 | #endif /* WEXITSTATUS */ |
| 5187 | |
| 5188 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5189 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5190 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5191 | 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] | 5192 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5193 | |
| 5194 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5195 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5196 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5197 | WAIT_TYPE status; |
| 5198 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5199 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5200 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5201 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5202 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5203 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5204 | } |
| 5205 | #endif /* WTERMSIG */ |
| 5206 | |
| 5207 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5208 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5209 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5210 | Return the signal that stopped the process that provided\n\ |
| 5211 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5212 | |
| 5213 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5214 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5215 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5216 | WAIT_TYPE status; |
| 5217 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5218 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5219 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5220 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5221 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5222 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5223 | } |
| 5224 | #endif /* WSTOPSIG */ |
| 5225 | |
| 5226 | #endif /* HAVE_SYS_WAIT_H */ |
| 5227 | |
| 5228 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5229 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5230 | #ifdef _SCO_DS |
| 5231 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5232 | needed definitions in sys/statvfs.h */ |
| 5233 | #define _SVID3 |
| 5234 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5235 | #include <sys/statvfs.h> |
| 5236 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5237 | static PyObject* |
| 5238 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5239 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5240 | if (v == NULL) |
| 5241 | return NULL; |
| 5242 | |
| 5243 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5244 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5245 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5246 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5247 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5248 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5249 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5250 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5251 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5252 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5253 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5254 | #else |
| 5255 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5256 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5257 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5258 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5259 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5260 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5261 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5262 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5263 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5264 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5265 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5266 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5267 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5268 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5269 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5270 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5271 | #endif |
| 5272 | |
| 5273 | return v; |
| 5274 | } |
| 5275 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5276 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5277 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5278 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5279 | |
| 5280 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5281 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5282 | { |
| 5283 | int fd, res; |
| 5284 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5285 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5286 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5287 | return NULL; |
| 5288 | Py_BEGIN_ALLOW_THREADS |
| 5289 | res = fstatvfs(fd, &st); |
| 5290 | Py_END_ALLOW_THREADS |
| 5291 | if (res != 0) |
| 5292 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5293 | |
| 5294 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5295 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5296 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5297 | |
| 5298 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5299 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5300 | #include <sys/statvfs.h> |
| 5301 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5302 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5303 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5304 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5305 | |
| 5306 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5307 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5308 | { |
| 5309 | char *path; |
| 5310 | int res; |
| 5311 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5312 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5313 | return NULL; |
| 5314 | Py_BEGIN_ALLOW_THREADS |
| 5315 | res = statvfs(path, &st); |
| 5316 | Py_END_ALLOW_THREADS |
| 5317 | if (res != 0) |
| 5318 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5319 | |
| 5320 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5321 | } |
| 5322 | #endif /* HAVE_STATVFS */ |
| 5323 | |
| 5324 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5325 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5326 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5327 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5328 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 5329 | The directory and a prefix may be specified as strings; they may be omitted\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5330 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5331 | |
| 5332 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5333 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5334 | { |
| 5335 | PyObject *result = NULL; |
| 5336 | char *dir = NULL; |
| 5337 | char *pfx = NULL; |
| 5338 | char *name; |
| 5339 | |
| 5340 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5341 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5342 | |
| 5343 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5344 | "tempnam is a potential security risk to your program") < 0) |
| 5345 | return NULL; |
| 5346 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5347 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5348 | name = _tempnam(dir, pfx); |
| 5349 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5350 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5351 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5352 | if (name == NULL) |
| 5353 | return PyErr_NoMemory(); |
| 5354 | result = PyString_FromString(name); |
| 5355 | free(name); |
| 5356 | return result; |
| 5357 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5358 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5359 | |
| 5360 | |
| 5361 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5362 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5363 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5364 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5365 | |
| 5366 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5367 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5368 | { |
| 5369 | FILE *fp; |
| 5370 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5371 | fp = tmpfile(); |
| 5372 | if (fp == NULL) |
| 5373 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 5374 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5375 | } |
| 5376 | #endif |
| 5377 | |
| 5378 | |
| 5379 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5380 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5381 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5382 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5383 | |
| 5384 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5385 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5386 | { |
| 5387 | char buffer[L_tmpnam]; |
| 5388 | char *name; |
| 5389 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5390 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5391 | "tmpnam is a potential security risk to your program") < 0) |
| 5392 | return NULL; |
| 5393 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5394 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5395 | name = tmpnam_r(buffer); |
| 5396 | #else |
| 5397 | name = tmpnam(buffer); |
| 5398 | #endif |
| 5399 | if (name == NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5400 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5401 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5402 | "unexpected NULL from tmpnam_r" |
| 5403 | #else |
| 5404 | "unexpected NULL from tmpnam" |
| 5405 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5406 | ); |
| 5407 | PyErr_SetObject(PyExc_OSError, err); |
| 5408 | Py_XDECREF(err); |
| 5409 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5410 | } |
| 5411 | return PyString_FromString(buffer); |
| 5412 | } |
| 5413 | #endif |
| 5414 | |
| 5415 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5416 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5417 | * It maps strings representing configuration variable names to |
| 5418 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5419 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5420 | * rarely-used constants. There are three separate tables that use |
| 5421 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5422 | * |
| 5423 | * This code is always included, even if none of the interfaces that |
| 5424 | * need it are included. The #if hackery needed to avoid it would be |
| 5425 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5426 | */ |
| 5427 | struct constdef { |
| 5428 | char *name; |
| 5429 | long value; |
| 5430 | }; |
| 5431 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5432 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5433 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 5434 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5435 | { |
| 5436 | if (PyInt_Check(arg)) { |
| 5437 | *valuep = PyInt_AS_LONG(arg); |
| 5438 | return 1; |
| 5439 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5440 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5441 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5442 | size_t lo = 0; |
| 5443 | size_t mid; |
| 5444 | size_t hi = tablesize; |
| 5445 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5446 | const char *confname; |
| 5447 | Py_ssize_t namelen; |
| 5448 | if (PyObject_AsCharBuffer(arg, &confname, &namelen) < 0) { |
| 5449 | PyErr_SetString(PyExc_TypeError, |
| 5450 | "configuration names must be strings or integers"); |
| 5451 | return 0; |
| 5452 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5453 | while (lo < hi) { |
| 5454 | mid = (lo + hi) / 2; |
| 5455 | cmp = strcmp(confname, table[mid].name); |
| 5456 | if (cmp < 0) |
| 5457 | hi = mid; |
| 5458 | else if (cmp > 0) |
| 5459 | lo = mid + 1; |
| 5460 | else { |
| 5461 | *valuep = table[mid].value; |
| 5462 | return 1; |
| 5463 | } |
| 5464 | } |
| 5465 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5466 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5467 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5468 | } |
| 5469 | |
| 5470 | |
| 5471 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5472 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5473 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5474 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5475 | #endif |
| 5476 | #ifdef _PC_ABI_ASYNC_IO |
| 5477 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5478 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5479 | #ifdef _PC_ASYNC_IO |
| 5480 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5481 | #endif |
| 5482 | #ifdef _PC_CHOWN_RESTRICTED |
| 5483 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5484 | #endif |
| 5485 | #ifdef _PC_FILESIZEBITS |
| 5486 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5487 | #endif |
| 5488 | #ifdef _PC_LAST |
| 5489 | {"PC_LAST", _PC_LAST}, |
| 5490 | #endif |
| 5491 | #ifdef _PC_LINK_MAX |
| 5492 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5493 | #endif |
| 5494 | #ifdef _PC_MAX_CANON |
| 5495 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5496 | #endif |
| 5497 | #ifdef _PC_MAX_INPUT |
| 5498 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5499 | #endif |
| 5500 | #ifdef _PC_NAME_MAX |
| 5501 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5502 | #endif |
| 5503 | #ifdef _PC_NO_TRUNC |
| 5504 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5505 | #endif |
| 5506 | #ifdef _PC_PATH_MAX |
| 5507 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5508 | #endif |
| 5509 | #ifdef _PC_PIPE_BUF |
| 5510 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5511 | #endif |
| 5512 | #ifdef _PC_PRIO_IO |
| 5513 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5514 | #endif |
| 5515 | #ifdef _PC_SOCK_MAXBUF |
| 5516 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5517 | #endif |
| 5518 | #ifdef _PC_SYNC_IO |
| 5519 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5520 | #endif |
| 5521 | #ifdef _PC_VDISABLE |
| 5522 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5523 | #endif |
| 5524 | }; |
| 5525 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5526 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5527 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5528 | { |
| 5529 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5530 | sizeof(posix_constants_pathconf) |
| 5531 | / sizeof(struct constdef)); |
| 5532 | } |
| 5533 | #endif |
| 5534 | |
| 5535 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5536 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5537 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5538 | 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] | 5539 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5540 | |
| 5541 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5542 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5543 | { |
| 5544 | PyObject *result = NULL; |
| 5545 | int name, fd; |
| 5546 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5547 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5548 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5549 | long limit; |
| 5550 | |
| 5551 | errno = 0; |
| 5552 | limit = fpathconf(fd, name); |
| 5553 | if (limit == -1 && errno != 0) |
| 5554 | posix_error(); |
| 5555 | else |
| 5556 | result = PyInt_FromLong(limit); |
| 5557 | } |
| 5558 | return result; |
| 5559 | } |
| 5560 | #endif |
| 5561 | |
| 5562 | |
| 5563 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5564 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5565 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5566 | 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] | 5567 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5568 | |
| 5569 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5570 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5571 | { |
| 5572 | PyObject *result = NULL; |
| 5573 | int name; |
| 5574 | char *path; |
| 5575 | |
| 5576 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5577 | conv_path_confname, &name)) { |
| 5578 | long limit; |
| 5579 | |
| 5580 | errno = 0; |
| 5581 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5582 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5583 | if (errno == EINVAL) |
| 5584 | /* could be a path or name problem */ |
| 5585 | posix_error(); |
| 5586 | else |
| 5587 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5588 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5589 | else |
| 5590 | result = PyInt_FromLong(limit); |
| 5591 | } |
| 5592 | return result; |
| 5593 | } |
| 5594 | #endif |
| 5595 | |
| 5596 | #ifdef HAVE_CONFSTR |
| 5597 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5598 | #ifdef _CS_ARCHITECTURE |
| 5599 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5600 | #endif |
| 5601 | #ifdef _CS_HOSTNAME |
| 5602 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5603 | #endif |
| 5604 | #ifdef _CS_HW_PROVIDER |
| 5605 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5606 | #endif |
| 5607 | #ifdef _CS_HW_SERIAL |
| 5608 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5609 | #endif |
| 5610 | #ifdef _CS_INITTAB_NAME |
| 5611 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5612 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5613 | #ifdef _CS_LFS64_CFLAGS |
| 5614 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5615 | #endif |
| 5616 | #ifdef _CS_LFS64_LDFLAGS |
| 5617 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5618 | #endif |
| 5619 | #ifdef _CS_LFS64_LIBS |
| 5620 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5621 | #endif |
| 5622 | #ifdef _CS_LFS64_LINTFLAGS |
| 5623 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5624 | #endif |
| 5625 | #ifdef _CS_LFS_CFLAGS |
| 5626 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5627 | #endif |
| 5628 | #ifdef _CS_LFS_LDFLAGS |
| 5629 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5630 | #endif |
| 5631 | #ifdef _CS_LFS_LIBS |
| 5632 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5633 | #endif |
| 5634 | #ifdef _CS_LFS_LINTFLAGS |
| 5635 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5636 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5637 | #ifdef _CS_MACHINE |
| 5638 | {"CS_MACHINE", _CS_MACHINE}, |
| 5639 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5640 | #ifdef _CS_PATH |
| 5641 | {"CS_PATH", _CS_PATH}, |
| 5642 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5643 | #ifdef _CS_RELEASE |
| 5644 | {"CS_RELEASE", _CS_RELEASE}, |
| 5645 | #endif |
| 5646 | #ifdef _CS_SRPC_DOMAIN |
| 5647 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5648 | #endif |
| 5649 | #ifdef _CS_SYSNAME |
| 5650 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5651 | #endif |
| 5652 | #ifdef _CS_VERSION |
| 5653 | {"CS_VERSION", _CS_VERSION}, |
| 5654 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5655 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5656 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5657 | #endif |
| 5658 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5659 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5660 | #endif |
| 5661 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5662 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5663 | #endif |
| 5664 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5665 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5666 | #endif |
| 5667 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5668 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5669 | #endif |
| 5670 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5671 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5672 | #endif |
| 5673 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5674 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5675 | #endif |
| 5676 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5677 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5678 | #endif |
| 5679 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5680 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5681 | #endif |
| 5682 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5683 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5684 | #endif |
| 5685 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5686 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5687 | #endif |
| 5688 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5689 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5690 | #endif |
| 5691 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5692 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5693 | #endif |
| 5694 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5695 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5696 | #endif |
| 5697 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5698 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5699 | #endif |
| 5700 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5701 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5702 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5703 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5704 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5705 | #endif |
| 5706 | #ifdef _MIPS_CS_BASE |
| 5707 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5708 | #endif |
| 5709 | #ifdef _MIPS_CS_HOSTID |
| 5710 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5711 | #endif |
| 5712 | #ifdef _MIPS_CS_HW_NAME |
| 5713 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5714 | #endif |
| 5715 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5716 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5717 | #endif |
| 5718 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5719 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5720 | #endif |
| 5721 | #ifdef _MIPS_CS_OSREL_MIN |
| 5722 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5723 | #endif |
| 5724 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5725 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5726 | #endif |
| 5727 | #ifdef _MIPS_CS_OS_NAME |
| 5728 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5729 | #endif |
| 5730 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5731 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5732 | #endif |
| 5733 | #ifdef _MIPS_CS_PROCESSORS |
| 5734 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5735 | #endif |
| 5736 | #ifdef _MIPS_CS_SERIAL |
| 5737 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5738 | #endif |
| 5739 | #ifdef _MIPS_CS_VENDOR |
| 5740 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5741 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5742 | }; |
| 5743 | |
| 5744 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5745 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5746 | { |
| 5747 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5748 | sizeof(posix_constants_confstr) |
| 5749 | / sizeof(struct constdef)); |
| 5750 | } |
| 5751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5752 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5753 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5754 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5755 | |
| 5756 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5757 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5758 | { |
| 5759 | PyObject *result = NULL; |
| 5760 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5761 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5762 | |
| 5763 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5764 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5765 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5766 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5767 | len = confstr(name, buffer, sizeof(buffer)); |
| 5768 | if (len == 0) { |
| 5769 | if (errno) { |
| 5770 | posix_error(); |
| 5771 | } |
| 5772 | else { |
| 5773 | result = Py_None; |
| 5774 | Py_INCREF(Py_None); |
| 5775 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5776 | } |
| 5777 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5778 | if ((unsigned int)len >= sizeof(buffer)) { |
| 5779 | result = PyString_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5780 | if (result != NULL) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5781 | confstr(name, PyString_AS_STRING(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5782 | } |
| 5783 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5784 | result = PyString_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5785 | } |
| 5786 | } |
| 5787 | return result; |
| 5788 | } |
| 5789 | #endif |
| 5790 | |
| 5791 | |
| 5792 | #ifdef HAVE_SYSCONF |
| 5793 | static struct constdef posix_constants_sysconf[] = { |
| 5794 | #ifdef _SC_2_CHAR_TERM |
| 5795 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5796 | #endif |
| 5797 | #ifdef _SC_2_C_BIND |
| 5798 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5799 | #endif |
| 5800 | #ifdef _SC_2_C_DEV |
| 5801 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5802 | #endif |
| 5803 | #ifdef _SC_2_C_VERSION |
| 5804 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5805 | #endif |
| 5806 | #ifdef _SC_2_FORT_DEV |
| 5807 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5808 | #endif |
| 5809 | #ifdef _SC_2_FORT_RUN |
| 5810 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5811 | #endif |
| 5812 | #ifdef _SC_2_LOCALEDEF |
| 5813 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5814 | #endif |
| 5815 | #ifdef _SC_2_SW_DEV |
| 5816 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5817 | #endif |
| 5818 | #ifdef _SC_2_UPE |
| 5819 | {"SC_2_UPE", _SC_2_UPE}, |
| 5820 | #endif |
| 5821 | #ifdef _SC_2_VERSION |
| 5822 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5823 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5824 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5825 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5826 | #endif |
| 5827 | #ifdef _SC_ACL |
| 5828 | {"SC_ACL", _SC_ACL}, |
| 5829 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5830 | #ifdef _SC_AIO_LISTIO_MAX |
| 5831 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5832 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5833 | #ifdef _SC_AIO_MAX |
| 5834 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5835 | #endif |
| 5836 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5837 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5838 | #endif |
| 5839 | #ifdef _SC_ARG_MAX |
| 5840 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5841 | #endif |
| 5842 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5843 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5844 | #endif |
| 5845 | #ifdef _SC_ATEXIT_MAX |
| 5846 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5847 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5848 | #ifdef _SC_AUDIT |
| 5849 | {"SC_AUDIT", _SC_AUDIT}, |
| 5850 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5851 | #ifdef _SC_AVPHYS_PAGES |
| 5852 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5853 | #endif |
| 5854 | #ifdef _SC_BC_BASE_MAX |
| 5855 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5856 | #endif |
| 5857 | #ifdef _SC_BC_DIM_MAX |
| 5858 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5859 | #endif |
| 5860 | #ifdef _SC_BC_SCALE_MAX |
| 5861 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5862 | #endif |
| 5863 | #ifdef _SC_BC_STRING_MAX |
| 5864 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5865 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5866 | #ifdef _SC_CAP |
| 5867 | {"SC_CAP", _SC_CAP}, |
| 5868 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5869 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5870 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5871 | #endif |
| 5872 | #ifdef _SC_CHAR_BIT |
| 5873 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5874 | #endif |
| 5875 | #ifdef _SC_CHAR_MAX |
| 5876 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5877 | #endif |
| 5878 | #ifdef _SC_CHAR_MIN |
| 5879 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5880 | #endif |
| 5881 | #ifdef _SC_CHILD_MAX |
| 5882 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5883 | #endif |
| 5884 | #ifdef _SC_CLK_TCK |
| 5885 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 5886 | #endif |
| 5887 | #ifdef _SC_COHER_BLKSZ |
| 5888 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 5889 | #endif |
| 5890 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 5891 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 5892 | #endif |
| 5893 | #ifdef _SC_DCACHE_ASSOC |
| 5894 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5895 | #endif |
| 5896 | #ifdef _SC_DCACHE_BLKSZ |
| 5897 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5898 | #endif |
| 5899 | #ifdef _SC_DCACHE_LINESZ |
| 5900 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 5901 | #endif |
| 5902 | #ifdef _SC_DCACHE_SZ |
| 5903 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5904 | #endif |
| 5905 | #ifdef _SC_DCACHE_TBLKSZ |
| 5906 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5907 | #endif |
| 5908 | #ifdef _SC_DELAYTIMER_MAX |
| 5909 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5910 | #endif |
| 5911 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5912 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5913 | #endif |
| 5914 | #ifdef _SC_EXPR_NEST_MAX |
| 5915 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5916 | #endif |
| 5917 | #ifdef _SC_FSYNC |
| 5918 | {"SC_FSYNC", _SC_FSYNC}, |
| 5919 | #endif |
| 5920 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5921 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5922 | #endif |
| 5923 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5924 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5925 | #endif |
| 5926 | #ifdef _SC_ICACHE_ASSOC |
| 5927 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5928 | #endif |
| 5929 | #ifdef _SC_ICACHE_BLKSZ |
| 5930 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5931 | #endif |
| 5932 | #ifdef _SC_ICACHE_LINESZ |
| 5933 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5934 | #endif |
| 5935 | #ifdef _SC_ICACHE_SZ |
| 5936 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5937 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5938 | #ifdef _SC_INF |
| 5939 | {"SC_INF", _SC_INF}, |
| 5940 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5941 | #ifdef _SC_INT_MAX |
| 5942 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5943 | #endif |
| 5944 | #ifdef _SC_INT_MIN |
| 5945 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5946 | #endif |
| 5947 | #ifdef _SC_IOV_MAX |
| 5948 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5949 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5950 | #ifdef _SC_IP_SECOPTS |
| 5951 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5952 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5953 | #ifdef _SC_JOB_CONTROL |
| 5954 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5955 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5956 | #ifdef _SC_KERN_POINTERS |
| 5957 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5958 | #endif |
| 5959 | #ifdef _SC_KERN_SIM |
| 5960 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5961 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5962 | #ifdef _SC_LINE_MAX |
| 5963 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5964 | #endif |
| 5965 | #ifdef _SC_LOGIN_NAME_MAX |
| 5966 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5967 | #endif |
| 5968 | #ifdef _SC_LOGNAME_MAX |
| 5969 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5970 | #endif |
| 5971 | #ifdef _SC_LONG_BIT |
| 5972 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5973 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5974 | #ifdef _SC_MAC |
| 5975 | {"SC_MAC", _SC_MAC}, |
| 5976 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5977 | #ifdef _SC_MAPPED_FILES |
| 5978 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5979 | #endif |
| 5980 | #ifdef _SC_MAXPID |
| 5981 | {"SC_MAXPID", _SC_MAXPID}, |
| 5982 | #endif |
| 5983 | #ifdef _SC_MB_LEN_MAX |
| 5984 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5985 | #endif |
| 5986 | #ifdef _SC_MEMLOCK |
| 5987 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5988 | #endif |
| 5989 | #ifdef _SC_MEMLOCK_RANGE |
| 5990 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5991 | #endif |
| 5992 | #ifdef _SC_MEMORY_PROTECTION |
| 5993 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5994 | #endif |
| 5995 | #ifdef _SC_MESSAGE_PASSING |
| 5996 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5997 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5998 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5999 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6000 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6001 | #ifdef _SC_MQ_OPEN_MAX |
| 6002 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6003 | #endif |
| 6004 | #ifdef _SC_MQ_PRIO_MAX |
| 6005 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6006 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6007 | #ifdef _SC_NACLS_MAX |
| 6008 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6009 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6010 | #ifdef _SC_NGROUPS_MAX |
| 6011 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6012 | #endif |
| 6013 | #ifdef _SC_NL_ARGMAX |
| 6014 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6015 | #endif |
| 6016 | #ifdef _SC_NL_LANGMAX |
| 6017 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6018 | #endif |
| 6019 | #ifdef _SC_NL_MSGMAX |
| 6020 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6021 | #endif |
| 6022 | #ifdef _SC_NL_NMAX |
| 6023 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6024 | #endif |
| 6025 | #ifdef _SC_NL_SETMAX |
| 6026 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6027 | #endif |
| 6028 | #ifdef _SC_NL_TEXTMAX |
| 6029 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6030 | #endif |
| 6031 | #ifdef _SC_NPROCESSORS_CONF |
| 6032 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6033 | #endif |
| 6034 | #ifdef _SC_NPROCESSORS_ONLN |
| 6035 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6036 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6037 | #ifdef _SC_NPROC_CONF |
| 6038 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6039 | #endif |
| 6040 | #ifdef _SC_NPROC_ONLN |
| 6041 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6042 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6043 | #ifdef _SC_NZERO |
| 6044 | {"SC_NZERO", _SC_NZERO}, |
| 6045 | #endif |
| 6046 | #ifdef _SC_OPEN_MAX |
| 6047 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6048 | #endif |
| 6049 | #ifdef _SC_PAGESIZE |
| 6050 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6051 | #endif |
| 6052 | #ifdef _SC_PAGE_SIZE |
| 6053 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6054 | #endif |
| 6055 | #ifdef _SC_PASS_MAX |
| 6056 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6057 | #endif |
| 6058 | #ifdef _SC_PHYS_PAGES |
| 6059 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6060 | #endif |
| 6061 | #ifdef _SC_PII |
| 6062 | {"SC_PII", _SC_PII}, |
| 6063 | #endif |
| 6064 | #ifdef _SC_PII_INTERNET |
| 6065 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6066 | #endif |
| 6067 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6068 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6069 | #endif |
| 6070 | #ifdef _SC_PII_INTERNET_STREAM |
| 6071 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6072 | #endif |
| 6073 | #ifdef _SC_PII_OSI |
| 6074 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6075 | #endif |
| 6076 | #ifdef _SC_PII_OSI_CLTS |
| 6077 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6078 | #endif |
| 6079 | #ifdef _SC_PII_OSI_COTS |
| 6080 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6081 | #endif |
| 6082 | #ifdef _SC_PII_OSI_M |
| 6083 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6084 | #endif |
| 6085 | #ifdef _SC_PII_SOCKET |
| 6086 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6087 | #endif |
| 6088 | #ifdef _SC_PII_XTI |
| 6089 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6090 | #endif |
| 6091 | #ifdef _SC_POLL |
| 6092 | {"SC_POLL", _SC_POLL}, |
| 6093 | #endif |
| 6094 | #ifdef _SC_PRIORITIZED_IO |
| 6095 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6096 | #endif |
| 6097 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6098 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6099 | #endif |
| 6100 | #ifdef _SC_REALTIME_SIGNALS |
| 6101 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6102 | #endif |
| 6103 | #ifdef _SC_RE_DUP_MAX |
| 6104 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6105 | #endif |
| 6106 | #ifdef _SC_RTSIG_MAX |
| 6107 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6108 | #endif |
| 6109 | #ifdef _SC_SAVED_IDS |
| 6110 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6111 | #endif |
| 6112 | #ifdef _SC_SCHAR_MAX |
| 6113 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6114 | #endif |
| 6115 | #ifdef _SC_SCHAR_MIN |
| 6116 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6117 | #endif |
| 6118 | #ifdef _SC_SELECT |
| 6119 | {"SC_SELECT", _SC_SELECT}, |
| 6120 | #endif |
| 6121 | #ifdef _SC_SEMAPHORES |
| 6122 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6123 | #endif |
| 6124 | #ifdef _SC_SEM_NSEMS_MAX |
| 6125 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6126 | #endif |
| 6127 | #ifdef _SC_SEM_VALUE_MAX |
| 6128 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6129 | #endif |
| 6130 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6131 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6132 | #endif |
| 6133 | #ifdef _SC_SHRT_MAX |
| 6134 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6135 | #endif |
| 6136 | #ifdef _SC_SHRT_MIN |
| 6137 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6138 | #endif |
| 6139 | #ifdef _SC_SIGQUEUE_MAX |
| 6140 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6141 | #endif |
| 6142 | #ifdef _SC_SIGRT_MAX |
| 6143 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6144 | #endif |
| 6145 | #ifdef _SC_SIGRT_MIN |
| 6146 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6147 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6148 | #ifdef _SC_SOFTPOWER |
| 6149 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6150 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6151 | #ifdef _SC_SPLIT_CACHE |
| 6152 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6153 | #endif |
| 6154 | #ifdef _SC_SSIZE_MAX |
| 6155 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6156 | #endif |
| 6157 | #ifdef _SC_STACK_PROT |
| 6158 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6159 | #endif |
| 6160 | #ifdef _SC_STREAM_MAX |
| 6161 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6162 | #endif |
| 6163 | #ifdef _SC_SYNCHRONIZED_IO |
| 6164 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6165 | #endif |
| 6166 | #ifdef _SC_THREADS |
| 6167 | {"SC_THREADS", _SC_THREADS}, |
| 6168 | #endif |
| 6169 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6170 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6171 | #endif |
| 6172 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6173 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6174 | #endif |
| 6175 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6176 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6177 | #endif |
| 6178 | #ifdef _SC_THREAD_KEYS_MAX |
| 6179 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6180 | #endif |
| 6181 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6182 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6183 | #endif |
| 6184 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6185 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6186 | #endif |
| 6187 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6188 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6189 | #endif |
| 6190 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6191 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6192 | #endif |
| 6193 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6194 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6195 | #endif |
| 6196 | #ifdef _SC_THREAD_STACK_MIN |
| 6197 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6198 | #endif |
| 6199 | #ifdef _SC_THREAD_THREADS_MAX |
| 6200 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6201 | #endif |
| 6202 | #ifdef _SC_TIMERS |
| 6203 | {"SC_TIMERS", _SC_TIMERS}, |
| 6204 | #endif |
| 6205 | #ifdef _SC_TIMER_MAX |
| 6206 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6207 | #endif |
| 6208 | #ifdef _SC_TTY_NAME_MAX |
| 6209 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6210 | #endif |
| 6211 | #ifdef _SC_TZNAME_MAX |
| 6212 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6213 | #endif |
| 6214 | #ifdef _SC_T_IOV_MAX |
| 6215 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6216 | #endif |
| 6217 | #ifdef _SC_UCHAR_MAX |
| 6218 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6219 | #endif |
| 6220 | #ifdef _SC_UINT_MAX |
| 6221 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6222 | #endif |
| 6223 | #ifdef _SC_UIO_MAXIOV |
| 6224 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6225 | #endif |
| 6226 | #ifdef _SC_ULONG_MAX |
| 6227 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6228 | #endif |
| 6229 | #ifdef _SC_USHRT_MAX |
| 6230 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6231 | #endif |
| 6232 | #ifdef _SC_VERSION |
| 6233 | {"SC_VERSION", _SC_VERSION}, |
| 6234 | #endif |
| 6235 | #ifdef _SC_WORD_BIT |
| 6236 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6237 | #endif |
| 6238 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6239 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6240 | #endif |
| 6241 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6242 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6243 | #endif |
| 6244 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6245 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6246 | #endif |
| 6247 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6248 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6249 | #endif |
| 6250 | #ifdef _SC_XOPEN_CRYPT |
| 6251 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6252 | #endif |
| 6253 | #ifdef _SC_XOPEN_ENH_I18N |
| 6254 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6255 | #endif |
| 6256 | #ifdef _SC_XOPEN_LEGACY |
| 6257 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6258 | #endif |
| 6259 | #ifdef _SC_XOPEN_REALTIME |
| 6260 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6261 | #endif |
| 6262 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6263 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6264 | #endif |
| 6265 | #ifdef _SC_XOPEN_SHM |
| 6266 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6267 | #endif |
| 6268 | #ifdef _SC_XOPEN_UNIX |
| 6269 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6270 | #endif |
| 6271 | #ifdef _SC_XOPEN_VERSION |
| 6272 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6273 | #endif |
| 6274 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6275 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6276 | #endif |
| 6277 | #ifdef _SC_XOPEN_XPG2 |
| 6278 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6279 | #endif |
| 6280 | #ifdef _SC_XOPEN_XPG3 |
| 6281 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6282 | #endif |
| 6283 | #ifdef _SC_XOPEN_XPG4 |
| 6284 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6285 | #endif |
| 6286 | }; |
| 6287 | |
| 6288 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6289 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6290 | { |
| 6291 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6292 | sizeof(posix_constants_sysconf) |
| 6293 | / sizeof(struct constdef)); |
| 6294 | } |
| 6295 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6296 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6297 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6298 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6299 | |
| 6300 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6301 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6302 | { |
| 6303 | PyObject *result = NULL; |
| 6304 | int name; |
| 6305 | |
| 6306 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6307 | int value; |
| 6308 | |
| 6309 | errno = 0; |
| 6310 | value = sysconf(name); |
| 6311 | if (value == -1 && errno != 0) |
| 6312 | posix_error(); |
| 6313 | else |
| 6314 | result = PyInt_FromLong(value); |
| 6315 | } |
| 6316 | return result; |
| 6317 | } |
| 6318 | #endif |
| 6319 | |
| 6320 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6321 | /* This code is used to ensure that the tables of configuration value names |
| 6322 | * are in sorted order as required by conv_confname(), and also to build the |
| 6323 | * the exported dictionaries that are used to publish information about the |
| 6324 | * names available on the host platform. |
| 6325 | * |
| 6326 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6327 | * when used, even for platforms we're not able to test on. It also makes |
| 6328 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6329 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6330 | |
| 6331 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6332 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6333 | { |
| 6334 | const struct constdef *c1 = |
| 6335 | (const struct constdef *) v1; |
| 6336 | const struct constdef *c2 = |
| 6337 | (const struct constdef *) v2; |
| 6338 | |
| 6339 | return strcmp(c1->name, c2->name); |
| 6340 | } |
| 6341 | |
| 6342 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6343 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6344 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6345 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6346 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6347 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6348 | |
| 6349 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6350 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6351 | if (d == NULL) |
| 6352 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6353 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6354 | for (i=0; i < tablesize; ++i) { |
| 6355 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6356 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6357 | Py_XDECREF(o); |
| 6358 | Py_DECREF(d); |
| 6359 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6360 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6361 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6362 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6363 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6364 | } |
| 6365 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6366 | /* Return -1 on failure, 0 on success. */ |
| 6367 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6368 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6369 | { |
| 6370 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6371 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6372 | sizeof(posix_constants_pathconf) |
| 6373 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6374 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6375 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6376 | #endif |
| 6377 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6378 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6379 | sizeof(posix_constants_confstr) |
| 6380 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6381 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6382 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6383 | #endif |
| 6384 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6385 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6386 | sizeof(posix_constants_sysconf) |
| 6387 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6388 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6389 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6390 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6391 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6392 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6393 | |
| 6394 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6395 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6396 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6397 | 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] | 6398 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6399 | |
| 6400 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6401 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6402 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6403 | abort(); |
| 6404 | /*NOTREACHED*/ |
| 6405 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6406 | return NULL; |
| 6407 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6408 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6409 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6410 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6411 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6412 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6413 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6414 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6415 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6416 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6417 | application (if any) its extension is associated.\n\ |
| 6418 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6419 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6420 | \n\ |
| 6421 | startfile returns as soon as the associated application is launched.\n\ |
| 6422 | There is no option to wait for the application to close, and no way\n\ |
| 6423 | to retrieve the application's exit status.\n\ |
| 6424 | \n\ |
| 6425 | The filepath is relative to the current directory. If you want to use\n\ |
| 6426 | 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] | 6427 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6428 | |
| 6429 | static PyObject * |
| 6430 | win32_startfile(PyObject *self, PyObject *args) |
| 6431 | { |
| 6432 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6433 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6434 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6435 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6436 | if (unicode_file_names()) { |
| 6437 | PyObject *unipath, *woperation = NULL; |
| 6438 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6439 | &unipath, &operation)) { |
| 6440 | PyErr_Clear(); |
| 6441 | goto normal; |
| 6442 | } |
| 6443 | |
| 6444 | |
| 6445 | if (operation) { |
| 6446 | woperation = PyUnicode_DecodeASCII(operation, |
| 6447 | strlen(operation), NULL); |
| 6448 | if (!woperation) { |
| 6449 | PyErr_Clear(); |
| 6450 | operation = NULL; |
| 6451 | goto normal; |
| 6452 | } |
| 6453 | } |
| 6454 | |
| 6455 | Py_BEGIN_ALLOW_THREADS |
| 6456 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6457 | PyUnicode_AS_UNICODE(unipath), |
| 6458 | NULL, NULL, SW_SHOWNORMAL); |
| 6459 | Py_END_ALLOW_THREADS |
| 6460 | |
| 6461 | Py_XDECREF(woperation); |
| 6462 | if (rc <= (HINSTANCE)32) { |
| 6463 | PyObject *errval = win32_error_unicode("startfile", |
| 6464 | PyUnicode_AS_UNICODE(unipath)); |
| 6465 | return errval; |
| 6466 | } |
| 6467 | Py_INCREF(Py_None); |
| 6468 | return Py_None; |
| 6469 | } |
| 6470 | #endif |
| 6471 | |
| 6472 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6473 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 6474 | Py_FileSystemDefaultEncoding, &filepath, |
| 6475 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6476 | return NULL; |
| 6477 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6478 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6479 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6480 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6481 | if (rc <= (HINSTANCE)32) { |
| 6482 | PyObject *errval = win32_error("startfile", filepath); |
| 6483 | PyMem_Free(filepath); |
| 6484 | return errval; |
| 6485 | } |
| 6486 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6487 | Py_INCREF(Py_None); |
| 6488 | return Py_None; |
| 6489 | } |
| 6490 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6491 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6492 | #ifdef HAVE_GETLOADAVG |
| 6493 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6494 | "getloadavg() -> (float, float, float)\n\n\ |
| 6495 | Return the number of processes in the system run queue averaged over\n\ |
| 6496 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6497 | was unobtainable"); |
| 6498 | |
| 6499 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6500 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6501 | { |
| 6502 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6503 | if (getloadavg(loadavg, 3)!=3) { |
| 6504 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6505 | return NULL; |
| 6506 | } else |
| 6507 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6508 | } |
| 6509 | #endif |
| 6510 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6511 | #ifdef MS_WINDOWS |
| 6512 | |
| 6513 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6514 | "urandom(n) -> str\n\n\ |
| 6515 | Return a string of n random bytes suitable for cryptographic use."); |
| 6516 | |
| 6517 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6518 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6519 | DWORD dwFlags ); |
| 6520 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6521 | BYTE *pbBuffer ); |
| 6522 | |
| 6523 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 6524 | static HCRYPTPROV hCryptProv = 0; |
| 6525 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6526 | static PyObject* |
| 6527 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6528 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6529 | int howMany; |
| 6530 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6531 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6532 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6533 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6534 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6535 | if (howMany < 0) |
| 6536 | return PyErr_Format(PyExc_ValueError, |
| 6537 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6538 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6539 | if (hCryptProv == 0) { |
| 6540 | HINSTANCE hAdvAPI32 = NULL; |
| 6541 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6542 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6543 | /* Obtain handle to the DLL containing CryptoAPI |
| 6544 | This should not fail */ |
| 6545 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6546 | if(hAdvAPI32 == NULL) |
| 6547 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6548 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6549 | /* Obtain pointers to the CryptoAPI functions |
| 6550 | This will fail on some early versions of Win95 */ |
| 6551 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6552 | hAdvAPI32, |
| 6553 | "CryptAcquireContextA"); |
| 6554 | if (pCryptAcquireContext == NULL) |
| 6555 | return PyErr_Format(PyExc_NotImplementedError, |
| 6556 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6557 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6558 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6559 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6560 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6561 | return PyErr_Format(PyExc_NotImplementedError, |
| 6562 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6563 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6564 | /* Acquire context */ |
| 6565 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6566 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6567 | return win32_error("CryptAcquireContext", NULL); |
| 6568 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6569 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6570 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6571 | result = PyString_FromStringAndSize(NULL, howMany); |
| 6572 | if (result != NULL) { |
| 6573 | /* Get random data */ |
| 6574 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 6575 | PyString_AS_STRING(result))) { |
| 6576 | Py_DECREF(result); |
| 6577 | return win32_error("CryptGenRandom", NULL); |
| 6578 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6579 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6580 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6581 | } |
| 6582 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6583 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6584 | #ifdef __VMS |
| 6585 | /* Use openssl random routine */ |
| 6586 | #include <openssl/rand.h> |
| 6587 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6588 | "urandom(n) -> str\n\n\ |
| 6589 | Return a string of n random bytes suitable for cryptographic use."); |
| 6590 | |
| 6591 | static PyObject* |
| 6592 | vms_urandom(PyObject *self, PyObject *args) |
| 6593 | { |
| 6594 | int howMany; |
| 6595 | PyObject* result; |
| 6596 | |
| 6597 | /* Read arguments */ |
| 6598 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6599 | return NULL; |
| 6600 | if (howMany < 0) |
| 6601 | return PyErr_Format(PyExc_ValueError, |
| 6602 | "negative argument not allowed"); |
| 6603 | |
| 6604 | /* Allocate bytes */ |
| 6605 | result = PyString_FromStringAndSize(NULL, howMany); |
| 6606 | if (result != NULL) { |
| 6607 | /* Get random data */ |
| 6608 | if (RAND_pseudo_bytes((unsigned char*) |
| 6609 | PyString_AS_STRING(result), |
| 6610 | howMany) < 0) { |
| 6611 | Py_DECREF(result); |
| 6612 | return PyErr_Format(PyExc_ValueError, |
| 6613 | "RAND_pseudo_bytes"); |
| 6614 | } |
| 6615 | } |
| 6616 | return result; |
| 6617 | } |
| 6618 | #endif |
| 6619 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6620 | static PyMethodDef posix_methods[] = { |
| 6621 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6622 | #ifdef HAVE_TTYNAME |
| 6623 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6624 | #endif |
| 6625 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6626 | #ifdef HAVE_CHFLAGS |
| 6627 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 6628 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6629 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6630 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6631 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6632 | #endif /* HAVE_CHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6633 | #ifdef HAVE_LCHFLAGS |
| 6634 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 6635 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6636 | #ifdef HAVE_LCHOWN |
| 6637 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6638 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6639 | #ifdef HAVE_CHROOT |
| 6640 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6641 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6642 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6643 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6644 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6645 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6646 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6647 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6648 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6649 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6650 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6651 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6652 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6653 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6654 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6655 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6656 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6657 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6658 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6659 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6660 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6661 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6662 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6663 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6664 | {"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] | 6665 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6666 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6667 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6668 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6669 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6670 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6671 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6672 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6673 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6674 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6675 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6676 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6677 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6678 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6679 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6680 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6681 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6682 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6683 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6684 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6685 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6686 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6687 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6688 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6689 | #if defined(PYOS_OS2) |
| 6690 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 6691 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 6692 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6693 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6694 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6695 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6696 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6697 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6698 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6699 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6700 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6701 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6702 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6703 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6704 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6705 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6706 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6707 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6708 | #endif /* HAVE_GETEGID */ |
| 6709 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6710 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6711 | #endif /* HAVE_GETEUID */ |
| 6712 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6713 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6714 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6715 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6716 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6717 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6718 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6719 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6720 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6721 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6722 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6723 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6724 | #endif /* HAVE_GETPPID */ |
| 6725 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6726 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6727 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6728 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6729 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6730 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6731 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6732 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6733 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6734 | #ifdef HAVE_KILLPG |
| 6735 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6736 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6737 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6738 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6739 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6740 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6741 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6742 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6743 | #ifdef HAVE_SETEUID |
| 6744 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6745 | #endif /* HAVE_SETEUID */ |
| 6746 | #ifdef HAVE_SETEGID |
| 6747 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6748 | #endif /* HAVE_SETEGID */ |
| 6749 | #ifdef HAVE_SETREUID |
| 6750 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6751 | #endif /* HAVE_SETREUID */ |
| 6752 | #ifdef HAVE_SETREGID |
| 6753 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6754 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6755 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6756 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6757 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6758 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6759 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6760 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6761 | #ifdef HAVE_GETPGID |
| 6762 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 6763 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6764 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6765 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6766 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6767 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6768 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6769 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6770 | #ifdef HAVE_WAIT3 |
| 6771 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 6772 | #endif /* HAVE_WAIT3 */ |
| 6773 | #ifdef HAVE_WAIT4 |
| 6774 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 6775 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6776 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6777 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6778 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6779 | #ifdef HAVE_GETSID |
| 6780 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 6781 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6782 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6783 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6784 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6785 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6786 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6787 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6788 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6789 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6790 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6791 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6792 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6793 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6794 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6795 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 6796 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6797 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6798 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6799 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6800 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6801 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6802 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6803 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6804 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6805 | #endif |
| 6806 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6807 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6808 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6809 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6810 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 6811 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6812 | #ifdef HAVE_DEVICE_MACROS |
| 6813 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 6814 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 6815 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 6816 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6817 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6818 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6819 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6820 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6821 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6822 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6823 | #ifdef HAVE_UNSETENV |
| 6824 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6825 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6826 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6827 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6828 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6829 | #ifdef HAVE_FCHDIR |
| 6830 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 6831 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6832 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6833 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6834 | #endif |
| 6835 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6836 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6837 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6838 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6839 | #ifdef WCOREDUMP |
| 6840 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 6841 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6842 | #ifdef WIFCONTINUED |
| 6843 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 6844 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6845 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6846 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6847 | #endif /* WIFSTOPPED */ |
| 6848 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6849 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6850 | #endif /* WIFSIGNALED */ |
| 6851 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6852 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6853 | #endif /* WIFEXITED */ |
| 6854 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6855 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6856 | #endif /* WEXITSTATUS */ |
| 6857 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6858 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6859 | #endif /* WTERMSIG */ |
| 6860 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6861 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6862 | #endif /* WSTOPSIG */ |
| 6863 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6864 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6865 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6866 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6867 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6868 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6869 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 6870 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6871 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6872 | #endif |
| 6873 | #ifdef HAVE_TEMPNAM |
| 6874 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 6875 | #endif |
| 6876 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6877 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6878 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6879 | #ifdef HAVE_CONFSTR |
| 6880 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 6881 | #endif |
| 6882 | #ifdef HAVE_SYSCONF |
| 6883 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 6884 | #endif |
| 6885 | #ifdef HAVE_FPATHCONF |
| 6886 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 6887 | #endif |
| 6888 | #ifdef HAVE_PATHCONF |
| 6889 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 6890 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6891 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6892 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 6893 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 6894 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6895 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6896 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6897 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6898 | #ifdef MS_WINDOWS |
| 6899 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 6900 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6901 | #ifdef __VMS |
| 6902 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 6903 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6904 | {NULL, NULL} /* Sentinel */ |
| 6905 | }; |
| 6906 | |
| 6907 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6908 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6909 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6910 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6911 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6912 | } |
| 6913 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6914 | #if defined(PYOS_OS2) |
| 6915 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6916 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6917 | { |
| 6918 | APIRET rc; |
| 6919 | ULONG values[QSV_MAX+1]; |
| 6920 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 6921 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6922 | |
| 6923 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 6924 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6925 | Py_END_ALLOW_THREADS |
| 6926 | |
| 6927 | if (rc != NO_ERROR) { |
| 6928 | os2_error(rc); |
| 6929 | return -1; |
| 6930 | } |
| 6931 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6932 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 6933 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 6934 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 6935 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 6936 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 6937 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 6938 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6939 | |
| 6940 | switch (values[QSV_VERSION_MINOR]) { |
| 6941 | case 0: ver = "2.00"; break; |
| 6942 | case 10: ver = "2.10"; break; |
| 6943 | case 11: ver = "2.11"; break; |
| 6944 | case 30: ver = "3.00"; break; |
| 6945 | case 40: ver = "4.00"; break; |
| 6946 | case 50: ver = "5.00"; break; |
| 6947 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 6948 | PyOS_snprintf(tmp, sizeof(tmp), |
| 6949 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 6950 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6951 | ver = &tmp[0]; |
| 6952 | } |
| 6953 | |
| 6954 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6955 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6956 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6957 | |
| 6958 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 6959 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 6960 | tmp[1] = ':'; |
| 6961 | tmp[2] = '\0'; |
| 6962 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6963 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6964 | } |
| 6965 | #endif |
| 6966 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6967 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6968 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6969 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6970 | #ifdef F_OK |
| 6971 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6972 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6973 | #ifdef R_OK |
| 6974 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6975 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6976 | #ifdef W_OK |
| 6977 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6978 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6979 | #ifdef X_OK |
| 6980 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6981 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6982 | #ifdef NGROUPS_MAX |
| 6983 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 6984 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6985 | #ifdef TMP_MAX |
| 6986 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 6987 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6988 | #ifdef WCONTINUED |
| 6989 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 6990 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6991 | #ifdef WNOHANG |
| 6992 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6993 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6994 | #ifdef WUNTRACED |
| 6995 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 6996 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6997 | #ifdef O_RDONLY |
| 6998 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 6999 | #endif |
| 7000 | #ifdef O_WRONLY |
| 7001 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7002 | #endif |
| 7003 | #ifdef O_RDWR |
| 7004 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7005 | #endif |
| 7006 | #ifdef O_NDELAY |
| 7007 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7008 | #endif |
| 7009 | #ifdef O_NONBLOCK |
| 7010 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7011 | #endif |
| 7012 | #ifdef O_APPEND |
| 7013 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7014 | #endif |
| 7015 | #ifdef O_DSYNC |
| 7016 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7017 | #endif |
| 7018 | #ifdef O_RSYNC |
| 7019 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7020 | #endif |
| 7021 | #ifdef O_SYNC |
| 7022 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7023 | #endif |
| 7024 | #ifdef O_NOCTTY |
| 7025 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7026 | #endif |
| 7027 | #ifdef O_CREAT |
| 7028 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7029 | #endif |
| 7030 | #ifdef O_EXCL |
| 7031 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7032 | #endif |
| 7033 | #ifdef O_TRUNC |
| 7034 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7035 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7036 | #ifdef O_BINARY |
| 7037 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7038 | #endif |
| 7039 | #ifdef O_TEXT |
| 7040 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7041 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7042 | #ifdef O_LARGEFILE |
| 7043 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7044 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7045 | #ifdef O_SHLOCK |
| 7046 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7047 | #endif |
| 7048 | #ifdef O_EXLOCK |
| 7049 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7050 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7051 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7052 | /* MS Windows */ |
| 7053 | #ifdef O_NOINHERIT |
| 7054 | /* Don't inherit in child processes. */ |
| 7055 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7056 | #endif |
| 7057 | #ifdef _O_SHORT_LIVED |
| 7058 | /* Optimize for short life (keep in memory). */ |
| 7059 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7060 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7061 | #endif |
| 7062 | #ifdef O_TEMPORARY |
| 7063 | /* Automatically delete when last handle is closed. */ |
| 7064 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7065 | #endif |
| 7066 | #ifdef O_RANDOM |
| 7067 | /* Optimize for random access. */ |
| 7068 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7069 | #endif |
| 7070 | #ifdef O_SEQUENTIAL |
| 7071 | /* Optimize for sequential access. */ |
| 7072 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7073 | #endif |
| 7074 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7075 | /* GNU extensions. */ |
| 7076 | #ifdef O_DIRECT |
| 7077 | /* Direct disk access. */ |
| 7078 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7079 | #endif |
| 7080 | #ifdef O_DIRECTORY |
| 7081 | /* Must be a directory. */ |
| 7082 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7083 | #endif |
| 7084 | #ifdef O_NOFOLLOW |
| 7085 | /* Do not follow links. */ |
| 7086 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7087 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7088 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7089 | /* These come from sysexits.h */ |
| 7090 | #ifdef EX_OK |
| 7091 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7092 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7093 | #ifdef EX_USAGE |
| 7094 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7095 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7096 | #ifdef EX_DATAERR |
| 7097 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7098 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7099 | #ifdef EX_NOINPUT |
| 7100 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7101 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7102 | #ifdef EX_NOUSER |
| 7103 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7104 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7105 | #ifdef EX_NOHOST |
| 7106 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7107 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7108 | #ifdef EX_UNAVAILABLE |
| 7109 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7110 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7111 | #ifdef EX_SOFTWARE |
| 7112 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7113 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7114 | #ifdef EX_OSERR |
| 7115 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7116 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7117 | #ifdef EX_OSFILE |
| 7118 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7119 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7120 | #ifdef EX_CANTCREAT |
| 7121 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7122 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7123 | #ifdef EX_IOERR |
| 7124 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7125 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7126 | #ifdef EX_TEMPFAIL |
| 7127 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7128 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7129 | #ifdef EX_PROTOCOL |
| 7130 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7131 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7132 | #ifdef EX_NOPERM |
| 7133 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7134 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7135 | #ifdef EX_CONFIG |
| 7136 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7137 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7138 | #ifdef EX_NOTFOUND |
| 7139 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7140 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7141 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7142 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7143 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7144 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7145 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7146 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7147 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7148 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7149 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7150 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7151 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7152 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7153 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7154 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7155 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7156 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7157 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7158 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7159 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7160 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7161 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7162 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7163 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7164 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7165 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7166 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7167 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7168 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7169 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7170 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7171 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7172 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7173 | #if defined(PYOS_OS2) |
| 7174 | if (insertvalues(d)) return -1; |
| 7175 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7176 | return 0; |
| 7177 | } |
| 7178 | |
| 7179 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7180 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7181 | #define INITFUNC initnt |
| 7182 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7183 | |
| 7184 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7185 | #define INITFUNC initos2 |
| 7186 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7187 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7188 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7189 | #define INITFUNC initposix |
| 7190 | #define MODNAME "posix" |
| 7191 | #endif |
| 7192 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7193 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7194 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7195 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7196 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7197 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7198 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7199 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7200 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7201 | if (m == NULL) |
| 7202 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7203 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7204 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7205 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7206 | Py_XINCREF(v); |
| 7207 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7208 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7209 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7210 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7211 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7212 | return; |
| 7213 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7214 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7215 | return; |
| 7216 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7217 | Py_INCREF(PyExc_OSError); |
| 7218 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7219 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7220 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7221 | if (posix_putenv_garbage == NULL) |
| 7222 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7223 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7224 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7225 | if (!initialized) { |
| 7226 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7227 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7228 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7229 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7230 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7231 | structseq_new = StatResultType.tp_new; |
| 7232 | StatResultType.tp_new = statresult_new; |
| 7233 | |
| 7234 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7235 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 7236 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7237 | Py_INCREF((PyObject*) &StatResultType); |
| 7238 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7239 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7240 | PyModule_AddObject(m, "statvfs_result", |
| 7241 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7242 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7243 | |
| 7244 | #ifdef __APPLE__ |
| 7245 | /* |
| 7246 | * Step 2 of weak-linking support on Mac OS X. |
| 7247 | * |
| 7248 | * The code below removes functions that are not available on the |
| 7249 | * currently active platform. |
| 7250 | * |
| 7251 | * This block allow one to use a python binary that was build on |
| 7252 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7253 | * OSX 10.4. |
| 7254 | */ |
| 7255 | #ifdef HAVE_FSTATVFS |
| 7256 | if (fstatvfs == NULL) { |
| 7257 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 7258 | return; |
| 7259 | } |
| 7260 | } |
| 7261 | #endif /* HAVE_FSTATVFS */ |
| 7262 | |
| 7263 | #ifdef HAVE_STATVFS |
| 7264 | if (statvfs == NULL) { |
| 7265 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 7266 | return; |
| 7267 | } |
| 7268 | } |
| 7269 | #endif /* HAVE_STATVFS */ |
| 7270 | |
| 7271 | # ifdef HAVE_LCHOWN |
| 7272 | if (lchown == NULL) { |
| 7273 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 7274 | return; |
| 7275 | } |
| 7276 | } |
| 7277 | #endif /* HAVE_LCHOWN */ |
| 7278 | |
| 7279 | |
| 7280 | #endif /* __APPLE__ */ |
| 7281 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7282 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7283 | |
| 7284 | #ifdef __cplusplus |
| 7285 | } |
| 7286 | #endif |