Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
| 18 | * Step 1 of support for weak-linking a number of symbols existing on |
| 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
| 31 | #include "structseq.h" |
| 32 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #if defined(PYOS_OS2) |
| 49 | #define INCL_DOS |
| 50 | #define INCL_DOSERRORS |
| 51 | #define INCL_DOSPROCESS |
| 52 | #define INCL_NOPMAPI |
| 53 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 54 | #if defined(PYCC_GCC) |
| 55 | #include <ctype.h> |
| 56 | #include <io.h> |
| 57 | #include <stdio.h> |
| 58 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 59 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 60 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | #endif /* HAVE_SYS_TYPES_H */ |
| 66 | |
| 67 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYS_WAIT_H |
| 72 | #include <sys/wait.h> /* For WNOHANG */ |
| 73 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 76 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | #ifdef HAVE_FCNTL_H |
| 80 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 81 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | #ifdef HAVE_GRP_H |
| 84 | #include <grp.h> |
| 85 | #endif |
| 86 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 87 | #ifdef HAVE_SYSEXITS_H |
| 88 | #include <sysexits.h> |
| 89 | #endif /* HAVE_SYSEXITS_H */ |
| 90 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYS_LOADAVG_H |
| 92 | #include <sys/loadavg.h> |
| 93 | #endif |
| 94 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 95 | #ifdef HAVE_LANGINFO_H |
| 96 | #include <langinfo.h> |
| 97 | #endif |
| 98 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> |
| 103 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
| 106 | #define HAVE_OPENDIR 1 |
| 107 | #define HAVE_SYSTEM 1 |
| 108 | #if defined(__OS2__) |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> |
| 113 | #else |
| 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR 1 |
| 118 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 119 | #define HAVE_SYSTEM 1 |
| 120 | #define HAVE_WAIT 1 |
| 121 | #else |
| 122 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 123 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 124 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #define HAVE_EXECV 1 |
| 126 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 128 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 129 | #define HAVE_FSYNC 1 |
| 130 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 131 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 132 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 133 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #else /* all other compilers */ |
| 135 | /* Unix functions that the configure script doesn't check for */ |
| 136 | #define HAVE_EXECV 1 |
| 137 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 138 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 139 | #define HAVE_FORK1 1 |
| 140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #define HAVE_GETCWD 1 |
| 142 | #define HAVE_GETEGID 1 |
| 143 | #define HAVE_GETEUID 1 |
| 144 | #define HAVE_GETGID 1 |
| 145 | #define HAVE_GETPPID 1 |
| 146 | #define HAVE_GETUID 1 |
| 147 | #define HAVE_KILL 1 |
| 148 | #define HAVE_OPENDIR 1 |
| 149 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
| 151 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 152 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 153 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #endif /* _MSC_VER */ |
| 155 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 156 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 157 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 161 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 162 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 163 | (default) */ |
| 164 | extern char *ctermid_r(char *); |
| 165 | #endif |
| 166 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 167 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 170 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 171 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #endif |
| 177 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int chdir(char *); |
| 179 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 180 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int chdir(const char *); |
| 182 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 184 | #ifdef __BORLANDC__ |
| 185 | extern int chmod(const char *, int); |
| 186 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 188 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 189 | extern int chown(const char *, uid_t, gid_t); |
| 190 | extern char *getcwd(char *, int); |
| 191 | extern char *strerror(int); |
| 192 | extern int link(const char *, const char *); |
| 193 | extern int rename(const char *, const char *); |
| 194 | extern int stat(const char *, struct stat *); |
| 195 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 197 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 198 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 201 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 204 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | #ifdef HAVE_UTIME_H |
| 207 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 208 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 210 | #ifdef HAVE_SYS_UTIME_H |
| 211 | #include <sys/utime.h> |
| 212 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 213 | #endif /* HAVE_SYS_UTIME_H */ |
| 214 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #ifdef HAVE_SYS_TIMES_H |
| 216 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 217 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | |
| 219 | #ifdef HAVE_SYS_PARAM_H |
| 220 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 221 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
| 223 | #ifdef HAVE_SYS_UTSNAME_H |
| 224 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 225 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 229 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 230 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 231 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #include <direct.h> |
| 233 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 234 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 236 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 237 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 238 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 240 | #endif |
| 241 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 243 | #endif |
| 244 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 249 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 250 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 255 | #endif |
| 256 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 258 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 259 | #include "osdefs.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 261 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 262 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 264 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 266 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 268 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 269 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 270 | #define MAXPATHLEN PATH_MAX |
| 271 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 272 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 273 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 274 | #endif /* MAXPATHLEN */ |
| 275 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 276 | #ifdef UNION_WAIT |
| 277 | /* Emulate some macros on systems that have a union instead of macros */ |
| 278 | |
| 279 | #ifndef WIFEXITED |
| 280 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 281 | #endif |
| 282 | |
| 283 | #ifndef WEXITSTATUS |
| 284 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 285 | #endif |
| 286 | |
| 287 | #ifndef WTERMSIG |
| 288 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 289 | #endif |
| 290 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 291 | #define WAIT_TYPE union wait |
| 292 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 293 | |
| 294 | #else /* !UNION_WAIT */ |
| 295 | #define WAIT_TYPE int |
| 296 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 297 | #endif /* UNION_WAIT */ |
| 298 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 299 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 300 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 301 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 302 | #define USE_CTERMID_R |
| 303 | #endif |
| 304 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 305 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 306 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 307 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 308 | # define STAT win32_stat |
| 309 | # define FSTAT win32_fstat |
| 310 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 311 | #else |
| 312 | # define STAT stat |
| 313 | # define FSTAT fstat |
| 314 | # define STRUCT_STAT struct stat |
| 315 | #endif |
| 316 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 317 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 318 | #include <sys/mkdev.h> |
| 319 | #else |
| 320 | #if defined(MAJOR_IN_SYSMACROS) |
| 321 | #include <sys/sysmacros.h> |
| 322 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 323 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 324 | #include <sys/mkdev.h> |
| 325 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 326 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 329 | #ifdef WITH_NEXT_FRAMEWORK |
| 330 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 331 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 332 | */ |
| 333 | #include <crt_externs.h> |
| 334 | static char **environ; |
| 335 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 337 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 339 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 340 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 342 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 343 | #ifdef MS_WINDOWS |
| 344 | wchar_t **e; |
| 345 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 346 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 347 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 348 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | if (d == NULL) |
| 350 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 351 | #ifdef WITH_NEXT_FRAMEWORK |
| 352 | if (environ == NULL) |
| 353 | environ = *_NSGetEnviron(); |
| 354 | #endif |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 355 | #ifdef MS_WINDOWS |
| 356 | /* _wenviron must be initialized in this way if the program is started |
| 357 | through main() instead of wmain(). */ |
| 358 | _wgetenv(L""); |
| 359 | if (_wenviron == NULL) |
| 360 | return d; |
| 361 | /* This part ignores errors */ |
| 362 | for (e = _wenviron; *e != NULL; e++) { |
| 363 | PyObject *k; |
| 364 | PyObject *v; |
| 365 | wchar_t *p = wcschr(*e, L'='); |
| 366 | if (p == NULL) |
| 367 | continue; |
| 368 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 369 | if (k == NULL) { |
| 370 | PyErr_Clear(); |
| 371 | continue; |
| 372 | } |
| 373 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 374 | if (v == NULL) { |
| 375 | PyErr_Clear(); |
| 376 | Py_DECREF(k); |
| 377 | continue; |
| 378 | } |
| 379 | if (PyDict_GetItem(d, k) == NULL) { |
| 380 | if (PyDict_SetItem(d, k, v) != 0) |
| 381 | PyErr_Clear(); |
| 382 | } |
| 383 | Py_DECREF(k); |
| 384 | Py_DECREF(v); |
| 385 | } |
| 386 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | if (environ == NULL) |
| 388 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 389 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 391 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 392 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | char *p = strchr(*e, '='); |
| 394 | if (p == NULL) |
| 395 | continue; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 396 | k = PyUnicode_FromStringAndSize(*e, (int)(p-*e)); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 397 | if (k == NULL) { |
| 398 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 399 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 400 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 401 | v = PyUnicode_FromString(p+1); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 402 | if (v == NULL) { |
| 403 | PyErr_Clear(); |
| 404 | Py_DECREF(k); |
| 405 | continue; |
| 406 | } |
| 407 | if (PyDict_GetItem(d, k) == NULL) { |
| 408 | if (PyDict_SetItem(d, k, v) != 0) |
| 409 | PyErr_Clear(); |
| 410 | } |
| 411 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 412 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 413 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 414 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 415 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 416 | { |
| 417 | APIRET rc; |
| 418 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 419 | |
| 420 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 421 | 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] | 422 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 423 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 424 | Py_DECREF(v); |
| 425 | } |
| 426 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 427 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 428 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 429 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 430 | Py_DECREF(v); |
| 431 | } |
| 432 | } |
| 433 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 434 | return d; |
| 435 | } |
| 436 | |
| 437 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 438 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 439 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 440 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 441 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 442 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 443 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 444 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 445 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 446 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 447 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 448 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 451 | #ifdef Py_WIN_WIDE_FILENAMES |
| 452 | static PyObject * |
| 453 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 454 | { |
| 455 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 456 | } |
| 457 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 458 | |
| 459 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 460 | static PyObject * |
| 461 | posix_error_with_allocated_filename(char* name) |
| 462 | { |
| 463 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 464 | PyMem_Free(name); |
| 465 | return rc; |
| 466 | } |
| 467 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 468 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 469 | static PyObject * |
| 470 | win32_error(char* function, char* filename) |
| 471 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 472 | /* XXX We should pass the function name along in the future. |
| 473 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 474 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 475 | Windows error object, which is non-trivial. |
| 476 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 477 | errno = GetLastError(); |
| 478 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 479 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 480 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 481 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 482 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 483 | |
| 484 | #ifdef Py_WIN_WIDE_FILENAMES |
| 485 | static PyObject * |
| 486 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 487 | { |
| 488 | /* XXX - see win32_error for comments on 'function' */ |
| 489 | errno = GetLastError(); |
| 490 | if (filename) |
| 491 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 492 | else |
| 493 | return PyErr_SetFromWindowsErr(errno); |
| 494 | } |
| 495 | |
| 496 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 497 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /* Function suitable for O& conversion */ |
| 501 | static int |
| 502 | convert_to_unicode(PyObject *arg, void* _param) |
| 503 | { |
| 504 | PyObject **param = (PyObject**)_param; |
| 505 | if (PyUnicode_CheckExact(arg)) { |
| 506 | Py_INCREF(arg); |
| 507 | *param = arg; |
| 508 | } |
| 509 | else if (PyUnicode_Check(arg)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 510 | /* For a Unicode subtype that's not a Unicode object, |
| 511 | return a true Unicode object with the same data. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 512 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), |
| 513 | PyUnicode_GET_SIZE(arg)); |
| 514 | return *param != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 515 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 516 | else |
| 517 | *param = PyUnicode_FromEncodedObject(arg, |
| 518 | Py_FileSystemDefaultEncoding, |
| 519 | "strict"); |
| 520 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 524 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 525 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 526 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 527 | #if defined(PYOS_OS2) |
| 528 | /********************************************************************** |
| 529 | * Helper Function to Trim and Format OS/2 Messages |
| 530 | **********************************************************************/ |
| 531 | static void |
| 532 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 533 | { |
| 534 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 535 | |
| 536 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 537 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 538 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 539 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 540 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 541 | } |
| 542 | |
| 543 | /* Add Optional Reason Text */ |
| 544 | if (reason) { |
| 545 | strcat(msgbuf, " : "); |
| 546 | strcat(msgbuf, reason); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /********************************************************************** |
| 551 | * Decode an OS/2 Operating System Error Code |
| 552 | * |
| 553 | * A convenience function to lookup an OS/2 error code and return a |
| 554 | * text message we can use to raise a Python exception. |
| 555 | * |
| 556 | * Notes: |
| 557 | * The messages for errors returned from the OS/2 kernel reside in |
| 558 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 559 | * |
| 560 | **********************************************************************/ |
| 561 | static char * |
| 562 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 563 | { |
| 564 | APIRET rc; |
| 565 | ULONG msglen; |
| 566 | |
| 567 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 568 | Py_BEGIN_ALLOW_THREADS |
| 569 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 570 | errorcode, "oso001.msg", &msglen); |
| 571 | Py_END_ALLOW_THREADS |
| 572 | |
| 573 | if (rc == NO_ERROR) |
| 574 | os2_formatmsg(msgbuf, msglen, reason); |
| 575 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 576 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 577 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 578 | |
| 579 | return msgbuf; |
| 580 | } |
| 581 | |
| 582 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 583 | errors are not in a global variable e.g. 'errno' nor are |
| 584 | they congruent with posix error numbers. */ |
| 585 | |
| 586 | static PyObject * os2_error(int code) |
| 587 | { |
| 588 | char text[1024]; |
| 589 | PyObject *v; |
| 590 | |
| 591 | os2_strerror(text, sizeof(text), code, ""); |
| 592 | |
| 593 | v = Py_BuildValue("(is)", code, text); |
| 594 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 595 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 596 | Py_DECREF(v); |
| 597 | } |
| 598 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 599 | } |
| 600 | |
| 601 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 602 | |
| 603 | /* POSIX generic methods */ |
| 604 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 605 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 606 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 607 | { |
| 608 | int fd; |
| 609 | int res; |
| 610 | fd = PyObject_AsFileDescriptor(fdobj); |
| 611 | if (fd < 0) |
| 612 | return NULL; |
| 613 | Py_BEGIN_ALLOW_THREADS |
| 614 | res = (*func)(fd); |
| 615 | Py_END_ALLOW_THREADS |
| 616 | if (res < 0) |
| 617 | return posix_error(); |
| 618 | Py_INCREF(Py_None); |
| 619 | return Py_None; |
| 620 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 621 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 622 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 623 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 624 | unicode_file_names(void) |
| 625 | { |
| 626 | static int canusewide = -1; |
| 627 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 628 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 629 | the Windows NT family. */ |
| 630 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 631 | } |
| 632 | return canusewide; |
| 633 | } |
| 634 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 636 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 637 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 638 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 639 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 640 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 641 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 642 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 643 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 644 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 645 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 646 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 647 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 648 | return posix_error_with_allocated_filename(path1); |
| 649 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 650 | Py_INCREF(Py_None); |
| 651 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 654 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 655 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 656 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 657 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 659 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 660 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 661 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 662 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 663 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 664 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 665 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 666 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 667 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 668 | PyMem_Free(path1); |
| 669 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 670 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 671 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 672 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 673 | Py_INCREF(Py_None); |
| 674 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 677 | #ifdef Py_WIN_WIDE_FILENAMES |
| 678 | static PyObject* |
| 679 | win32_1str(PyObject* args, char* func, |
| 680 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 681 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 682 | { |
| 683 | PyObject *uni; |
| 684 | char *ansi; |
| 685 | BOOL result; |
| 686 | if (unicode_file_names()) { |
| 687 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 688 | PyErr_Clear(); |
| 689 | else { |
| 690 | Py_BEGIN_ALLOW_THREADS |
| 691 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 692 | Py_END_ALLOW_THREADS |
| 693 | if (!result) |
| 694 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 695 | Py_INCREF(Py_None); |
| 696 | return Py_None; |
| 697 | } |
| 698 | } |
| 699 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 700 | return NULL; |
| 701 | Py_BEGIN_ALLOW_THREADS |
| 702 | result = funcA(ansi); |
| 703 | Py_END_ALLOW_THREADS |
| 704 | if (!result) |
| 705 | return win32_error(func, ansi); |
| 706 | Py_INCREF(Py_None); |
| 707 | return Py_None; |
| 708 | |
| 709 | } |
| 710 | |
| 711 | /* This is a reimplementation of the C library's chdir function, |
| 712 | but one that produces Win32 errors instead of DOS error codes. |
| 713 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 714 | it also needs to set "magic" environment variables indicating |
| 715 | the per-drive current directory, which are of the form =<drive>: */ |
| 716 | BOOL __stdcall |
| 717 | win32_chdir(LPCSTR path) |
| 718 | { |
| 719 | char new_path[MAX_PATH+1]; |
| 720 | int result; |
| 721 | char env[4] = "=x:"; |
| 722 | |
| 723 | if(!SetCurrentDirectoryA(path)) |
| 724 | return FALSE; |
| 725 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 726 | if (!result) |
| 727 | return FALSE; |
| 728 | /* In the ANSI API, there should not be any paths longer |
| 729 | than MAX_PATH. */ |
| 730 | assert(result <= MAX_PATH+1); |
| 731 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 732 | strncmp(new_path, "//", 2) == 0) |
| 733 | /* UNC path, nothing to do. */ |
| 734 | return TRUE; |
| 735 | env[1] = new_path[0]; |
| 736 | return SetEnvironmentVariableA(env, new_path); |
| 737 | } |
| 738 | |
| 739 | /* The Unicode version differs from the ANSI version |
| 740 | since the current directory might exceed MAX_PATH characters */ |
| 741 | BOOL __stdcall |
| 742 | win32_wchdir(LPCWSTR path) |
| 743 | { |
| 744 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 745 | int result; |
| 746 | wchar_t env[4] = L"=x:"; |
| 747 | |
| 748 | if(!SetCurrentDirectoryW(path)) |
| 749 | return FALSE; |
| 750 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 751 | if (!result) |
| 752 | return FALSE; |
| 753 | if (result > MAX_PATH+1) { |
| 754 | new_path = malloc(result); |
| 755 | if (!new_path) { |
| 756 | SetLastError(ERROR_OUTOFMEMORY); |
| 757 | return FALSE; |
| 758 | } |
| 759 | } |
| 760 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 761 | wcsncmp(new_path, L"//", 2) == 0) |
| 762 | /* UNC path, nothing to do. */ |
| 763 | return TRUE; |
| 764 | env[1] = new_path[0]; |
| 765 | result = SetEnvironmentVariableW(env, new_path); |
| 766 | if (new_path != _new_path) |
| 767 | free(new_path); |
| 768 | return result; |
| 769 | } |
| 770 | #endif |
| 771 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 772 | #ifdef MS_WINDOWS |
| 773 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 774 | - time stamps are restricted to second resolution |
| 775 | - file modification times suffer from forth-and-back conversions between |
| 776 | UTC and local time |
| 777 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 778 | */ |
| 779 | #define HAVE_STAT_NSEC 1 |
| 780 | |
| 781 | struct win32_stat{ |
| 782 | int st_dev; |
| 783 | __int64 st_ino; |
| 784 | unsigned short st_mode; |
| 785 | int st_nlink; |
| 786 | int st_uid; |
| 787 | int st_gid; |
| 788 | int st_rdev; |
| 789 | __int64 st_size; |
| 790 | int st_atime; |
| 791 | int st_atime_nsec; |
| 792 | int st_mtime; |
| 793 | int st_mtime_nsec; |
| 794 | int st_ctime; |
| 795 | int st_ctime_nsec; |
| 796 | }; |
| 797 | |
| 798 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 799 | |
| 800 | static void |
| 801 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 802 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 803 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 804 | /* Cannot simply cast and dereference in_ptr, |
| 805 | since it might not be aligned properly */ |
| 806 | __int64 in; |
| 807 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 808 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 809 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 810 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 811 | } |
| 812 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 813 | static void |
| 814 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 815 | { |
| 816 | /* XXX endianness */ |
| 817 | __int64 out; |
| 818 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 819 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 820 | memcpy(out_ptr, &out, sizeof(out)); |
| 821 | } |
| 822 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 823 | /* Below, we *know* that ugo+r is 0444 */ |
| 824 | #if _S_IREAD != 0400 |
| 825 | #error Unsupported C library |
| 826 | #endif |
| 827 | static int |
| 828 | attributes_to_mode(DWORD attr) |
| 829 | { |
| 830 | int m = 0; |
| 831 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 832 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 833 | else |
| 834 | m |= _S_IFREG; |
| 835 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 836 | m |= 0444; |
| 837 | else |
| 838 | m |= 0666; |
| 839 | return m; |
| 840 | } |
| 841 | |
| 842 | static int |
| 843 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 844 | { |
| 845 | memset(result, 0, sizeof(*result)); |
| 846 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 847 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 848 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 849 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 850 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 855 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 856 | static int checked = 0; |
| 857 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 858 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 859 | static void |
| 860 | check_gfax() |
| 861 | { |
| 862 | HINSTANCE hKernel32; |
| 863 | if (checked) |
| 864 | return; |
| 865 | checked = 1; |
| 866 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 867 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 868 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 869 | } |
| 870 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 871 | static BOOL |
| 872 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 873 | { |
| 874 | HANDLE hFindFile; |
| 875 | WIN32_FIND_DATAA FileData; |
| 876 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 877 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 878 | return FALSE; |
| 879 | FindClose(hFindFile); |
| 880 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 881 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 882 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 883 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 884 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 885 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 886 | return TRUE; |
| 887 | } |
| 888 | |
| 889 | static BOOL |
| 890 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 891 | { |
| 892 | HANDLE hFindFile; |
| 893 | WIN32_FIND_DATAW FileData; |
| 894 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 895 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 896 | return FALSE; |
| 897 | FindClose(hFindFile); |
| 898 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 899 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 900 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 901 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 902 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 903 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 904 | return TRUE; |
| 905 | } |
| 906 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 907 | static BOOL WINAPI |
| 908 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 909 | GET_FILEEX_INFO_LEVELS level, |
| 910 | LPVOID pv) |
| 911 | { |
| 912 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 913 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 914 | /* First try to use the system's implementation, if that is |
| 915 | available and either succeeds to gives an error other than |
| 916 | that it isn't implemented. */ |
| 917 | check_gfax(); |
| 918 | if (gfaxa) { |
| 919 | result = gfaxa(pszFile, level, pv); |
| 920 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 921 | return result; |
| 922 | } |
| 923 | /* It's either not present, or not implemented. |
| 924 | Emulate using FindFirstFile. */ |
| 925 | if (level != GetFileExInfoStandard) { |
| 926 | SetLastError(ERROR_INVALID_PARAMETER); |
| 927 | return FALSE; |
| 928 | } |
| 929 | /* Use GetFileAttributes to validate that the file name |
| 930 | does not contain wildcards (which FindFirstFile would |
| 931 | accept). */ |
| 932 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 933 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 934 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static BOOL WINAPI |
| 938 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 939 | GET_FILEEX_INFO_LEVELS level, |
| 940 | LPVOID pv) |
| 941 | { |
| 942 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 943 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 944 | /* First try to use the system's implementation, if that is |
| 945 | available and either succeeds to gives an error other than |
| 946 | that it isn't implemented. */ |
| 947 | check_gfax(); |
| 948 | if (gfaxa) { |
| 949 | result = gfaxw(pszFile, level, pv); |
| 950 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 951 | return result; |
| 952 | } |
| 953 | /* It's either not present, or not implemented. |
| 954 | Emulate using FindFirstFile. */ |
| 955 | if (level != GetFileExInfoStandard) { |
| 956 | SetLastError(ERROR_INVALID_PARAMETER); |
| 957 | return FALSE; |
| 958 | } |
| 959 | /* Use GetFileAttributes to validate that the file name |
| 960 | does not contain wildcards (which FindFirstFile would |
| 961 | accept). */ |
| 962 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 963 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 964 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 967 | static int |
| 968 | win32_stat(const char* path, struct win32_stat *result) |
| 969 | { |
| 970 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 971 | int code; |
| 972 | char *dot; |
| 973 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 974 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 975 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 976 | /* Protocol violation: we explicitly clear errno, instead of |
| 977 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 978 | errno = 0; |
| 979 | return -1; |
| 980 | } else { |
| 981 | /* Could not get attributes on open file. Fall back to |
| 982 | reading the directory. */ |
| 983 | if (!attributes_from_dir(path, &info)) { |
| 984 | /* Very strange. This should not fail now */ |
| 985 | errno = 0; |
| 986 | return -1; |
| 987 | } |
| 988 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 989 | } |
| 990 | code = attribute_data_to_stat(&info, result); |
| 991 | if (code != 0) |
| 992 | return code; |
| 993 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 994 | dot = strrchr(path, '.'); |
| 995 | if (dot) { |
| 996 | if (stricmp(dot, ".bat") == 0 || |
| 997 | stricmp(dot, ".cmd") == 0 || |
| 998 | stricmp(dot, ".exe") == 0 || |
| 999 | stricmp(dot, ".com") == 0) |
| 1000 | result->st_mode |= 0111; |
| 1001 | } |
| 1002 | return code; |
| 1003 | } |
| 1004 | |
| 1005 | static int |
| 1006 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1007 | { |
| 1008 | int code; |
| 1009 | const wchar_t *dot; |
| 1010 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1011 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1012 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1013 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1014 | /* Protocol violation: we explicitly clear errno, instead of |
| 1015 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1016 | errno = 0; |
| 1017 | return -1; |
| 1018 | } else { |
| 1019 | /* Could not get attributes on open file. Fall back to |
| 1020 | reading the directory. */ |
| 1021 | if (!attributes_from_dir_w(path, &info)) { |
| 1022 | /* Very strange. This should not fail now */ |
| 1023 | errno = 0; |
| 1024 | return -1; |
| 1025 | } |
| 1026 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1027 | } |
| 1028 | code = attribute_data_to_stat(&info, result); |
| 1029 | if (code < 0) |
| 1030 | return code; |
| 1031 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1032 | dot = wcsrchr(path, '.'); |
| 1033 | if (dot) { |
| 1034 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1035 | _wcsicmp(dot, L".cmd") == 0 || |
| 1036 | _wcsicmp(dot, L".exe") == 0 || |
| 1037 | _wcsicmp(dot, L".com") == 0) |
| 1038 | result->st_mode |= 0111; |
| 1039 | } |
| 1040 | return code; |
| 1041 | } |
| 1042 | |
| 1043 | static int |
| 1044 | win32_fstat(int file_number, struct win32_stat *result) |
| 1045 | { |
| 1046 | BY_HANDLE_FILE_INFORMATION info; |
| 1047 | HANDLE h; |
| 1048 | int type; |
| 1049 | |
| 1050 | h = (HANDLE)_get_osfhandle(file_number); |
| 1051 | |
| 1052 | /* Protocol violation: we explicitly clear errno, instead of |
| 1053 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1054 | errno = 0; |
| 1055 | |
| 1056 | if (h == INVALID_HANDLE_VALUE) { |
| 1057 | /* This is really a C library error (invalid file handle). |
| 1058 | We set the Win32 error to the closes one matching. */ |
| 1059 | SetLastError(ERROR_INVALID_HANDLE); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | memset(result, 0, sizeof(*result)); |
| 1063 | |
| 1064 | type = GetFileType(h); |
| 1065 | if (type == FILE_TYPE_UNKNOWN) { |
| 1066 | DWORD error = GetLastError(); |
| 1067 | if (error != 0) { |
| 1068 | return -1; |
| 1069 | } |
| 1070 | /* else: valid but unknown file */ |
| 1071 | } |
| 1072 | |
| 1073 | if (type != FILE_TYPE_DISK) { |
| 1074 | if (type == FILE_TYPE_CHAR) |
| 1075 | result->st_mode = _S_IFCHR; |
| 1076 | else if (type == FILE_TYPE_PIPE) |
| 1077 | result->st_mode = _S_IFIFO; |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | if (!GetFileInformationByHandle(h, &info)) { |
| 1082 | return -1; |
| 1083 | } |
| 1084 | |
| 1085 | /* similar to stat() */ |
| 1086 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1087 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1088 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1089 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1090 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1091 | /* specific to fstat() */ |
| 1092 | result->st_nlink = info.nNumberOfLinks; |
| 1093 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | #endif /* MS_WINDOWS */ |
| 1098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1099 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1100 | "stat_result: Result from stat or lstat.\n\n\ |
| 1101 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1102 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1103 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1104 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1105 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1106 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1107 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1108 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1109 | |
| 1110 | static PyStructSequence_Field stat_result_fields[] = { |
| 1111 | {"st_mode", "protection bits"}, |
| 1112 | {"st_ino", "inode"}, |
| 1113 | {"st_dev", "device"}, |
| 1114 | {"st_nlink", "number of hard links"}, |
| 1115 | {"st_uid", "user ID of owner"}, |
| 1116 | {"st_gid", "group ID of owner"}, |
| 1117 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1118 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1119 | {NULL, "integer time of last access"}, |
| 1120 | {NULL, "integer time of last modification"}, |
| 1121 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1122 | {"st_atime", "time of last access"}, |
| 1123 | {"st_mtime", "time of last modification"}, |
| 1124 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1125 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1126 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1127 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1128 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1129 | {"st_blocks", "number of blocks allocated"}, |
| 1130 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1131 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1132 | {"st_rdev", "device type (if inode device)"}, |
| 1133 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1134 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1135 | {"st_flags", "user defined flags for file"}, |
| 1136 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1137 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1138 | {"st_gen", "generation number"}, |
| 1139 | #endif |
| 1140 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1141 | {"st_birthtime", "time of creation"}, |
| 1142 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1143 | {0} |
| 1144 | }; |
| 1145 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1146 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1147 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1148 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1149 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1150 | #endif |
| 1151 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1152 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1153 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1154 | #else |
| 1155 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1156 | #endif |
| 1157 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1158 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1159 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1160 | #else |
| 1161 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1162 | #endif |
| 1163 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1164 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1165 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1166 | #else |
| 1167 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1168 | #endif |
| 1169 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1170 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1171 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1172 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1173 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1174 | #endif |
| 1175 | |
| 1176 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1177 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1178 | #else |
| 1179 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1180 | #endif |
| 1181 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1182 | static PyStructSequence_Desc stat_result_desc = { |
| 1183 | "stat_result", /* name */ |
| 1184 | stat_result__doc__, /* doc */ |
| 1185 | stat_result_fields, |
| 1186 | 10 |
| 1187 | }; |
| 1188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1189 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1190 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1191 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1192 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1193 | 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] | 1194 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1195 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1196 | |
| 1197 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1198 | {"f_bsize", }, |
| 1199 | {"f_frsize", }, |
| 1200 | {"f_blocks", }, |
| 1201 | {"f_bfree", }, |
| 1202 | {"f_bavail", }, |
| 1203 | {"f_files", }, |
| 1204 | {"f_ffree", }, |
| 1205 | {"f_favail", }, |
| 1206 | {"f_flag", }, |
| 1207 | {"f_namemax",}, |
| 1208 | {0} |
| 1209 | }; |
| 1210 | |
| 1211 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1212 | "statvfs_result", /* name */ |
| 1213 | statvfs_result__doc__, /* doc */ |
| 1214 | statvfs_result_fields, |
| 1215 | 10 |
| 1216 | }; |
| 1217 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1218 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1219 | static PyTypeObject StatResultType; |
| 1220 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1221 | static newfunc structseq_new; |
| 1222 | |
| 1223 | static PyObject * |
| 1224 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1225 | { |
| 1226 | PyStructSequence *result; |
| 1227 | int i; |
| 1228 | |
| 1229 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1230 | if (!result) |
| 1231 | return NULL; |
| 1232 | /* If we have been initialized from a tuple, |
| 1233 | st_?time might be set to None. Initialize it |
| 1234 | from the int slots. */ |
| 1235 | for (i = 7; i <= 9; i++) { |
| 1236 | if (result->ob_item[i+3] == Py_None) { |
| 1237 | Py_DECREF(Py_None); |
| 1238 | Py_INCREF(result->ob_item[i]); |
| 1239 | result->ob_item[i+3] = result->ob_item[i]; |
| 1240 | } |
| 1241 | } |
| 1242 | return (PyObject*)result; |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1248 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1249 | |
| 1250 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1251 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1252 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1253 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1254 | future calls return ints. \n\ |
| 1255 | If newval is omitted, return the current setting.\n"); |
| 1256 | |
| 1257 | static PyObject* |
| 1258 | stat_float_times(PyObject* self, PyObject *args) |
| 1259 | { |
| 1260 | int newval = -1; |
| 1261 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1262 | return NULL; |
| 1263 | if (newval == -1) |
| 1264 | /* Return old value */ |
| 1265 | return PyBool_FromLong(_stat_float_times); |
| 1266 | _stat_float_times = newval; |
| 1267 | Py_INCREF(Py_None); |
| 1268 | return Py_None; |
| 1269 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1270 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1271 | static void |
| 1272 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1273 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1274 | PyObject *fval,*ival; |
| 1275 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1276 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1277 | #else |
| 1278 | ival = PyInt_FromLong((long)sec); |
| 1279 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1280 | if (!ival) |
| 1281 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1282 | if (_stat_float_times) { |
| 1283 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1284 | } else { |
| 1285 | fval = ival; |
| 1286 | Py_INCREF(fval); |
| 1287 | } |
| 1288 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1289 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1292 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1293 | (used by posix_stat() and posix_fstat()) */ |
| 1294 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1295 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1296 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1297 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1298 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1299 | if (v == NULL) |
| 1300 | return NULL; |
| 1301 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1302 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1303 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1304 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1305 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1306 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1307 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1308 | #endif |
| 1309 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1310 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1311 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1312 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1313 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1314 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1315 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1316 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1317 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1318 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1319 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1320 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1321 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1322 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1323 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1324 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1325 | #if defined(HAVE_STAT_TV_NSEC) |
| 1326 | ansec = st->st_atim.tv_nsec; |
| 1327 | mnsec = st->st_mtim.tv_nsec; |
| 1328 | cnsec = st->st_ctim.tv_nsec; |
| 1329 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1330 | ansec = st->st_atimespec.tv_nsec; |
| 1331 | mnsec = st->st_mtimespec.tv_nsec; |
| 1332 | cnsec = st->st_ctimespec.tv_nsec; |
| 1333 | #elif defined(HAVE_STAT_NSEC) |
| 1334 | ansec = st->st_atime_nsec; |
| 1335 | mnsec = st->st_mtime_nsec; |
| 1336 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1337 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1338 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1339 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1340 | fill_time(v, 7, st->st_atime, ansec); |
| 1341 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1342 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1343 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1344 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1345 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1346 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1347 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1348 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1349 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1350 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1351 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1352 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1353 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1354 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1355 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1356 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1357 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1358 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1359 | #endif |
| 1360 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1361 | { |
| 1362 | PyObject *val; |
| 1363 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1364 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1365 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1366 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1367 | #else |
| 1368 | bnsec = 0; |
| 1369 | #endif |
| 1370 | if (_stat_float_times) { |
| 1371 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1372 | } else { |
| 1373 | val = PyInt_FromLong((long)bsec); |
| 1374 | } |
| 1375 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1376 | val); |
| 1377 | } |
| 1378 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1379 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1380 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1381 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1382 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1383 | |
| 1384 | if (PyErr_Occurred()) { |
| 1385 | Py_DECREF(v); |
| 1386 | return NULL; |
| 1387 | } |
| 1388 | |
| 1389 | return v; |
| 1390 | } |
| 1391 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1392 | #ifdef MS_WINDOWS |
| 1393 | |
| 1394 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1395 | where / can be used in place of \ and the trailing slash is optional. |
| 1396 | Both SERVER and SHARE must have at least one character. |
| 1397 | */ |
| 1398 | |
| 1399 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1400 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1401 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1402 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1403 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1404 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1405 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1406 | IsUNCRootA(char *path, int pathlen) |
| 1407 | { |
| 1408 | #define ISSLASH ISSLASHA |
| 1409 | |
| 1410 | int i, share; |
| 1411 | |
| 1412 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1413 | /* minimum UNCRoot is \\x\y */ |
| 1414 | return FALSE; |
| 1415 | for (i = 2; i < pathlen ; i++) |
| 1416 | if (ISSLASH(path[i])) break; |
| 1417 | if (i == 2 || i == pathlen) |
| 1418 | /* do not allow \\\SHARE or \\SERVER */ |
| 1419 | return FALSE; |
| 1420 | share = i+1; |
| 1421 | for (i = share; i < pathlen; i++) |
| 1422 | if (ISSLASH(path[i])) break; |
| 1423 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1424 | |
| 1425 | #undef ISSLASH |
| 1426 | } |
| 1427 | |
| 1428 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1429 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1430 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1431 | { |
| 1432 | #define ISSLASH ISSLASHW |
| 1433 | |
| 1434 | int i, share; |
| 1435 | |
| 1436 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1437 | /* minimum UNCRoot is \\x\y */ |
| 1438 | return FALSE; |
| 1439 | for (i = 2; i < pathlen ; i++) |
| 1440 | if (ISSLASH(path[i])) break; |
| 1441 | if (i == 2 || i == pathlen) |
| 1442 | /* do not allow \\\SHARE or \\SERVER */ |
| 1443 | return FALSE; |
| 1444 | share = i+1; |
| 1445 | for (i = share; i < pathlen; i++) |
| 1446 | if (ISSLASH(path[i])) break; |
| 1447 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1448 | |
| 1449 | #undef ISSLASH |
| 1450 | } |
| 1451 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1452 | #endif /* MS_WINDOWS */ |
| 1453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1454 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1455 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1456 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1457 | #ifdef __VMS |
| 1458 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1459 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1460 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1461 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1462 | char *wformat, |
| 1463 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1464 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1465 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1466 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1467 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1468 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1469 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1470 | |
| 1471 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1472 | /* If on wide-character-capable OS see if argument |
| 1473 | is Unicode and if so use wide API. */ |
| 1474 | if (unicode_file_names()) { |
| 1475 | PyUnicodeObject *po; |
| 1476 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1477 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1478 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1479 | Py_BEGIN_ALLOW_THREADS |
| 1480 | /* PyUnicode_AS_UNICODE result OK without |
| 1481 | thread lock as it is a simple dereference. */ |
| 1482 | res = wstatfunc(wpath, &st); |
| 1483 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1484 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1485 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1486 | return win32_error_unicode("stat", wpath); |
| 1487 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1488 | } |
| 1489 | /* Drop the argument parsing error as narrow strings |
| 1490 | are also valid. */ |
| 1491 | PyErr_Clear(); |
| 1492 | } |
| 1493 | #endif |
| 1494 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1495 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1496 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1497 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1498 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1499 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1500 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1501 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1502 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1503 | |
| 1504 | if (res != 0) { |
| 1505 | #ifdef MS_WINDOWS |
| 1506 | result = win32_error("stat", pathfree); |
| 1507 | #else |
| 1508 | result = posix_error_with_filename(pathfree); |
| 1509 | #endif |
| 1510 | } |
| 1511 | else |
| 1512 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1513 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1514 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1515 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1518 | /* POSIX methods */ |
| 1519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1520 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1521 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1522 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1523 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1524 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1525 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1526 | 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] | 1527 | |
| 1528 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1529 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1530 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1531 | char *path; |
| 1532 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1533 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1534 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1535 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1536 | if (unicode_file_names()) { |
| 1537 | PyUnicodeObject *po; |
| 1538 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1539 | Py_BEGIN_ALLOW_THREADS |
| 1540 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1541 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1542 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1543 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1544 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1545 | } |
| 1546 | /* Drop the argument parsing error as narrow strings |
| 1547 | are also valid. */ |
| 1548 | PyErr_Clear(); |
| 1549 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1550 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1551 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1552 | return 0; |
| 1553 | Py_BEGIN_ALLOW_THREADS |
| 1554 | attr = GetFileAttributesA(path); |
| 1555 | Py_END_ALLOW_THREADS |
| 1556 | PyMem_Free(path); |
| 1557 | finish: |
| 1558 | if (attr == 0xFFFFFFFF) |
| 1559 | /* File does not exist, or cannot read attributes */ |
| 1560 | return PyBool_FromLong(0); |
| 1561 | /* Access is possible if either write access wasn't requested, or |
| 1562 | the file isn't read-only. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1563 | return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1564 | #else |
| 1565 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1566 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1567 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1568 | return NULL; |
| 1569 | Py_BEGIN_ALLOW_THREADS |
| 1570 | res = access(path, mode); |
| 1571 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1572 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1573 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1574 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1577 | #ifndef F_OK |
| 1578 | #define F_OK 0 |
| 1579 | #endif |
| 1580 | #ifndef R_OK |
| 1581 | #define R_OK 4 |
| 1582 | #endif |
| 1583 | #ifndef W_OK |
| 1584 | #define W_OK 2 |
| 1585 | #endif |
| 1586 | #ifndef X_OK |
| 1587 | #define X_OK 1 |
| 1588 | #endif |
| 1589 | |
| 1590 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1591 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1592 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1593 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1594 | |
| 1595 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1596 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1597 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1598 | int id; |
| 1599 | char *ret; |
| 1600 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1601 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1602 | return NULL; |
| 1603 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1604 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1605 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1606 | if (id == 0) { |
| 1607 | ret = ttyname(); |
| 1608 | } |
| 1609 | else { |
| 1610 | ret = NULL; |
| 1611 | } |
| 1612 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1613 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1614 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1615 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1616 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1617 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1618 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1619 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1620 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1621 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1622 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1623 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1624 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1625 | |
| 1626 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1627 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1628 | { |
| 1629 | char *ret; |
| 1630 | char buffer[L_ctermid]; |
| 1631 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1632 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1633 | ret = ctermid_r(buffer); |
| 1634 | #else |
| 1635 | ret = ctermid(buffer); |
| 1636 | #endif |
| 1637 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1638 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1639 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1640 | } |
| 1641 | #endif |
| 1642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1643 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1644 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1645 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1646 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1647 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1648 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1649 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1650 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1651 | 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] | 1652 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1653 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1654 | #elif defined(__VMS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1655 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1656 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1657 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1658 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1661 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1662 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1663 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1664 | 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] | 1665 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1666 | |
| 1667 | static PyObject * |
| 1668 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1669 | { |
| 1670 | return posix_fildes(fdobj, fchdir); |
| 1671 | } |
| 1672 | #endif /* HAVE_FCHDIR */ |
| 1673 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1675 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1676 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1677 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1678 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1679 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1680 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1681 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1682 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1683 | int i; |
| 1684 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1685 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1686 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1687 | if (unicode_file_names()) { |
| 1688 | PyUnicodeObject *po; |
| 1689 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1690 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1691 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1692 | if (attr != 0xFFFFFFFF) { |
| 1693 | if (i & _S_IWRITE) |
| 1694 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1695 | else |
| 1696 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1697 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1698 | } |
| 1699 | else |
| 1700 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1701 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1702 | if (!res) |
| 1703 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1704 | PyUnicode_AS_UNICODE(po)); |
| 1705 | Py_INCREF(Py_None); |
| 1706 | return Py_None; |
| 1707 | } |
| 1708 | /* Drop the argument parsing error as narrow strings |
| 1709 | are also valid. */ |
| 1710 | PyErr_Clear(); |
| 1711 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1712 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1713 | &path, &i)) |
| 1714 | return NULL; |
| 1715 | Py_BEGIN_ALLOW_THREADS |
| 1716 | attr = GetFileAttributesA(path); |
| 1717 | if (attr != 0xFFFFFFFF) { |
| 1718 | if (i & _S_IWRITE) |
| 1719 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1720 | else |
| 1721 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1722 | res = SetFileAttributesA(path, attr); |
| 1723 | } |
| 1724 | else |
| 1725 | res = 0; |
| 1726 | Py_END_ALLOW_THREADS |
| 1727 | if (!res) { |
| 1728 | win32_error("chmod", path); |
| 1729 | PyMem_Free(path); |
| 1730 | return NULL; |
| 1731 | } |
| 1732 | PyMem_Free(path); |
| 1733 | Py_INCREF(Py_None); |
| 1734 | return Py_None; |
| 1735 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1736 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1737 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1738 | return NULL; |
| 1739 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1740 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1741 | Py_END_ALLOW_THREADS |
| 1742 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1743 | return posix_error_with_allocated_filename(path); |
| 1744 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1745 | Py_INCREF(Py_None); |
| 1746 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1747 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1750 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1751 | #ifdef HAVE_CHFLAGS |
| 1752 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1753 | "chflags(path, flags)\n\n\ |
| 1754 | Set file flags."); |
| 1755 | |
| 1756 | static PyObject * |
| 1757 | posix_chflags(PyObject *self, PyObject *args) |
| 1758 | { |
| 1759 | char *path; |
| 1760 | unsigned long flags; |
| 1761 | int res; |
| 1762 | if (!PyArg_ParseTuple(args, "etk:chflags", |
| 1763 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1764 | return NULL; |
| 1765 | Py_BEGIN_ALLOW_THREADS |
| 1766 | res = chflags(path, flags); |
| 1767 | Py_END_ALLOW_THREADS |
| 1768 | if (res < 0) |
| 1769 | return posix_error_with_allocated_filename(path); |
| 1770 | PyMem_Free(path); |
| 1771 | Py_INCREF(Py_None); |
| 1772 | return Py_None; |
| 1773 | } |
| 1774 | #endif /* HAVE_CHFLAGS */ |
| 1775 | |
| 1776 | #ifdef HAVE_LCHFLAGS |
| 1777 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1778 | "lchflags(path, flags)\n\n\ |
| 1779 | Set file flags.\n\ |
| 1780 | This function will not follow symbolic links."); |
| 1781 | |
| 1782 | static PyObject * |
| 1783 | posix_lchflags(PyObject *self, PyObject *args) |
| 1784 | { |
| 1785 | char *path; |
| 1786 | unsigned long flags; |
| 1787 | int res; |
| 1788 | if (!PyArg_ParseTuple(args, "etk:lchflags", |
| 1789 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1790 | return NULL; |
| 1791 | Py_BEGIN_ALLOW_THREADS |
| 1792 | res = lchflags(path, flags); |
| 1793 | Py_END_ALLOW_THREADS |
| 1794 | if (res < 0) |
| 1795 | return posix_error_with_allocated_filename(path); |
| 1796 | PyMem_Free(path); |
| 1797 | Py_INCREF(Py_None); |
| 1798 | return Py_None; |
| 1799 | } |
| 1800 | #endif /* HAVE_LCHFLAGS */ |
| 1801 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1802 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1803 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1804 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1805 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1806 | |
| 1807 | static PyObject * |
| 1808 | posix_chroot(PyObject *self, PyObject *args) |
| 1809 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1810 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1811 | } |
| 1812 | #endif |
| 1813 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1814 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1815 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1816 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1817 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1818 | |
| 1819 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1820 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1821 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1822 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1823 | } |
| 1824 | #endif /* HAVE_FSYNC */ |
| 1825 | |
| 1826 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1827 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1828 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1829 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1830 | #endif |
| 1831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1832 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1833 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1834 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1835 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1836 | |
| 1837 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1838 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1839 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1840 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1841 | } |
| 1842 | #endif /* HAVE_FDATASYNC */ |
| 1843 | |
| 1844 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1845 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1846 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1847 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1848 | 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] | 1849 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1850 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1851 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1852 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1853 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1854 | int uid, gid; |
| 1855 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1856 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1857 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1858 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1859 | return NULL; |
| 1860 | Py_BEGIN_ALLOW_THREADS |
| 1861 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1862 | Py_END_ALLOW_THREADS |
| 1863 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1864 | return posix_error_with_allocated_filename(path); |
| 1865 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1866 | Py_INCREF(Py_None); |
| 1867 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1868 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1869 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1870 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1871 | #ifdef HAVE_LCHOWN |
| 1872 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1873 | "lchown(path, uid, gid)\n\n\ |
| 1874 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1875 | This function will not follow symbolic links."); |
| 1876 | |
| 1877 | static PyObject * |
| 1878 | posix_lchown(PyObject *self, PyObject *args) |
| 1879 | { |
| 1880 | char *path = NULL; |
| 1881 | int uid, gid; |
| 1882 | int res; |
| 1883 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1884 | Py_FileSystemDefaultEncoding, &path, |
| 1885 | &uid, &gid)) |
| 1886 | return NULL; |
| 1887 | Py_BEGIN_ALLOW_THREADS |
| 1888 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1889 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1890 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1891 | return posix_error_with_allocated_filename(path); |
| 1892 | PyMem_Free(path); |
| 1893 | Py_INCREF(Py_None); |
| 1894 | return Py_None; |
| 1895 | } |
| 1896 | #endif /* HAVE_LCHOWN */ |
| 1897 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1898 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1899 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1900 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1901 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1902 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1903 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1904 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1905 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1906 | { |
| 1907 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1908 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1909 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1910 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1911 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1912 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1913 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1914 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1915 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1916 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1917 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1918 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1919 | return PyUnicode_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1920 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1921 | |
| 1922 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1923 | "getcwdu() -> path\n\n\ |
| 1924 | Return a unicode string representing the current working directory."); |
| 1925 | |
| 1926 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1927 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1928 | { |
| 1929 | char buf[1026]; |
| 1930 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1931 | |
| 1932 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1933 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1934 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1935 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1936 | wchar_t *wbuf2 = wbuf; |
| 1937 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1938 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1939 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 1940 | /* If the buffer is large enough, len does not include the |
| 1941 | terminating \0. If the buffer is too small, len includes |
| 1942 | the space needed for the terminator. */ |
| 1943 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 1944 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 1945 | if (wbuf2) |
| 1946 | len = GetCurrentDirectoryW(len, wbuf2); |
| 1947 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1948 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1949 | if (!wbuf2) { |
| 1950 | PyErr_NoMemory(); |
| 1951 | return NULL; |
| 1952 | } |
| 1953 | if (!len) { |
| 1954 | if (wbuf2 != wbuf) free(wbuf2); |
| 1955 | return win32_error("getcwdu", NULL); |
| 1956 | } |
| 1957 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 1958 | if (wbuf2 != wbuf) free(wbuf2); |
| 1959 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1960 | } |
| 1961 | #endif |
| 1962 | |
| 1963 | Py_BEGIN_ALLOW_THREADS |
| 1964 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1965 | res = _getcwd2(buf, sizeof buf); |
| 1966 | #else |
| 1967 | res = getcwd(buf, sizeof buf); |
| 1968 | #endif |
| 1969 | Py_END_ALLOW_THREADS |
| 1970 | if (res == NULL) |
| 1971 | return posix_error(); |
| 1972 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1973 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1974 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1975 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1976 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1977 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1978 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1979 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1980 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1981 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1982 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1983 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1984 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1985 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1986 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1987 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1988 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1990 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1991 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1992 | Return a list containing the names of the entries in the directory.\n\ |
| 1993 | \n\ |
| 1994 | path: path of directory to list\n\ |
| 1995 | \n\ |
| 1996 | 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] | 1997 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1998 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1999 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2000 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2001 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2002 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2003 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2004 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2005 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2006 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2007 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2008 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2009 | WIN32_FIND_DATA FileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2010 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2011 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2012 | 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] | 2013 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2014 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2015 | /* If on wide-character-capable OS see if argument |
| 2016 | is Unicode and if so use wide API. */ |
| 2017 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2018 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2019 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2020 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2021 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2022 | Py_UNICODE wch; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2023 | /* Overallocate for \\*.*\0 */ |
| 2024 | len = PyUnicode_GET_SIZE(po); |
| 2025 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2026 | if (!wnamebuf) { |
| 2027 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2028 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2029 | } |
| 2030 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2031 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 2032 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2033 | wnamebuf[len++] = L'\\'; |
| 2034 | wcscpy(wnamebuf + len, L"*.*"); |
| 2035 | if ((d = PyList_New(0)) == NULL) { |
| 2036 | free(wnamebuf); |
| 2037 | return NULL; |
| 2038 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2039 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2040 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2041 | int error = GetLastError(); |
| 2042 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2043 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2044 | return d; |
| 2045 | } |
| 2046 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2047 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2048 | free(wnamebuf); |
| 2049 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2050 | } |
| 2051 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2052 | /* Skip over . and .. */ |
| 2053 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2054 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2055 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2056 | if (v == NULL) { |
| 2057 | Py_DECREF(d); |
| 2058 | d = NULL; |
| 2059 | break; |
| 2060 | } |
| 2061 | if (PyList_Append(d, v) != 0) { |
| 2062 | Py_DECREF(v); |
| 2063 | Py_DECREF(d); |
| 2064 | d = NULL; |
| 2065 | break; |
| 2066 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2067 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2068 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2069 | Py_BEGIN_ALLOW_THREADS |
| 2070 | result = FindNextFileW(hFindFile, &wFileData); |
| 2071 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2072 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2073 | it got to the end of the directory. */ |
| 2074 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2075 | Py_DECREF(d); |
| 2076 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2077 | FindClose(hFindFile); |
| 2078 | free(wnamebuf); |
| 2079 | return NULL; |
| 2080 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2081 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2082 | |
| 2083 | if (FindClose(hFindFile) == FALSE) { |
| 2084 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2085 | win32_error_unicode("FindClose", wnamebuf); |
| 2086 | free(wnamebuf); |
| 2087 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2088 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2089 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2090 | return d; |
| 2091 | } |
| 2092 | /* Drop the argument parsing error as narrow strings |
| 2093 | are also valid. */ |
| 2094 | PyErr_Clear(); |
| 2095 | } |
| 2096 | #endif |
| 2097 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2098 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2099 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2100 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2101 | if (len > 0) { |
| 2102 | char ch = namebuf[len-1]; |
| 2103 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2104 | namebuf[len++] = '/'; |
| 2105 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2106 | strcpy(namebuf + len, "*.*"); |
| 2107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2108 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2109 | return NULL; |
| 2110 | |
| 2111 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2112 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2113 | int error = GetLastError(); |
| 2114 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2115 | return d; |
| 2116 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2117 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2118 | } |
| 2119 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2120 | /* Skip over . and .. */ |
| 2121 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2122 | strcmp(FileData.cFileName, "..") != 0) { |
| 2123 | v = PyString_FromString(FileData.cFileName); |
| 2124 | if (v == NULL) { |
| 2125 | Py_DECREF(d); |
| 2126 | d = NULL; |
| 2127 | break; |
| 2128 | } |
| 2129 | if (PyList_Append(d, v) != 0) { |
| 2130 | Py_DECREF(v); |
| 2131 | Py_DECREF(d); |
| 2132 | d = NULL; |
| 2133 | break; |
| 2134 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2135 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2136 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2137 | Py_BEGIN_ALLOW_THREADS |
| 2138 | result = FindNextFile(hFindFile, &FileData); |
| 2139 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2140 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2141 | it got to the end of the directory. */ |
| 2142 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2143 | Py_DECREF(d); |
| 2144 | win32_error("FindNextFile", namebuf); |
| 2145 | FindClose(hFindFile); |
| 2146 | return NULL; |
| 2147 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2148 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2149 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2150 | if (FindClose(hFindFile) == FALSE) { |
| 2151 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2152 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2153 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2154 | |
| 2155 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2156 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2157 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2158 | |
| 2159 | #ifndef MAX_PATH |
| 2160 | #define MAX_PATH CCHMAXPATH |
| 2161 | #endif |
| 2162 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2163 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2164 | PyObject *d, *v; |
| 2165 | char namebuf[MAX_PATH+5]; |
| 2166 | HDIR hdir = 1; |
| 2167 | ULONG srchcnt = 1; |
| 2168 | FILEFINDBUF3 ep; |
| 2169 | APIRET rc; |
| 2170 | |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 2171 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 2172 | Py_FileSystemDefaultEncoding, &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2173 | return NULL; |
| 2174 | if (len >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2175 | PyMem_Free(name); |
| 2176 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2177 | return NULL; |
| 2178 | } |
| 2179 | strcpy(namebuf, name); |
| 2180 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2181 | if (*pt == ALTSEP) |
| 2182 | *pt = SEP; |
| 2183 | if (namebuf[len-1] != SEP) |
| 2184 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2185 | strcpy(namebuf + len, "*.*"); |
| 2186 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2187 | if ((d = PyList_New(0)) == NULL) { |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2188 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2189 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2190 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2191 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2192 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2193 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2194 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2195 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2196 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2197 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2198 | |
| 2199 | if (rc != NO_ERROR) { |
| 2200 | errno = ENOENT; |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2201 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2204 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2205 | do { |
| 2206 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2207 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2208 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2209 | |
| 2210 | strcpy(namebuf, ep.achName); |
| 2211 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2212 | /* Leave Case of Name Alone -- In Native Form */ |
| 2213 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2214 | |
| 2215 | v = PyString_FromString(namebuf); |
| 2216 | if (v == NULL) { |
| 2217 | Py_DECREF(d); |
| 2218 | d = NULL; |
| 2219 | break; |
| 2220 | } |
| 2221 | if (PyList_Append(d, v) != 0) { |
| 2222 | Py_DECREF(v); |
| 2223 | Py_DECREF(d); |
| 2224 | d = NULL; |
| 2225 | break; |
| 2226 | } |
| 2227 | Py_DECREF(v); |
| 2228 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2229 | } |
| 2230 | |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2231 | PyMem_Free(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2232 | return d; |
| 2233 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2234 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2235 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2236 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2237 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2238 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2239 | int arg_is_unicode = 1; |
| 2240 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2241 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2242 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2243 | arg_is_unicode = 0; |
| 2244 | PyErr_Clear(); |
| 2245 | } |
| 2246 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2247 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2248 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2249 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2250 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2251 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2252 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2253 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2254 | return NULL; |
| 2255 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2256 | for (;;) { |
| 2257 | Py_BEGIN_ALLOW_THREADS |
| 2258 | ep = readdir(dirp); |
| 2259 | Py_END_ALLOW_THREADS |
| 2260 | if (ep == NULL) |
| 2261 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2262 | if (ep->d_name[0] == '.' && |
| 2263 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2264 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2265 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2266 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2267 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2268 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2269 | d = NULL; |
| 2270 | break; |
| 2271 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2272 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2273 | PyObject *w; |
| 2274 | |
| 2275 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2276 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2277 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2278 | if (w != NULL) { |
| 2279 | Py_DECREF(v); |
| 2280 | v = w; |
| 2281 | } |
| 2282 | else { |
| 2283 | /* fall back to the original byte string, as |
| 2284 | discussed in patch #683592 */ |
| 2285 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2286 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2287 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2288 | if (PyList_Append(d, v) != 0) { |
| 2289 | Py_DECREF(v); |
| 2290 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2291 | d = NULL; |
| 2292 | break; |
| 2293 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2294 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2295 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2296 | if (errno != 0 && d != NULL) { |
| 2297 | /* readdir() returned NULL and set errno */ |
| 2298 | closedir(dirp); |
| 2299 | Py_DECREF(d); |
| 2300 | return posix_error_with_allocated_filename(name); |
| 2301 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2302 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2303 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2304 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2305 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2306 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2307 | #endif /* which OS */ |
| 2308 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2309 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2310 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2311 | /* A helper function for abspath on win32 */ |
| 2312 | static PyObject * |
| 2313 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2314 | { |
| 2315 | /* assume encoded strings wont more than double no of chars */ |
| 2316 | char inbuf[MAX_PATH*2]; |
| 2317 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2318 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2319 | char outbuf[MAX_PATH*2]; |
| 2320 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2321 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2322 | if (unicode_file_names()) { |
| 2323 | PyUnicodeObject *po; |
| 2324 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2325 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2326 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2327 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2328 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2329 | woutbuf, &wtemp)) |
| 2330 | return win32_error("GetFullPathName", ""); |
| 2331 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2332 | } |
| 2333 | /* Drop the argument parsing error as narrow strings |
| 2334 | are also valid. */ |
| 2335 | PyErr_Clear(); |
| 2336 | } |
| 2337 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2338 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2339 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2340 | &insize)) |
| 2341 | return NULL; |
| 2342 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2343 | outbuf, &temp)) |
| 2344 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2345 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2346 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2347 | Py_FileSystemDefaultEncoding, NULL); |
| 2348 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2349 | return PyString_FromString(outbuf); |
| 2350 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2351 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2352 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2353 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2354 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2355 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2356 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2357 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2358 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2359 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2360 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2361 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2362 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2363 | |
| 2364 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2365 | if (unicode_file_names()) { |
| 2366 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2367 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2368 | Py_BEGIN_ALLOW_THREADS |
| 2369 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2370 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2371 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2372 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2373 | if (!res) |
| 2374 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2375 | Py_INCREF(Py_None); |
| 2376 | return Py_None; |
| 2377 | } |
| 2378 | /* Drop the argument parsing error as narrow strings |
| 2379 | are also valid. */ |
| 2380 | PyErr_Clear(); |
| 2381 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2382 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2383 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2384 | return NULL; |
| 2385 | Py_BEGIN_ALLOW_THREADS |
| 2386 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2387 | it is a simple dereference. */ |
| 2388 | res = CreateDirectoryA(path, NULL); |
| 2389 | Py_END_ALLOW_THREADS |
| 2390 | if (!res) { |
| 2391 | win32_error("mkdir", path); |
| 2392 | PyMem_Free(path); |
| 2393 | return NULL; |
| 2394 | } |
| 2395 | PyMem_Free(path); |
| 2396 | Py_INCREF(Py_None); |
| 2397 | return Py_None; |
| 2398 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2399 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2400 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2401 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2402 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2403 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2404 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2405 | res = mkdir(path); |
| 2406 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2407 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2408 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2409 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2410 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2411 | return posix_error_with_allocated_filename(path); |
| 2412 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2413 | Py_INCREF(Py_None); |
| 2414 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2415 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2418 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2419 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2420 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2421 | #include <sys/resource.h> |
| 2422 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2423 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2424 | |
| 2425 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2426 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2427 | "nice(inc) -> new_priority\n\n\ |
| 2428 | 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] | 2429 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2430 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2431 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2432 | { |
| 2433 | int increment, value; |
| 2434 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2435 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2436 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2437 | |
| 2438 | /* There are two flavours of 'nice': one that returns the new |
| 2439 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2440 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2441 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2442 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2443 | If we are of the nice family that returns the new priority, we |
| 2444 | need to clear errno before the call, and check if errno is filled |
| 2445 | before calling posix_error() on a returnvalue of -1, because the |
| 2446 | -1 may be the actual new priority! */ |
| 2447 | |
| 2448 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2449 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2450 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2451 | if (value == 0) |
| 2452 | value = getpriority(PRIO_PROCESS, 0); |
| 2453 | #endif |
| 2454 | if (value == -1 && errno != 0) |
| 2455 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2456 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2457 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2458 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2459 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2460 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2461 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2462 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2463 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2464 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2465 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2466 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2467 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2468 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2469 | PyObject *o1, *o2; |
| 2470 | char *p1, *p2; |
| 2471 | BOOL result; |
| 2472 | if (unicode_file_names()) { |
| 2473 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2474 | convert_to_unicode, &o1, |
| 2475 | convert_to_unicode, &o2)) |
| 2476 | PyErr_Clear(); |
| 2477 | else { |
| 2478 | Py_BEGIN_ALLOW_THREADS |
| 2479 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2480 | PyUnicode_AsUnicode(o2)); |
| 2481 | Py_END_ALLOW_THREADS |
| 2482 | Py_DECREF(o1); |
| 2483 | Py_DECREF(o2); |
| 2484 | if (!result) |
| 2485 | return win32_error("rename", NULL); |
| 2486 | Py_INCREF(Py_None); |
| 2487 | return Py_None; |
| 2488 | } |
| 2489 | } |
| 2490 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2491 | return NULL; |
| 2492 | Py_BEGIN_ALLOW_THREADS |
| 2493 | result = MoveFileA(p1, p2); |
| 2494 | Py_END_ALLOW_THREADS |
| 2495 | if (!result) |
| 2496 | return win32_error("rename", NULL); |
| 2497 | Py_INCREF(Py_None); |
| 2498 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2499 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2500 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2501 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2504 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2505 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2506 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2507 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2508 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2509 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2510 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2511 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2512 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2513 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2514 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2515 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2516 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2520 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2521 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2522 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2523 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2524 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2525 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2526 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2527 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2528 | 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] | 2529 | #else |
| 2530 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2531 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2534 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2535 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2536 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2537 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2538 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2539 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2540 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2541 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2542 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2543 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2544 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2545 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2546 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2547 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2548 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2549 | Py_END_ALLOW_THREADS |
| 2550 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2551 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2552 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2553 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2554 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2555 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2556 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2557 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2558 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2559 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2560 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2561 | { |
| 2562 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2563 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2564 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2565 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2566 | if (i < 0) |
| 2567 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2568 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2571 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2572 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2573 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2574 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2576 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2577 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2578 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2579 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2580 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2581 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2582 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2583 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2584 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2585 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2586 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2587 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2590 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2591 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2592 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2593 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2594 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2595 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2596 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2597 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2598 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2599 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2600 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2601 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2602 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2603 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2604 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2605 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2606 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2607 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2608 | u.sysname, |
| 2609 | u.nodename, |
| 2610 | u.release, |
| 2611 | u.version, |
| 2612 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2613 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2614 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2615 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2616 | static int |
| 2617 | extract_time(PyObject *t, long* sec, long* usec) |
| 2618 | { |
| 2619 | long intval; |
| 2620 | if (PyFloat_Check(t)) { |
| 2621 | double tval = PyFloat_AsDouble(t); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2622 | PyObject *intobj = Py_Type(t)->tp_as_number->nb_int(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2623 | if (!intobj) |
| 2624 | return -1; |
| 2625 | intval = PyInt_AsLong(intobj); |
| 2626 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2627 | if (intval == -1 && PyErr_Occurred()) |
| 2628 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2629 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2630 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2631 | if (*usec < 0) |
| 2632 | /* If rounding gave us a negative number, |
| 2633 | truncate. */ |
| 2634 | *usec = 0; |
| 2635 | return 0; |
| 2636 | } |
| 2637 | intval = PyInt_AsLong(t); |
| 2638 | if (intval == -1 && PyErr_Occurred()) |
| 2639 | return -1; |
| 2640 | *sec = intval; |
| 2641 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2642 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2643 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2644 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2645 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2646 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2647 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2648 | 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] | 2649 | 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] | 2650 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2651 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2652 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2653 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2654 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2655 | PyObject *arg; |
| 2656 | PyUnicodeObject *obwpath; |
| 2657 | wchar_t *wpath = NULL; |
| 2658 | char *apath = NULL; |
| 2659 | HANDLE hFile; |
| 2660 | long atimesec, mtimesec, ausec, musec; |
| 2661 | FILETIME atime, mtime; |
| 2662 | PyObject *result = NULL; |
| 2663 | |
| 2664 | if (unicode_file_names()) { |
| 2665 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2666 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2667 | Py_BEGIN_ALLOW_THREADS |
| 2668 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2669 | NULL, OPEN_EXISTING, |
| 2670 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2671 | Py_END_ALLOW_THREADS |
| 2672 | if (hFile == INVALID_HANDLE_VALUE) |
| 2673 | return win32_error_unicode("utime", wpath); |
| 2674 | } else |
| 2675 | /* Drop the argument parsing error as narrow strings |
| 2676 | are also valid. */ |
| 2677 | PyErr_Clear(); |
| 2678 | } |
| 2679 | if (!wpath) { |
| 2680 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2681 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2682 | return NULL; |
| 2683 | Py_BEGIN_ALLOW_THREADS |
| 2684 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2685 | NULL, OPEN_EXISTING, |
| 2686 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2687 | Py_END_ALLOW_THREADS |
| 2688 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2689 | win32_error("utime", apath); |
| 2690 | PyMem_Free(apath); |
| 2691 | return NULL; |
| 2692 | } |
| 2693 | PyMem_Free(apath); |
| 2694 | } |
| 2695 | |
| 2696 | if (arg == Py_None) { |
| 2697 | SYSTEMTIME now; |
| 2698 | GetSystemTime(&now); |
| 2699 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2700 | !SystemTimeToFileTime(&now, &atime)) { |
| 2701 | win32_error("utime", NULL); |
| 2702 | goto done; |
| 2703 | } |
| 2704 | } |
| 2705 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2706 | PyErr_SetString(PyExc_TypeError, |
| 2707 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2708 | goto done; |
| 2709 | } |
| 2710 | else { |
| 2711 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2712 | &atimesec, &ausec) == -1) |
| 2713 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2714 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2715 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2716 | &mtimesec, &musec) == -1) |
| 2717 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2718 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2719 | } |
| 2720 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2721 | /* Avoid putting the file name into the error here, |
| 2722 | as that may confuse the user into believing that |
| 2723 | something is wrong with the file, when it also |
| 2724 | could be the time stamp that gives a problem. */ |
| 2725 | win32_error("utime", NULL); |
| 2726 | } |
| 2727 | Py_INCREF(Py_None); |
| 2728 | result = Py_None; |
| 2729 | done: |
| 2730 | CloseHandle(hFile); |
| 2731 | return result; |
| 2732 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2733 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2734 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2735 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2736 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2737 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2738 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2739 | #if defined(HAVE_UTIMES) |
| 2740 | struct timeval buf[2]; |
| 2741 | #define ATIME buf[0].tv_sec |
| 2742 | #define MTIME buf[1].tv_sec |
| 2743 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2744 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2745 | struct utimbuf buf; |
| 2746 | #define ATIME buf.actime |
| 2747 | #define MTIME buf.modtime |
| 2748 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2749 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2750 | time_t buf[2]; |
| 2751 | #define ATIME buf[0] |
| 2752 | #define MTIME buf[1] |
| 2753 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2754 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2755 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2756 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2757 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2758 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2759 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2760 | if (arg == Py_None) { |
| 2761 | /* optional time values not given */ |
| 2762 | Py_BEGIN_ALLOW_THREADS |
| 2763 | res = utime(path, NULL); |
| 2764 | Py_END_ALLOW_THREADS |
| 2765 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2766 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2767 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2768 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2769 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2770 | return NULL; |
| 2771 | } |
| 2772 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2773 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2774 | &atime, &ausec) == -1) { |
| 2775 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2776 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2777 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2778 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2779 | &mtime, &musec) == -1) { |
| 2780 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2781 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2782 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2783 | ATIME = atime; |
| 2784 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2785 | #ifdef HAVE_UTIMES |
| 2786 | buf[0].tv_usec = ausec; |
| 2787 | buf[1].tv_usec = musec; |
| 2788 | Py_BEGIN_ALLOW_THREADS |
| 2789 | res = utimes(path, buf); |
| 2790 | Py_END_ALLOW_THREADS |
| 2791 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2792 | Py_BEGIN_ALLOW_THREADS |
| 2793 | res = utime(path, UTIME_ARG); |
| 2794 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2795 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2796 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2797 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2798 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2799 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2800 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2801 | Py_INCREF(Py_None); |
| 2802 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2803 | #undef UTIME_ARG |
| 2804 | #undef ATIME |
| 2805 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2806 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2809 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2810 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2812 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2813 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2814 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2815 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2816 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2817 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2818 | { |
| 2819 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2820 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2821 | return NULL; |
| 2822 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2823 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2826 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2827 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2828 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2829 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2830 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2831 | for (i = 0; i < count; i++) |
| 2832 | PyMem_Free(array[i]); |
| 2833 | PyMem_DEL(array); |
| 2834 | } |
| 2835 | #endif |
| 2836 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2837 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2838 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2839 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2840 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2841 | Execute an executable path with arguments, replacing current process.\n\ |
| 2842 | \n\ |
| 2843 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2844 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2845 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2846 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2847 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2848 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2849 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2850 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2851 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2852 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2853 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2854 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2855 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2856 | argv is a list or tuple of strings. */ |
| 2857 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2858 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2859 | Py_FileSystemDefaultEncoding, |
| 2860 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2861 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2862 | if (PyList_Check(argv)) { |
| 2863 | argc = PyList_Size(argv); |
| 2864 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2865 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2866 | else if (PyTuple_Check(argv)) { |
| 2867 | argc = PyTuple_Size(argv); |
| 2868 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2869 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2870 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2871 | 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] | 2872 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2873 | return NULL; |
| 2874 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 2875 | if (argc < 1) { |
| 2876 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 2877 | PyMem_Free(path); |
| 2878 | return NULL; |
| 2879 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2880 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2881 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2882 | if (argvlist == NULL) { |
| 2883 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2884 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2885 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2886 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2887 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2888 | Py_FileSystemDefaultEncoding, |
| 2889 | &argvlist[i])) { |
| 2890 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2891 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2892 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2893 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2894 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2895 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2896 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2897 | } |
| 2898 | argvlist[argc] = NULL; |
| 2899 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2900 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2901 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2902 | /* If we get here it's definitely an error */ |
| 2903 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2904 | free_string_array(argvlist, argc); |
| 2905 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2906 | return posix_error(); |
| 2907 | } |
| 2908 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2909 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2910 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2911 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2912 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2913 | \n\ |
| 2914 | path: path of executable file\n\ |
| 2915 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2916 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2917 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2918 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2919 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2920 | { |
| 2921 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2922 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2923 | char **argvlist; |
| 2924 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2925 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2926 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2927 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2928 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2929 | |
| 2930 | /* execve has three arguments: (path, argv, env), where |
| 2931 | argv is a list or tuple of strings and env is a dictionary |
| 2932 | like posix.environ. */ |
| 2933 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2934 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2935 | Py_FileSystemDefaultEncoding, |
| 2936 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2937 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2938 | if (PyList_Check(argv)) { |
| 2939 | argc = PyList_Size(argv); |
| 2940 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2941 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2942 | else if (PyTuple_Check(argv)) { |
| 2943 | argc = PyTuple_Size(argv); |
| 2944 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2945 | } |
| 2946 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2947 | PyErr_SetString(PyExc_TypeError, |
| 2948 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2949 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2950 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2951 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2952 | PyErr_SetString(PyExc_TypeError, |
| 2953 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2954 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2957 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2958 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2959 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2960 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2961 | } |
| 2962 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2963 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2964 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2965 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2966 | &argvlist[i])) |
| 2967 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2968 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2969 | goto fail_1; |
| 2970 | } |
| 2971 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2972 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2973 | argvlist[argc] = NULL; |
| 2974 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2975 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2976 | if (i < 0) |
| 2977 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2978 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2979 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2980 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2981 | goto fail_1; |
| 2982 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2983 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2984 | keys = PyMapping_Keys(env); |
| 2985 | vals = PyMapping_Values(env); |
| 2986 | if (!keys || !vals) |
| 2987 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2988 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2989 | PyErr_SetString(PyExc_TypeError, |
| 2990 | "execve(): env.keys() or env.values() is not a list"); |
| 2991 | goto fail_2; |
| 2992 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2993 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2994 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2995 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2996 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2997 | |
| 2998 | key = PyList_GetItem(keys, pos); |
| 2999 | val = PyList_GetItem(vals, pos); |
| 3000 | if (!key || !val) |
| 3001 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3002 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3003 | if (!PyArg_Parse( |
| 3004 | key, |
| 3005 | "s;execve() arg 3 contains a non-string key", |
| 3006 | &k) || |
| 3007 | !PyArg_Parse( |
| 3008 | val, |
| 3009 | "s;execve() arg 3 contains a non-string value", |
| 3010 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3011 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3012 | goto fail_2; |
| 3013 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3014 | |
| 3015 | #if defined(PYOS_OS2) |
| 3016 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3017 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3018 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3019 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3020 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3021 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3022 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3023 | goto fail_2; |
| 3024 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3025 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3026 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3027 | #if defined(PYOS_OS2) |
| 3028 | } |
| 3029 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3030 | } |
| 3031 | envlist[envc] = 0; |
| 3032 | |
| 3033 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3034 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3035 | /* If we get here it's definitely an error */ |
| 3036 | |
| 3037 | (void) posix_error(); |
| 3038 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3039 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3040 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3041 | PyMem_DEL(envlist[envc]); |
| 3042 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3043 | fail_1: |
| 3044 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3045 | Py_XDECREF(vals); |
| 3046 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3047 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3048 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3049 | return NULL; |
| 3050 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3051 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3052 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3053 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3054 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3055 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3056 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3057 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3058 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3059 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3060 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3061 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3062 | |
| 3063 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3064 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3065 | { |
| 3066 | char *path; |
| 3067 | PyObject *argv; |
| 3068 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3069 | int mode, i; |
| 3070 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3071 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3072 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3073 | |
| 3074 | /* spawnv has three arguments: (mode, path, argv), where |
| 3075 | argv is a list or tuple of strings. */ |
| 3076 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3077 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3078 | Py_FileSystemDefaultEncoding, |
| 3079 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3080 | return NULL; |
| 3081 | if (PyList_Check(argv)) { |
| 3082 | argc = PyList_Size(argv); |
| 3083 | getitem = PyList_GetItem; |
| 3084 | } |
| 3085 | else if (PyTuple_Check(argv)) { |
| 3086 | argc = PyTuple_Size(argv); |
| 3087 | getitem = PyTuple_GetItem; |
| 3088 | } |
| 3089 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3090 | PyErr_SetString(PyExc_TypeError, |
| 3091 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3092 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3093 | return NULL; |
| 3094 | } |
| 3095 | |
| 3096 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3097 | if (argvlist == NULL) { |
| 3098 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3099 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3100 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3101 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3102 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3103 | Py_FileSystemDefaultEncoding, |
| 3104 | &argvlist[i])) { |
| 3105 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3106 | PyErr_SetString( |
| 3107 | PyExc_TypeError, |
| 3108 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3109 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3110 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | argvlist[argc] = NULL; |
| 3114 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3115 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3116 | Py_BEGIN_ALLOW_THREADS |
| 3117 | spawnval = spawnv(mode, path, argvlist); |
| 3118 | Py_END_ALLOW_THREADS |
| 3119 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3120 | if (mode == _OLD_P_OVERLAY) |
| 3121 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3122 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3123 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3124 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3125 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3126 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3127 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3128 | free_string_array(argvlist, argc); |
| 3129 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3130 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3131 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3132 | return posix_error(); |
| 3133 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3134 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3135 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3136 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3137 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3138 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3142 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3143 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3144 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3145 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3146 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3147 | path: path of executable file\n\ |
| 3148 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3149 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3150 | |
| 3151 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3152 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3153 | { |
| 3154 | char *path; |
| 3155 | PyObject *argv, *env; |
| 3156 | char **argvlist; |
| 3157 | char **envlist; |
| 3158 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3159 | int mode, pos, envc; |
| 3160 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3161 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3162 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3163 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3164 | |
| 3165 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3166 | argv is a list or tuple of strings and env is a dictionary |
| 3167 | like posix.environ. */ |
| 3168 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3169 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3170 | Py_FileSystemDefaultEncoding, |
| 3171 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3172 | return NULL; |
| 3173 | if (PyList_Check(argv)) { |
| 3174 | argc = PyList_Size(argv); |
| 3175 | getitem = PyList_GetItem; |
| 3176 | } |
| 3177 | else if (PyTuple_Check(argv)) { |
| 3178 | argc = PyTuple_Size(argv); |
| 3179 | getitem = PyTuple_GetItem; |
| 3180 | } |
| 3181 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3182 | PyErr_SetString(PyExc_TypeError, |
| 3183 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3184 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3185 | } |
| 3186 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3187 | PyErr_SetString(PyExc_TypeError, |
| 3188 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3189 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | argvlist = PyMem_NEW(char *, argc+1); |
| 3193 | if (argvlist == NULL) { |
| 3194 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3195 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3196 | } |
| 3197 | for (i = 0; i < argc; i++) { |
| 3198 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3199 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3200 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3201 | &argvlist[i])) |
| 3202 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3203 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3204 | goto fail_1; |
| 3205 | } |
| 3206 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3207 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3208 | argvlist[argc] = NULL; |
| 3209 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3210 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3211 | if (i < 0) |
| 3212 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3213 | envlist = PyMem_NEW(char *, i + 1); |
| 3214 | if (envlist == NULL) { |
| 3215 | PyErr_NoMemory(); |
| 3216 | goto fail_1; |
| 3217 | } |
| 3218 | envc = 0; |
| 3219 | keys = PyMapping_Keys(env); |
| 3220 | vals = PyMapping_Values(env); |
| 3221 | if (!keys || !vals) |
| 3222 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3223 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3224 | PyErr_SetString(PyExc_TypeError, |
| 3225 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3226 | goto fail_2; |
| 3227 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3228 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3229 | for (pos = 0; pos < i; pos++) { |
| 3230 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3231 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3232 | |
| 3233 | key = PyList_GetItem(keys, pos); |
| 3234 | val = PyList_GetItem(vals, pos); |
| 3235 | if (!key || !val) |
| 3236 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3237 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3238 | if (!PyArg_Parse( |
| 3239 | key, |
| 3240 | "s;spawnve() arg 3 contains a non-string key", |
| 3241 | &k) || |
| 3242 | !PyArg_Parse( |
| 3243 | val, |
| 3244 | "s;spawnve() arg 3 contains a non-string value", |
| 3245 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3246 | { |
| 3247 | goto fail_2; |
| 3248 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3249 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3250 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3251 | if (p == NULL) { |
| 3252 | PyErr_NoMemory(); |
| 3253 | goto fail_2; |
| 3254 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3255 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3256 | envlist[envc++] = p; |
| 3257 | } |
| 3258 | envlist[envc] = 0; |
| 3259 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3260 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3261 | Py_BEGIN_ALLOW_THREADS |
| 3262 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3263 | Py_END_ALLOW_THREADS |
| 3264 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3265 | if (mode == _OLD_P_OVERLAY) |
| 3266 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3267 | |
| 3268 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3269 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3270 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3271 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3272 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3273 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3274 | (void) posix_error(); |
| 3275 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3276 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3277 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3278 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3279 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3280 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3281 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3282 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3283 | while (--envc >= 0) |
| 3284 | PyMem_DEL(envlist[envc]); |
| 3285 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3286 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3287 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3288 | Py_XDECREF(vals); |
| 3289 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3290 | fail_0: |
| 3291 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3292 | return res; |
| 3293 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3294 | |
| 3295 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3296 | #if defined(PYOS_OS2) |
| 3297 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3298 | "spawnvp(mode, file, args)\n\n\ |
| 3299 | Execute the program 'file' in a new process, using the environment\n\ |
| 3300 | search path to find the file.\n\ |
| 3301 | \n\ |
| 3302 | mode: mode of process creation\n\ |
| 3303 | file: executable file name\n\ |
| 3304 | args: tuple or list of strings"); |
| 3305 | |
| 3306 | static PyObject * |
| 3307 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3308 | { |
| 3309 | char *path; |
| 3310 | PyObject *argv; |
| 3311 | char **argvlist; |
| 3312 | int mode, i, argc; |
| 3313 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3314 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3315 | |
| 3316 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3317 | argv is a list or tuple of strings. */ |
| 3318 | |
| 3319 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3320 | Py_FileSystemDefaultEncoding, |
| 3321 | &path, &argv)) |
| 3322 | return NULL; |
| 3323 | if (PyList_Check(argv)) { |
| 3324 | argc = PyList_Size(argv); |
| 3325 | getitem = PyList_GetItem; |
| 3326 | } |
| 3327 | else if (PyTuple_Check(argv)) { |
| 3328 | argc = PyTuple_Size(argv); |
| 3329 | getitem = PyTuple_GetItem; |
| 3330 | } |
| 3331 | else { |
| 3332 | PyErr_SetString(PyExc_TypeError, |
| 3333 | "spawnvp() arg 2 must be a tuple or list"); |
| 3334 | PyMem_Free(path); |
| 3335 | return NULL; |
| 3336 | } |
| 3337 | |
| 3338 | argvlist = PyMem_NEW(char *, argc+1); |
| 3339 | if (argvlist == NULL) { |
| 3340 | PyMem_Free(path); |
| 3341 | return PyErr_NoMemory(); |
| 3342 | } |
| 3343 | for (i = 0; i < argc; i++) { |
| 3344 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3345 | Py_FileSystemDefaultEncoding, |
| 3346 | &argvlist[i])) { |
| 3347 | free_string_array(argvlist, i); |
| 3348 | PyErr_SetString( |
| 3349 | PyExc_TypeError, |
| 3350 | "spawnvp() arg 2 must contain only strings"); |
| 3351 | PyMem_Free(path); |
| 3352 | return NULL; |
| 3353 | } |
| 3354 | } |
| 3355 | argvlist[argc] = NULL; |
| 3356 | |
| 3357 | Py_BEGIN_ALLOW_THREADS |
| 3358 | #if defined(PYCC_GCC) |
| 3359 | spawnval = spawnvp(mode, path, argvlist); |
| 3360 | #else |
| 3361 | spawnval = _spawnvp(mode, path, argvlist); |
| 3362 | #endif |
| 3363 | Py_END_ALLOW_THREADS |
| 3364 | |
| 3365 | free_string_array(argvlist, argc); |
| 3366 | PyMem_Free(path); |
| 3367 | |
| 3368 | if (spawnval == -1) |
| 3369 | return posix_error(); |
| 3370 | else |
| 3371 | return Py_BuildValue("l", (long) spawnval); |
| 3372 | } |
| 3373 | |
| 3374 | |
| 3375 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3376 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3377 | Execute the program 'file' in a new process, using the environment\n\ |
| 3378 | search path to find the file.\n\ |
| 3379 | \n\ |
| 3380 | mode: mode of process creation\n\ |
| 3381 | file: executable file name\n\ |
| 3382 | args: tuple or list of arguments\n\ |
| 3383 | env: dictionary of strings mapping to strings"); |
| 3384 | |
| 3385 | static PyObject * |
| 3386 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3387 | { |
| 3388 | char *path; |
| 3389 | PyObject *argv, *env; |
| 3390 | char **argvlist; |
| 3391 | char **envlist; |
| 3392 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3393 | int mode, i, pos, argc, envc; |
| 3394 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3395 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3396 | int lastarg = 0; |
| 3397 | |
| 3398 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3399 | argv is a list or tuple of strings and env is a dictionary |
| 3400 | like posix.environ. */ |
| 3401 | |
| 3402 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3403 | Py_FileSystemDefaultEncoding, |
| 3404 | &path, &argv, &env)) |
| 3405 | return NULL; |
| 3406 | if (PyList_Check(argv)) { |
| 3407 | argc = PyList_Size(argv); |
| 3408 | getitem = PyList_GetItem; |
| 3409 | } |
| 3410 | else if (PyTuple_Check(argv)) { |
| 3411 | argc = PyTuple_Size(argv); |
| 3412 | getitem = PyTuple_GetItem; |
| 3413 | } |
| 3414 | else { |
| 3415 | PyErr_SetString(PyExc_TypeError, |
| 3416 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3417 | goto fail_0; |
| 3418 | } |
| 3419 | if (!PyMapping_Check(env)) { |
| 3420 | PyErr_SetString(PyExc_TypeError, |
| 3421 | "spawnvpe() arg 3 must be a mapping object"); |
| 3422 | goto fail_0; |
| 3423 | } |
| 3424 | |
| 3425 | argvlist = PyMem_NEW(char *, argc+1); |
| 3426 | if (argvlist == NULL) { |
| 3427 | PyErr_NoMemory(); |
| 3428 | goto fail_0; |
| 3429 | } |
| 3430 | for (i = 0; i < argc; i++) { |
| 3431 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3432 | "et;spawnvpe() arg 2 must contain only strings", |
| 3433 | Py_FileSystemDefaultEncoding, |
| 3434 | &argvlist[i])) |
| 3435 | { |
| 3436 | lastarg = i; |
| 3437 | goto fail_1; |
| 3438 | } |
| 3439 | } |
| 3440 | lastarg = argc; |
| 3441 | argvlist[argc] = NULL; |
| 3442 | |
| 3443 | i = PyMapping_Size(env); |
| 3444 | if (i < 0) |
| 3445 | goto fail_1; |
| 3446 | envlist = PyMem_NEW(char *, i + 1); |
| 3447 | if (envlist == NULL) { |
| 3448 | PyErr_NoMemory(); |
| 3449 | goto fail_1; |
| 3450 | } |
| 3451 | envc = 0; |
| 3452 | keys = PyMapping_Keys(env); |
| 3453 | vals = PyMapping_Values(env); |
| 3454 | if (!keys || !vals) |
| 3455 | goto fail_2; |
| 3456 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3457 | PyErr_SetString(PyExc_TypeError, |
| 3458 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3459 | goto fail_2; |
| 3460 | } |
| 3461 | |
| 3462 | for (pos = 0; pos < i; pos++) { |
| 3463 | char *p, *k, *v; |
| 3464 | size_t len; |
| 3465 | |
| 3466 | key = PyList_GetItem(keys, pos); |
| 3467 | val = PyList_GetItem(vals, pos); |
| 3468 | if (!key || !val) |
| 3469 | goto fail_2; |
| 3470 | |
| 3471 | if (!PyArg_Parse( |
| 3472 | key, |
| 3473 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3474 | &k) || |
| 3475 | !PyArg_Parse( |
| 3476 | val, |
| 3477 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3478 | &v)) |
| 3479 | { |
| 3480 | goto fail_2; |
| 3481 | } |
| 3482 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3483 | p = PyMem_NEW(char, len); |
| 3484 | if (p == NULL) { |
| 3485 | PyErr_NoMemory(); |
| 3486 | goto fail_2; |
| 3487 | } |
| 3488 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3489 | envlist[envc++] = p; |
| 3490 | } |
| 3491 | envlist[envc] = 0; |
| 3492 | |
| 3493 | Py_BEGIN_ALLOW_THREADS |
| 3494 | #if defined(PYCC_GCC) |
| 3495 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3496 | #else |
| 3497 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3498 | #endif |
| 3499 | Py_END_ALLOW_THREADS |
| 3500 | |
| 3501 | if (spawnval == -1) |
| 3502 | (void) posix_error(); |
| 3503 | else |
| 3504 | res = Py_BuildValue("l", (long) spawnval); |
| 3505 | |
| 3506 | fail_2: |
| 3507 | while (--envc >= 0) |
| 3508 | PyMem_DEL(envlist[envc]); |
| 3509 | PyMem_DEL(envlist); |
| 3510 | fail_1: |
| 3511 | free_string_array(argvlist, lastarg); |
| 3512 | Py_XDECREF(vals); |
| 3513 | Py_XDECREF(keys); |
| 3514 | fail_0: |
| 3515 | PyMem_Free(path); |
| 3516 | return res; |
| 3517 | } |
| 3518 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3519 | #endif /* HAVE_SPAWNV */ |
| 3520 | |
| 3521 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3522 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3523 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3524 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3525 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3526 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3527 | 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] | 3528 | |
| 3529 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3530 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3531 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3532 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3533 | if (pid == -1) |
| 3534 | return posix_error(); |
| 3535 | PyOS_AfterFork(); |
| 3536 | return PyInt_FromLong((long)pid); |
| 3537 | } |
| 3538 | #endif |
| 3539 | |
| 3540 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3541 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3542 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3543 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3544 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3545 | 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] | 3546 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3547 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3548 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3549 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3550 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3551 | if (pid == -1) |
| 3552 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3553 | if (pid == 0) |
| 3554 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3555 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3556 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3557 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3558 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3559 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3560 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3561 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3562 | #define DEV_PTY_FILE "/dev/ptc" |
| 3563 | #define HAVE_DEV_PTMX |
| 3564 | #else |
| 3565 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3566 | #endif |
| 3567 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3568 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3569 | #ifdef HAVE_PTY_H |
| 3570 | #include <pty.h> |
| 3571 | #else |
| 3572 | #ifdef HAVE_LIBUTIL_H |
| 3573 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3574 | #endif /* HAVE_LIBUTIL_H */ |
| 3575 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3576 | #ifdef HAVE_STROPTS_H |
| 3577 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3578 | #endif |
| 3579 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3580 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3581 | #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] | 3582 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3583 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3584 | 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] | 3585 | |
| 3586 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3587 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3588 | { |
| 3589 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3590 | #ifndef HAVE_OPENPTY |
| 3591 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3592 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3593 | #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] | 3594 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3595 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3596 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3597 | #endif |
| 3598 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3599 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3600 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3601 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3602 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3603 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3604 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3605 | if (slave_name == NULL) |
| 3606 | return posix_error(); |
| 3607 | |
| 3608 | slave_fd = open(slave_name, O_RDWR); |
| 3609 | if (slave_fd < 0) |
| 3610 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3611 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3612 | 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] | 3613 | if (master_fd < 0) |
| 3614 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3615 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3616 | /* change permission of slave */ |
| 3617 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3618 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3619 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3620 | } |
| 3621 | /* unlock slave */ |
| 3622 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3623 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3624 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3625 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3626 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3627 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3628 | if (slave_name == NULL) |
| 3629 | return posix_error(); |
| 3630 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3631 | if (slave_fd < 0) |
| 3632 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3633 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3634 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3635 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3636 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3637 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3638 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3639 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3640 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3641 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3642 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3643 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3644 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3645 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3646 | |
| 3647 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3648 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3649 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3650 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3651 | 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] | 3652 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3653 | |
| 3654 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3655 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3656 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3657 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3658 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3659 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3660 | if (pid == -1) |
| 3661 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3662 | if (pid == 0) |
| 3663 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3664 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3665 | } |
| 3666 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3667 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3668 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3669 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3670 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3671 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3672 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3673 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3674 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3675 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3676 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3677 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3678 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3679 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3680 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3681 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3682 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3683 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3684 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3685 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3686 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3687 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3688 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3689 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3690 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3691 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3692 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3693 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3694 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3695 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3696 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3697 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3698 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3699 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3700 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3701 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3702 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3703 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3704 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3705 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3707 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3708 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3709 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3710 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3711 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3712 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3713 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3714 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3717 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3718 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3719 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3720 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3721 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3722 | |
| 3723 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3724 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3725 | { |
| 3726 | PyObject *result = NULL; |
| 3727 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3728 | #ifdef NGROUPS_MAX |
| 3729 | #define MAX_GROUPS NGROUPS_MAX |
| 3730 | #else |
| 3731 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3732 | #define MAX_GROUPS 64 |
| 3733 | #endif |
| 3734 | gid_t grouplist[MAX_GROUPS]; |
| 3735 | int n; |
| 3736 | |
| 3737 | n = getgroups(MAX_GROUPS, grouplist); |
| 3738 | if (n < 0) |
| 3739 | posix_error(); |
| 3740 | else { |
| 3741 | result = PyList_New(n); |
| 3742 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3743 | int i; |
| 3744 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3745 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3746 | if (o == NULL) { |
| 3747 | Py_DECREF(result); |
| 3748 | result = NULL; |
| 3749 | break; |
| 3750 | } |
| 3751 | PyList_SET_ITEM(result, i, o); |
| 3752 | } |
| 3753 | } |
| 3754 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3755 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3756 | return result; |
| 3757 | } |
| 3758 | #endif |
| 3759 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3760 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3761 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3762 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3763 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3764 | |
| 3765 | static PyObject * |
| 3766 | posix_getpgid(PyObject *self, PyObject *args) |
| 3767 | { |
| 3768 | int pid, pgid; |
| 3769 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3770 | return NULL; |
| 3771 | pgid = getpgid(pid); |
| 3772 | if (pgid < 0) |
| 3773 | return posix_error(); |
| 3774 | return PyInt_FromLong((long)pgid); |
| 3775 | } |
| 3776 | #endif /* HAVE_GETPGID */ |
| 3777 | |
| 3778 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3779 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3780 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3781 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3782 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3783 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3784 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3785 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3786 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3787 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3788 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3789 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3790 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3791 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3792 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3793 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3794 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3795 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3796 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3797 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3798 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3799 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3800 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3801 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3802 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3803 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3804 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3805 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3806 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3807 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3808 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3809 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3810 | Py_INCREF(Py_None); |
| 3811 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3814 | #endif /* HAVE_SETPGRP */ |
| 3815 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3816 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3817 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3818 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3819 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3820 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3821 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3822 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3823 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3824 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3825 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3826 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3827 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3828 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3829 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3830 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3831 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3832 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3833 | |
| 3834 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3835 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3836 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3837 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3838 | char *name; |
| 3839 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3840 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3841 | errno = 0; |
| 3842 | name = getlogin(); |
| 3843 | if (name == NULL) { |
| 3844 | if (errno) |
| 3845 | posix_error(); |
| 3846 | else |
| 3847 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3848 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3849 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3850 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 3851 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3852 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3853 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3854 | return result; |
| 3855 | } |
| 3856 | #endif |
| 3857 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3858 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3859 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3860 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3861 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3863 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3864 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3865 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3866 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3867 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3868 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3869 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3870 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3871 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3872 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3873 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3874 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3875 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3876 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3877 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3878 | { |
| 3879 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3880 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3881 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3882 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3883 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3884 | APIRET rc; |
| 3885 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3886 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3887 | |
| 3888 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3889 | APIRET rc; |
| 3890 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3891 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3892 | |
| 3893 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3894 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3895 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3896 | if (kill(pid, sig) == -1) |
| 3897 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3898 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3899 | Py_INCREF(Py_None); |
| 3900 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3901 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3902 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3903 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3904 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3905 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3906 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3907 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3908 | |
| 3909 | static PyObject * |
| 3910 | posix_killpg(PyObject *self, PyObject *args) |
| 3911 | { |
| 3912 | int pgid, sig; |
| 3913 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3914 | return NULL; |
| 3915 | if (killpg(pgid, sig) == -1) |
| 3916 | return posix_error(); |
| 3917 | Py_INCREF(Py_None); |
| 3918 | return Py_None; |
| 3919 | } |
| 3920 | #endif |
| 3921 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3922 | #ifdef HAVE_PLOCK |
| 3923 | |
| 3924 | #ifdef HAVE_SYS_LOCK_H |
| 3925 | #include <sys/lock.h> |
| 3926 | #endif |
| 3927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3928 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3929 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3930 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3931 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3932 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3933 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3934 | { |
| 3935 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3936 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3937 | return NULL; |
| 3938 | if (plock(op) == -1) |
| 3939 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3940 | Py_INCREF(Py_None); |
| 3941 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3942 | } |
| 3943 | #endif |
| 3944 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3945 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3946 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3947 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3948 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3949 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3950 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3951 | Set the current process's user id."); |
| 3952 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3953 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3954 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3955 | { |
| 3956 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3957 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3958 | return NULL; |
| 3959 | if (setuid(uid) < 0) |
| 3960 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3961 | Py_INCREF(Py_None); |
| 3962 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3963 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3964 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3965 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3966 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3967 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3968 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3969 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3970 | Set the current process's effective user id."); |
| 3971 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3972 | static PyObject * |
| 3973 | posix_seteuid (PyObject *self, PyObject *args) |
| 3974 | { |
| 3975 | int euid; |
| 3976 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3977 | return NULL; |
| 3978 | } else if (seteuid(euid) < 0) { |
| 3979 | return posix_error(); |
| 3980 | } else { |
| 3981 | Py_INCREF(Py_None); |
| 3982 | return Py_None; |
| 3983 | } |
| 3984 | } |
| 3985 | #endif /* HAVE_SETEUID */ |
| 3986 | |
| 3987 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3988 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3989 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3990 | Set the current process's effective group id."); |
| 3991 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3992 | static PyObject * |
| 3993 | posix_setegid (PyObject *self, PyObject *args) |
| 3994 | { |
| 3995 | int egid; |
| 3996 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3997 | return NULL; |
| 3998 | } else if (setegid(egid) < 0) { |
| 3999 | return posix_error(); |
| 4000 | } else { |
| 4001 | Py_INCREF(Py_None); |
| 4002 | return Py_None; |
| 4003 | } |
| 4004 | } |
| 4005 | #endif /* HAVE_SETEGID */ |
| 4006 | |
| 4007 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4008 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4009 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4010 | Set the current process's real and effective user ids."); |
| 4011 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4012 | static PyObject * |
| 4013 | posix_setreuid (PyObject *self, PyObject *args) |
| 4014 | { |
| 4015 | int ruid, euid; |
| 4016 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4017 | return NULL; |
| 4018 | } else if (setreuid(ruid, euid) < 0) { |
| 4019 | return posix_error(); |
| 4020 | } else { |
| 4021 | Py_INCREF(Py_None); |
| 4022 | return Py_None; |
| 4023 | } |
| 4024 | } |
| 4025 | #endif /* HAVE_SETREUID */ |
| 4026 | |
| 4027 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4028 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4029 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4030 | Set the current process's real and effective group ids."); |
| 4031 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4032 | static PyObject * |
| 4033 | posix_setregid (PyObject *self, PyObject *args) |
| 4034 | { |
| 4035 | int rgid, egid; |
| 4036 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4037 | return NULL; |
| 4038 | } else if (setregid(rgid, egid) < 0) { |
| 4039 | return posix_error(); |
| 4040 | } else { |
| 4041 | Py_INCREF(Py_None); |
| 4042 | return Py_None; |
| 4043 | } |
| 4044 | } |
| 4045 | #endif /* HAVE_SETREGID */ |
| 4046 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4047 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4048 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4049 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4050 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4051 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4052 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4053 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4054 | { |
| 4055 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4056 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4057 | return NULL; |
| 4058 | if (setgid(gid) < 0) |
| 4059 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4060 | Py_INCREF(Py_None); |
| 4061 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4062 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4063 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4064 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4065 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4066 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4067 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4068 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4069 | |
| 4070 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4071 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4072 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4073 | int i, len; |
| 4074 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4075 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4076 | if (!PySequence_Check(groups)) { |
| 4077 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4078 | return NULL; |
| 4079 | } |
| 4080 | len = PySequence_Size(groups); |
| 4081 | if (len > MAX_GROUPS) { |
| 4082 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4083 | return NULL; |
| 4084 | } |
| 4085 | for(i = 0; i < len; i++) { |
| 4086 | PyObject *elem; |
| 4087 | elem = PySequence_GetItem(groups, i); |
| 4088 | if (!elem) |
| 4089 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4090 | if (!PyLong_Check(elem)) { |
| 4091 | PyErr_SetString(PyExc_TypeError, |
| 4092 | "groups must be integers"); |
| 4093 | Py_DECREF(elem); |
| 4094 | return NULL; |
| 4095 | } else { |
| 4096 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4097 | if (PyErr_Occurred()) { |
| 4098 | PyErr_SetString(PyExc_TypeError, |
| 4099 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4100 | Py_DECREF(elem); |
| 4101 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4102 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4103 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4104 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4105 | if (grouplist[i] != x) { |
| 4106 | PyErr_SetString(PyExc_TypeError, |
| 4107 | "group id too big"); |
| 4108 | Py_DECREF(elem); |
| 4109 | return NULL; |
| 4110 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4111 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4112 | Py_DECREF(elem); |
| 4113 | } |
| 4114 | |
| 4115 | if (setgroups(len, grouplist) < 0) |
| 4116 | return posix_error(); |
| 4117 | Py_INCREF(Py_None); |
| 4118 | return Py_None; |
| 4119 | } |
| 4120 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4121 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4122 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4123 | static PyObject * |
| 4124 | wait_helper(int pid, int status, struct rusage *ru) |
| 4125 | { |
| 4126 | PyObject *result; |
| 4127 | static PyObject *struct_rusage; |
| 4128 | |
| 4129 | if (pid == -1) |
| 4130 | return posix_error(); |
| 4131 | |
| 4132 | if (struct_rusage == NULL) { |
| 4133 | PyObject *m = PyImport_ImportModule("resource"); |
| 4134 | if (m == NULL) |
| 4135 | return NULL; |
| 4136 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4137 | Py_DECREF(m); |
| 4138 | if (struct_rusage == NULL) |
| 4139 | return NULL; |
| 4140 | } |
| 4141 | |
| 4142 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4143 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4144 | if (!result) |
| 4145 | return NULL; |
| 4146 | |
| 4147 | #ifndef doubletime |
| 4148 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4149 | #endif |
| 4150 | |
| 4151 | PyStructSequence_SET_ITEM(result, 0, |
| 4152 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4153 | PyStructSequence_SET_ITEM(result, 1, |
| 4154 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4155 | #define SET_INT(result, index, value)\ |
| 4156 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 4157 | SET_INT(result, 2, ru->ru_maxrss); |
| 4158 | SET_INT(result, 3, ru->ru_ixrss); |
| 4159 | SET_INT(result, 4, ru->ru_idrss); |
| 4160 | SET_INT(result, 5, ru->ru_isrss); |
| 4161 | SET_INT(result, 6, ru->ru_minflt); |
| 4162 | SET_INT(result, 7, ru->ru_majflt); |
| 4163 | SET_INT(result, 8, ru->ru_nswap); |
| 4164 | SET_INT(result, 9, ru->ru_inblock); |
| 4165 | SET_INT(result, 10, ru->ru_oublock); |
| 4166 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4167 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4168 | SET_INT(result, 13, ru->ru_nsignals); |
| 4169 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4170 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4171 | #undef SET_INT |
| 4172 | |
| 4173 | if (PyErr_Occurred()) { |
| 4174 | Py_DECREF(result); |
| 4175 | return NULL; |
| 4176 | } |
| 4177 | |
| 4178 | return Py_BuildValue("iiN", pid, status, result); |
| 4179 | } |
| 4180 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4181 | |
| 4182 | #ifdef HAVE_WAIT3 |
| 4183 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4184 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4185 | Wait for completion of a child process."); |
| 4186 | |
| 4187 | static PyObject * |
| 4188 | posix_wait3(PyObject *self, PyObject *args) |
| 4189 | { |
| 4190 | int pid, options; |
| 4191 | struct rusage ru; |
| 4192 | WAIT_TYPE status; |
| 4193 | WAIT_STATUS_INT(status) = 0; |
| 4194 | |
| 4195 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4196 | return NULL; |
| 4197 | |
| 4198 | Py_BEGIN_ALLOW_THREADS |
| 4199 | pid = wait3(&status, options, &ru); |
| 4200 | Py_END_ALLOW_THREADS |
| 4201 | |
| 4202 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4203 | } |
| 4204 | #endif /* HAVE_WAIT3 */ |
| 4205 | |
| 4206 | #ifdef HAVE_WAIT4 |
| 4207 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4208 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4209 | Wait for completion of a given child process."); |
| 4210 | |
| 4211 | static PyObject * |
| 4212 | posix_wait4(PyObject *self, PyObject *args) |
| 4213 | { |
| 4214 | int pid, options; |
| 4215 | struct rusage ru; |
| 4216 | WAIT_TYPE status; |
| 4217 | WAIT_STATUS_INT(status) = 0; |
| 4218 | |
| 4219 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4220 | return NULL; |
| 4221 | |
| 4222 | Py_BEGIN_ALLOW_THREADS |
| 4223 | pid = wait4(pid, &status, options, &ru); |
| 4224 | Py_END_ALLOW_THREADS |
| 4225 | |
| 4226 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4227 | } |
| 4228 | #endif /* HAVE_WAIT4 */ |
| 4229 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4230 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4231 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4232 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4233 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4234 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4235 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4236 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4237 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4238 | int pid, options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4239 | WAIT_TYPE status; |
| 4240 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4241 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4242 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4243 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4244 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4245 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4246 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +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 | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4253 | #elif defined(HAVE_CWAIT) |
| 4254 | |
| 4255 | /* 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] | 4256 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4257 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4258 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4259 | |
| 4260 | static PyObject * |
| 4261 | posix_waitpid(PyObject *self, PyObject *args) |
| 4262 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4263 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4264 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4265 | |
| 4266 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4267 | return NULL; |
| 4268 | Py_BEGIN_ALLOW_THREADS |
| 4269 | pid = _cwait(&status, pid, options); |
| 4270 | Py_END_ALLOW_THREADS |
| 4271 | if (pid == -1) |
| 4272 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4273 | |
| 4274 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4275 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4276 | } |
| 4277 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4278 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4279 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4280 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4281 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4282 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4284 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4285 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4286 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4287 | int pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4288 | WAIT_TYPE status; |
| 4289 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4290 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4291 | Py_BEGIN_ALLOW_THREADS |
| 4292 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4293 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4294 | if (pid == -1) |
| 4295 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4296 | |
| 4297 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4298 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4299 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4300 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4301 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4302 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4303 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4304 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4305 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4306 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4307 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4308 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4309 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4310 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4311 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4312 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4313 | 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] | 4314 | #else |
| 4315 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4316 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4317 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4320 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4321 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4322 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4323 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4324 | 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] | 4325 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4326 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4327 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4328 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4329 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4330 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4331 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4332 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4333 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4334 | |
| 4335 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 4336 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4337 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4338 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4339 | if (v == NULL) { |
| 4340 | PyMem_Free(path); |
| 4341 | return NULL; |
| 4342 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4343 | |
| 4344 | if (PyUnicode_Check(v)) { |
| 4345 | arg_is_unicode = 1; |
| 4346 | } |
| 4347 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4348 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4349 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4350 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4351 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4352 | if (n < 0) |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4353 | return posix_error_with_allocated_filename(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4354 | |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4355 | PyMem_Free(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4356 | v = PyString_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4357 | if (arg_is_unicode) { |
| 4358 | PyObject *w; |
| 4359 | |
| 4360 | w = PyUnicode_FromEncodedObject(v, |
| 4361 | Py_FileSystemDefaultEncoding, |
| 4362 | "strict"); |
| 4363 | if (w != NULL) { |
| 4364 | Py_DECREF(v); |
| 4365 | v = w; |
| 4366 | } |
| 4367 | else { |
| 4368 | /* fall back to the original byte string, as |
| 4369 | discussed in patch #683592 */ |
| 4370 | PyErr_Clear(); |
| 4371 | } |
| 4372 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4373 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4374 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4375 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4376 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4377 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4378 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4379 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4380 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4381 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4382 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4383 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4384 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4385 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4386 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4387 | } |
| 4388 | #endif /* HAVE_SYMLINK */ |
| 4389 | |
| 4390 | |
| 4391 | #ifdef HAVE_TIMES |
| 4392 | #ifndef HZ |
| 4393 | #define HZ 60 /* Universal constant :-) */ |
| 4394 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4395 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4396 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4397 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4398 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4399 | { |
| 4400 | ULONG value = 0; |
| 4401 | |
| 4402 | Py_BEGIN_ALLOW_THREADS |
| 4403 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4404 | Py_END_ALLOW_THREADS |
| 4405 | |
| 4406 | return value; |
| 4407 | } |
| 4408 | |
| 4409 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4410 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4411 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4412 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4413 | return Py_BuildValue("ddddd", |
| 4414 | (double)0 /* t.tms_utime / HZ */, |
| 4415 | (double)0 /* t.tms_stime / HZ */, |
| 4416 | (double)0 /* t.tms_cutime / HZ */, |
| 4417 | (double)0 /* t.tms_cstime / HZ */, |
| 4418 | (double)system_uptime() / 1000); |
| 4419 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4420 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4421 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4422 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4423 | { |
| 4424 | struct tms t; |
| 4425 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4426 | errno = 0; |
| 4427 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4428 | if (c == (clock_t) -1) |
| 4429 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4430 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4431 | (double)t.tms_utime / HZ, |
| 4432 | (double)t.tms_stime / HZ, |
| 4433 | (double)t.tms_cutime / HZ, |
| 4434 | (double)t.tms_cstime / HZ, |
| 4435 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4436 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4437 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4438 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4439 | |
| 4440 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4441 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4442 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4443 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4444 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4445 | { |
| 4446 | FILETIME create, exit, kernel, user; |
| 4447 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4448 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4449 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4450 | /* The fields of a FILETIME structure are the hi and lo part |
| 4451 | of a 64-bit value expressed in 100 nanosecond units. |
| 4452 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4453 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4454 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4455 | return Py_BuildValue( |
| 4456 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4457 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4458 | kernel.dwLowDateTime*1e-7), |
| 4459 | (double)(user.dwHighDateTime*429.4967296 + |
| 4460 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4461 | (double)0, |
| 4462 | (double)0, |
| 4463 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4464 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4465 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4466 | |
| 4467 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4468 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4469 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4470 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4471 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4472 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4473 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4474 | #ifdef HAVE_GETSID |
| 4475 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4476 | "getsid(pid) -> sid\n\n\ |
| 4477 | Call the system call getsid()."); |
| 4478 | |
| 4479 | static PyObject * |
| 4480 | posix_getsid(PyObject *self, PyObject *args) |
| 4481 | { |
| 4482 | int pid, sid; |
| 4483 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4484 | return NULL; |
| 4485 | sid = getsid(pid); |
| 4486 | if (sid < 0) |
| 4487 | return posix_error(); |
| 4488 | return PyInt_FromLong((long)sid); |
| 4489 | } |
| 4490 | #endif /* HAVE_GETSID */ |
| 4491 | |
| 4492 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4493 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4494 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4495 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4496 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4497 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4498 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4499 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4500 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4501 | if (setsid() < 0) |
| 4502 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4503 | Py_INCREF(Py_None); |
| 4504 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4505 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4506 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4507 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4508 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4509 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4510 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4511 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4512 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4513 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4514 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4515 | { |
| 4516 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4517 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4518 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4519 | if (setpgid(pid, pgrp) < 0) |
| 4520 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4521 | Py_INCREF(Py_None); |
| 4522 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4523 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4524 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4525 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4526 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4527 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4528 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4529 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4530 | 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] | 4531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4532 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4533 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4534 | { |
| 4535 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4536 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4537 | return NULL; |
| 4538 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4539 | if (pgid < 0) |
| 4540 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4541 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4542 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4543 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4544 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4545 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4546 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4547 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4548 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4549 | 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] | 4550 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4551 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4552 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4553 | { |
| 4554 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4555 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4556 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4557 | if (tcsetpgrp(fd, pgid) < 0) |
| 4558 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4559 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4560 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4561 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4562 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4563 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4564 | /* Functions acting on file descriptors */ |
| 4565 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4566 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4567 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4568 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4569 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4570 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4571 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4572 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4573 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4574 | int flag; |
| 4575 | int mode = 0777; |
| 4576 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4577 | |
| 4578 | #ifdef MS_WINDOWS |
| 4579 | if (unicode_file_names()) { |
| 4580 | PyUnicodeObject *po; |
| 4581 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4582 | Py_BEGIN_ALLOW_THREADS |
| 4583 | /* PyUnicode_AS_UNICODE OK without thread |
| 4584 | lock as it is a simple dereference. */ |
| 4585 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4586 | Py_END_ALLOW_THREADS |
| 4587 | if (fd < 0) |
| 4588 | return posix_error(); |
| 4589 | return PyInt_FromLong((long)fd); |
| 4590 | } |
| 4591 | /* Drop the argument parsing error as narrow strings |
| 4592 | are also valid. */ |
| 4593 | PyErr_Clear(); |
| 4594 | } |
| 4595 | #endif |
| 4596 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4597 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4598 | Py_FileSystemDefaultEncoding, &file, |
| 4599 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4600 | return NULL; |
| 4601 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4602 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4603 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4604 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4605 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4606 | return posix_error_with_allocated_filename(file); |
| 4607 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4608 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4611 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4612 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4613 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4614 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4615 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4616 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4617 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4618 | { |
| 4619 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4620 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4621 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4622 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4623 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4624 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4625 | if (res < 0) |
| 4626 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4627 | Py_INCREF(Py_None); |
| 4628 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4631 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4632 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4633 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4634 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4635 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4636 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4637 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4638 | { |
| 4639 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4640 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4641 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4642 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4643 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4644 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4645 | if (fd < 0) |
| 4646 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4647 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4650 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4651 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4652 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4653 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4654 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4655 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4656 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4657 | { |
| 4658 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4659 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4660 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4661 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4662 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4663 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4664 | if (res < 0) |
| 4665 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4666 | Py_INCREF(Py_None); |
| 4667 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4671 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4672 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4673 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4674 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4675 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4676 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4677 | { |
| 4678 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4679 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4680 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4681 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4682 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4683 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4684 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4685 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4686 | return NULL; |
| 4687 | #ifdef SEEK_SET |
| 4688 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4689 | switch (how) { |
| 4690 | case 0: how = SEEK_SET; break; |
| 4691 | case 1: how = SEEK_CUR; break; |
| 4692 | case 2: how = SEEK_END; break; |
| 4693 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4694 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4695 | |
| 4696 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4697 | pos = PyInt_AsLong(posobj); |
| 4698 | #else |
| 4699 | pos = PyLong_Check(posobj) ? |
| 4700 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4701 | #endif |
| 4702 | if (PyErr_Occurred()) |
| 4703 | return NULL; |
| 4704 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4705 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4706 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4707 | res = _lseeki64(fd, pos, how); |
| 4708 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4709 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4710 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4711 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4712 | if (res < 0) |
| 4713 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4714 | |
| 4715 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4716 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4717 | #else |
| 4718 | return PyLong_FromLongLong(res); |
| 4719 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4723 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4724 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4725 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4726 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4727 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4728 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4729 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4730 | int fd, size; |
| 4731 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4732 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4733 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4734 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 4735 | if (size < 0) { |
| 4736 | errno = EINVAL; |
| 4737 | return posix_error(); |
| 4738 | } |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4739 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4740 | if (buffer == NULL) |
| 4741 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4742 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4743 | n = read(fd, PyBytes_AsString(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4744 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4745 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4746 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4747 | return posix_error(); |
| 4748 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4749 | if (n != size) |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4750 | PyBytes_Resize(buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4751 | return buffer; |
| 4752 | } |
| 4753 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4754 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4755 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4756 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4757 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4758 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4759 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4760 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4761 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4762 | int fd; |
| 4763 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4764 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4765 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4766 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4767 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4768 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4769 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4770 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4771 | if (size < 0) |
| 4772 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4773 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4777 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4778 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4779 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4780 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4781 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4782 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4783 | { |
| 4784 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4785 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4786 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4787 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4788 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 4789 | #ifdef __VMS |
| 4790 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 4791 | fsync(fd); |
| 4792 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4793 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4794 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4795 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4796 | if (res != 0) { |
| 4797 | #ifdef MS_WINDOWS |
| 4798 | return win32_error("fstat", NULL); |
| 4799 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4800 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4801 | #endif |
| 4802 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4803 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4804 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4807 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4808 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4809 | 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] | 4810 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4811 | |
| 4812 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4813 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4814 | { |
| 4815 | int fd; |
| 4816 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4817 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4818 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4819 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4820 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4821 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4822 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4823 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4824 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4825 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4826 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4827 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4828 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4829 | #if defined(PYOS_OS2) |
| 4830 | HFILE read, write; |
| 4831 | APIRET rc; |
| 4832 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4833 | Py_BEGIN_ALLOW_THREADS |
| 4834 | rc = DosCreatePipe( &read, &write, 4096); |
| 4835 | Py_END_ALLOW_THREADS |
| 4836 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4837 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4838 | |
| 4839 | return Py_BuildValue("(ii)", read, write); |
| 4840 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4841 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4842 | int fds[2]; |
| 4843 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4844 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4845 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4846 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4847 | if (res != 0) |
| 4848 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4849 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4850 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4851 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4852 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4853 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4854 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4855 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4856 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4857 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4858 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4859 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4860 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4861 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4862 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4863 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4864 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4865 | #endif /* HAVE_PIPE */ |
| 4866 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4867 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4868 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4869 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4870 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4871 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4872 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4873 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4874 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4875 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4876 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4877 | int mode = 0666; |
| 4878 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4879 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4880 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4881 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4882 | res = mkfifo(filename, mode); |
| 4883 | Py_END_ALLOW_THREADS |
| 4884 | if (res < 0) |
| 4885 | return posix_error(); |
| 4886 | Py_INCREF(Py_None); |
| 4887 | return Py_None; |
| 4888 | } |
| 4889 | #endif |
| 4890 | |
| 4891 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 4892 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4893 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4894 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4895 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 4896 | named filename. mode specifies both the permissions to use and the\n\ |
| 4897 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 4898 | 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] | 4899 | device defines the newly created device special file (probably using\n\ |
| 4900 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4901 | |
| 4902 | |
| 4903 | static PyObject * |
| 4904 | posix_mknod(PyObject *self, PyObject *args) |
| 4905 | { |
| 4906 | char *filename; |
| 4907 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4908 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4909 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 4910 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4911 | return NULL; |
| 4912 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4913 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4914 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4915 | if (res < 0) |
| 4916 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4917 | Py_INCREF(Py_None); |
| 4918 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4919 | } |
| 4920 | #endif |
| 4921 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4922 | #ifdef HAVE_DEVICE_MACROS |
| 4923 | PyDoc_STRVAR(posix_major__doc__, |
| 4924 | "major(device) -> major number\n\ |
| 4925 | Extracts a device major number from a raw device number."); |
| 4926 | |
| 4927 | static PyObject * |
| 4928 | posix_major(PyObject *self, PyObject *args) |
| 4929 | { |
| 4930 | int device; |
| 4931 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 4932 | return NULL; |
| 4933 | return PyInt_FromLong((long)major(device)); |
| 4934 | } |
| 4935 | |
| 4936 | PyDoc_STRVAR(posix_minor__doc__, |
| 4937 | "minor(device) -> minor number\n\ |
| 4938 | Extracts a device minor number from a raw device number."); |
| 4939 | |
| 4940 | static PyObject * |
| 4941 | posix_minor(PyObject *self, PyObject *args) |
| 4942 | { |
| 4943 | int device; |
| 4944 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 4945 | return NULL; |
| 4946 | return PyInt_FromLong((long)minor(device)); |
| 4947 | } |
| 4948 | |
| 4949 | PyDoc_STRVAR(posix_makedev__doc__, |
| 4950 | "makedev(major, minor) -> device number\n\ |
| 4951 | Composes a raw device number from the major and minor device numbers."); |
| 4952 | |
| 4953 | static PyObject * |
| 4954 | posix_makedev(PyObject *self, PyObject *args) |
| 4955 | { |
| 4956 | int major, minor; |
| 4957 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 4958 | return NULL; |
| 4959 | return PyInt_FromLong((long)makedev(major, minor)); |
| 4960 | } |
| 4961 | #endif /* device macros */ |
| 4962 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4963 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4964 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4965 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4966 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4967 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4968 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4969 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4970 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4971 | { |
| 4972 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4973 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4974 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4975 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4976 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4977 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4978 | return NULL; |
| 4979 | |
| 4980 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4981 | length = PyInt_AsLong(lenobj); |
| 4982 | #else |
| 4983 | length = PyLong_Check(lenobj) ? |
| 4984 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 4985 | #endif |
| 4986 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4987 | return NULL; |
| 4988 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4989 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4990 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4991 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4992 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4993 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4994 | return NULL; |
| 4995 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4996 | Py_INCREF(Py_None); |
| 4997 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4998 | } |
| 4999 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5000 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5001 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5002 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5003 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5004 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5005 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5006 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5007 | * get re-set with another call for the same key. */ |
| 5008 | static PyObject *posix_putenv_garbage; |
| 5009 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5010 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5011 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5012 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5013 | #ifdef MS_WINDOWS |
| 5014 | wchar_t *s1, *s2; |
| 5015 | wchar_t *newenv; |
| 5016 | #else |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5017 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5018 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5019 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5020 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5021 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5022 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5023 | if (!PyArg_ParseTuple(args, |
| 5024 | #ifdef MS_WINDOWS |
| 5025 | "uu:putenv", |
| 5026 | #else |
| 5027 | "ss:putenv", |
| 5028 | #endif |
| 5029 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5030 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5031 | |
| 5032 | #if defined(PYOS_OS2) |
| 5033 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5034 | APIRET rc; |
| 5035 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5036 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5037 | if (rc != NO_ERROR) |
| 5038 | return os2_error(rc); |
| 5039 | |
| 5040 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5041 | APIRET rc; |
| 5042 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5043 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5044 | if (rc != NO_ERROR) |
| 5045 | return os2_error(rc); |
| 5046 | } else { |
| 5047 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5048 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5049 | /* len includes space for a trailing \0; the size arg to |
| 5050 | PyString_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5051 | #ifdef MS_WINDOWS |
| 5052 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5053 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5054 | #else |
| 5055 | len = strlen(s1) + strlen(s2) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5056 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5057 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5058 | if (newstr == NULL) |
| 5059 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5060 | #ifdef MS_WINDOWS |
| 5061 | newenv = PyUnicode_AsUnicode(newstr); |
| 5062 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5063 | if (_wputenv(newenv)) { |
| 5064 | Py_DECREF(newstr); |
| 5065 | posix_error(); |
| 5066 | return NULL; |
| 5067 | } |
| 5068 | #else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5069 | newenv = PyString_AS_STRING(newstr); |
| 5070 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5071 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5072 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5073 | posix_error(); |
| 5074 | return NULL; |
| 5075 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5076 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5077 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5078 | * this will cause previous value to be collected. This has to |
| 5079 | * happen after the real putenv() call because the old value |
| 5080 | * was still accessible until then. */ |
| 5081 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5082 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5083 | /* really not much we can do; just leak */ |
| 5084 | PyErr_Clear(); |
| 5085 | } |
| 5086 | else { |
| 5087 | Py_DECREF(newstr); |
| 5088 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5089 | |
| 5090 | #if defined(PYOS_OS2) |
| 5091 | } |
| 5092 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5093 | Py_INCREF(Py_None); |
| 5094 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5095 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5096 | #endif /* putenv */ |
| 5097 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5098 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5099 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5100 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5101 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5102 | |
| 5103 | static PyObject * |
| 5104 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5105 | { |
| 5106 | char *s1; |
| 5107 | |
| 5108 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5109 | return NULL; |
| 5110 | |
| 5111 | unsetenv(s1); |
| 5112 | |
| 5113 | /* Remove the key from posix_putenv_garbage; |
| 5114 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5115 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5116 | * old value was still accessible until then. |
| 5117 | */ |
| 5118 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5119 | PyTuple_GET_ITEM(args, 0))) { |
| 5120 | /* really not much we can do; just leak */ |
| 5121 | PyErr_Clear(); |
| 5122 | } |
| 5123 | |
| 5124 | Py_INCREF(Py_None); |
| 5125 | return Py_None; |
| 5126 | } |
| 5127 | #endif /* unsetenv */ |
| 5128 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5129 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5130 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5131 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5132 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5133 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5134 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5135 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5136 | { |
| 5137 | int code; |
| 5138 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5139 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5140 | return NULL; |
| 5141 | message = strerror(code); |
| 5142 | if (message == NULL) { |
| 5143 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5144 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5145 | return NULL; |
| 5146 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5147 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5148 | } |
| 5149 | #endif /* strerror */ |
| 5150 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5151 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5152 | #ifdef HAVE_SYS_WAIT_H |
| 5153 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5154 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5155 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5156 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5157 | 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] | 5158 | |
| 5159 | static PyObject * |
| 5160 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5161 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5162 | WAIT_TYPE status; |
| 5163 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5164 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5165 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5166 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5167 | |
| 5168 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5169 | } |
| 5170 | #endif /* WCOREDUMP */ |
| 5171 | |
| 5172 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5173 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5174 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5175 | 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] | 5176 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5177 | |
| 5178 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5179 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5180 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5181 | WAIT_TYPE status; |
| 5182 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5183 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5184 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5185 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5186 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5187 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5188 | } |
| 5189 | #endif /* WIFCONTINUED */ |
| 5190 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5191 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5192 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5193 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5194 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5195 | |
| 5196 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5197 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5198 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5199 | WAIT_TYPE status; |
| 5200 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5201 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5202 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5203 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5204 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5205 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5206 | } |
| 5207 | #endif /* WIFSTOPPED */ |
| 5208 | |
| 5209 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5210 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5211 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5212 | 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] | 5213 | |
| 5214 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5215 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5216 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5217 | WAIT_TYPE status; |
| 5218 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5219 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5220 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5221 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5222 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5223 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5224 | } |
| 5225 | #endif /* WIFSIGNALED */ |
| 5226 | |
| 5227 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5228 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5229 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5230 | 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] | 5231 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5232 | |
| 5233 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5234 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5235 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5236 | WAIT_TYPE status; |
| 5237 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5238 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5239 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5240 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5241 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5242 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5243 | } |
| 5244 | #endif /* WIFEXITED */ |
| 5245 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5246 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5247 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5248 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5249 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5250 | |
| 5251 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5252 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5253 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5254 | WAIT_TYPE status; |
| 5255 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5256 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5257 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5258 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5259 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5260 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5261 | } |
| 5262 | #endif /* WEXITSTATUS */ |
| 5263 | |
| 5264 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5265 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5266 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5267 | 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] | 5268 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5269 | |
| 5270 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5271 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5272 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5273 | WAIT_TYPE status; |
| 5274 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5275 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5276 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5277 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5278 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5279 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5280 | } |
| 5281 | #endif /* WTERMSIG */ |
| 5282 | |
| 5283 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5284 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5285 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5286 | Return the signal that stopped the process that provided\n\ |
| 5287 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5288 | |
| 5289 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5290 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5291 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5292 | WAIT_TYPE status; |
| 5293 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5294 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5295 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5296 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5297 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5298 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5299 | } |
| 5300 | #endif /* WSTOPSIG */ |
| 5301 | |
| 5302 | #endif /* HAVE_SYS_WAIT_H */ |
| 5303 | |
| 5304 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5305 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5306 | #ifdef _SCO_DS |
| 5307 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5308 | needed definitions in sys/statvfs.h */ |
| 5309 | #define _SVID3 |
| 5310 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5311 | #include <sys/statvfs.h> |
| 5312 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5313 | static PyObject* |
| 5314 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5315 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5316 | if (v == NULL) |
| 5317 | return NULL; |
| 5318 | |
| 5319 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5320 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5321 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5322 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5323 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5324 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5325 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5326 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5327 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5328 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5329 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5330 | #else |
| 5331 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5332 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5333 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5334 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5335 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5336 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5337 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5338 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5339 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5340 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5341 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5342 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5343 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5344 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5345 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5346 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5347 | #endif |
| 5348 | |
| 5349 | return v; |
| 5350 | } |
| 5351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5352 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5353 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5354 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5355 | |
| 5356 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5357 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5358 | { |
| 5359 | int fd, res; |
| 5360 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5361 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5362 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5363 | return NULL; |
| 5364 | Py_BEGIN_ALLOW_THREADS |
| 5365 | res = fstatvfs(fd, &st); |
| 5366 | Py_END_ALLOW_THREADS |
| 5367 | if (res != 0) |
| 5368 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5369 | |
| 5370 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5371 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5372 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5373 | |
| 5374 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5375 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5376 | #include <sys/statvfs.h> |
| 5377 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5378 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5379 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5380 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5381 | |
| 5382 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5383 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5384 | { |
| 5385 | char *path; |
| 5386 | int res; |
| 5387 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5388 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5389 | return NULL; |
| 5390 | Py_BEGIN_ALLOW_THREADS |
| 5391 | res = statvfs(path, &st); |
| 5392 | Py_END_ALLOW_THREADS |
| 5393 | if (res != 0) |
| 5394 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5395 | |
| 5396 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5397 | } |
| 5398 | #endif /* HAVE_STATVFS */ |
| 5399 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5400 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5401 | * It maps strings representing configuration variable names to |
| 5402 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5403 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5404 | * rarely-used constants. There are three separate tables that use |
| 5405 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5406 | * |
| 5407 | * This code is always included, even if none of the interfaces that |
| 5408 | * need it are included. The #if hackery needed to avoid it would be |
| 5409 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5410 | */ |
| 5411 | struct constdef { |
| 5412 | char *name; |
| 5413 | long value; |
| 5414 | }; |
| 5415 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5416 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5417 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5418 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5419 | { |
| 5420 | if (PyInt_Check(arg)) { |
| 5421 | *valuep = PyInt_AS_LONG(arg); |
| 5422 | return 1; |
| 5423 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5424 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5425 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5426 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5427 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5428 | size_t hi = tablesize; |
| 5429 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5430 | const char *confname; |
| 5431 | Py_ssize_t namelen; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5432 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5433 | PyErr_SetString(PyExc_TypeError, |
| 5434 | "configuration names must be strings or integers"); |
| 5435 | return 0; |
| 5436 | } |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5437 | confname = PyUnicode_AsString(arg); |
| 5438 | if (confname == NULL) |
| 5439 | return 0; |
| 5440 | namelen = strlen(confname); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5441 | while (lo < hi) { |
| 5442 | mid = (lo + hi) / 2; |
| 5443 | cmp = strcmp(confname, table[mid].name); |
| 5444 | if (cmp < 0) |
| 5445 | hi = mid; |
| 5446 | else if (cmp > 0) |
| 5447 | lo = mid + 1; |
| 5448 | else { |
| 5449 | *valuep = table[mid].value; |
| 5450 | return 1; |
| 5451 | } |
| 5452 | } |
| 5453 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5454 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5455 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | |
| 5459 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5460 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5461 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5462 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5463 | #endif |
| 5464 | #ifdef _PC_ABI_ASYNC_IO |
| 5465 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5466 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5467 | #ifdef _PC_ASYNC_IO |
| 5468 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5469 | #endif |
| 5470 | #ifdef _PC_CHOWN_RESTRICTED |
| 5471 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5472 | #endif |
| 5473 | #ifdef _PC_FILESIZEBITS |
| 5474 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5475 | #endif |
| 5476 | #ifdef _PC_LAST |
| 5477 | {"PC_LAST", _PC_LAST}, |
| 5478 | #endif |
| 5479 | #ifdef _PC_LINK_MAX |
| 5480 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5481 | #endif |
| 5482 | #ifdef _PC_MAX_CANON |
| 5483 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5484 | #endif |
| 5485 | #ifdef _PC_MAX_INPUT |
| 5486 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5487 | #endif |
| 5488 | #ifdef _PC_NAME_MAX |
| 5489 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5490 | #endif |
| 5491 | #ifdef _PC_NO_TRUNC |
| 5492 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5493 | #endif |
| 5494 | #ifdef _PC_PATH_MAX |
| 5495 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5496 | #endif |
| 5497 | #ifdef _PC_PIPE_BUF |
| 5498 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5499 | #endif |
| 5500 | #ifdef _PC_PRIO_IO |
| 5501 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5502 | #endif |
| 5503 | #ifdef _PC_SOCK_MAXBUF |
| 5504 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5505 | #endif |
| 5506 | #ifdef _PC_SYNC_IO |
| 5507 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5508 | #endif |
| 5509 | #ifdef _PC_VDISABLE |
| 5510 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5511 | #endif |
| 5512 | }; |
| 5513 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5514 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5515 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5516 | { |
| 5517 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5518 | sizeof(posix_constants_pathconf) |
| 5519 | / sizeof(struct constdef)); |
| 5520 | } |
| 5521 | #endif |
| 5522 | |
| 5523 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5524 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5525 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5526 | 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] | 5527 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5528 | |
| 5529 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5530 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5531 | { |
| 5532 | PyObject *result = NULL; |
| 5533 | int name, fd; |
| 5534 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5535 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5536 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5537 | long limit; |
| 5538 | |
| 5539 | errno = 0; |
| 5540 | limit = fpathconf(fd, name); |
| 5541 | if (limit == -1 && errno != 0) |
| 5542 | posix_error(); |
| 5543 | else |
| 5544 | result = PyInt_FromLong(limit); |
| 5545 | } |
| 5546 | return result; |
| 5547 | } |
| 5548 | #endif |
| 5549 | |
| 5550 | |
| 5551 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5552 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5553 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5554 | 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] | 5555 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5556 | |
| 5557 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5558 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5559 | { |
| 5560 | PyObject *result = NULL; |
| 5561 | int name; |
| 5562 | char *path; |
| 5563 | |
| 5564 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5565 | conv_path_confname, &name)) { |
| 5566 | long limit; |
| 5567 | |
| 5568 | errno = 0; |
| 5569 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5570 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5571 | if (errno == EINVAL) |
| 5572 | /* could be a path or name problem */ |
| 5573 | posix_error(); |
| 5574 | else |
| 5575 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5576 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5577 | else |
| 5578 | result = PyInt_FromLong(limit); |
| 5579 | } |
| 5580 | return result; |
| 5581 | } |
| 5582 | #endif |
| 5583 | |
| 5584 | #ifdef HAVE_CONFSTR |
| 5585 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5586 | #ifdef _CS_ARCHITECTURE |
| 5587 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5588 | #endif |
| 5589 | #ifdef _CS_HOSTNAME |
| 5590 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5591 | #endif |
| 5592 | #ifdef _CS_HW_PROVIDER |
| 5593 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5594 | #endif |
| 5595 | #ifdef _CS_HW_SERIAL |
| 5596 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5597 | #endif |
| 5598 | #ifdef _CS_INITTAB_NAME |
| 5599 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5600 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5601 | #ifdef _CS_LFS64_CFLAGS |
| 5602 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5603 | #endif |
| 5604 | #ifdef _CS_LFS64_LDFLAGS |
| 5605 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5606 | #endif |
| 5607 | #ifdef _CS_LFS64_LIBS |
| 5608 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5609 | #endif |
| 5610 | #ifdef _CS_LFS64_LINTFLAGS |
| 5611 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5612 | #endif |
| 5613 | #ifdef _CS_LFS_CFLAGS |
| 5614 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5615 | #endif |
| 5616 | #ifdef _CS_LFS_LDFLAGS |
| 5617 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5618 | #endif |
| 5619 | #ifdef _CS_LFS_LIBS |
| 5620 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5621 | #endif |
| 5622 | #ifdef _CS_LFS_LINTFLAGS |
| 5623 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5624 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5625 | #ifdef _CS_MACHINE |
| 5626 | {"CS_MACHINE", _CS_MACHINE}, |
| 5627 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5628 | #ifdef _CS_PATH |
| 5629 | {"CS_PATH", _CS_PATH}, |
| 5630 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5631 | #ifdef _CS_RELEASE |
| 5632 | {"CS_RELEASE", _CS_RELEASE}, |
| 5633 | #endif |
| 5634 | #ifdef _CS_SRPC_DOMAIN |
| 5635 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5636 | #endif |
| 5637 | #ifdef _CS_SYSNAME |
| 5638 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5639 | #endif |
| 5640 | #ifdef _CS_VERSION |
| 5641 | {"CS_VERSION", _CS_VERSION}, |
| 5642 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5643 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5644 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5645 | #endif |
| 5646 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5647 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5648 | #endif |
| 5649 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5650 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5651 | #endif |
| 5652 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5653 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5654 | #endif |
| 5655 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5656 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5657 | #endif |
| 5658 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5659 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5660 | #endif |
| 5661 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5662 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5663 | #endif |
| 5664 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5665 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5666 | #endif |
| 5667 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5668 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5669 | #endif |
| 5670 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5671 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5672 | #endif |
| 5673 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5674 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5675 | #endif |
| 5676 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5677 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5678 | #endif |
| 5679 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5680 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5681 | #endif |
| 5682 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5683 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5684 | #endif |
| 5685 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5686 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5687 | #endif |
| 5688 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5689 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5690 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5691 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5692 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5693 | #endif |
| 5694 | #ifdef _MIPS_CS_BASE |
| 5695 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5696 | #endif |
| 5697 | #ifdef _MIPS_CS_HOSTID |
| 5698 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5699 | #endif |
| 5700 | #ifdef _MIPS_CS_HW_NAME |
| 5701 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5702 | #endif |
| 5703 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5704 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5705 | #endif |
| 5706 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5707 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5708 | #endif |
| 5709 | #ifdef _MIPS_CS_OSREL_MIN |
| 5710 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5711 | #endif |
| 5712 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5713 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5714 | #endif |
| 5715 | #ifdef _MIPS_CS_OS_NAME |
| 5716 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5717 | #endif |
| 5718 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5719 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5720 | #endif |
| 5721 | #ifdef _MIPS_CS_PROCESSORS |
| 5722 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5723 | #endif |
| 5724 | #ifdef _MIPS_CS_SERIAL |
| 5725 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5726 | #endif |
| 5727 | #ifdef _MIPS_CS_VENDOR |
| 5728 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5729 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5730 | }; |
| 5731 | |
| 5732 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5733 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5734 | { |
| 5735 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5736 | sizeof(posix_constants_confstr) |
| 5737 | / sizeof(struct constdef)); |
| 5738 | } |
| 5739 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5740 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5741 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5742 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5743 | |
| 5744 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5745 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5746 | { |
| 5747 | PyObject *result = NULL; |
| 5748 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5749 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5750 | |
| 5751 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5752 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5753 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5754 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5755 | len = confstr(name, buffer, sizeof(buffer)); |
| 5756 | if (len == 0) { |
| 5757 | if (errno) { |
| 5758 | posix_error(); |
| 5759 | } |
| 5760 | else { |
| 5761 | result = Py_None; |
| 5762 | Py_INCREF(Py_None); |
| 5763 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5764 | } |
| 5765 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5766 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5767 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5768 | if (result != NULL) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5769 | confstr(name, PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5770 | } |
| 5771 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5772 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5773 | } |
| 5774 | } |
| 5775 | return result; |
| 5776 | } |
| 5777 | #endif |
| 5778 | |
| 5779 | |
| 5780 | #ifdef HAVE_SYSCONF |
| 5781 | static struct constdef posix_constants_sysconf[] = { |
| 5782 | #ifdef _SC_2_CHAR_TERM |
| 5783 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5784 | #endif |
| 5785 | #ifdef _SC_2_C_BIND |
| 5786 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5787 | #endif |
| 5788 | #ifdef _SC_2_C_DEV |
| 5789 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5790 | #endif |
| 5791 | #ifdef _SC_2_C_VERSION |
| 5792 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5793 | #endif |
| 5794 | #ifdef _SC_2_FORT_DEV |
| 5795 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5796 | #endif |
| 5797 | #ifdef _SC_2_FORT_RUN |
| 5798 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5799 | #endif |
| 5800 | #ifdef _SC_2_LOCALEDEF |
| 5801 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5802 | #endif |
| 5803 | #ifdef _SC_2_SW_DEV |
| 5804 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5805 | #endif |
| 5806 | #ifdef _SC_2_UPE |
| 5807 | {"SC_2_UPE", _SC_2_UPE}, |
| 5808 | #endif |
| 5809 | #ifdef _SC_2_VERSION |
| 5810 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5811 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5812 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5813 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5814 | #endif |
| 5815 | #ifdef _SC_ACL |
| 5816 | {"SC_ACL", _SC_ACL}, |
| 5817 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5818 | #ifdef _SC_AIO_LISTIO_MAX |
| 5819 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5820 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5821 | #ifdef _SC_AIO_MAX |
| 5822 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5823 | #endif |
| 5824 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5825 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5826 | #endif |
| 5827 | #ifdef _SC_ARG_MAX |
| 5828 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5829 | #endif |
| 5830 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5831 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5832 | #endif |
| 5833 | #ifdef _SC_ATEXIT_MAX |
| 5834 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5835 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5836 | #ifdef _SC_AUDIT |
| 5837 | {"SC_AUDIT", _SC_AUDIT}, |
| 5838 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5839 | #ifdef _SC_AVPHYS_PAGES |
| 5840 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5841 | #endif |
| 5842 | #ifdef _SC_BC_BASE_MAX |
| 5843 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5844 | #endif |
| 5845 | #ifdef _SC_BC_DIM_MAX |
| 5846 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5847 | #endif |
| 5848 | #ifdef _SC_BC_SCALE_MAX |
| 5849 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5850 | #endif |
| 5851 | #ifdef _SC_BC_STRING_MAX |
| 5852 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5853 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5854 | #ifdef _SC_CAP |
| 5855 | {"SC_CAP", _SC_CAP}, |
| 5856 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5857 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5858 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5859 | #endif |
| 5860 | #ifdef _SC_CHAR_BIT |
| 5861 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5862 | #endif |
| 5863 | #ifdef _SC_CHAR_MAX |
| 5864 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5865 | #endif |
| 5866 | #ifdef _SC_CHAR_MIN |
| 5867 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5868 | #endif |
| 5869 | #ifdef _SC_CHILD_MAX |
| 5870 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5871 | #endif |
| 5872 | #ifdef _SC_CLK_TCK |
| 5873 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 5874 | #endif |
| 5875 | #ifdef _SC_COHER_BLKSZ |
| 5876 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 5877 | #endif |
| 5878 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 5879 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 5880 | #endif |
| 5881 | #ifdef _SC_DCACHE_ASSOC |
| 5882 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5883 | #endif |
| 5884 | #ifdef _SC_DCACHE_BLKSZ |
| 5885 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5886 | #endif |
| 5887 | #ifdef _SC_DCACHE_LINESZ |
| 5888 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 5889 | #endif |
| 5890 | #ifdef _SC_DCACHE_SZ |
| 5891 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5892 | #endif |
| 5893 | #ifdef _SC_DCACHE_TBLKSZ |
| 5894 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5895 | #endif |
| 5896 | #ifdef _SC_DELAYTIMER_MAX |
| 5897 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5898 | #endif |
| 5899 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5900 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5901 | #endif |
| 5902 | #ifdef _SC_EXPR_NEST_MAX |
| 5903 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5904 | #endif |
| 5905 | #ifdef _SC_FSYNC |
| 5906 | {"SC_FSYNC", _SC_FSYNC}, |
| 5907 | #endif |
| 5908 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5909 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5910 | #endif |
| 5911 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5912 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5913 | #endif |
| 5914 | #ifdef _SC_ICACHE_ASSOC |
| 5915 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5916 | #endif |
| 5917 | #ifdef _SC_ICACHE_BLKSZ |
| 5918 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5919 | #endif |
| 5920 | #ifdef _SC_ICACHE_LINESZ |
| 5921 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5922 | #endif |
| 5923 | #ifdef _SC_ICACHE_SZ |
| 5924 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5925 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5926 | #ifdef _SC_INF |
| 5927 | {"SC_INF", _SC_INF}, |
| 5928 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5929 | #ifdef _SC_INT_MAX |
| 5930 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5931 | #endif |
| 5932 | #ifdef _SC_INT_MIN |
| 5933 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5934 | #endif |
| 5935 | #ifdef _SC_IOV_MAX |
| 5936 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5937 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5938 | #ifdef _SC_IP_SECOPTS |
| 5939 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5940 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5941 | #ifdef _SC_JOB_CONTROL |
| 5942 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5943 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5944 | #ifdef _SC_KERN_POINTERS |
| 5945 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5946 | #endif |
| 5947 | #ifdef _SC_KERN_SIM |
| 5948 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5949 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5950 | #ifdef _SC_LINE_MAX |
| 5951 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5952 | #endif |
| 5953 | #ifdef _SC_LOGIN_NAME_MAX |
| 5954 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5955 | #endif |
| 5956 | #ifdef _SC_LOGNAME_MAX |
| 5957 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5958 | #endif |
| 5959 | #ifdef _SC_LONG_BIT |
| 5960 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5961 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5962 | #ifdef _SC_MAC |
| 5963 | {"SC_MAC", _SC_MAC}, |
| 5964 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5965 | #ifdef _SC_MAPPED_FILES |
| 5966 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5967 | #endif |
| 5968 | #ifdef _SC_MAXPID |
| 5969 | {"SC_MAXPID", _SC_MAXPID}, |
| 5970 | #endif |
| 5971 | #ifdef _SC_MB_LEN_MAX |
| 5972 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5973 | #endif |
| 5974 | #ifdef _SC_MEMLOCK |
| 5975 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5976 | #endif |
| 5977 | #ifdef _SC_MEMLOCK_RANGE |
| 5978 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5979 | #endif |
| 5980 | #ifdef _SC_MEMORY_PROTECTION |
| 5981 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5982 | #endif |
| 5983 | #ifdef _SC_MESSAGE_PASSING |
| 5984 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5985 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5986 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5987 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 5988 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5989 | #ifdef _SC_MQ_OPEN_MAX |
| 5990 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 5991 | #endif |
| 5992 | #ifdef _SC_MQ_PRIO_MAX |
| 5993 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 5994 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5995 | #ifdef _SC_NACLS_MAX |
| 5996 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 5997 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5998 | #ifdef _SC_NGROUPS_MAX |
| 5999 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6000 | #endif |
| 6001 | #ifdef _SC_NL_ARGMAX |
| 6002 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6003 | #endif |
| 6004 | #ifdef _SC_NL_LANGMAX |
| 6005 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6006 | #endif |
| 6007 | #ifdef _SC_NL_MSGMAX |
| 6008 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6009 | #endif |
| 6010 | #ifdef _SC_NL_NMAX |
| 6011 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6012 | #endif |
| 6013 | #ifdef _SC_NL_SETMAX |
| 6014 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6015 | #endif |
| 6016 | #ifdef _SC_NL_TEXTMAX |
| 6017 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6018 | #endif |
| 6019 | #ifdef _SC_NPROCESSORS_CONF |
| 6020 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6021 | #endif |
| 6022 | #ifdef _SC_NPROCESSORS_ONLN |
| 6023 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6024 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6025 | #ifdef _SC_NPROC_CONF |
| 6026 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6027 | #endif |
| 6028 | #ifdef _SC_NPROC_ONLN |
| 6029 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6030 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6031 | #ifdef _SC_NZERO |
| 6032 | {"SC_NZERO", _SC_NZERO}, |
| 6033 | #endif |
| 6034 | #ifdef _SC_OPEN_MAX |
| 6035 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6036 | #endif |
| 6037 | #ifdef _SC_PAGESIZE |
| 6038 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6039 | #endif |
| 6040 | #ifdef _SC_PAGE_SIZE |
| 6041 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6042 | #endif |
| 6043 | #ifdef _SC_PASS_MAX |
| 6044 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6045 | #endif |
| 6046 | #ifdef _SC_PHYS_PAGES |
| 6047 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6048 | #endif |
| 6049 | #ifdef _SC_PII |
| 6050 | {"SC_PII", _SC_PII}, |
| 6051 | #endif |
| 6052 | #ifdef _SC_PII_INTERNET |
| 6053 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6054 | #endif |
| 6055 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6056 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6057 | #endif |
| 6058 | #ifdef _SC_PII_INTERNET_STREAM |
| 6059 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6060 | #endif |
| 6061 | #ifdef _SC_PII_OSI |
| 6062 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6063 | #endif |
| 6064 | #ifdef _SC_PII_OSI_CLTS |
| 6065 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6066 | #endif |
| 6067 | #ifdef _SC_PII_OSI_COTS |
| 6068 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6069 | #endif |
| 6070 | #ifdef _SC_PII_OSI_M |
| 6071 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6072 | #endif |
| 6073 | #ifdef _SC_PII_SOCKET |
| 6074 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6075 | #endif |
| 6076 | #ifdef _SC_PII_XTI |
| 6077 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6078 | #endif |
| 6079 | #ifdef _SC_POLL |
| 6080 | {"SC_POLL", _SC_POLL}, |
| 6081 | #endif |
| 6082 | #ifdef _SC_PRIORITIZED_IO |
| 6083 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6084 | #endif |
| 6085 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6086 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6087 | #endif |
| 6088 | #ifdef _SC_REALTIME_SIGNALS |
| 6089 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6090 | #endif |
| 6091 | #ifdef _SC_RE_DUP_MAX |
| 6092 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6093 | #endif |
| 6094 | #ifdef _SC_RTSIG_MAX |
| 6095 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6096 | #endif |
| 6097 | #ifdef _SC_SAVED_IDS |
| 6098 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6099 | #endif |
| 6100 | #ifdef _SC_SCHAR_MAX |
| 6101 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6102 | #endif |
| 6103 | #ifdef _SC_SCHAR_MIN |
| 6104 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6105 | #endif |
| 6106 | #ifdef _SC_SELECT |
| 6107 | {"SC_SELECT", _SC_SELECT}, |
| 6108 | #endif |
| 6109 | #ifdef _SC_SEMAPHORES |
| 6110 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6111 | #endif |
| 6112 | #ifdef _SC_SEM_NSEMS_MAX |
| 6113 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6114 | #endif |
| 6115 | #ifdef _SC_SEM_VALUE_MAX |
| 6116 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6117 | #endif |
| 6118 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6119 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6120 | #endif |
| 6121 | #ifdef _SC_SHRT_MAX |
| 6122 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6123 | #endif |
| 6124 | #ifdef _SC_SHRT_MIN |
| 6125 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6126 | #endif |
| 6127 | #ifdef _SC_SIGQUEUE_MAX |
| 6128 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6129 | #endif |
| 6130 | #ifdef _SC_SIGRT_MAX |
| 6131 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6132 | #endif |
| 6133 | #ifdef _SC_SIGRT_MIN |
| 6134 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6135 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6136 | #ifdef _SC_SOFTPOWER |
| 6137 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6138 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6139 | #ifdef _SC_SPLIT_CACHE |
| 6140 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6141 | #endif |
| 6142 | #ifdef _SC_SSIZE_MAX |
| 6143 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6144 | #endif |
| 6145 | #ifdef _SC_STACK_PROT |
| 6146 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6147 | #endif |
| 6148 | #ifdef _SC_STREAM_MAX |
| 6149 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6150 | #endif |
| 6151 | #ifdef _SC_SYNCHRONIZED_IO |
| 6152 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6153 | #endif |
| 6154 | #ifdef _SC_THREADS |
| 6155 | {"SC_THREADS", _SC_THREADS}, |
| 6156 | #endif |
| 6157 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6158 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6159 | #endif |
| 6160 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6161 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6162 | #endif |
| 6163 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6164 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6165 | #endif |
| 6166 | #ifdef _SC_THREAD_KEYS_MAX |
| 6167 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6168 | #endif |
| 6169 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6170 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6171 | #endif |
| 6172 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6173 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6174 | #endif |
| 6175 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6176 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6177 | #endif |
| 6178 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6179 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6180 | #endif |
| 6181 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6182 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6183 | #endif |
| 6184 | #ifdef _SC_THREAD_STACK_MIN |
| 6185 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6186 | #endif |
| 6187 | #ifdef _SC_THREAD_THREADS_MAX |
| 6188 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6189 | #endif |
| 6190 | #ifdef _SC_TIMERS |
| 6191 | {"SC_TIMERS", _SC_TIMERS}, |
| 6192 | #endif |
| 6193 | #ifdef _SC_TIMER_MAX |
| 6194 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6195 | #endif |
| 6196 | #ifdef _SC_TTY_NAME_MAX |
| 6197 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6198 | #endif |
| 6199 | #ifdef _SC_TZNAME_MAX |
| 6200 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6201 | #endif |
| 6202 | #ifdef _SC_T_IOV_MAX |
| 6203 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6204 | #endif |
| 6205 | #ifdef _SC_UCHAR_MAX |
| 6206 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6207 | #endif |
| 6208 | #ifdef _SC_UINT_MAX |
| 6209 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6210 | #endif |
| 6211 | #ifdef _SC_UIO_MAXIOV |
| 6212 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6213 | #endif |
| 6214 | #ifdef _SC_ULONG_MAX |
| 6215 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6216 | #endif |
| 6217 | #ifdef _SC_USHRT_MAX |
| 6218 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6219 | #endif |
| 6220 | #ifdef _SC_VERSION |
| 6221 | {"SC_VERSION", _SC_VERSION}, |
| 6222 | #endif |
| 6223 | #ifdef _SC_WORD_BIT |
| 6224 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6225 | #endif |
| 6226 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6227 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6228 | #endif |
| 6229 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6230 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6231 | #endif |
| 6232 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6233 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6234 | #endif |
| 6235 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6236 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6237 | #endif |
| 6238 | #ifdef _SC_XOPEN_CRYPT |
| 6239 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6240 | #endif |
| 6241 | #ifdef _SC_XOPEN_ENH_I18N |
| 6242 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6243 | #endif |
| 6244 | #ifdef _SC_XOPEN_LEGACY |
| 6245 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6246 | #endif |
| 6247 | #ifdef _SC_XOPEN_REALTIME |
| 6248 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6249 | #endif |
| 6250 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6251 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6252 | #endif |
| 6253 | #ifdef _SC_XOPEN_SHM |
| 6254 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6255 | #endif |
| 6256 | #ifdef _SC_XOPEN_UNIX |
| 6257 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6258 | #endif |
| 6259 | #ifdef _SC_XOPEN_VERSION |
| 6260 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6261 | #endif |
| 6262 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6263 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6264 | #endif |
| 6265 | #ifdef _SC_XOPEN_XPG2 |
| 6266 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6267 | #endif |
| 6268 | #ifdef _SC_XOPEN_XPG3 |
| 6269 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6270 | #endif |
| 6271 | #ifdef _SC_XOPEN_XPG4 |
| 6272 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6273 | #endif |
| 6274 | }; |
| 6275 | |
| 6276 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6277 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6278 | { |
| 6279 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6280 | sizeof(posix_constants_sysconf) |
| 6281 | / sizeof(struct constdef)); |
| 6282 | } |
| 6283 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6284 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6285 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6286 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6287 | |
| 6288 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6289 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6290 | { |
| 6291 | PyObject *result = NULL; |
| 6292 | int name; |
| 6293 | |
| 6294 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6295 | int value; |
| 6296 | |
| 6297 | errno = 0; |
| 6298 | value = sysconf(name); |
| 6299 | if (value == -1 && errno != 0) |
| 6300 | posix_error(); |
| 6301 | else |
| 6302 | result = PyInt_FromLong(value); |
| 6303 | } |
| 6304 | return result; |
| 6305 | } |
| 6306 | #endif |
| 6307 | |
| 6308 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6309 | /* This code is used to ensure that the tables of configuration value names |
| 6310 | * are in sorted order as required by conv_confname(), and also to build the |
| 6311 | * the exported dictionaries that are used to publish information about the |
| 6312 | * names available on the host platform. |
| 6313 | * |
| 6314 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6315 | * when used, even for platforms we're not able to test on. It also makes |
| 6316 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6317 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6318 | |
| 6319 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6320 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6321 | { |
| 6322 | const struct constdef *c1 = |
| 6323 | (const struct constdef *) v1; |
| 6324 | const struct constdef *c2 = |
| 6325 | (const struct constdef *) v2; |
| 6326 | |
| 6327 | return strcmp(c1->name, c2->name); |
| 6328 | } |
| 6329 | |
| 6330 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6331 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6332 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6333 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6334 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6335 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6336 | |
| 6337 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6338 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6339 | if (d == NULL) |
| 6340 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6341 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6342 | for (i=0; i < tablesize; ++i) { |
| 6343 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6344 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6345 | Py_XDECREF(o); |
| 6346 | Py_DECREF(d); |
| 6347 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6348 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6349 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6350 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6351 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6354 | /* Return -1 on failure, 0 on success. */ |
| 6355 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6356 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6357 | { |
| 6358 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6359 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6360 | sizeof(posix_constants_pathconf) |
| 6361 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6362 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6363 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6364 | #endif |
| 6365 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6366 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6367 | sizeof(posix_constants_confstr) |
| 6368 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6369 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6370 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6371 | #endif |
| 6372 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6373 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6374 | sizeof(posix_constants_sysconf) |
| 6375 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6376 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6377 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6378 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6379 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6380 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6381 | |
| 6382 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6383 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6384 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6385 | 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] | 6386 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6387 | |
| 6388 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6389 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6390 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6391 | abort(); |
| 6392 | /*NOTREACHED*/ |
| 6393 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6394 | return NULL; |
| 6395 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6396 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6397 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6398 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6399 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6400 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6401 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6402 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6403 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6404 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6405 | application (if any) its extension is associated.\n\ |
| 6406 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6407 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6408 | \n\ |
| 6409 | startfile returns as soon as the associated application is launched.\n\ |
| 6410 | There is no option to wait for the application to close, and no way\n\ |
| 6411 | to retrieve the application's exit status.\n\ |
| 6412 | \n\ |
| 6413 | The filepath is relative to the current directory. If you want to use\n\ |
| 6414 | 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] | 6415 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6416 | |
| 6417 | static PyObject * |
| 6418 | win32_startfile(PyObject *self, PyObject *args) |
| 6419 | { |
| 6420 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6421 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6422 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6423 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6424 | if (unicode_file_names()) { |
| 6425 | PyObject *unipath, *woperation = NULL; |
| 6426 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6427 | &unipath, &operation)) { |
| 6428 | PyErr_Clear(); |
| 6429 | goto normal; |
| 6430 | } |
| 6431 | |
| 6432 | |
| 6433 | if (operation) { |
| 6434 | woperation = PyUnicode_DecodeASCII(operation, |
| 6435 | strlen(operation), NULL); |
| 6436 | if (!woperation) { |
| 6437 | PyErr_Clear(); |
| 6438 | operation = NULL; |
| 6439 | goto normal; |
| 6440 | } |
| 6441 | } |
| 6442 | |
| 6443 | Py_BEGIN_ALLOW_THREADS |
| 6444 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6445 | PyUnicode_AS_UNICODE(unipath), |
| 6446 | NULL, NULL, SW_SHOWNORMAL); |
| 6447 | Py_END_ALLOW_THREADS |
| 6448 | |
| 6449 | Py_XDECREF(woperation); |
| 6450 | if (rc <= (HINSTANCE)32) { |
| 6451 | PyObject *errval = win32_error_unicode("startfile", |
| 6452 | PyUnicode_AS_UNICODE(unipath)); |
| 6453 | return errval; |
| 6454 | } |
| 6455 | Py_INCREF(Py_None); |
| 6456 | return Py_None; |
| 6457 | } |
| 6458 | #endif |
| 6459 | |
| 6460 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6461 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 6462 | Py_FileSystemDefaultEncoding, &filepath, |
| 6463 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6464 | return NULL; |
| 6465 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6466 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6467 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6468 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6469 | if (rc <= (HINSTANCE)32) { |
| 6470 | PyObject *errval = win32_error("startfile", filepath); |
| 6471 | PyMem_Free(filepath); |
| 6472 | return errval; |
| 6473 | } |
| 6474 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6475 | Py_INCREF(Py_None); |
| 6476 | return Py_None; |
| 6477 | } |
| 6478 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6479 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6480 | #ifdef HAVE_GETLOADAVG |
| 6481 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6482 | "getloadavg() -> (float, float, float)\n\n\ |
| 6483 | Return the number of processes in the system run queue averaged over\n\ |
| 6484 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6485 | was unobtainable"); |
| 6486 | |
| 6487 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6488 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6489 | { |
| 6490 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6491 | if (getloadavg(loadavg, 3)!=3) { |
| 6492 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6493 | return NULL; |
| 6494 | } else |
| 6495 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6496 | } |
| 6497 | #endif |
| 6498 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6499 | #ifdef MS_WINDOWS |
| 6500 | |
| 6501 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6502 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6503 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6504 | |
| 6505 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6506 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6507 | DWORD dwFlags ); |
| 6508 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6509 | BYTE *pbBuffer ); |
| 6510 | |
| 6511 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6512 | /* This handle is never explicitly released. Instead, the operating |
| 6513 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6514 | static HCRYPTPROV hCryptProv = 0; |
| 6515 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6516 | static PyObject* |
| 6517 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6518 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6519 | int howMany; |
| 6520 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6521 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6522 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6523 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6524 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6525 | if (howMany < 0) |
| 6526 | return PyErr_Format(PyExc_ValueError, |
| 6527 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6528 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6529 | if (hCryptProv == 0) { |
| 6530 | HINSTANCE hAdvAPI32 = NULL; |
| 6531 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6532 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6533 | /* Obtain handle to the DLL containing CryptoAPI |
| 6534 | This should not fail */ |
| 6535 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6536 | if(hAdvAPI32 == NULL) |
| 6537 | return win32_error("GetModuleHandle", NULL); |
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 | /* Obtain pointers to the CryptoAPI functions |
| 6540 | This will fail on some early versions of Win95 */ |
| 6541 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6542 | hAdvAPI32, |
| 6543 | "CryptAcquireContextA"); |
| 6544 | if (pCryptAcquireContext == NULL) |
| 6545 | return PyErr_Format(PyExc_NotImplementedError, |
| 6546 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6547 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6548 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6549 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6550 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6551 | return PyErr_Format(PyExc_NotImplementedError, |
| 6552 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6553 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6554 | /* Acquire context */ |
| 6555 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6556 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6557 | return win32_error("CryptAcquireContext", NULL); |
| 6558 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6559 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6560 | /* Allocate bytes */ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6561 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6562 | if (result != NULL) { |
| 6563 | /* Get random data */ |
| 6564 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6565 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6566 | Py_DECREF(result); |
| 6567 | return win32_error("CryptGenRandom", NULL); |
| 6568 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6569 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6570 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6571 | } |
| 6572 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6573 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6574 | PyDoc_STRVAR(device_encoding__doc__, |
| 6575 | "device_encoding(fd) -> str\n\n\ |
| 6576 | Return a string describing the encoding of the device\n\ |
| 6577 | if the output is a terminal; else return None."); |
| 6578 | |
| 6579 | static PyObject * |
| 6580 | device_encoding(PyObject *self, PyObject *args) |
| 6581 | { |
| 6582 | int fd; |
| 6583 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6584 | return NULL; |
| 6585 | if (!isatty(fd)) { |
| 6586 | Py_INCREF(Py_None); |
| 6587 | return Py_None; |
| 6588 | } |
| 6589 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6590 | if (fd == 0) { |
| 6591 | char buf[100]; |
| 6592 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6593 | return PyUnicode_FromString(buf); |
| 6594 | } |
| 6595 | if (fd == 1 || fd == 2) { |
| 6596 | char buf[100]; |
| 6597 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6598 | return PyUnicode_FromString(buf); |
| 6599 | } |
| 6600 | #elif defined(CODESET) |
| 6601 | { |
| 6602 | char *codeset = nl_langinfo(CODESET); |
| 6603 | if (codeset) |
| 6604 | return PyUnicode_FromString(codeset); |
| 6605 | } |
| 6606 | #endif |
| 6607 | Py_INCREF(Py_None); |
| 6608 | return Py_None; |
| 6609 | } |
| 6610 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6611 | #ifdef __VMS |
| 6612 | /* Use openssl random routine */ |
| 6613 | #include <openssl/rand.h> |
| 6614 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6615 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6616 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6617 | |
| 6618 | static PyObject* |
| 6619 | vms_urandom(PyObject *self, PyObject *args) |
| 6620 | { |
| 6621 | int howMany; |
| 6622 | PyObject* result; |
| 6623 | |
| 6624 | /* Read arguments */ |
| 6625 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6626 | return NULL; |
| 6627 | if (howMany < 0) |
| 6628 | return PyErr_Format(PyExc_ValueError, |
| 6629 | "negative argument not allowed"); |
| 6630 | |
| 6631 | /* Allocate bytes */ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6632 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6633 | if (result != NULL) { |
| 6634 | /* Get random data */ |
| 6635 | if (RAND_pseudo_bytes((unsigned char*) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6636 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6637 | howMany) < 0) { |
| 6638 | Py_DECREF(result); |
| 6639 | return PyErr_Format(PyExc_ValueError, |
| 6640 | "RAND_pseudo_bytes"); |
| 6641 | } |
| 6642 | } |
| 6643 | return result; |
| 6644 | } |
| 6645 | #endif |
| 6646 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6647 | static PyMethodDef posix_methods[] = { |
| 6648 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6649 | #ifdef HAVE_TTYNAME |
| 6650 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6651 | #endif |
| 6652 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6653 | #ifdef HAVE_CHFLAGS |
| 6654 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 6655 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6656 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6657 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6658 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6659 | #endif /* HAVE_CHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6660 | #ifdef HAVE_LCHFLAGS |
| 6661 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 6662 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6663 | #ifdef HAVE_LCHOWN |
| 6664 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6665 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6666 | #ifdef HAVE_CHROOT |
| 6667 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6668 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6669 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6670 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6671 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6672 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6673 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6674 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6675 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6676 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6677 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6678 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6679 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6680 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6681 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6682 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6683 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6684 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6685 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6686 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6687 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6688 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6689 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6690 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6691 | {"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] | 6692 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6693 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6694 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6695 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6696 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6697 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6698 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6699 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6700 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6701 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6702 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6703 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6704 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6705 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6706 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6707 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6708 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6709 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6710 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6711 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6712 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6713 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6714 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6715 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6716 | #if defined(PYOS_OS2) |
| 6717 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 6718 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 6719 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6720 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6721 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6722 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6723 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6724 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6725 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6726 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6727 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6728 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6729 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6730 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6731 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6732 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6733 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6734 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6735 | #endif /* HAVE_GETEGID */ |
| 6736 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6737 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6738 | #endif /* HAVE_GETEUID */ |
| 6739 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6740 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6741 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6742 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6743 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6744 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6745 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6746 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6747 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6748 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6749 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6750 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6751 | #endif /* HAVE_GETPPID */ |
| 6752 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6753 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6754 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6755 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6756 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6757 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6758 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6759 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6760 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6761 | #ifdef HAVE_KILLPG |
| 6762 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6763 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6764 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6765 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6766 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 6767 | #ifdef MS_WINDOWS |
| 6768 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 6769 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6770 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6771 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6772 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6773 | #ifdef HAVE_SETEUID |
| 6774 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6775 | #endif /* HAVE_SETEUID */ |
| 6776 | #ifdef HAVE_SETEGID |
| 6777 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6778 | #endif /* HAVE_SETEGID */ |
| 6779 | #ifdef HAVE_SETREUID |
| 6780 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6781 | #endif /* HAVE_SETREUID */ |
| 6782 | #ifdef HAVE_SETREGID |
| 6783 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6784 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6785 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6786 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6787 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6788 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6789 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6790 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6791 | #ifdef HAVE_GETPGID |
| 6792 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 6793 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6794 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6795 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6796 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6797 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6798 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6799 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6800 | #ifdef HAVE_WAIT3 |
| 6801 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 6802 | #endif /* HAVE_WAIT3 */ |
| 6803 | #ifdef HAVE_WAIT4 |
| 6804 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 6805 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6806 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6807 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6808 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6809 | #ifdef HAVE_GETSID |
| 6810 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 6811 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6812 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6813 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6814 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6815 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6816 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6817 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6818 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6819 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6820 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6821 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6822 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6823 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6824 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6825 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6826 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6827 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6828 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6829 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6830 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6831 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6832 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6833 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6834 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6835 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6836 | #endif |
| 6837 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6838 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6839 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6840 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6841 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 6842 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6843 | #ifdef HAVE_DEVICE_MACROS |
| 6844 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 6845 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 6846 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 6847 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6848 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6849 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6850 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6851 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6852 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6853 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6854 | #ifdef HAVE_UNSETENV |
| 6855 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6856 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6857 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6858 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6859 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6860 | #ifdef HAVE_FCHDIR |
| 6861 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 6862 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6863 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6864 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6865 | #endif |
| 6866 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6867 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6868 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6869 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6870 | #ifdef WCOREDUMP |
| 6871 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 6872 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6873 | #ifdef WIFCONTINUED |
| 6874 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 6875 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6876 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6877 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6878 | #endif /* WIFSTOPPED */ |
| 6879 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6880 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6881 | #endif /* WIFSIGNALED */ |
| 6882 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6883 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6884 | #endif /* WIFEXITED */ |
| 6885 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6886 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6887 | #endif /* WEXITSTATUS */ |
| 6888 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6889 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6890 | #endif /* WTERMSIG */ |
| 6891 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6892 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6893 | #endif /* WSTOPSIG */ |
| 6894 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6895 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6896 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6897 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6898 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6899 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6900 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6901 | #ifdef HAVE_CONFSTR |
| 6902 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 6903 | #endif |
| 6904 | #ifdef HAVE_SYSCONF |
| 6905 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 6906 | #endif |
| 6907 | #ifdef HAVE_FPATHCONF |
| 6908 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 6909 | #endif |
| 6910 | #ifdef HAVE_PATHCONF |
| 6911 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 6912 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6913 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6914 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 6915 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 6916 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6917 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6918 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6919 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6920 | #ifdef MS_WINDOWS |
| 6921 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 6922 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6923 | #ifdef __VMS |
| 6924 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 6925 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6926 | {NULL, NULL} /* Sentinel */ |
| 6927 | }; |
| 6928 | |
| 6929 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6930 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6931 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6932 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6933 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6934 | } |
| 6935 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6936 | #if defined(PYOS_OS2) |
| 6937 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6938 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6939 | { |
| 6940 | APIRET rc; |
| 6941 | ULONG values[QSV_MAX+1]; |
| 6942 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 6943 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6944 | |
| 6945 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 6946 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6947 | Py_END_ALLOW_THREADS |
| 6948 | |
| 6949 | if (rc != NO_ERROR) { |
| 6950 | os2_error(rc); |
| 6951 | return -1; |
| 6952 | } |
| 6953 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6954 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 6955 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 6956 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 6957 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 6958 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 6959 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 6960 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6961 | |
| 6962 | switch (values[QSV_VERSION_MINOR]) { |
| 6963 | case 0: ver = "2.00"; break; |
| 6964 | case 10: ver = "2.10"; break; |
| 6965 | case 11: ver = "2.11"; break; |
| 6966 | case 30: ver = "3.00"; break; |
| 6967 | case 40: ver = "4.00"; break; |
| 6968 | case 50: ver = "5.00"; break; |
| 6969 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 6970 | PyOS_snprintf(tmp, sizeof(tmp), |
| 6971 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 6972 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6973 | ver = &tmp[0]; |
| 6974 | } |
| 6975 | |
| 6976 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6977 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6978 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6979 | |
| 6980 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 6981 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 6982 | tmp[1] = ':'; |
| 6983 | tmp[2] = '\0'; |
| 6984 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6985 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6986 | } |
| 6987 | #endif |
| 6988 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6989 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6990 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6991 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6992 | #ifdef F_OK |
| 6993 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6994 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6995 | #ifdef R_OK |
| 6996 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6997 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6998 | #ifdef W_OK |
| 6999 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7000 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7001 | #ifdef X_OK |
| 7002 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7003 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7004 | #ifdef NGROUPS_MAX |
| 7005 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7006 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7007 | #ifdef TMP_MAX |
| 7008 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7009 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7010 | #ifdef WCONTINUED |
| 7011 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7012 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7013 | #ifdef WNOHANG |
| 7014 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7015 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7016 | #ifdef WUNTRACED |
| 7017 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7018 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7019 | #ifdef O_RDONLY |
| 7020 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7021 | #endif |
| 7022 | #ifdef O_WRONLY |
| 7023 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7024 | #endif |
| 7025 | #ifdef O_RDWR |
| 7026 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7027 | #endif |
| 7028 | #ifdef O_NDELAY |
| 7029 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7030 | #endif |
| 7031 | #ifdef O_NONBLOCK |
| 7032 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7033 | #endif |
| 7034 | #ifdef O_APPEND |
| 7035 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7036 | #endif |
| 7037 | #ifdef O_DSYNC |
| 7038 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7039 | #endif |
| 7040 | #ifdef O_RSYNC |
| 7041 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7042 | #endif |
| 7043 | #ifdef O_SYNC |
| 7044 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7045 | #endif |
| 7046 | #ifdef O_NOCTTY |
| 7047 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7048 | #endif |
| 7049 | #ifdef O_CREAT |
| 7050 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7051 | #endif |
| 7052 | #ifdef O_EXCL |
| 7053 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7054 | #endif |
| 7055 | #ifdef O_TRUNC |
| 7056 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7057 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7058 | #ifdef O_BINARY |
| 7059 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7060 | #endif |
| 7061 | #ifdef O_TEXT |
| 7062 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7063 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7064 | #ifdef O_LARGEFILE |
| 7065 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7066 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7067 | #ifdef O_SHLOCK |
| 7068 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7069 | #endif |
| 7070 | #ifdef O_EXLOCK |
| 7071 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7072 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7073 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7074 | /* MS Windows */ |
| 7075 | #ifdef O_NOINHERIT |
| 7076 | /* Don't inherit in child processes. */ |
| 7077 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7078 | #endif |
| 7079 | #ifdef _O_SHORT_LIVED |
| 7080 | /* Optimize for short life (keep in memory). */ |
| 7081 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7082 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7083 | #endif |
| 7084 | #ifdef O_TEMPORARY |
| 7085 | /* Automatically delete when last handle is closed. */ |
| 7086 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7087 | #endif |
| 7088 | #ifdef O_RANDOM |
| 7089 | /* Optimize for random access. */ |
| 7090 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7091 | #endif |
| 7092 | #ifdef O_SEQUENTIAL |
| 7093 | /* Optimize for sequential access. */ |
| 7094 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7095 | #endif |
| 7096 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7097 | /* GNU extensions. */ |
| 7098 | #ifdef O_DIRECT |
| 7099 | /* Direct disk access. */ |
| 7100 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7101 | #endif |
| 7102 | #ifdef O_DIRECTORY |
| 7103 | /* Must be a directory. */ |
| 7104 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7105 | #endif |
| 7106 | #ifdef O_NOFOLLOW |
| 7107 | /* Do not follow links. */ |
| 7108 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7109 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7110 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7111 | /* These come from sysexits.h */ |
| 7112 | #ifdef EX_OK |
| 7113 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7114 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7115 | #ifdef EX_USAGE |
| 7116 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7117 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7118 | #ifdef EX_DATAERR |
| 7119 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7120 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7121 | #ifdef EX_NOINPUT |
| 7122 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7123 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7124 | #ifdef EX_NOUSER |
| 7125 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7126 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7127 | #ifdef EX_NOHOST |
| 7128 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7129 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7130 | #ifdef EX_UNAVAILABLE |
| 7131 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7132 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7133 | #ifdef EX_SOFTWARE |
| 7134 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7135 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7136 | #ifdef EX_OSERR |
| 7137 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7138 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7139 | #ifdef EX_OSFILE |
| 7140 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7141 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7142 | #ifdef EX_CANTCREAT |
| 7143 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7144 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7145 | #ifdef EX_IOERR |
| 7146 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7147 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7148 | #ifdef EX_TEMPFAIL |
| 7149 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7150 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7151 | #ifdef EX_PROTOCOL |
| 7152 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7153 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7154 | #ifdef EX_NOPERM |
| 7155 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7156 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7157 | #ifdef EX_CONFIG |
| 7158 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7159 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7160 | #ifdef EX_NOTFOUND |
| 7161 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7162 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7163 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7164 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7165 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7166 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7167 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7168 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7169 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7170 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7171 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7172 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7173 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7174 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7175 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7176 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7177 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7178 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7179 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7180 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7181 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7182 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7183 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7184 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7185 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7186 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7187 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7188 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7189 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7190 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7191 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7192 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7193 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7194 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7195 | #if defined(PYOS_OS2) |
| 7196 | if (insertvalues(d)) return -1; |
| 7197 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7198 | return 0; |
| 7199 | } |
| 7200 | |
| 7201 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7202 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7203 | #define INITFUNC initnt |
| 7204 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7205 | |
| 7206 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7207 | #define INITFUNC initos2 |
| 7208 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7209 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7210 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7211 | #define INITFUNC initposix |
| 7212 | #define MODNAME "posix" |
| 7213 | #endif |
| 7214 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7215 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7216 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7217 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7218 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7219 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7220 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7221 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7222 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7223 | if (m == NULL) |
| 7224 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7225 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7226 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7227 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7228 | Py_XINCREF(v); |
| 7229 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7230 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7231 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7232 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7233 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7234 | return; |
| 7235 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7236 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7237 | return; |
| 7238 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7239 | Py_INCREF(PyExc_OSError); |
| 7240 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7241 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7242 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7243 | if (posix_putenv_garbage == NULL) |
| 7244 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7245 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7246 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7247 | if (!initialized) { |
| 7248 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7249 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7250 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7251 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7252 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7253 | structseq_new = StatResultType.tp_new; |
| 7254 | StatResultType.tp_new = statresult_new; |
| 7255 | |
| 7256 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7257 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 7258 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7259 | Py_INCREF((PyObject*) &StatResultType); |
| 7260 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7261 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7262 | PyModule_AddObject(m, "statvfs_result", |
| 7263 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7264 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7265 | |
| 7266 | #ifdef __APPLE__ |
| 7267 | /* |
| 7268 | * Step 2 of weak-linking support on Mac OS X. |
| 7269 | * |
| 7270 | * The code below removes functions that are not available on the |
| 7271 | * currently active platform. |
| 7272 | * |
| 7273 | * This block allow one to use a python binary that was build on |
| 7274 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7275 | * OSX 10.4. |
| 7276 | */ |
| 7277 | #ifdef HAVE_FSTATVFS |
| 7278 | if (fstatvfs == NULL) { |
| 7279 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 7280 | return; |
| 7281 | } |
| 7282 | } |
| 7283 | #endif /* HAVE_FSTATVFS */ |
| 7284 | |
| 7285 | #ifdef HAVE_STATVFS |
| 7286 | if (statvfs == NULL) { |
| 7287 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 7288 | return; |
| 7289 | } |
| 7290 | } |
| 7291 | #endif /* HAVE_STATVFS */ |
| 7292 | |
| 7293 | # ifdef HAVE_LCHOWN |
| 7294 | if (lchown == NULL) { |
| 7295 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 7296 | return; |
| 7297 | } |
| 7298 | } |
| 7299 | #endif /* HAVE_LCHOWN */ |
| 7300 | |
| 7301 | |
| 7302 | #endif /* __APPLE__ */ |
| 7303 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7304 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7305 | |
| 7306 | #ifdef __cplusplus |
| 7307 | } |
| 7308 | #endif |