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 | |
| 305 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 306 | #define USE_TMPNAM_R |
| 307 | #endif |
| 308 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 309 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 310 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 311 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 312 | # define STAT win32_stat |
| 313 | # define FSTAT win32_fstat |
| 314 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 315 | #else |
| 316 | # define STAT stat |
| 317 | # define FSTAT fstat |
| 318 | # define STRUCT_STAT struct stat |
| 319 | #endif |
| 320 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 321 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 322 | #include <sys/mkdev.h> |
| 323 | #else |
| 324 | #if defined(MAJOR_IN_SYSMACROS) |
| 325 | #include <sys/sysmacros.h> |
| 326 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 327 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 328 | #include <sys/mkdev.h> |
| 329 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 330 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 331 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 332 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 333 | #ifdef WITH_NEXT_FRAMEWORK |
| 334 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 335 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 336 | */ |
| 337 | #include <crt_externs.h> |
| 338 | static char **environ; |
| 339 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 340 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 341 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 343 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 344 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 346 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | char **e; |
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 |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 355 | if (environ == NULL) |
| 356 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 357 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 358 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 359 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 360 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 361 | char *p = strchr(*e, '='); |
| 362 | if (p == NULL) |
| 363 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 364 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 365 | if (k == NULL) { |
| 366 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 367 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 368 | } |
| 369 | v = PyString_FromString(p+1); |
| 370 | if (v == NULL) { |
| 371 | PyErr_Clear(); |
| 372 | Py_DECREF(k); |
| 373 | continue; |
| 374 | } |
| 375 | if (PyDict_GetItem(d, k) == NULL) { |
| 376 | if (PyDict_SetItem(d, k, v) != 0) |
| 377 | PyErr_Clear(); |
| 378 | } |
| 379 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 380 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 381 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 382 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 383 | { |
| 384 | APIRET rc; |
| 385 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 386 | |
| 387 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 388 | 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] | 389 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 390 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 391 | Py_DECREF(v); |
| 392 | } |
| 393 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 394 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 395 | PyObject *v = PyString_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 396 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 397 | Py_DECREF(v); |
| 398 | } |
| 399 | } |
| 400 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 401 | return d; |
| 402 | } |
| 403 | |
| 404 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 405 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 406 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 407 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 408 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 409 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 410 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 411 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 412 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 413 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 414 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 415 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 418 | #ifdef Py_WIN_WIDE_FILENAMES |
| 419 | static PyObject * |
| 420 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 421 | { |
| 422 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 423 | } |
| 424 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 425 | |
| 426 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 427 | static PyObject * |
| 428 | posix_error_with_allocated_filename(char* name) |
| 429 | { |
| 430 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 431 | PyMem_Free(name); |
| 432 | return rc; |
| 433 | } |
| 434 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 435 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 436 | static PyObject * |
| 437 | win32_error(char* function, char* filename) |
| 438 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 439 | /* XXX We should pass the function name along in the future. |
| 440 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 441 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 442 | Windows error object, which is non-trivial. |
| 443 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 444 | errno = GetLastError(); |
| 445 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 446 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 447 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 448 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 449 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 450 | |
| 451 | #ifdef Py_WIN_WIDE_FILENAMES |
| 452 | static PyObject * |
| 453 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 454 | { |
| 455 | /* XXX - see win32_error for comments on 'function' */ |
| 456 | errno = GetLastError(); |
| 457 | if (filename) |
| 458 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 459 | else |
| 460 | return PyErr_SetFromWindowsErr(errno); |
| 461 | } |
| 462 | |
| 463 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 464 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* Function suitable for O& conversion */ |
| 468 | static int |
| 469 | convert_to_unicode(PyObject *arg, void* _param) |
| 470 | { |
| 471 | PyObject **param = (PyObject**)_param; |
| 472 | if (PyUnicode_CheckExact(arg)) { |
| 473 | Py_INCREF(arg); |
| 474 | *param = arg; |
| 475 | } |
| 476 | else if (PyUnicode_Check(arg)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 477 | /* For a Unicode subtype that's not a Unicode object, |
| 478 | return a true Unicode object with the same data. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 479 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), |
| 480 | PyUnicode_GET_SIZE(arg)); |
| 481 | return *param != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 482 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 483 | else |
| 484 | *param = PyUnicode_FromEncodedObject(arg, |
| 485 | Py_FileSystemDefaultEncoding, |
| 486 | "strict"); |
| 487 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 491 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 492 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 493 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 494 | #if defined(PYOS_OS2) |
| 495 | /********************************************************************** |
| 496 | * Helper Function to Trim and Format OS/2 Messages |
| 497 | **********************************************************************/ |
| 498 | static void |
| 499 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 500 | { |
| 501 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 502 | |
| 503 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 504 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 505 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 506 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 507 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 508 | } |
| 509 | |
| 510 | /* Add Optional Reason Text */ |
| 511 | if (reason) { |
| 512 | strcat(msgbuf, " : "); |
| 513 | strcat(msgbuf, reason); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /********************************************************************** |
| 518 | * Decode an OS/2 Operating System Error Code |
| 519 | * |
| 520 | * A convenience function to lookup an OS/2 error code and return a |
| 521 | * text message we can use to raise a Python exception. |
| 522 | * |
| 523 | * Notes: |
| 524 | * The messages for errors returned from the OS/2 kernel reside in |
| 525 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 526 | * |
| 527 | **********************************************************************/ |
| 528 | static char * |
| 529 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 530 | { |
| 531 | APIRET rc; |
| 532 | ULONG msglen; |
| 533 | |
| 534 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 535 | Py_BEGIN_ALLOW_THREADS |
| 536 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 537 | errorcode, "oso001.msg", &msglen); |
| 538 | Py_END_ALLOW_THREADS |
| 539 | |
| 540 | if (rc == NO_ERROR) |
| 541 | os2_formatmsg(msgbuf, msglen, reason); |
| 542 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 543 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 544 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 545 | |
| 546 | return msgbuf; |
| 547 | } |
| 548 | |
| 549 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 550 | errors are not in a global variable e.g. 'errno' nor are |
| 551 | they congruent with posix error numbers. */ |
| 552 | |
| 553 | static PyObject * os2_error(int code) |
| 554 | { |
| 555 | char text[1024]; |
| 556 | PyObject *v; |
| 557 | |
| 558 | os2_strerror(text, sizeof(text), code, ""); |
| 559 | |
| 560 | v = Py_BuildValue("(is)", code, text); |
| 561 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 562 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 563 | Py_DECREF(v); |
| 564 | } |
| 565 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 566 | } |
| 567 | |
| 568 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 569 | |
| 570 | /* POSIX generic methods */ |
| 571 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 572 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 573 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 574 | { |
| 575 | int fd; |
| 576 | int res; |
| 577 | fd = PyObject_AsFileDescriptor(fdobj); |
| 578 | if (fd < 0) |
| 579 | return NULL; |
| 580 | Py_BEGIN_ALLOW_THREADS |
| 581 | res = (*func)(fd); |
| 582 | Py_END_ALLOW_THREADS |
| 583 | if (res < 0) |
| 584 | return posix_error(); |
| 585 | Py_INCREF(Py_None); |
| 586 | return Py_None; |
| 587 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 588 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 589 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 590 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 591 | unicode_file_names(void) |
| 592 | { |
| 593 | static int canusewide = -1; |
| 594 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 595 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 596 | the Windows NT family. */ |
| 597 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 598 | } |
| 599 | return canusewide; |
| 600 | } |
| 601 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 602 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 603 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 604 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 605 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 606 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 607 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 608 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 609 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 610 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 611 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 612 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 613 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 614 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 615 | return posix_error_with_allocated_filename(path1); |
| 616 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 617 | Py_INCREF(Py_None); |
| 618 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 621 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 622 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 623 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 624 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 625 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 626 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 627 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 628 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 629 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 630 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 631 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 632 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 633 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 634 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 635 | PyMem_Free(path1); |
| 636 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 637 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 638 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 639 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 640 | Py_INCREF(Py_None); |
| 641 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 644 | #ifdef Py_WIN_WIDE_FILENAMES |
| 645 | static PyObject* |
| 646 | win32_1str(PyObject* args, char* func, |
| 647 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 648 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 649 | { |
| 650 | PyObject *uni; |
| 651 | char *ansi; |
| 652 | BOOL result; |
| 653 | if (unicode_file_names()) { |
| 654 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 655 | PyErr_Clear(); |
| 656 | else { |
| 657 | Py_BEGIN_ALLOW_THREADS |
| 658 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 659 | Py_END_ALLOW_THREADS |
| 660 | if (!result) |
| 661 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 662 | Py_INCREF(Py_None); |
| 663 | return Py_None; |
| 664 | } |
| 665 | } |
| 666 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 667 | return NULL; |
| 668 | Py_BEGIN_ALLOW_THREADS |
| 669 | result = funcA(ansi); |
| 670 | Py_END_ALLOW_THREADS |
| 671 | if (!result) |
| 672 | return win32_error(func, ansi); |
| 673 | Py_INCREF(Py_None); |
| 674 | return Py_None; |
| 675 | |
| 676 | } |
| 677 | |
| 678 | /* This is a reimplementation of the C library's chdir function, |
| 679 | but one that produces Win32 errors instead of DOS error codes. |
| 680 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 681 | it also needs to set "magic" environment variables indicating |
| 682 | the per-drive current directory, which are of the form =<drive>: */ |
| 683 | BOOL __stdcall |
| 684 | win32_chdir(LPCSTR path) |
| 685 | { |
| 686 | char new_path[MAX_PATH+1]; |
| 687 | int result; |
| 688 | char env[4] = "=x:"; |
| 689 | |
| 690 | if(!SetCurrentDirectoryA(path)) |
| 691 | return FALSE; |
| 692 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 693 | if (!result) |
| 694 | return FALSE; |
| 695 | /* In the ANSI API, there should not be any paths longer |
| 696 | than MAX_PATH. */ |
| 697 | assert(result <= MAX_PATH+1); |
| 698 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 699 | strncmp(new_path, "//", 2) == 0) |
| 700 | /* UNC path, nothing to do. */ |
| 701 | return TRUE; |
| 702 | env[1] = new_path[0]; |
| 703 | return SetEnvironmentVariableA(env, new_path); |
| 704 | } |
| 705 | |
| 706 | /* The Unicode version differs from the ANSI version |
| 707 | since the current directory might exceed MAX_PATH characters */ |
| 708 | BOOL __stdcall |
| 709 | win32_wchdir(LPCWSTR path) |
| 710 | { |
| 711 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 712 | int result; |
| 713 | wchar_t env[4] = L"=x:"; |
| 714 | |
| 715 | if(!SetCurrentDirectoryW(path)) |
| 716 | return FALSE; |
| 717 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 718 | if (!result) |
| 719 | return FALSE; |
| 720 | if (result > MAX_PATH+1) { |
| 721 | new_path = malloc(result); |
| 722 | if (!new_path) { |
| 723 | SetLastError(ERROR_OUTOFMEMORY); |
| 724 | return FALSE; |
| 725 | } |
| 726 | } |
| 727 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 728 | wcsncmp(new_path, L"//", 2) == 0) |
| 729 | /* UNC path, nothing to do. */ |
| 730 | return TRUE; |
| 731 | env[1] = new_path[0]; |
| 732 | result = SetEnvironmentVariableW(env, new_path); |
| 733 | if (new_path != _new_path) |
| 734 | free(new_path); |
| 735 | return result; |
| 736 | } |
| 737 | #endif |
| 738 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 739 | #ifdef MS_WINDOWS |
| 740 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 741 | - time stamps are restricted to second resolution |
| 742 | - file modification times suffer from forth-and-back conversions between |
| 743 | UTC and local time |
| 744 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 745 | */ |
| 746 | #define HAVE_STAT_NSEC 1 |
| 747 | |
| 748 | struct win32_stat{ |
| 749 | int st_dev; |
| 750 | __int64 st_ino; |
| 751 | unsigned short st_mode; |
| 752 | int st_nlink; |
| 753 | int st_uid; |
| 754 | int st_gid; |
| 755 | int st_rdev; |
| 756 | __int64 st_size; |
| 757 | int st_atime; |
| 758 | int st_atime_nsec; |
| 759 | int st_mtime; |
| 760 | int st_mtime_nsec; |
| 761 | int st_ctime; |
| 762 | int st_ctime_nsec; |
| 763 | }; |
| 764 | |
| 765 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 766 | |
| 767 | static void |
| 768 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 769 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 770 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 771 | /* Cannot simply cast and dereference in_ptr, |
| 772 | since it might not be aligned properly */ |
| 773 | __int64 in; |
| 774 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 775 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 776 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 777 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 778 | } |
| 779 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 780 | static void |
| 781 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 782 | { |
| 783 | /* XXX endianness */ |
| 784 | __int64 out; |
| 785 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 786 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 787 | memcpy(out_ptr, &out, sizeof(out)); |
| 788 | } |
| 789 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 790 | /* Below, we *know* that ugo+r is 0444 */ |
| 791 | #if _S_IREAD != 0400 |
| 792 | #error Unsupported C library |
| 793 | #endif |
| 794 | static int |
| 795 | attributes_to_mode(DWORD attr) |
| 796 | { |
| 797 | int m = 0; |
| 798 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 799 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 800 | else |
| 801 | m |= _S_IFREG; |
| 802 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 803 | m |= 0444; |
| 804 | else |
| 805 | m |= 0666; |
| 806 | return m; |
| 807 | } |
| 808 | |
| 809 | static int |
| 810 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 811 | { |
| 812 | memset(result, 0, sizeof(*result)); |
| 813 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 814 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 815 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 816 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 817 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 822 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 823 | static int checked = 0; |
| 824 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 825 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 826 | static void |
| 827 | check_gfax() |
| 828 | { |
| 829 | HINSTANCE hKernel32; |
| 830 | if (checked) |
| 831 | return; |
| 832 | checked = 1; |
| 833 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 834 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 835 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 836 | } |
| 837 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 838 | static BOOL |
| 839 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 840 | { |
| 841 | HANDLE hFindFile; |
| 842 | WIN32_FIND_DATAA FileData; |
| 843 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 844 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 845 | return FALSE; |
| 846 | FindClose(hFindFile); |
| 847 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 848 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 849 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 850 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 851 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 852 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 853 | return TRUE; |
| 854 | } |
| 855 | |
| 856 | static BOOL |
| 857 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 858 | { |
| 859 | HANDLE hFindFile; |
| 860 | WIN32_FIND_DATAW FileData; |
| 861 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 862 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 863 | return FALSE; |
| 864 | FindClose(hFindFile); |
| 865 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 866 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 867 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 868 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 869 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 870 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 871 | return TRUE; |
| 872 | } |
| 873 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 874 | static BOOL WINAPI |
| 875 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 876 | GET_FILEEX_INFO_LEVELS level, |
| 877 | LPVOID pv) |
| 878 | { |
| 879 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 880 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 881 | /* First try to use the system's implementation, if that is |
| 882 | available and either succeeds to gives an error other than |
| 883 | that it isn't implemented. */ |
| 884 | check_gfax(); |
| 885 | if (gfaxa) { |
| 886 | result = gfaxa(pszFile, level, pv); |
| 887 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 888 | return result; |
| 889 | } |
| 890 | /* It's either not present, or not implemented. |
| 891 | Emulate using FindFirstFile. */ |
| 892 | if (level != GetFileExInfoStandard) { |
| 893 | SetLastError(ERROR_INVALID_PARAMETER); |
| 894 | return FALSE; |
| 895 | } |
| 896 | /* Use GetFileAttributes to validate that the file name |
| 897 | does not contain wildcards (which FindFirstFile would |
| 898 | accept). */ |
| 899 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 900 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 901 | return attributes_from_dir(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static BOOL WINAPI |
| 905 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 906 | GET_FILEEX_INFO_LEVELS level, |
| 907 | LPVOID pv) |
| 908 | { |
| 909 | BOOL result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 910 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 911 | /* First try to use the system's implementation, if that is |
| 912 | available and either succeeds to gives an error other than |
| 913 | that it isn't implemented. */ |
| 914 | check_gfax(); |
| 915 | if (gfaxa) { |
| 916 | result = gfaxw(pszFile, level, pv); |
| 917 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 918 | return result; |
| 919 | } |
| 920 | /* It's either not present, or not implemented. |
| 921 | Emulate using FindFirstFile. */ |
| 922 | if (level != GetFileExInfoStandard) { |
| 923 | SetLastError(ERROR_INVALID_PARAMETER); |
| 924 | return FALSE; |
| 925 | } |
| 926 | /* Use GetFileAttributes to validate that the file name |
| 927 | does not contain wildcards (which FindFirstFile would |
| 928 | accept). */ |
| 929 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 930 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 931 | return attributes_from_dir_w(pszFile, pfad); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 934 | static int |
| 935 | win32_stat(const char* path, struct win32_stat *result) |
| 936 | { |
| 937 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 938 | int code; |
| 939 | char *dot; |
| 940 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 941 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 942 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 943 | /* Protocol violation: we explicitly clear errno, instead of |
| 944 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 945 | errno = 0; |
| 946 | return -1; |
| 947 | } else { |
| 948 | /* Could not get attributes on open file. Fall back to |
| 949 | reading the directory. */ |
| 950 | if (!attributes_from_dir(path, &info)) { |
| 951 | /* Very strange. This should not fail now */ |
| 952 | errno = 0; |
| 953 | return -1; |
| 954 | } |
| 955 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 956 | } |
| 957 | code = attribute_data_to_stat(&info, result); |
| 958 | if (code != 0) |
| 959 | return code; |
| 960 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 961 | dot = strrchr(path, '.'); |
| 962 | if (dot) { |
| 963 | if (stricmp(dot, ".bat") == 0 || |
| 964 | stricmp(dot, ".cmd") == 0 || |
| 965 | stricmp(dot, ".exe") == 0 || |
| 966 | stricmp(dot, ".com") == 0) |
| 967 | result->st_mode |= 0111; |
| 968 | } |
| 969 | return code; |
| 970 | } |
| 971 | |
| 972 | static int |
| 973 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 974 | { |
| 975 | int code; |
| 976 | const wchar_t *dot; |
| 977 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 978 | /* XXX not supported on Win95 and NT 3.x */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 979 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 980 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 981 | /* Protocol violation: we explicitly clear errno, instead of |
| 982 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 983 | errno = 0; |
| 984 | return -1; |
| 985 | } else { |
| 986 | /* Could not get attributes on open file. Fall back to |
| 987 | reading the directory. */ |
| 988 | if (!attributes_from_dir_w(path, &info)) { |
| 989 | /* Very strange. This should not fail now */ |
| 990 | errno = 0; |
| 991 | return -1; |
| 992 | } |
| 993 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 994 | } |
| 995 | code = attribute_data_to_stat(&info, result); |
| 996 | if (code < 0) |
| 997 | return code; |
| 998 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 999 | dot = wcsrchr(path, '.'); |
| 1000 | if (dot) { |
| 1001 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1002 | _wcsicmp(dot, L".cmd") == 0 || |
| 1003 | _wcsicmp(dot, L".exe") == 0 || |
| 1004 | _wcsicmp(dot, L".com") == 0) |
| 1005 | result->st_mode |= 0111; |
| 1006 | } |
| 1007 | return code; |
| 1008 | } |
| 1009 | |
| 1010 | static int |
| 1011 | win32_fstat(int file_number, struct win32_stat *result) |
| 1012 | { |
| 1013 | BY_HANDLE_FILE_INFORMATION info; |
| 1014 | HANDLE h; |
| 1015 | int type; |
| 1016 | |
| 1017 | h = (HANDLE)_get_osfhandle(file_number); |
| 1018 | |
| 1019 | /* Protocol violation: we explicitly clear errno, instead of |
| 1020 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1021 | errno = 0; |
| 1022 | |
| 1023 | if (h == INVALID_HANDLE_VALUE) { |
| 1024 | /* This is really a C library error (invalid file handle). |
| 1025 | We set the Win32 error to the closes one matching. */ |
| 1026 | SetLastError(ERROR_INVALID_HANDLE); |
| 1027 | return -1; |
| 1028 | } |
| 1029 | memset(result, 0, sizeof(*result)); |
| 1030 | |
| 1031 | type = GetFileType(h); |
| 1032 | if (type == FILE_TYPE_UNKNOWN) { |
| 1033 | DWORD error = GetLastError(); |
| 1034 | if (error != 0) { |
| 1035 | return -1; |
| 1036 | } |
| 1037 | /* else: valid but unknown file */ |
| 1038 | } |
| 1039 | |
| 1040 | if (type != FILE_TYPE_DISK) { |
| 1041 | if (type == FILE_TYPE_CHAR) |
| 1042 | result->st_mode = _S_IFCHR; |
| 1043 | else if (type == FILE_TYPE_PIPE) |
| 1044 | result->st_mode = _S_IFIFO; |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | if (!GetFileInformationByHandle(h, &info)) { |
| 1049 | return -1; |
| 1050 | } |
| 1051 | |
| 1052 | /* similar to stat() */ |
| 1053 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1054 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1055 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1056 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1057 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1058 | /* specific to fstat() */ |
| 1059 | result->st_nlink = info.nNumberOfLinks; |
| 1060 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | #endif /* MS_WINDOWS */ |
| 1065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1066 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1067 | "stat_result: Result from stat or lstat.\n\n\ |
| 1068 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1069 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1070 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1071 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1072 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1073 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1074 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1075 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1076 | |
| 1077 | static PyStructSequence_Field stat_result_fields[] = { |
| 1078 | {"st_mode", "protection bits"}, |
| 1079 | {"st_ino", "inode"}, |
| 1080 | {"st_dev", "device"}, |
| 1081 | {"st_nlink", "number of hard links"}, |
| 1082 | {"st_uid", "user ID of owner"}, |
| 1083 | {"st_gid", "group ID of owner"}, |
| 1084 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1085 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1086 | {NULL, "integer time of last access"}, |
| 1087 | {NULL, "integer time of last modification"}, |
| 1088 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1089 | {"st_atime", "time of last access"}, |
| 1090 | {"st_mtime", "time of last modification"}, |
| 1091 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1092 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1093 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1094 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1095 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1096 | {"st_blocks", "number of blocks allocated"}, |
| 1097 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1098 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1099 | {"st_rdev", "device type (if inode device)"}, |
| 1100 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1101 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1102 | {"st_flags", "user defined flags for file"}, |
| 1103 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1104 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1105 | {"st_gen", "generation number"}, |
| 1106 | #endif |
| 1107 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1108 | {"st_birthtime", "time of creation"}, |
| 1109 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1110 | {0} |
| 1111 | }; |
| 1112 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1113 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1114 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1115 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1116 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1117 | #endif |
| 1118 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1119 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1120 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1121 | #else |
| 1122 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1123 | #endif |
| 1124 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1125 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1126 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1127 | #else |
| 1128 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1129 | #endif |
| 1130 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1131 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1132 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1133 | #else |
| 1134 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1135 | #endif |
| 1136 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1137 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1138 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1139 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1140 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1141 | #endif |
| 1142 | |
| 1143 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1144 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1145 | #else |
| 1146 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1147 | #endif |
| 1148 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1149 | static PyStructSequence_Desc stat_result_desc = { |
| 1150 | "stat_result", /* name */ |
| 1151 | stat_result__doc__, /* doc */ |
| 1152 | stat_result_fields, |
| 1153 | 10 |
| 1154 | }; |
| 1155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1156 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1157 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1158 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1159 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1160 | 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] | 1161 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1162 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1163 | |
| 1164 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1165 | {"f_bsize", }, |
| 1166 | {"f_frsize", }, |
| 1167 | {"f_blocks", }, |
| 1168 | {"f_bfree", }, |
| 1169 | {"f_bavail", }, |
| 1170 | {"f_files", }, |
| 1171 | {"f_ffree", }, |
| 1172 | {"f_favail", }, |
| 1173 | {"f_flag", }, |
| 1174 | {"f_namemax",}, |
| 1175 | {0} |
| 1176 | }; |
| 1177 | |
| 1178 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1179 | "statvfs_result", /* name */ |
| 1180 | statvfs_result__doc__, /* doc */ |
| 1181 | statvfs_result_fields, |
| 1182 | 10 |
| 1183 | }; |
| 1184 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1185 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1186 | static PyTypeObject StatResultType; |
| 1187 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1188 | static newfunc structseq_new; |
| 1189 | |
| 1190 | static PyObject * |
| 1191 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1192 | { |
| 1193 | PyStructSequence *result; |
| 1194 | int i; |
| 1195 | |
| 1196 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1197 | if (!result) |
| 1198 | return NULL; |
| 1199 | /* If we have been initialized from a tuple, |
| 1200 | st_?time might be set to None. Initialize it |
| 1201 | from the int slots. */ |
| 1202 | for (i = 7; i <= 9; i++) { |
| 1203 | if (result->ob_item[i+3] == Py_None) { |
| 1204 | Py_DECREF(Py_None); |
| 1205 | Py_INCREF(result->ob_item[i]); |
| 1206 | result->ob_item[i+3] = result->ob_item[i]; |
| 1207 | } |
| 1208 | } |
| 1209 | return (PyObject*)result; |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1215 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1216 | |
| 1217 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1218 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1219 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1220 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1221 | future calls return ints. \n\ |
| 1222 | If newval is omitted, return the current setting.\n"); |
| 1223 | |
| 1224 | static PyObject* |
| 1225 | stat_float_times(PyObject* self, PyObject *args) |
| 1226 | { |
| 1227 | int newval = -1; |
| 1228 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1229 | return NULL; |
| 1230 | if (newval == -1) |
| 1231 | /* Return old value */ |
| 1232 | return PyBool_FromLong(_stat_float_times); |
| 1233 | _stat_float_times = newval; |
| 1234 | Py_INCREF(Py_None); |
| 1235 | return Py_None; |
| 1236 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1237 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1238 | static void |
| 1239 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1240 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1241 | PyObject *fval,*ival; |
| 1242 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1243 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1244 | #else |
| 1245 | ival = PyInt_FromLong((long)sec); |
| 1246 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1247 | if (!ival) |
| 1248 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1249 | if (_stat_float_times) { |
| 1250 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1251 | } else { |
| 1252 | fval = ival; |
| 1253 | Py_INCREF(fval); |
| 1254 | } |
| 1255 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1256 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1259 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1260 | (used by posix_stat() and posix_fstat()) */ |
| 1261 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1262 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1263 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1264 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1265 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1266 | if (v == NULL) |
| 1267 | return NULL; |
| 1268 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1269 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1270 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1271 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1272 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1273 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1274 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1275 | #endif |
| 1276 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1277 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1278 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1279 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1280 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1281 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1282 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1283 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1284 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1285 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1286 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1287 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1288 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1289 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1290 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1291 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1292 | #if defined(HAVE_STAT_TV_NSEC) |
| 1293 | ansec = st->st_atim.tv_nsec; |
| 1294 | mnsec = st->st_mtim.tv_nsec; |
| 1295 | cnsec = st->st_ctim.tv_nsec; |
| 1296 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1297 | ansec = st->st_atimespec.tv_nsec; |
| 1298 | mnsec = st->st_mtimespec.tv_nsec; |
| 1299 | cnsec = st->st_ctimespec.tv_nsec; |
| 1300 | #elif defined(HAVE_STAT_NSEC) |
| 1301 | ansec = st->st_atime_nsec; |
| 1302 | mnsec = st->st_mtime_nsec; |
| 1303 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1304 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1305 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1306 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1307 | fill_time(v, 7, st->st_atime, ansec); |
| 1308 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1309 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1310 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1311 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1312 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1313 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1314 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1315 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1316 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1317 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1318 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1319 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1320 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1321 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1322 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1323 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1324 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1325 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1328 | { |
| 1329 | PyObject *val; |
| 1330 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1331 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1332 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1333 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1334 | #else |
| 1335 | bnsec = 0; |
| 1336 | #endif |
| 1337 | if (_stat_float_times) { |
| 1338 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1339 | } else { |
| 1340 | val = PyInt_FromLong((long)bsec); |
| 1341 | } |
| 1342 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1343 | val); |
| 1344 | } |
| 1345 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1346 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1347 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1348 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1349 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1350 | |
| 1351 | if (PyErr_Occurred()) { |
| 1352 | Py_DECREF(v); |
| 1353 | return NULL; |
| 1354 | } |
| 1355 | |
| 1356 | return v; |
| 1357 | } |
| 1358 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1359 | #ifdef MS_WINDOWS |
| 1360 | |
| 1361 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1362 | where / can be used in place of \ and the trailing slash is optional. |
| 1363 | Both SERVER and SHARE must have at least one character. |
| 1364 | */ |
| 1365 | |
| 1366 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1367 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1368 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1369 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1370 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1371 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1372 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1373 | IsUNCRootA(char *path, int pathlen) |
| 1374 | { |
| 1375 | #define ISSLASH ISSLASHA |
| 1376 | |
| 1377 | int i, share; |
| 1378 | |
| 1379 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1380 | /* minimum UNCRoot is \\x\y */ |
| 1381 | return FALSE; |
| 1382 | for (i = 2; i < pathlen ; i++) |
| 1383 | if (ISSLASH(path[i])) break; |
| 1384 | if (i == 2 || i == pathlen) |
| 1385 | /* do not allow \\\SHARE or \\SERVER */ |
| 1386 | return FALSE; |
| 1387 | share = i+1; |
| 1388 | for (i = share; i < pathlen; i++) |
| 1389 | if (ISSLASH(path[i])) break; |
| 1390 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1391 | |
| 1392 | #undef ISSLASH |
| 1393 | } |
| 1394 | |
| 1395 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1396 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1397 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1398 | { |
| 1399 | #define ISSLASH ISSLASHW |
| 1400 | |
| 1401 | int i, share; |
| 1402 | |
| 1403 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1404 | /* minimum UNCRoot is \\x\y */ |
| 1405 | return FALSE; |
| 1406 | for (i = 2; i < pathlen ; i++) |
| 1407 | if (ISSLASH(path[i])) break; |
| 1408 | if (i == 2 || i == pathlen) |
| 1409 | /* do not allow \\\SHARE or \\SERVER */ |
| 1410 | return FALSE; |
| 1411 | share = i+1; |
| 1412 | for (i = share; i < pathlen; i++) |
| 1413 | if (ISSLASH(path[i])) break; |
| 1414 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1415 | |
| 1416 | #undef ISSLASH |
| 1417 | } |
| 1418 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1419 | #endif /* MS_WINDOWS */ |
| 1420 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1421 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1422 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1423 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1424 | #ifdef __VMS |
| 1425 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1426 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1427 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1428 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1429 | char *wformat, |
| 1430 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1431 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1432 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1433 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1434 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1435 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1436 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1437 | |
| 1438 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1439 | /* If on wide-character-capable OS see if argument |
| 1440 | is Unicode and if so use wide API. */ |
| 1441 | if (unicode_file_names()) { |
| 1442 | PyUnicodeObject *po; |
| 1443 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1444 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1445 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1446 | Py_BEGIN_ALLOW_THREADS |
| 1447 | /* PyUnicode_AS_UNICODE result OK without |
| 1448 | thread lock as it is a simple dereference. */ |
| 1449 | res = wstatfunc(wpath, &st); |
| 1450 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1451 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1452 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1453 | return win32_error_unicode("stat", wpath); |
| 1454 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1455 | } |
| 1456 | /* Drop the argument parsing error as narrow strings |
| 1457 | are also valid. */ |
| 1458 | PyErr_Clear(); |
| 1459 | } |
| 1460 | #endif |
| 1461 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1462 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1463 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1464 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1465 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1466 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1467 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1468 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1469 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1470 | |
| 1471 | if (res != 0) { |
| 1472 | #ifdef MS_WINDOWS |
| 1473 | result = win32_error("stat", pathfree); |
| 1474 | #else |
| 1475 | result = posix_error_with_filename(pathfree); |
| 1476 | #endif |
| 1477 | } |
| 1478 | else |
| 1479 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1480 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1481 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1482 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1485 | /* POSIX methods */ |
| 1486 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1487 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1488 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1489 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1490 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1491 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1492 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1493 | 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] | 1494 | |
| 1495 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1496 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1497 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1498 | char *path; |
| 1499 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1500 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1501 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1502 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1503 | if (unicode_file_names()) { |
| 1504 | PyUnicodeObject *po; |
| 1505 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1506 | Py_BEGIN_ALLOW_THREADS |
| 1507 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1508 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1509 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1510 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1511 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1512 | } |
| 1513 | /* Drop the argument parsing error as narrow strings |
| 1514 | are also valid. */ |
| 1515 | PyErr_Clear(); |
| 1516 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1517 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1518 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1519 | return 0; |
| 1520 | Py_BEGIN_ALLOW_THREADS |
| 1521 | attr = GetFileAttributesA(path); |
| 1522 | Py_END_ALLOW_THREADS |
| 1523 | PyMem_Free(path); |
| 1524 | finish: |
| 1525 | if (attr == 0xFFFFFFFF) |
| 1526 | /* File does not exist, or cannot read attributes */ |
| 1527 | return PyBool_FromLong(0); |
| 1528 | /* Access is possible if either write access wasn't requested, or |
| 1529 | the file isn't read-only. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1530 | return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1531 | #else |
| 1532 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1533 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1534 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1535 | return NULL; |
| 1536 | Py_BEGIN_ALLOW_THREADS |
| 1537 | res = access(path, mode); |
| 1538 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1539 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1540 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1541 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1544 | #ifndef F_OK |
| 1545 | #define F_OK 0 |
| 1546 | #endif |
| 1547 | #ifndef R_OK |
| 1548 | #define R_OK 4 |
| 1549 | #endif |
| 1550 | #ifndef W_OK |
| 1551 | #define W_OK 2 |
| 1552 | #endif |
| 1553 | #ifndef X_OK |
| 1554 | #define X_OK 1 |
| 1555 | #endif |
| 1556 | |
| 1557 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1558 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1559 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1560 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1561 | |
| 1562 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1563 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1564 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1565 | int id; |
| 1566 | char *ret; |
| 1567 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1568 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1569 | return NULL; |
| 1570 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1571 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1572 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1573 | if (id == 0) { |
| 1574 | ret = ttyname(); |
| 1575 | } |
| 1576 | else { |
| 1577 | ret = NULL; |
| 1578 | } |
| 1579 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1580 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1581 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1582 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1583 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1584 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1585 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1586 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1587 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1588 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1589 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1590 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1591 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1592 | |
| 1593 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1594 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1595 | { |
| 1596 | char *ret; |
| 1597 | char buffer[L_ctermid]; |
| 1598 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1599 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1600 | ret = ctermid_r(buffer); |
| 1601 | #else |
| 1602 | ret = ctermid(buffer); |
| 1603 | #endif |
| 1604 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1605 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1606 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1607 | } |
| 1608 | #endif |
| 1609 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1610 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1611 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1612 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1613 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1614 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1615 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1616 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1617 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1618 | 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] | 1619 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1620 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1621 | #elif defined(__VMS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1622 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1623 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1624 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1625 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1628 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1629 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1630 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1631 | 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] | 1632 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1633 | |
| 1634 | static PyObject * |
| 1635 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1636 | { |
| 1637 | return posix_fildes(fdobj, fchdir); |
| 1638 | } |
| 1639 | #endif /* HAVE_FCHDIR */ |
| 1640 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1641 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1642 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1643 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1644 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1645 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1646 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1647 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1648 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1649 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1650 | int i; |
| 1651 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1652 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1653 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1654 | if (unicode_file_names()) { |
| 1655 | PyUnicodeObject *po; |
| 1656 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1657 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1658 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1659 | if (attr != 0xFFFFFFFF) { |
| 1660 | if (i & _S_IWRITE) |
| 1661 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1662 | else |
| 1663 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1664 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1665 | } |
| 1666 | else |
| 1667 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1668 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1669 | if (!res) |
| 1670 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1671 | PyUnicode_AS_UNICODE(po)); |
| 1672 | Py_INCREF(Py_None); |
| 1673 | return Py_None; |
| 1674 | } |
| 1675 | /* Drop the argument parsing error as narrow strings |
| 1676 | are also valid. */ |
| 1677 | PyErr_Clear(); |
| 1678 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1679 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1680 | &path, &i)) |
| 1681 | return NULL; |
| 1682 | Py_BEGIN_ALLOW_THREADS |
| 1683 | attr = GetFileAttributesA(path); |
| 1684 | if (attr != 0xFFFFFFFF) { |
| 1685 | if (i & _S_IWRITE) |
| 1686 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1687 | else |
| 1688 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1689 | res = SetFileAttributesA(path, attr); |
| 1690 | } |
| 1691 | else |
| 1692 | res = 0; |
| 1693 | Py_END_ALLOW_THREADS |
| 1694 | if (!res) { |
| 1695 | win32_error("chmod", path); |
| 1696 | PyMem_Free(path); |
| 1697 | return NULL; |
| 1698 | } |
| 1699 | PyMem_Free(path); |
| 1700 | Py_INCREF(Py_None); |
| 1701 | return Py_None; |
| 1702 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1703 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1704 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1705 | return NULL; |
| 1706 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1707 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1708 | Py_END_ALLOW_THREADS |
| 1709 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1710 | return posix_error_with_allocated_filename(path); |
| 1711 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1712 | Py_INCREF(Py_None); |
| 1713 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1714 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1717 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1718 | #ifdef HAVE_CHFLAGS |
| 1719 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1720 | "chflags(path, flags)\n\n\ |
| 1721 | Set file flags."); |
| 1722 | |
| 1723 | static PyObject * |
| 1724 | posix_chflags(PyObject *self, PyObject *args) |
| 1725 | { |
| 1726 | char *path; |
| 1727 | unsigned long flags; |
| 1728 | int res; |
| 1729 | if (!PyArg_ParseTuple(args, "etk:chflags", |
| 1730 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1731 | return NULL; |
| 1732 | Py_BEGIN_ALLOW_THREADS |
| 1733 | res = chflags(path, flags); |
| 1734 | Py_END_ALLOW_THREADS |
| 1735 | if (res < 0) |
| 1736 | return posix_error_with_allocated_filename(path); |
| 1737 | PyMem_Free(path); |
| 1738 | Py_INCREF(Py_None); |
| 1739 | return Py_None; |
| 1740 | } |
| 1741 | #endif /* HAVE_CHFLAGS */ |
| 1742 | |
| 1743 | #ifdef HAVE_LCHFLAGS |
| 1744 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1745 | "lchflags(path, flags)\n\n\ |
| 1746 | Set file flags.\n\ |
| 1747 | This function will not follow symbolic links."); |
| 1748 | |
| 1749 | static PyObject * |
| 1750 | posix_lchflags(PyObject *self, PyObject *args) |
| 1751 | { |
| 1752 | char *path; |
| 1753 | unsigned long flags; |
| 1754 | int res; |
| 1755 | if (!PyArg_ParseTuple(args, "etk:lchflags", |
| 1756 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1757 | return NULL; |
| 1758 | Py_BEGIN_ALLOW_THREADS |
| 1759 | res = lchflags(path, flags); |
| 1760 | Py_END_ALLOW_THREADS |
| 1761 | if (res < 0) |
| 1762 | return posix_error_with_allocated_filename(path); |
| 1763 | PyMem_Free(path); |
| 1764 | Py_INCREF(Py_None); |
| 1765 | return Py_None; |
| 1766 | } |
| 1767 | #endif /* HAVE_LCHFLAGS */ |
| 1768 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1769 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1770 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1771 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1772 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1773 | |
| 1774 | static PyObject * |
| 1775 | posix_chroot(PyObject *self, PyObject *args) |
| 1776 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1777 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1778 | } |
| 1779 | #endif |
| 1780 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1781 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1782 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1783 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1784 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1785 | |
| 1786 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1787 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1788 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1789 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1790 | } |
| 1791 | #endif /* HAVE_FSYNC */ |
| 1792 | |
| 1793 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1794 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1795 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1796 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1797 | #endif |
| 1798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1799 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1800 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1801 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1802 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1803 | |
| 1804 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1805 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1806 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1807 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1808 | } |
| 1809 | #endif /* HAVE_FDATASYNC */ |
| 1810 | |
| 1811 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1812 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1813 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1814 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1815 | 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] | 1816 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1817 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1818 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1819 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1820 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1821 | int uid, gid; |
| 1822 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1823 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1824 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1825 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1826 | return NULL; |
| 1827 | Py_BEGIN_ALLOW_THREADS |
| 1828 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1829 | Py_END_ALLOW_THREADS |
| 1830 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1831 | return posix_error_with_allocated_filename(path); |
| 1832 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1833 | Py_INCREF(Py_None); |
| 1834 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1835 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1836 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1837 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1838 | #ifdef HAVE_LCHOWN |
| 1839 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1840 | "lchown(path, uid, gid)\n\n\ |
| 1841 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1842 | This function will not follow symbolic links."); |
| 1843 | |
| 1844 | static PyObject * |
| 1845 | posix_lchown(PyObject *self, PyObject *args) |
| 1846 | { |
| 1847 | char *path = NULL; |
| 1848 | int uid, gid; |
| 1849 | int res; |
| 1850 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1851 | Py_FileSystemDefaultEncoding, &path, |
| 1852 | &uid, &gid)) |
| 1853 | return NULL; |
| 1854 | Py_BEGIN_ALLOW_THREADS |
| 1855 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1856 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1857 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1858 | return posix_error_with_allocated_filename(path); |
| 1859 | PyMem_Free(path); |
| 1860 | Py_INCREF(Py_None); |
| 1861 | return Py_None; |
| 1862 | } |
| 1863 | #endif /* HAVE_LCHOWN */ |
| 1864 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1865 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1866 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1867 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1868 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1869 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1870 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1871 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1872 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1873 | { |
| 1874 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1875 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1876 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1877 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1878 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1879 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1880 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1881 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1882 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1883 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1884 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1885 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1886 | return PyUnicode_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1887 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1888 | |
| 1889 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1890 | "getcwdu() -> path\n\n\ |
| 1891 | Return a unicode string representing the current working directory."); |
| 1892 | |
| 1893 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1894 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1895 | { |
| 1896 | char buf[1026]; |
| 1897 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1898 | |
| 1899 | #ifdef Py_WIN_WIDE_FILENAMES |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1900 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1901 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1902 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1903 | wchar_t *wbuf2 = wbuf; |
| 1904 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1905 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1906 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 1907 | /* If the buffer is large enough, len does not include the |
| 1908 | terminating \0. If the buffer is too small, len includes |
| 1909 | the space needed for the terminator. */ |
| 1910 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 1911 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 1912 | if (wbuf2) |
| 1913 | len = GetCurrentDirectoryW(len, wbuf2); |
| 1914 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1915 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1916 | if (!wbuf2) { |
| 1917 | PyErr_NoMemory(); |
| 1918 | return NULL; |
| 1919 | } |
| 1920 | if (!len) { |
| 1921 | if (wbuf2 != wbuf) free(wbuf2); |
| 1922 | return win32_error("getcwdu", NULL); |
| 1923 | } |
| 1924 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 1925 | if (wbuf2 != wbuf) free(wbuf2); |
| 1926 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1927 | } |
| 1928 | #endif |
| 1929 | |
| 1930 | Py_BEGIN_ALLOW_THREADS |
| 1931 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1932 | res = _getcwd2(buf, sizeof buf); |
| 1933 | #else |
| 1934 | res = getcwd(buf, sizeof buf); |
| 1935 | #endif |
| 1936 | Py_END_ALLOW_THREADS |
| 1937 | if (res == NULL) |
| 1938 | return posix_error(); |
| 1939 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1940 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1941 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1942 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1943 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1944 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1945 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1946 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1947 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1948 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1949 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1950 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1951 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1952 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1953 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1954 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1955 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1956 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1957 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1958 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1959 | Return a list containing the names of the entries in the directory.\n\ |
| 1960 | \n\ |
| 1961 | path: path of directory to list\n\ |
| 1962 | \n\ |
| 1963 | 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] | 1964 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1965 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1966 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1967 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1968 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1969 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1970 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1971 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1972 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1973 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1974 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1975 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1976 | WIN32_FIND_DATA FileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1977 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1978 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1979 | 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] | 1980 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1981 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1982 | /* If on wide-character-capable OS see if argument |
| 1983 | is Unicode and if so use wide API. */ |
| 1984 | if (unicode_file_names()) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1985 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1986 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1987 | WIN32_FIND_DATAW wFileData; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1988 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1989 | Py_UNICODE wch; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1990 | /* Overallocate for \\*.*\0 */ |
| 1991 | len = PyUnicode_GET_SIZE(po); |
| 1992 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 1993 | if (!wnamebuf) { |
| 1994 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1995 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1996 | } |
| 1997 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 1998 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 1999 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2000 | wnamebuf[len++] = L'\\'; |
| 2001 | wcscpy(wnamebuf + len, L"*.*"); |
| 2002 | if ((d = PyList_New(0)) == NULL) { |
| 2003 | free(wnamebuf); |
| 2004 | return NULL; |
| 2005 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2006 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2007 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2008 | int error = GetLastError(); |
| 2009 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2010 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2011 | return d; |
| 2012 | } |
| 2013 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2014 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2015 | free(wnamebuf); |
| 2016 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2017 | } |
| 2018 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2019 | /* Skip over . and .. */ |
| 2020 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2021 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2022 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2023 | if (v == NULL) { |
| 2024 | Py_DECREF(d); |
| 2025 | d = NULL; |
| 2026 | break; |
| 2027 | } |
| 2028 | if (PyList_Append(d, v) != 0) { |
| 2029 | Py_DECREF(v); |
| 2030 | Py_DECREF(d); |
| 2031 | d = NULL; |
| 2032 | break; |
| 2033 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2034 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2035 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2036 | Py_BEGIN_ALLOW_THREADS |
| 2037 | result = FindNextFileW(hFindFile, &wFileData); |
| 2038 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2039 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2040 | it got to the end of the directory. */ |
| 2041 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2042 | Py_DECREF(d); |
| 2043 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2044 | FindClose(hFindFile); |
| 2045 | free(wnamebuf); |
| 2046 | return NULL; |
| 2047 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2048 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2049 | |
| 2050 | if (FindClose(hFindFile) == FALSE) { |
| 2051 | Py_DECREF(d); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2052 | win32_error_unicode("FindClose", wnamebuf); |
| 2053 | free(wnamebuf); |
| 2054 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2055 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2056 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2057 | return d; |
| 2058 | } |
| 2059 | /* Drop the argument parsing error as narrow strings |
| 2060 | are also valid. */ |
| 2061 | PyErr_Clear(); |
| 2062 | } |
| 2063 | #endif |
| 2064 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2065 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2066 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2067 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2068 | if (len > 0) { |
| 2069 | char ch = namebuf[len-1]; |
| 2070 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2071 | namebuf[len++] = '/'; |
| 2072 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2073 | strcpy(namebuf + len, "*.*"); |
| 2074 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2075 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2076 | return NULL; |
| 2077 | |
| 2078 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2079 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2080 | int error = GetLastError(); |
| 2081 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2082 | return d; |
| 2083 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2084 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2085 | } |
| 2086 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2087 | /* Skip over . and .. */ |
| 2088 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2089 | strcmp(FileData.cFileName, "..") != 0) { |
| 2090 | v = PyString_FromString(FileData.cFileName); |
| 2091 | if (v == NULL) { |
| 2092 | Py_DECREF(d); |
| 2093 | d = NULL; |
| 2094 | break; |
| 2095 | } |
| 2096 | if (PyList_Append(d, v) != 0) { |
| 2097 | Py_DECREF(v); |
| 2098 | Py_DECREF(d); |
| 2099 | d = NULL; |
| 2100 | break; |
| 2101 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2102 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2103 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2104 | Py_BEGIN_ALLOW_THREADS |
| 2105 | result = FindNextFile(hFindFile, &FileData); |
| 2106 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2107 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2108 | it got to the end of the directory. */ |
| 2109 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2110 | Py_DECREF(d); |
| 2111 | win32_error("FindNextFile", namebuf); |
| 2112 | FindClose(hFindFile); |
| 2113 | return NULL; |
| 2114 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2115 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2116 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2117 | if (FindClose(hFindFile) == FALSE) { |
| 2118 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2119 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2120 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2121 | |
| 2122 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2123 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2124 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2125 | |
| 2126 | #ifndef MAX_PATH |
| 2127 | #define MAX_PATH CCHMAXPATH |
| 2128 | #endif |
| 2129 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2130 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2131 | PyObject *d, *v; |
| 2132 | char namebuf[MAX_PATH+5]; |
| 2133 | HDIR hdir = 1; |
| 2134 | ULONG srchcnt = 1; |
| 2135 | FILEFINDBUF3 ep; |
| 2136 | APIRET rc; |
| 2137 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2138 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2139 | return NULL; |
| 2140 | if (len >= MAX_PATH) { |
| 2141 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2142 | return NULL; |
| 2143 | } |
| 2144 | strcpy(namebuf, name); |
| 2145 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2146 | if (*pt == ALTSEP) |
| 2147 | *pt = SEP; |
| 2148 | if (namebuf[len-1] != SEP) |
| 2149 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2150 | strcpy(namebuf + len, "*.*"); |
| 2151 | |
| 2152 | if ((d = PyList_New(0)) == NULL) |
| 2153 | return NULL; |
| 2154 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2155 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2156 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2157 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2158 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2159 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2160 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2161 | |
| 2162 | if (rc != NO_ERROR) { |
| 2163 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 2164 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2167 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2168 | do { |
| 2169 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2170 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2171 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2172 | |
| 2173 | strcpy(namebuf, ep.achName); |
| 2174 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2175 | /* Leave Case of Name Alone -- In Native Form */ |
| 2176 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2177 | |
| 2178 | v = PyString_FromString(namebuf); |
| 2179 | if (v == NULL) { |
| 2180 | Py_DECREF(d); |
| 2181 | d = NULL; |
| 2182 | break; |
| 2183 | } |
| 2184 | if (PyList_Append(d, v) != 0) { |
| 2185 | Py_DECREF(v); |
| 2186 | Py_DECREF(d); |
| 2187 | d = NULL; |
| 2188 | break; |
| 2189 | } |
| 2190 | Py_DECREF(v); |
| 2191 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2192 | } |
| 2193 | |
| 2194 | return d; |
| 2195 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2196 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2197 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2198 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2199 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2200 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2201 | int arg_is_unicode = 1; |
| 2202 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2203 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2204 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2205 | arg_is_unicode = 0; |
| 2206 | PyErr_Clear(); |
| 2207 | } |
| 2208 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2209 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2210 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2211 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2212 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2213 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2214 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2215 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2216 | return NULL; |
| 2217 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2218 | for (;;) { |
| 2219 | Py_BEGIN_ALLOW_THREADS |
| 2220 | ep = readdir(dirp); |
| 2221 | Py_END_ALLOW_THREADS |
| 2222 | if (ep == NULL) |
| 2223 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2224 | if (ep->d_name[0] == '.' && |
| 2225 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2226 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2227 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2228 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2229 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2230 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2231 | d = NULL; |
| 2232 | break; |
| 2233 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2234 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2235 | PyObject *w; |
| 2236 | |
| 2237 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2238 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2239 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2240 | if (w != NULL) { |
| 2241 | Py_DECREF(v); |
| 2242 | v = w; |
| 2243 | } |
| 2244 | else { |
| 2245 | /* fall back to the original byte string, as |
| 2246 | discussed in patch #683592 */ |
| 2247 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2248 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2249 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2250 | if (PyList_Append(d, v) != 0) { |
| 2251 | Py_DECREF(v); |
| 2252 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2253 | d = NULL; |
| 2254 | break; |
| 2255 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2256 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2257 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2258 | if (errno != 0 && d != NULL) { |
| 2259 | /* readdir() returned NULL and set errno */ |
| 2260 | closedir(dirp); |
| 2261 | Py_DECREF(d); |
| 2262 | return posix_error_with_allocated_filename(name); |
| 2263 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2264 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2265 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2266 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2267 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2268 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2269 | #endif /* which OS */ |
| 2270 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2271 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2272 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2273 | /* A helper function for abspath on win32 */ |
| 2274 | static PyObject * |
| 2275 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2276 | { |
| 2277 | /* assume encoded strings wont more than double no of chars */ |
| 2278 | char inbuf[MAX_PATH*2]; |
| 2279 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2280 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2281 | char outbuf[MAX_PATH*2]; |
| 2282 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2283 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2284 | if (unicode_file_names()) { |
| 2285 | PyUnicodeObject *po; |
| 2286 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2287 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2288 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2289 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2290 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2291 | woutbuf, &wtemp)) |
| 2292 | return win32_error("GetFullPathName", ""); |
| 2293 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2294 | } |
| 2295 | /* Drop the argument parsing error as narrow strings |
| 2296 | are also valid. */ |
| 2297 | PyErr_Clear(); |
| 2298 | } |
| 2299 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2300 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2301 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2302 | &insize)) |
| 2303 | return NULL; |
| 2304 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2305 | outbuf, &temp)) |
| 2306 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2307 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2308 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2309 | Py_FileSystemDefaultEncoding, NULL); |
| 2310 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2311 | return PyString_FromString(outbuf); |
| 2312 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2313 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2314 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2315 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2316 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2317 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2318 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2319 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2320 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2321 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2322 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2323 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2324 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2325 | |
| 2326 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2327 | if (unicode_file_names()) { |
| 2328 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2329 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2330 | Py_BEGIN_ALLOW_THREADS |
| 2331 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2332 | it is a simple dereference. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2333 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2334 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2335 | if (!res) |
| 2336 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2337 | Py_INCREF(Py_None); |
| 2338 | return Py_None; |
| 2339 | } |
| 2340 | /* Drop the argument parsing error as narrow strings |
| 2341 | are also valid. */ |
| 2342 | PyErr_Clear(); |
| 2343 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2344 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2345 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2346 | return NULL; |
| 2347 | Py_BEGIN_ALLOW_THREADS |
| 2348 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2349 | it is a simple dereference. */ |
| 2350 | res = CreateDirectoryA(path, NULL); |
| 2351 | Py_END_ALLOW_THREADS |
| 2352 | if (!res) { |
| 2353 | win32_error("mkdir", path); |
| 2354 | PyMem_Free(path); |
| 2355 | return NULL; |
| 2356 | } |
| 2357 | PyMem_Free(path); |
| 2358 | Py_INCREF(Py_None); |
| 2359 | return Py_None; |
| 2360 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2361 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2362 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2363 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2364 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2365 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2366 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2367 | res = mkdir(path); |
| 2368 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2369 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2370 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2371 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2372 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2373 | return posix_error_with_allocated_filename(path); |
| 2374 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2375 | Py_INCREF(Py_None); |
| 2376 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2377 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2380 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2381 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2382 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2383 | #include <sys/resource.h> |
| 2384 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2385 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2386 | |
| 2387 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2388 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2389 | "nice(inc) -> new_priority\n\n\ |
| 2390 | 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] | 2391 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2392 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2393 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2394 | { |
| 2395 | int increment, value; |
| 2396 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2397 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2398 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2399 | |
| 2400 | /* There are two flavours of 'nice': one that returns the new |
| 2401 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2402 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2403 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2404 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2405 | If we are of the nice family that returns the new priority, we |
| 2406 | need to clear errno before the call, and check if errno is filled |
| 2407 | before calling posix_error() on a returnvalue of -1, because the |
| 2408 | -1 may be the actual new priority! */ |
| 2409 | |
| 2410 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2411 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2412 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2413 | if (value == 0) |
| 2414 | value = getpriority(PRIO_PROCESS, 0); |
| 2415 | #endif |
| 2416 | if (value == -1 && errno != 0) |
| 2417 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2418 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2419 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2420 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2421 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2422 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2423 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2424 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2425 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2426 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2427 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2428 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2429 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2430 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2431 | PyObject *o1, *o2; |
| 2432 | char *p1, *p2; |
| 2433 | BOOL result; |
| 2434 | if (unicode_file_names()) { |
| 2435 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2436 | convert_to_unicode, &o1, |
| 2437 | convert_to_unicode, &o2)) |
| 2438 | PyErr_Clear(); |
| 2439 | else { |
| 2440 | Py_BEGIN_ALLOW_THREADS |
| 2441 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2442 | PyUnicode_AsUnicode(o2)); |
| 2443 | Py_END_ALLOW_THREADS |
| 2444 | Py_DECREF(o1); |
| 2445 | Py_DECREF(o2); |
| 2446 | if (!result) |
| 2447 | return win32_error("rename", NULL); |
| 2448 | Py_INCREF(Py_None); |
| 2449 | return Py_None; |
| 2450 | } |
| 2451 | } |
| 2452 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2453 | return NULL; |
| 2454 | Py_BEGIN_ALLOW_THREADS |
| 2455 | result = MoveFileA(p1, p2); |
| 2456 | Py_END_ALLOW_THREADS |
| 2457 | if (!result) |
| 2458 | return win32_error("rename", NULL); |
| 2459 | Py_INCREF(Py_None); |
| 2460 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2461 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2462 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2463 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2467 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2468 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2469 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2470 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2471 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2472 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2473 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2474 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2475 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2476 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2477 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2478 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2482 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2483 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2484 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2485 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2486 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2487 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2488 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2489 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2490 | 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] | 2491 | #else |
| 2492 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2493 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2496 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2497 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2498 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2499 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2500 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2501 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2502 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2503 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2504 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2505 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2506 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2507 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2508 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2509 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2510 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2511 | Py_END_ALLOW_THREADS |
| 2512 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2513 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2514 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2515 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2516 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2517 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2518 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2519 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2520 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2521 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2522 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2523 | { |
| 2524 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2525 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2526 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2527 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2528 | if (i < 0) |
| 2529 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2530 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2534 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2535 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2536 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2537 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2538 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2539 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2540 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2542 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2543 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2544 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2545 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2546 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2547 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2548 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2549 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2552 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2553 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2554 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2555 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2556 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2558 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2559 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2560 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2561 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2562 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2563 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2564 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2565 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2566 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2567 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2568 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2569 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2570 | u.sysname, |
| 2571 | u.nodename, |
| 2572 | u.release, |
| 2573 | u.version, |
| 2574 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2575 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2576 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2577 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2578 | static int |
| 2579 | extract_time(PyObject *t, long* sec, long* usec) |
| 2580 | { |
| 2581 | long intval; |
| 2582 | if (PyFloat_Check(t)) { |
| 2583 | double tval = PyFloat_AsDouble(t); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2584 | 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] | 2585 | if (!intobj) |
| 2586 | return -1; |
| 2587 | intval = PyInt_AsLong(intobj); |
| 2588 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2589 | if (intval == -1 && PyErr_Occurred()) |
| 2590 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2591 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2592 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2593 | if (*usec < 0) |
| 2594 | /* If rounding gave us a negative number, |
| 2595 | truncate. */ |
| 2596 | *usec = 0; |
| 2597 | return 0; |
| 2598 | } |
| 2599 | intval = PyInt_AsLong(t); |
| 2600 | if (intval == -1 && PyErr_Occurred()) |
| 2601 | return -1; |
| 2602 | *sec = intval; |
| 2603 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2604 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2605 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2607 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2608 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2609 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2610 | 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] | 2611 | 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] | 2612 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2613 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2614 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2615 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2616 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2617 | PyObject *arg; |
| 2618 | PyUnicodeObject *obwpath; |
| 2619 | wchar_t *wpath = NULL; |
| 2620 | char *apath = NULL; |
| 2621 | HANDLE hFile; |
| 2622 | long atimesec, mtimesec, ausec, musec; |
| 2623 | FILETIME atime, mtime; |
| 2624 | PyObject *result = NULL; |
| 2625 | |
| 2626 | if (unicode_file_names()) { |
| 2627 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2628 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2629 | Py_BEGIN_ALLOW_THREADS |
| 2630 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2631 | NULL, OPEN_EXISTING, |
| 2632 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2633 | Py_END_ALLOW_THREADS |
| 2634 | if (hFile == INVALID_HANDLE_VALUE) |
| 2635 | return win32_error_unicode("utime", wpath); |
| 2636 | } else |
| 2637 | /* Drop the argument parsing error as narrow strings |
| 2638 | are also valid. */ |
| 2639 | PyErr_Clear(); |
| 2640 | } |
| 2641 | if (!wpath) { |
| 2642 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2643 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2644 | return NULL; |
| 2645 | Py_BEGIN_ALLOW_THREADS |
| 2646 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2647 | NULL, OPEN_EXISTING, |
| 2648 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2649 | Py_END_ALLOW_THREADS |
| 2650 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2651 | win32_error("utime", apath); |
| 2652 | PyMem_Free(apath); |
| 2653 | return NULL; |
| 2654 | } |
| 2655 | PyMem_Free(apath); |
| 2656 | } |
| 2657 | |
| 2658 | if (arg == Py_None) { |
| 2659 | SYSTEMTIME now; |
| 2660 | GetSystemTime(&now); |
| 2661 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2662 | !SystemTimeToFileTime(&now, &atime)) { |
| 2663 | win32_error("utime", NULL); |
| 2664 | goto done; |
| 2665 | } |
| 2666 | } |
| 2667 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2668 | PyErr_SetString(PyExc_TypeError, |
| 2669 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2670 | goto done; |
| 2671 | } |
| 2672 | else { |
| 2673 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2674 | &atimesec, &ausec) == -1) |
| 2675 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2676 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2677 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2678 | &mtimesec, &musec) == -1) |
| 2679 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2680 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2681 | } |
| 2682 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2683 | /* Avoid putting the file name into the error here, |
| 2684 | as that may confuse the user into believing that |
| 2685 | something is wrong with the file, when it also |
| 2686 | could be the time stamp that gives a problem. */ |
| 2687 | win32_error("utime", NULL); |
| 2688 | } |
| 2689 | Py_INCREF(Py_None); |
| 2690 | result = Py_None; |
| 2691 | done: |
| 2692 | CloseHandle(hFile); |
| 2693 | return result; |
| 2694 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2695 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2696 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2697 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2698 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2699 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2700 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2701 | #if defined(HAVE_UTIMES) |
| 2702 | struct timeval buf[2]; |
| 2703 | #define ATIME buf[0].tv_sec |
| 2704 | #define MTIME buf[1].tv_sec |
| 2705 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2706 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2707 | struct utimbuf buf; |
| 2708 | #define ATIME buf.actime |
| 2709 | #define MTIME buf.modtime |
| 2710 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2711 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2712 | time_t buf[2]; |
| 2713 | #define ATIME buf[0] |
| 2714 | #define MTIME buf[1] |
| 2715 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2716 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2717 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2718 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2719 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2720 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2721 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2722 | if (arg == Py_None) { |
| 2723 | /* optional time values not given */ |
| 2724 | Py_BEGIN_ALLOW_THREADS |
| 2725 | res = utime(path, NULL); |
| 2726 | Py_END_ALLOW_THREADS |
| 2727 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2728 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2729 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2730 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2731 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2732 | return NULL; |
| 2733 | } |
| 2734 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2735 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2736 | &atime, &ausec) == -1) { |
| 2737 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2738 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2739 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2740 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2741 | &mtime, &musec) == -1) { |
| 2742 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2743 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2744 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2745 | ATIME = atime; |
| 2746 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2747 | #ifdef HAVE_UTIMES |
| 2748 | buf[0].tv_usec = ausec; |
| 2749 | buf[1].tv_usec = musec; |
| 2750 | Py_BEGIN_ALLOW_THREADS |
| 2751 | res = utimes(path, buf); |
| 2752 | Py_END_ALLOW_THREADS |
| 2753 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2754 | Py_BEGIN_ALLOW_THREADS |
| 2755 | res = utime(path, UTIME_ARG); |
| 2756 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2757 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2758 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2759 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2760 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2761 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2762 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2763 | Py_INCREF(Py_None); |
| 2764 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2765 | #undef UTIME_ARG |
| 2766 | #undef ATIME |
| 2767 | #undef MTIME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2768 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2771 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2772 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2773 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2774 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2775 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2776 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2777 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2778 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2779 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2780 | { |
| 2781 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2782 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2783 | return NULL; |
| 2784 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2785 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2788 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2789 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2790 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2791 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2792 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2793 | for (i = 0; i < count; i++) |
| 2794 | PyMem_Free(array[i]); |
| 2795 | PyMem_DEL(array); |
| 2796 | } |
| 2797 | #endif |
| 2798 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2799 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2800 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2801 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2802 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2803 | Execute an executable path with arguments, replacing current process.\n\ |
| 2804 | \n\ |
| 2805 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2806 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2807 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2808 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2809 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2810 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2811 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2812 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2813 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2814 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2815 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2816 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2817 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2818 | argv is a list or tuple of strings. */ |
| 2819 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2820 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2821 | Py_FileSystemDefaultEncoding, |
| 2822 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2823 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2824 | if (PyList_Check(argv)) { |
| 2825 | argc = PyList_Size(argv); |
| 2826 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2827 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2828 | else if (PyTuple_Check(argv)) { |
| 2829 | argc = PyTuple_Size(argv); |
| 2830 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2831 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2832 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2833 | 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] | 2834 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2835 | return NULL; |
| 2836 | } |
| 2837 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2838 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2839 | if (argvlist == NULL) { |
| 2840 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2841 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2842 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2843 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2844 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2845 | Py_FileSystemDefaultEncoding, |
| 2846 | &argvlist[i])) { |
| 2847 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2848 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2849 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2850 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2851 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2852 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2853 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2854 | } |
| 2855 | argvlist[argc] = NULL; |
| 2856 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2857 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2858 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2859 | /* If we get here it's definitely an error */ |
| 2860 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2861 | free_string_array(argvlist, argc); |
| 2862 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2863 | return posix_error(); |
| 2864 | } |
| 2865 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2866 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2867 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2868 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2869 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2870 | \n\ |
| 2871 | path: path of executable file\n\ |
| 2872 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2873 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2874 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2875 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2876 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2877 | { |
| 2878 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2879 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2880 | char **argvlist; |
| 2881 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2882 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2883 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2884 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2885 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2886 | |
| 2887 | /* execve has three arguments: (path, argv, env), where |
| 2888 | argv is a list or tuple of strings and env is a dictionary |
| 2889 | like posix.environ. */ |
| 2890 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2891 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2892 | Py_FileSystemDefaultEncoding, |
| 2893 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2894 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2895 | if (PyList_Check(argv)) { |
| 2896 | argc = PyList_Size(argv); |
| 2897 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2898 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2899 | else if (PyTuple_Check(argv)) { |
| 2900 | argc = PyTuple_Size(argv); |
| 2901 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2902 | } |
| 2903 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2904 | PyErr_SetString(PyExc_TypeError, |
| 2905 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2906 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2907 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2908 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2909 | PyErr_SetString(PyExc_TypeError, |
| 2910 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2911 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2914 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2915 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2916 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2917 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2918 | } |
| 2919 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2920 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2921 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2922 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2923 | &argvlist[i])) |
| 2924 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2925 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2926 | goto fail_1; |
| 2927 | } |
| 2928 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2929 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2930 | argvlist[argc] = NULL; |
| 2931 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2932 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2933 | if (i < 0) |
| 2934 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2935 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2936 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2937 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2938 | goto fail_1; |
| 2939 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2940 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2941 | keys = PyMapping_Keys(env); |
| 2942 | vals = PyMapping_Values(env); |
| 2943 | if (!keys || !vals) |
| 2944 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2945 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2946 | PyErr_SetString(PyExc_TypeError, |
| 2947 | "execve(): env.keys() or env.values() is not a list"); |
| 2948 | goto fail_2; |
| 2949 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2950 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2951 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2952 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2953 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2954 | |
| 2955 | key = PyList_GetItem(keys, pos); |
| 2956 | val = PyList_GetItem(vals, pos); |
| 2957 | if (!key || !val) |
| 2958 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2959 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2960 | if (!PyArg_Parse( |
| 2961 | key, |
| 2962 | "s;execve() arg 3 contains a non-string key", |
| 2963 | &k) || |
| 2964 | !PyArg_Parse( |
| 2965 | val, |
| 2966 | "s;execve() arg 3 contains a non-string value", |
| 2967 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2968 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2969 | goto fail_2; |
| 2970 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2971 | |
| 2972 | #if defined(PYOS_OS2) |
| 2973 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2974 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2975 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2976 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2977 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2978 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2979 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2980 | goto fail_2; |
| 2981 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2982 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2983 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2984 | #if defined(PYOS_OS2) |
| 2985 | } |
| 2986 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2987 | } |
| 2988 | envlist[envc] = 0; |
| 2989 | |
| 2990 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2991 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2992 | /* If we get here it's definitely an error */ |
| 2993 | |
| 2994 | (void) posix_error(); |
| 2995 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2996 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2997 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2998 | PyMem_DEL(envlist[envc]); |
| 2999 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3000 | fail_1: |
| 3001 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3002 | Py_XDECREF(vals); |
| 3003 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3004 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3005 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3006 | return NULL; |
| 3007 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3008 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3009 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3010 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3011 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3012 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3013 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3014 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3015 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3016 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3017 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3018 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3019 | |
| 3020 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3021 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3022 | { |
| 3023 | char *path; |
| 3024 | PyObject *argv; |
| 3025 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3026 | int mode, i; |
| 3027 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3028 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3029 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3030 | |
| 3031 | /* spawnv has three arguments: (mode, path, argv), where |
| 3032 | argv is a list or tuple of strings. */ |
| 3033 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3034 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3035 | Py_FileSystemDefaultEncoding, |
| 3036 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3037 | return NULL; |
| 3038 | if (PyList_Check(argv)) { |
| 3039 | argc = PyList_Size(argv); |
| 3040 | getitem = PyList_GetItem; |
| 3041 | } |
| 3042 | else if (PyTuple_Check(argv)) { |
| 3043 | argc = PyTuple_Size(argv); |
| 3044 | getitem = PyTuple_GetItem; |
| 3045 | } |
| 3046 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3047 | PyErr_SetString(PyExc_TypeError, |
| 3048 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3049 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3050 | return NULL; |
| 3051 | } |
| 3052 | |
| 3053 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3054 | if (argvlist == NULL) { |
| 3055 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3056 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3057 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3058 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3059 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3060 | Py_FileSystemDefaultEncoding, |
| 3061 | &argvlist[i])) { |
| 3062 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3063 | PyErr_SetString( |
| 3064 | PyExc_TypeError, |
| 3065 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3066 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3067 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | argvlist[argc] = NULL; |
| 3071 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3072 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3073 | Py_BEGIN_ALLOW_THREADS |
| 3074 | spawnval = spawnv(mode, path, argvlist); |
| 3075 | Py_END_ALLOW_THREADS |
| 3076 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3077 | if (mode == _OLD_P_OVERLAY) |
| 3078 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3079 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3080 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3081 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3082 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3083 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3084 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3085 | free_string_array(argvlist, argc); |
| 3086 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3087 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3088 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3089 | return posix_error(); |
| 3090 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3091 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3092 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3093 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3094 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3095 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3099 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3100 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3101 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3102 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3103 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3104 | path: path of executable file\n\ |
| 3105 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3106 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3107 | |
| 3108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3109 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3110 | { |
| 3111 | char *path; |
| 3112 | PyObject *argv, *env; |
| 3113 | char **argvlist; |
| 3114 | char **envlist; |
| 3115 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3116 | int mode, pos, envc; |
| 3117 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3118 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3119 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3120 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3121 | |
| 3122 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3123 | argv is a list or tuple of strings and env is a dictionary |
| 3124 | like posix.environ. */ |
| 3125 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3126 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3127 | Py_FileSystemDefaultEncoding, |
| 3128 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3129 | return NULL; |
| 3130 | if (PyList_Check(argv)) { |
| 3131 | argc = PyList_Size(argv); |
| 3132 | getitem = PyList_GetItem; |
| 3133 | } |
| 3134 | else if (PyTuple_Check(argv)) { |
| 3135 | argc = PyTuple_Size(argv); |
| 3136 | getitem = PyTuple_GetItem; |
| 3137 | } |
| 3138 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3139 | PyErr_SetString(PyExc_TypeError, |
| 3140 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3141 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3142 | } |
| 3143 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3144 | PyErr_SetString(PyExc_TypeError, |
| 3145 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3146 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | argvlist = PyMem_NEW(char *, argc+1); |
| 3150 | if (argvlist == NULL) { |
| 3151 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3152 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3153 | } |
| 3154 | for (i = 0; i < argc; i++) { |
| 3155 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3156 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3157 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3158 | &argvlist[i])) |
| 3159 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3160 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3161 | goto fail_1; |
| 3162 | } |
| 3163 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3164 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3165 | argvlist[argc] = NULL; |
| 3166 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3167 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3168 | if (i < 0) |
| 3169 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3170 | envlist = PyMem_NEW(char *, i + 1); |
| 3171 | if (envlist == NULL) { |
| 3172 | PyErr_NoMemory(); |
| 3173 | goto fail_1; |
| 3174 | } |
| 3175 | envc = 0; |
| 3176 | keys = PyMapping_Keys(env); |
| 3177 | vals = PyMapping_Values(env); |
| 3178 | if (!keys || !vals) |
| 3179 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3180 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3181 | PyErr_SetString(PyExc_TypeError, |
| 3182 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3183 | goto fail_2; |
| 3184 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3185 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3186 | for (pos = 0; pos < i; pos++) { |
| 3187 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3188 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3189 | |
| 3190 | key = PyList_GetItem(keys, pos); |
| 3191 | val = PyList_GetItem(vals, pos); |
| 3192 | if (!key || !val) |
| 3193 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3194 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3195 | if (!PyArg_Parse( |
| 3196 | key, |
| 3197 | "s;spawnve() arg 3 contains a non-string key", |
| 3198 | &k) || |
| 3199 | !PyArg_Parse( |
| 3200 | val, |
| 3201 | "s;spawnve() arg 3 contains a non-string value", |
| 3202 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3203 | { |
| 3204 | goto fail_2; |
| 3205 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3206 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3207 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3208 | if (p == NULL) { |
| 3209 | PyErr_NoMemory(); |
| 3210 | goto fail_2; |
| 3211 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3212 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3213 | envlist[envc++] = p; |
| 3214 | } |
| 3215 | envlist[envc] = 0; |
| 3216 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3217 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3218 | Py_BEGIN_ALLOW_THREADS |
| 3219 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3220 | Py_END_ALLOW_THREADS |
| 3221 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3222 | if (mode == _OLD_P_OVERLAY) |
| 3223 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3224 | |
| 3225 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3226 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3227 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3228 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3229 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3230 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3231 | (void) posix_error(); |
| 3232 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3233 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3234 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3235 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3236 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3237 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3238 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3239 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3240 | while (--envc >= 0) |
| 3241 | PyMem_DEL(envlist[envc]); |
| 3242 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3243 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3244 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3245 | Py_XDECREF(vals); |
| 3246 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3247 | fail_0: |
| 3248 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3249 | return res; |
| 3250 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3251 | |
| 3252 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3253 | #if defined(PYOS_OS2) |
| 3254 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3255 | "spawnvp(mode, file, args)\n\n\ |
| 3256 | Execute the program 'file' in a new process, using the environment\n\ |
| 3257 | search path to find the file.\n\ |
| 3258 | \n\ |
| 3259 | mode: mode of process creation\n\ |
| 3260 | file: executable file name\n\ |
| 3261 | args: tuple or list of strings"); |
| 3262 | |
| 3263 | static PyObject * |
| 3264 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3265 | { |
| 3266 | char *path; |
| 3267 | PyObject *argv; |
| 3268 | char **argvlist; |
| 3269 | int mode, i, argc; |
| 3270 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3271 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3272 | |
| 3273 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3274 | argv is a list or tuple of strings. */ |
| 3275 | |
| 3276 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3277 | Py_FileSystemDefaultEncoding, |
| 3278 | &path, &argv)) |
| 3279 | return NULL; |
| 3280 | if (PyList_Check(argv)) { |
| 3281 | argc = PyList_Size(argv); |
| 3282 | getitem = PyList_GetItem; |
| 3283 | } |
| 3284 | else if (PyTuple_Check(argv)) { |
| 3285 | argc = PyTuple_Size(argv); |
| 3286 | getitem = PyTuple_GetItem; |
| 3287 | } |
| 3288 | else { |
| 3289 | PyErr_SetString(PyExc_TypeError, |
| 3290 | "spawnvp() arg 2 must be a tuple or list"); |
| 3291 | PyMem_Free(path); |
| 3292 | return NULL; |
| 3293 | } |
| 3294 | |
| 3295 | argvlist = PyMem_NEW(char *, argc+1); |
| 3296 | if (argvlist == NULL) { |
| 3297 | PyMem_Free(path); |
| 3298 | return PyErr_NoMemory(); |
| 3299 | } |
| 3300 | for (i = 0; i < argc; i++) { |
| 3301 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3302 | Py_FileSystemDefaultEncoding, |
| 3303 | &argvlist[i])) { |
| 3304 | free_string_array(argvlist, i); |
| 3305 | PyErr_SetString( |
| 3306 | PyExc_TypeError, |
| 3307 | "spawnvp() arg 2 must contain only strings"); |
| 3308 | PyMem_Free(path); |
| 3309 | return NULL; |
| 3310 | } |
| 3311 | } |
| 3312 | argvlist[argc] = NULL; |
| 3313 | |
| 3314 | Py_BEGIN_ALLOW_THREADS |
| 3315 | #if defined(PYCC_GCC) |
| 3316 | spawnval = spawnvp(mode, path, argvlist); |
| 3317 | #else |
| 3318 | spawnval = _spawnvp(mode, path, argvlist); |
| 3319 | #endif |
| 3320 | Py_END_ALLOW_THREADS |
| 3321 | |
| 3322 | free_string_array(argvlist, argc); |
| 3323 | PyMem_Free(path); |
| 3324 | |
| 3325 | if (spawnval == -1) |
| 3326 | return posix_error(); |
| 3327 | else |
| 3328 | return Py_BuildValue("l", (long) spawnval); |
| 3329 | } |
| 3330 | |
| 3331 | |
| 3332 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3333 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3334 | Execute the program 'file' in a new process, using the environment\n\ |
| 3335 | search path to find the file.\n\ |
| 3336 | \n\ |
| 3337 | mode: mode of process creation\n\ |
| 3338 | file: executable file name\n\ |
| 3339 | args: tuple or list of arguments\n\ |
| 3340 | env: dictionary of strings mapping to strings"); |
| 3341 | |
| 3342 | static PyObject * |
| 3343 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3344 | { |
| 3345 | char *path; |
| 3346 | PyObject *argv, *env; |
| 3347 | char **argvlist; |
| 3348 | char **envlist; |
| 3349 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3350 | int mode, i, pos, argc, envc; |
| 3351 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3352 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3353 | int lastarg = 0; |
| 3354 | |
| 3355 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3356 | argv is a list or tuple of strings and env is a dictionary |
| 3357 | like posix.environ. */ |
| 3358 | |
| 3359 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3360 | Py_FileSystemDefaultEncoding, |
| 3361 | &path, &argv, &env)) |
| 3362 | return NULL; |
| 3363 | if (PyList_Check(argv)) { |
| 3364 | argc = PyList_Size(argv); |
| 3365 | getitem = PyList_GetItem; |
| 3366 | } |
| 3367 | else if (PyTuple_Check(argv)) { |
| 3368 | argc = PyTuple_Size(argv); |
| 3369 | getitem = PyTuple_GetItem; |
| 3370 | } |
| 3371 | else { |
| 3372 | PyErr_SetString(PyExc_TypeError, |
| 3373 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3374 | goto fail_0; |
| 3375 | } |
| 3376 | if (!PyMapping_Check(env)) { |
| 3377 | PyErr_SetString(PyExc_TypeError, |
| 3378 | "spawnvpe() arg 3 must be a mapping object"); |
| 3379 | goto fail_0; |
| 3380 | } |
| 3381 | |
| 3382 | argvlist = PyMem_NEW(char *, argc+1); |
| 3383 | if (argvlist == NULL) { |
| 3384 | PyErr_NoMemory(); |
| 3385 | goto fail_0; |
| 3386 | } |
| 3387 | for (i = 0; i < argc; i++) { |
| 3388 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3389 | "et;spawnvpe() arg 2 must contain only strings", |
| 3390 | Py_FileSystemDefaultEncoding, |
| 3391 | &argvlist[i])) |
| 3392 | { |
| 3393 | lastarg = i; |
| 3394 | goto fail_1; |
| 3395 | } |
| 3396 | } |
| 3397 | lastarg = argc; |
| 3398 | argvlist[argc] = NULL; |
| 3399 | |
| 3400 | i = PyMapping_Size(env); |
| 3401 | if (i < 0) |
| 3402 | goto fail_1; |
| 3403 | envlist = PyMem_NEW(char *, i + 1); |
| 3404 | if (envlist == NULL) { |
| 3405 | PyErr_NoMemory(); |
| 3406 | goto fail_1; |
| 3407 | } |
| 3408 | envc = 0; |
| 3409 | keys = PyMapping_Keys(env); |
| 3410 | vals = PyMapping_Values(env); |
| 3411 | if (!keys || !vals) |
| 3412 | goto fail_2; |
| 3413 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3414 | PyErr_SetString(PyExc_TypeError, |
| 3415 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3416 | goto fail_2; |
| 3417 | } |
| 3418 | |
| 3419 | for (pos = 0; pos < i; pos++) { |
| 3420 | char *p, *k, *v; |
| 3421 | size_t len; |
| 3422 | |
| 3423 | key = PyList_GetItem(keys, pos); |
| 3424 | val = PyList_GetItem(vals, pos); |
| 3425 | if (!key || !val) |
| 3426 | goto fail_2; |
| 3427 | |
| 3428 | if (!PyArg_Parse( |
| 3429 | key, |
| 3430 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3431 | &k) || |
| 3432 | !PyArg_Parse( |
| 3433 | val, |
| 3434 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3435 | &v)) |
| 3436 | { |
| 3437 | goto fail_2; |
| 3438 | } |
| 3439 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3440 | p = PyMem_NEW(char, len); |
| 3441 | if (p == NULL) { |
| 3442 | PyErr_NoMemory(); |
| 3443 | goto fail_2; |
| 3444 | } |
| 3445 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3446 | envlist[envc++] = p; |
| 3447 | } |
| 3448 | envlist[envc] = 0; |
| 3449 | |
| 3450 | Py_BEGIN_ALLOW_THREADS |
| 3451 | #if defined(PYCC_GCC) |
| 3452 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3453 | #else |
| 3454 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3455 | #endif |
| 3456 | Py_END_ALLOW_THREADS |
| 3457 | |
| 3458 | if (spawnval == -1) |
| 3459 | (void) posix_error(); |
| 3460 | else |
| 3461 | res = Py_BuildValue("l", (long) spawnval); |
| 3462 | |
| 3463 | fail_2: |
| 3464 | while (--envc >= 0) |
| 3465 | PyMem_DEL(envlist[envc]); |
| 3466 | PyMem_DEL(envlist); |
| 3467 | fail_1: |
| 3468 | free_string_array(argvlist, lastarg); |
| 3469 | Py_XDECREF(vals); |
| 3470 | Py_XDECREF(keys); |
| 3471 | fail_0: |
| 3472 | PyMem_Free(path); |
| 3473 | return res; |
| 3474 | } |
| 3475 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3476 | #endif /* HAVE_SPAWNV */ |
| 3477 | |
| 3478 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3479 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3480 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3481 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3482 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3483 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3484 | 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] | 3485 | |
| 3486 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3487 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3488 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3489 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3490 | if (pid == -1) |
| 3491 | return posix_error(); |
| 3492 | PyOS_AfterFork(); |
| 3493 | return PyInt_FromLong((long)pid); |
| 3494 | } |
| 3495 | #endif |
| 3496 | |
| 3497 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3498 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3499 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3500 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3501 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3502 | 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] | 3503 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3504 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3505 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3506 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3507 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3508 | if (pid == -1) |
| 3509 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3510 | if (pid == 0) |
| 3511 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3512 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3513 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3514 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3515 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3516 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3517 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3518 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3519 | #define DEV_PTY_FILE "/dev/ptc" |
| 3520 | #define HAVE_DEV_PTMX |
| 3521 | #else |
| 3522 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3523 | #endif |
| 3524 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3525 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3526 | #ifdef HAVE_PTY_H |
| 3527 | #include <pty.h> |
| 3528 | #else |
| 3529 | #ifdef HAVE_LIBUTIL_H |
| 3530 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3531 | #endif /* HAVE_LIBUTIL_H */ |
| 3532 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3533 | #ifdef HAVE_STROPTS_H |
| 3534 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3535 | #endif |
| 3536 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3537 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3538 | #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] | 3539 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3540 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3541 | 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] | 3542 | |
| 3543 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3544 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3545 | { |
| 3546 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3547 | #ifndef HAVE_OPENPTY |
| 3548 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3549 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3550 | #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] | 3551 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3552 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3553 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3554 | #endif |
| 3555 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3556 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3557 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3558 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3559 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3560 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3561 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3562 | if (slave_name == NULL) |
| 3563 | return posix_error(); |
| 3564 | |
| 3565 | slave_fd = open(slave_name, O_RDWR); |
| 3566 | if (slave_fd < 0) |
| 3567 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3568 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3569 | 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] | 3570 | if (master_fd < 0) |
| 3571 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3572 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3573 | /* change permission of slave */ |
| 3574 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3575 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3576 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3577 | } |
| 3578 | /* unlock slave */ |
| 3579 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3580 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3581 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3582 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3583 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3584 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3585 | if (slave_name == NULL) |
| 3586 | return posix_error(); |
| 3587 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3588 | if (slave_fd < 0) |
| 3589 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3590 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3591 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3592 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3593 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3594 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3595 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3596 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3597 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3598 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3599 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3600 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3601 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3602 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3603 | |
| 3604 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3605 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3606 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3607 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3608 | 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] | 3609 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3610 | |
| 3611 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3612 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3613 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3614 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3615 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3616 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3617 | if (pid == -1) |
| 3618 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3619 | if (pid == 0) |
| 3620 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3621 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3622 | } |
| 3623 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3624 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3625 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3626 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3627 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3628 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3629 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3630 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3631 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3632 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3633 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3634 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3635 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3636 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3637 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3638 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3639 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3640 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3641 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3642 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3643 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3644 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3645 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3646 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3647 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3648 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3649 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3650 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3651 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3652 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3653 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3654 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3655 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3656 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3657 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3658 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3659 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3660 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3661 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3662 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3663 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3664 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3665 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3666 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3667 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3668 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3669 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3670 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3671 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3674 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3675 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3676 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3677 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3678 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3679 | |
| 3680 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3681 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3682 | { |
| 3683 | PyObject *result = NULL; |
| 3684 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3685 | #ifdef NGROUPS_MAX |
| 3686 | #define MAX_GROUPS NGROUPS_MAX |
| 3687 | #else |
| 3688 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3689 | #define MAX_GROUPS 64 |
| 3690 | #endif |
| 3691 | gid_t grouplist[MAX_GROUPS]; |
| 3692 | int n; |
| 3693 | |
| 3694 | n = getgroups(MAX_GROUPS, grouplist); |
| 3695 | if (n < 0) |
| 3696 | posix_error(); |
| 3697 | else { |
| 3698 | result = PyList_New(n); |
| 3699 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3700 | int i; |
| 3701 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3702 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3703 | if (o == NULL) { |
| 3704 | Py_DECREF(result); |
| 3705 | result = NULL; |
| 3706 | break; |
| 3707 | } |
| 3708 | PyList_SET_ITEM(result, i, o); |
| 3709 | } |
| 3710 | } |
| 3711 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3712 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3713 | return result; |
| 3714 | } |
| 3715 | #endif |
| 3716 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3717 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3718 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3719 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3720 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3721 | |
| 3722 | static PyObject * |
| 3723 | posix_getpgid(PyObject *self, PyObject *args) |
| 3724 | { |
| 3725 | int pid, pgid; |
| 3726 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3727 | return NULL; |
| 3728 | pgid = getpgid(pid); |
| 3729 | if (pgid < 0) |
| 3730 | return posix_error(); |
| 3731 | return PyInt_FromLong((long)pgid); |
| 3732 | } |
| 3733 | #endif /* HAVE_GETPGID */ |
| 3734 | |
| 3735 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3736 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3737 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3738 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3739 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3740 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3741 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3742 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3743 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3744 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3745 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3746 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3747 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3748 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3749 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3750 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3751 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3752 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3753 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3754 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3755 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3756 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3757 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3758 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3759 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3760 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3761 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3762 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3763 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3764 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3765 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3766 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3767 | Py_INCREF(Py_None); |
| 3768 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3771 | #endif /* HAVE_SETPGRP */ |
| 3772 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3773 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3774 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3775 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3776 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3777 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3778 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3779 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3780 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3781 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3782 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3783 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3784 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3785 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3786 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3787 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3788 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3789 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3790 | |
| 3791 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3792 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3793 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3794 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3795 | char *name; |
| 3796 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3797 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3798 | errno = 0; |
| 3799 | name = getlogin(); |
| 3800 | if (name == NULL) { |
| 3801 | if (errno) |
| 3802 | posix_error(); |
| 3803 | else |
| 3804 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3805 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3806 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3807 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 3808 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3809 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3810 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3811 | return result; |
| 3812 | } |
| 3813 | #endif |
| 3814 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3815 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3816 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3817 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3818 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3819 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3820 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3821 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3822 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3823 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3824 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3825 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3826 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3827 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3828 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3829 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3830 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3831 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3832 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3833 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3834 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3835 | { |
| 3836 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3837 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3838 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3839 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3840 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3841 | APIRET rc; |
| 3842 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3843 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3844 | |
| 3845 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3846 | APIRET rc; |
| 3847 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3848 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3849 | |
| 3850 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3851 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3852 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3853 | if (kill(pid, sig) == -1) |
| 3854 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3855 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3856 | Py_INCREF(Py_None); |
| 3857 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3858 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3859 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3860 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3861 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3862 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3863 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3864 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3865 | |
| 3866 | static PyObject * |
| 3867 | posix_killpg(PyObject *self, PyObject *args) |
| 3868 | { |
| 3869 | int pgid, sig; |
| 3870 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3871 | return NULL; |
| 3872 | if (killpg(pgid, sig) == -1) |
| 3873 | return posix_error(); |
| 3874 | Py_INCREF(Py_None); |
| 3875 | return Py_None; |
| 3876 | } |
| 3877 | #endif |
| 3878 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3879 | #ifdef HAVE_PLOCK |
| 3880 | |
| 3881 | #ifdef HAVE_SYS_LOCK_H |
| 3882 | #include <sys/lock.h> |
| 3883 | #endif |
| 3884 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3885 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3886 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3887 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3888 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3889 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3890 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3891 | { |
| 3892 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3893 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3894 | return NULL; |
| 3895 | if (plock(op) == -1) |
| 3896 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3897 | Py_INCREF(Py_None); |
| 3898 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3899 | } |
| 3900 | #endif |
| 3901 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3902 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3903 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3904 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3905 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3906 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3907 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3908 | Set the current process's user id."); |
| 3909 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3910 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3911 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3912 | { |
| 3913 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3914 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3915 | return NULL; |
| 3916 | if (setuid(uid) < 0) |
| 3917 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3918 | Py_INCREF(Py_None); |
| 3919 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3920 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3921 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3922 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3923 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3924 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3925 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3926 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3927 | Set the current process's effective user id."); |
| 3928 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3929 | static PyObject * |
| 3930 | posix_seteuid (PyObject *self, PyObject *args) |
| 3931 | { |
| 3932 | int euid; |
| 3933 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3934 | return NULL; |
| 3935 | } else if (seteuid(euid) < 0) { |
| 3936 | return posix_error(); |
| 3937 | } else { |
| 3938 | Py_INCREF(Py_None); |
| 3939 | return Py_None; |
| 3940 | } |
| 3941 | } |
| 3942 | #endif /* HAVE_SETEUID */ |
| 3943 | |
| 3944 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3945 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3946 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3947 | Set the current process's effective group id."); |
| 3948 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3949 | static PyObject * |
| 3950 | posix_setegid (PyObject *self, PyObject *args) |
| 3951 | { |
| 3952 | int egid; |
| 3953 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3954 | return NULL; |
| 3955 | } else if (setegid(egid) < 0) { |
| 3956 | return posix_error(); |
| 3957 | } else { |
| 3958 | Py_INCREF(Py_None); |
| 3959 | return Py_None; |
| 3960 | } |
| 3961 | } |
| 3962 | #endif /* HAVE_SETEGID */ |
| 3963 | |
| 3964 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3965 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 3966 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3967 | Set the current process's real and effective user ids."); |
| 3968 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3969 | static PyObject * |
| 3970 | posix_setreuid (PyObject *self, PyObject *args) |
| 3971 | { |
| 3972 | int ruid, euid; |
| 3973 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3974 | return NULL; |
| 3975 | } else if (setreuid(ruid, euid) < 0) { |
| 3976 | return posix_error(); |
| 3977 | } else { |
| 3978 | Py_INCREF(Py_None); |
| 3979 | return Py_None; |
| 3980 | } |
| 3981 | } |
| 3982 | #endif /* HAVE_SETREUID */ |
| 3983 | |
| 3984 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3985 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 3986 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3987 | Set the current process's real and effective group ids."); |
| 3988 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3989 | static PyObject * |
| 3990 | posix_setregid (PyObject *self, PyObject *args) |
| 3991 | { |
| 3992 | int rgid, egid; |
| 3993 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 3994 | return NULL; |
| 3995 | } else if (setregid(rgid, egid) < 0) { |
| 3996 | return posix_error(); |
| 3997 | } else { |
| 3998 | Py_INCREF(Py_None); |
| 3999 | return Py_None; |
| 4000 | } |
| 4001 | } |
| 4002 | #endif /* HAVE_SETREGID */ |
| 4003 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4004 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4005 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4006 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4007 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4008 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4009 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4010 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4011 | { |
| 4012 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4013 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4014 | return NULL; |
| 4015 | if (setgid(gid) < 0) |
| 4016 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4017 | Py_INCREF(Py_None); |
| 4018 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4019 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4020 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4021 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4022 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4023 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4024 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4025 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4026 | |
| 4027 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4028 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4029 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4030 | int i, len; |
| 4031 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4032 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4033 | if (!PySequence_Check(groups)) { |
| 4034 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4035 | return NULL; |
| 4036 | } |
| 4037 | len = PySequence_Size(groups); |
| 4038 | if (len > MAX_GROUPS) { |
| 4039 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4040 | return NULL; |
| 4041 | } |
| 4042 | for(i = 0; i < len; i++) { |
| 4043 | PyObject *elem; |
| 4044 | elem = PySequence_GetItem(groups, i); |
| 4045 | if (!elem) |
| 4046 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4047 | if (!PyLong_Check(elem)) { |
| 4048 | PyErr_SetString(PyExc_TypeError, |
| 4049 | "groups must be integers"); |
| 4050 | Py_DECREF(elem); |
| 4051 | return NULL; |
| 4052 | } else { |
| 4053 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4054 | if (PyErr_Occurred()) { |
| 4055 | PyErr_SetString(PyExc_TypeError, |
| 4056 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4057 | Py_DECREF(elem); |
| 4058 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4059 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4060 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4061 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4062 | if (grouplist[i] != x) { |
| 4063 | PyErr_SetString(PyExc_TypeError, |
| 4064 | "group id too big"); |
| 4065 | Py_DECREF(elem); |
| 4066 | return NULL; |
| 4067 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4068 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4069 | Py_DECREF(elem); |
| 4070 | } |
| 4071 | |
| 4072 | if (setgroups(len, grouplist) < 0) |
| 4073 | return posix_error(); |
| 4074 | Py_INCREF(Py_None); |
| 4075 | return Py_None; |
| 4076 | } |
| 4077 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4078 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4079 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4080 | static PyObject * |
| 4081 | wait_helper(int pid, int status, struct rusage *ru) |
| 4082 | { |
| 4083 | PyObject *result; |
| 4084 | static PyObject *struct_rusage; |
| 4085 | |
| 4086 | if (pid == -1) |
| 4087 | return posix_error(); |
| 4088 | |
| 4089 | if (struct_rusage == NULL) { |
| 4090 | PyObject *m = PyImport_ImportModule("resource"); |
| 4091 | if (m == NULL) |
| 4092 | return NULL; |
| 4093 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4094 | Py_DECREF(m); |
| 4095 | if (struct_rusage == NULL) |
| 4096 | return NULL; |
| 4097 | } |
| 4098 | |
| 4099 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4100 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4101 | if (!result) |
| 4102 | return NULL; |
| 4103 | |
| 4104 | #ifndef doubletime |
| 4105 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4106 | #endif |
| 4107 | |
| 4108 | PyStructSequence_SET_ITEM(result, 0, |
| 4109 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4110 | PyStructSequence_SET_ITEM(result, 1, |
| 4111 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4112 | #define SET_INT(result, index, value)\ |
| 4113 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 4114 | SET_INT(result, 2, ru->ru_maxrss); |
| 4115 | SET_INT(result, 3, ru->ru_ixrss); |
| 4116 | SET_INT(result, 4, ru->ru_idrss); |
| 4117 | SET_INT(result, 5, ru->ru_isrss); |
| 4118 | SET_INT(result, 6, ru->ru_minflt); |
| 4119 | SET_INT(result, 7, ru->ru_majflt); |
| 4120 | SET_INT(result, 8, ru->ru_nswap); |
| 4121 | SET_INT(result, 9, ru->ru_inblock); |
| 4122 | SET_INT(result, 10, ru->ru_oublock); |
| 4123 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4124 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4125 | SET_INT(result, 13, ru->ru_nsignals); |
| 4126 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4127 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4128 | #undef SET_INT |
| 4129 | |
| 4130 | if (PyErr_Occurred()) { |
| 4131 | Py_DECREF(result); |
| 4132 | return NULL; |
| 4133 | } |
| 4134 | |
| 4135 | return Py_BuildValue("iiN", pid, status, result); |
| 4136 | } |
| 4137 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4138 | |
| 4139 | #ifdef HAVE_WAIT3 |
| 4140 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4141 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4142 | Wait for completion of a child process."); |
| 4143 | |
| 4144 | static PyObject * |
| 4145 | posix_wait3(PyObject *self, PyObject *args) |
| 4146 | { |
| 4147 | int pid, options; |
| 4148 | struct rusage ru; |
| 4149 | WAIT_TYPE status; |
| 4150 | WAIT_STATUS_INT(status) = 0; |
| 4151 | |
| 4152 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4153 | return NULL; |
| 4154 | |
| 4155 | Py_BEGIN_ALLOW_THREADS |
| 4156 | pid = wait3(&status, options, &ru); |
| 4157 | Py_END_ALLOW_THREADS |
| 4158 | |
| 4159 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4160 | } |
| 4161 | #endif /* HAVE_WAIT3 */ |
| 4162 | |
| 4163 | #ifdef HAVE_WAIT4 |
| 4164 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4165 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4166 | Wait for completion of a given child process."); |
| 4167 | |
| 4168 | static PyObject * |
| 4169 | posix_wait4(PyObject *self, PyObject *args) |
| 4170 | { |
| 4171 | int pid, options; |
| 4172 | struct rusage ru; |
| 4173 | WAIT_TYPE status; |
| 4174 | WAIT_STATUS_INT(status) = 0; |
| 4175 | |
| 4176 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 4177 | return NULL; |
| 4178 | |
| 4179 | Py_BEGIN_ALLOW_THREADS |
| 4180 | pid = wait4(pid, &status, options, &ru); |
| 4181 | Py_END_ALLOW_THREADS |
| 4182 | |
| 4183 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4184 | } |
| 4185 | #endif /* HAVE_WAIT4 */ |
| 4186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4187 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4188 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4189 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4190 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4191 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4192 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4193 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4194 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4195 | int pid, options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4196 | WAIT_TYPE status; |
| 4197 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4198 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4199 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4200 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4201 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4202 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4203 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4204 | if (pid == -1) |
| 4205 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4206 | |
| 4207 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4210 | #elif defined(HAVE_CWAIT) |
| 4211 | |
| 4212 | /* 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] | 4213 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4214 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4215 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4216 | |
| 4217 | static PyObject * |
| 4218 | posix_waitpid(PyObject *self, PyObject *args) |
| 4219 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4220 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4221 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4222 | |
| 4223 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4224 | return NULL; |
| 4225 | Py_BEGIN_ALLOW_THREADS |
| 4226 | pid = _cwait(&status, pid, options); |
| 4227 | Py_END_ALLOW_THREADS |
| 4228 | if (pid == -1) |
| 4229 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4230 | |
| 4231 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 4232 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4233 | } |
| 4234 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4235 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4236 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4237 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4238 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4239 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4240 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4241 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4242 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4243 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4244 | int pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4245 | WAIT_TYPE status; |
| 4246 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4247 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4248 | Py_BEGIN_ALLOW_THREADS |
| 4249 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4250 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4251 | if (pid == -1) |
| 4252 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4253 | |
| 4254 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4255 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4256 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4257 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4259 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4260 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4261 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4262 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4263 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4264 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4265 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4266 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4267 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4268 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4269 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4270 | 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] | 4271 | #else |
| 4272 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4273 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4274 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4277 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4278 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4279 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4280 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4281 | 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] | 4282 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4283 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4284 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4285 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4286 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4287 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4288 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4289 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4290 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4291 | |
| 4292 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 4293 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4294 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4295 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4296 | if (v == NULL) { |
| 4297 | PyMem_Free(path); |
| 4298 | return NULL; |
| 4299 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4300 | |
| 4301 | if (PyUnicode_Check(v)) { |
| 4302 | arg_is_unicode = 1; |
| 4303 | } |
| 4304 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4305 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4306 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4307 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4308 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4309 | if (n < 0) |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4310 | return posix_error_with_allocated_filename(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4311 | |
Neal Norwitz | fca7005 | 2007-08-12 16:56:02 +0000 | [diff] [blame] | 4312 | PyMem_Free(path); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4313 | v = PyString_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4314 | if (arg_is_unicode) { |
| 4315 | PyObject *w; |
| 4316 | |
| 4317 | w = PyUnicode_FromEncodedObject(v, |
| 4318 | Py_FileSystemDefaultEncoding, |
| 4319 | "strict"); |
| 4320 | if (w != NULL) { |
| 4321 | Py_DECREF(v); |
| 4322 | v = w; |
| 4323 | } |
| 4324 | else { |
| 4325 | /* fall back to the original byte string, as |
| 4326 | discussed in patch #683592 */ |
| 4327 | PyErr_Clear(); |
| 4328 | } |
| 4329 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4330 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4331 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4332 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4333 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4334 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4335 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4336 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4337 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4338 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4339 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4340 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4341 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4342 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4343 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4344 | } |
| 4345 | #endif /* HAVE_SYMLINK */ |
| 4346 | |
| 4347 | |
| 4348 | #ifdef HAVE_TIMES |
| 4349 | #ifndef HZ |
| 4350 | #define HZ 60 /* Universal constant :-) */ |
| 4351 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4352 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4353 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4354 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4355 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4356 | { |
| 4357 | ULONG value = 0; |
| 4358 | |
| 4359 | Py_BEGIN_ALLOW_THREADS |
| 4360 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4361 | Py_END_ALLOW_THREADS |
| 4362 | |
| 4363 | return value; |
| 4364 | } |
| 4365 | |
| 4366 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4367 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4368 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4369 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4370 | return Py_BuildValue("ddddd", |
| 4371 | (double)0 /* t.tms_utime / HZ */, |
| 4372 | (double)0 /* t.tms_stime / HZ */, |
| 4373 | (double)0 /* t.tms_cutime / HZ */, |
| 4374 | (double)0 /* t.tms_cstime / HZ */, |
| 4375 | (double)system_uptime() / 1000); |
| 4376 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4377 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4378 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4379 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4380 | { |
| 4381 | struct tms t; |
| 4382 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4383 | errno = 0; |
| 4384 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4385 | if (c == (clock_t) -1) |
| 4386 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4387 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4388 | (double)t.tms_utime / HZ, |
| 4389 | (double)t.tms_stime / HZ, |
| 4390 | (double)t.tms_cutime / HZ, |
| 4391 | (double)t.tms_cstime / HZ, |
| 4392 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4393 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4394 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4395 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4396 | |
| 4397 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4398 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4399 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4400 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4401 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4402 | { |
| 4403 | FILETIME create, exit, kernel, user; |
| 4404 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4405 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4406 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4407 | /* The fields of a FILETIME structure are the hi and lo part |
| 4408 | of a 64-bit value expressed in 100 nanosecond units. |
| 4409 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4410 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4411 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4412 | return Py_BuildValue( |
| 4413 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4414 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4415 | kernel.dwLowDateTime*1e-7), |
| 4416 | (double)(user.dwHighDateTime*429.4967296 + |
| 4417 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4418 | (double)0, |
| 4419 | (double)0, |
| 4420 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4421 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4422 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4423 | |
| 4424 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4425 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4426 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4427 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4428 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4429 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4430 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4431 | #ifdef HAVE_GETSID |
| 4432 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4433 | "getsid(pid) -> sid\n\n\ |
| 4434 | Call the system call getsid()."); |
| 4435 | |
| 4436 | static PyObject * |
| 4437 | posix_getsid(PyObject *self, PyObject *args) |
| 4438 | { |
| 4439 | int pid, sid; |
| 4440 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4441 | return NULL; |
| 4442 | sid = getsid(pid); |
| 4443 | if (sid < 0) |
| 4444 | return posix_error(); |
| 4445 | return PyInt_FromLong((long)sid); |
| 4446 | } |
| 4447 | #endif /* HAVE_GETSID */ |
| 4448 | |
| 4449 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4450 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4451 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4452 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4453 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4454 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4455 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4456 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4457 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4458 | if (setsid() < 0) |
| 4459 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4460 | Py_INCREF(Py_None); |
| 4461 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4462 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4463 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4464 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4465 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4466 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4467 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4468 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4470 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4471 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4472 | { |
| 4473 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4474 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4475 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4476 | if (setpgid(pid, pgrp) < 0) |
| 4477 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4478 | Py_INCREF(Py_None); |
| 4479 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4480 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4481 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4482 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4483 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4484 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4485 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4486 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4487 | 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] | 4488 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4489 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4490 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4491 | { |
| 4492 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4493 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4494 | return NULL; |
| 4495 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4496 | if (pgid < 0) |
| 4497 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4498 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4499 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4500 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4501 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4502 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4503 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4504 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4505 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4506 | 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] | 4507 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4508 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4509 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4510 | { |
| 4511 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4512 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4513 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4514 | if (tcsetpgrp(fd, pgid) < 0) |
| 4515 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4516 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4517 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4518 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4519 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4520 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4521 | /* Functions acting on file descriptors */ |
| 4522 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4523 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4524 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4525 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4526 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4527 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4528 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4529 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4530 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4531 | int flag; |
| 4532 | int mode = 0777; |
| 4533 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4534 | |
| 4535 | #ifdef MS_WINDOWS |
| 4536 | if (unicode_file_names()) { |
| 4537 | PyUnicodeObject *po; |
| 4538 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4539 | Py_BEGIN_ALLOW_THREADS |
| 4540 | /* PyUnicode_AS_UNICODE OK without thread |
| 4541 | lock as it is a simple dereference. */ |
| 4542 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4543 | Py_END_ALLOW_THREADS |
| 4544 | if (fd < 0) |
| 4545 | return posix_error(); |
| 4546 | return PyInt_FromLong((long)fd); |
| 4547 | } |
| 4548 | /* Drop the argument parsing error as narrow strings |
| 4549 | are also valid. */ |
| 4550 | PyErr_Clear(); |
| 4551 | } |
| 4552 | #endif |
| 4553 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4554 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4555 | Py_FileSystemDefaultEncoding, &file, |
| 4556 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4557 | return NULL; |
| 4558 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4559 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4560 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4561 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4562 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4563 | return posix_error_with_allocated_filename(file); |
| 4564 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4565 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4569 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4570 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4571 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4572 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4573 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4574 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4575 | { |
| 4576 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4577 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4578 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4579 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4580 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4581 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4582 | if (res < 0) |
| 4583 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4584 | Py_INCREF(Py_None); |
| 4585 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4589 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4590 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4591 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4592 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4593 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4594 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4595 | { |
| 4596 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4597 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4598 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4599 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4600 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4601 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4602 | if (fd < 0) |
| 4603 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4604 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4608 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4609 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4610 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4611 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4612 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4613 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4614 | { |
| 4615 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4616 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4617 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4618 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4619 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4620 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4621 | if (res < 0) |
| 4622 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4623 | Py_INCREF(Py_None); |
| 4624 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4627 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4628 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4629 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4630 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4631 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4632 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4633 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4634 | { |
| 4635 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4636 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4637 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4638 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4639 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4640 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4641 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4642 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4643 | return NULL; |
| 4644 | #ifdef SEEK_SET |
| 4645 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4646 | switch (how) { |
| 4647 | case 0: how = SEEK_SET; break; |
| 4648 | case 1: how = SEEK_CUR; break; |
| 4649 | case 2: how = SEEK_END; break; |
| 4650 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4651 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4652 | |
| 4653 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4654 | pos = PyInt_AsLong(posobj); |
| 4655 | #else |
| 4656 | pos = PyLong_Check(posobj) ? |
| 4657 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4658 | #endif |
| 4659 | if (PyErr_Occurred()) |
| 4660 | return NULL; |
| 4661 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4662 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4663 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4664 | res = _lseeki64(fd, pos, how); |
| 4665 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4666 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4667 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4668 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4669 | if (res < 0) |
| 4670 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4671 | |
| 4672 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4673 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4674 | #else |
| 4675 | return PyLong_FromLongLong(res); |
| 4676 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4680 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4681 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4682 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4683 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4684 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4685 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4686 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4687 | int fd, size; |
| 4688 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4689 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4690 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4691 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 4692 | if (size < 0) { |
| 4693 | errno = EINVAL; |
| 4694 | return posix_error(); |
| 4695 | } |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4696 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4697 | if (buffer == NULL) |
| 4698 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4699 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4700 | n = read(fd, PyBytes_AsString(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4701 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4702 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4703 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4704 | return posix_error(); |
| 4705 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4706 | if (n != size) |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4707 | PyBytes_Resize(buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4708 | return buffer; |
| 4709 | } |
| 4710 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4712 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4713 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4714 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4715 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4716 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4717 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4718 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4719 | int fd; |
| 4720 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4721 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4722 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4723 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4724 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4725 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4726 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4727 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4728 | if (size < 0) |
| 4729 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 4730 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4734 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4735 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4736 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4738 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4739 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4740 | { |
| 4741 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4742 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4743 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4744 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4745 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 4746 | #ifdef __VMS |
| 4747 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 4748 | fsync(fd); |
| 4749 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4750 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4751 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4752 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4753 | if (res != 0) { |
| 4754 | #ifdef MS_WINDOWS |
| 4755 | return win32_error("fstat", NULL); |
| 4756 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4757 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4758 | #endif |
| 4759 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4760 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 4761 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4764 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4765 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4766 | 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] | 4767 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4768 | |
| 4769 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4770 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4771 | { |
| 4772 | int fd; |
| 4773 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4774 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4775 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4776 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4777 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4778 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4779 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4780 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4781 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4782 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4783 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4784 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4785 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4786 | #if defined(PYOS_OS2) |
| 4787 | HFILE read, write; |
| 4788 | APIRET rc; |
| 4789 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4790 | Py_BEGIN_ALLOW_THREADS |
| 4791 | rc = DosCreatePipe( &read, &write, 4096); |
| 4792 | Py_END_ALLOW_THREADS |
| 4793 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4794 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4795 | |
| 4796 | return Py_BuildValue("(ii)", read, write); |
| 4797 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4798 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4799 | int fds[2]; |
| 4800 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4801 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4802 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4803 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4804 | if (res != 0) |
| 4805 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4806 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4807 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4808 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4809 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4810 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4811 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4812 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4813 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4814 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4815 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4816 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4817 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4818 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4819 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4820 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4821 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4822 | #endif /* HAVE_PIPE */ |
| 4823 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4824 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4825 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4826 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4827 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4828 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4829 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4830 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4831 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4832 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4833 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4834 | int mode = 0666; |
| 4835 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4836 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4837 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4838 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4839 | res = mkfifo(filename, mode); |
| 4840 | Py_END_ALLOW_THREADS |
| 4841 | if (res < 0) |
| 4842 | return posix_error(); |
| 4843 | Py_INCREF(Py_None); |
| 4844 | return Py_None; |
| 4845 | } |
| 4846 | #endif |
| 4847 | |
| 4848 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 4849 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4850 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 4851 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4852 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 4853 | named filename. mode specifies both the permissions to use and the\n\ |
| 4854 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 4855 | 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] | 4856 | device defines the newly created device special file (probably using\n\ |
| 4857 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4858 | |
| 4859 | |
| 4860 | static PyObject * |
| 4861 | posix_mknod(PyObject *self, PyObject *args) |
| 4862 | { |
| 4863 | char *filename; |
| 4864 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4865 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4866 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 4867 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 4868 | return NULL; |
| 4869 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4870 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4871 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4872 | if (res < 0) |
| 4873 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4874 | Py_INCREF(Py_None); |
| 4875 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4876 | } |
| 4877 | #endif |
| 4878 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 4879 | #ifdef HAVE_DEVICE_MACROS |
| 4880 | PyDoc_STRVAR(posix_major__doc__, |
| 4881 | "major(device) -> major number\n\ |
| 4882 | Extracts a device major number from a raw device number."); |
| 4883 | |
| 4884 | static PyObject * |
| 4885 | posix_major(PyObject *self, PyObject *args) |
| 4886 | { |
| 4887 | int device; |
| 4888 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 4889 | return NULL; |
| 4890 | return PyInt_FromLong((long)major(device)); |
| 4891 | } |
| 4892 | |
| 4893 | PyDoc_STRVAR(posix_minor__doc__, |
| 4894 | "minor(device) -> minor number\n\ |
| 4895 | Extracts a device minor number from a raw device number."); |
| 4896 | |
| 4897 | static PyObject * |
| 4898 | posix_minor(PyObject *self, PyObject *args) |
| 4899 | { |
| 4900 | int device; |
| 4901 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 4902 | return NULL; |
| 4903 | return PyInt_FromLong((long)minor(device)); |
| 4904 | } |
| 4905 | |
| 4906 | PyDoc_STRVAR(posix_makedev__doc__, |
| 4907 | "makedev(major, minor) -> device number\n\ |
| 4908 | Composes a raw device number from the major and minor device numbers."); |
| 4909 | |
| 4910 | static PyObject * |
| 4911 | posix_makedev(PyObject *self, PyObject *args) |
| 4912 | { |
| 4913 | int major, minor; |
| 4914 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 4915 | return NULL; |
| 4916 | return PyInt_FromLong((long)makedev(major, minor)); |
| 4917 | } |
| 4918 | #endif /* device macros */ |
| 4919 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4920 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4921 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4922 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4923 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4924 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4925 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4926 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4927 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4928 | { |
| 4929 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4930 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4931 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4932 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4933 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4934 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4935 | return NULL; |
| 4936 | |
| 4937 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4938 | length = PyInt_AsLong(lenobj); |
| 4939 | #else |
| 4940 | length = PyLong_Check(lenobj) ? |
| 4941 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 4942 | #endif |
| 4943 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4944 | return NULL; |
| 4945 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4946 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4947 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4948 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4949 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4950 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4951 | return NULL; |
| 4952 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4953 | Py_INCREF(Py_None); |
| 4954 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4955 | } |
| 4956 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4957 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4958 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4959 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4960 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4961 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4962 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4963 | /* Save putenv() parameters as values here, so we can collect them when they |
| 4964 | * get re-set with another call for the same key. */ |
| 4965 | static PyObject *posix_putenv_garbage; |
| 4966 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4967 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4968 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4969 | { |
| 4970 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4971 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4972 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4973 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4974 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4975 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4976 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4977 | |
| 4978 | #if defined(PYOS_OS2) |
| 4979 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 4980 | APIRET rc; |
| 4981 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4982 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 4983 | if (rc != NO_ERROR) |
| 4984 | return os2_error(rc); |
| 4985 | |
| 4986 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 4987 | APIRET rc; |
| 4988 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4989 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 4990 | if (rc != NO_ERROR) |
| 4991 | return os2_error(rc); |
| 4992 | } else { |
| 4993 | #endif |
| 4994 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4995 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4996 | len = strlen(s1) + strlen(s2) + 2; |
| 4997 | /* len includes space for a trailing \0; the size arg to |
| 4998 | PyString_FromStringAndSize does not count that */ |
| 4999 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5000 | if (newstr == NULL) |
| 5001 | return PyErr_NoMemory(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5002 | newenv = PyString_AS_STRING(newstr); |
| 5003 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5004 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5005 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5006 | posix_error(); |
| 5007 | return NULL; |
| 5008 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5009 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5010 | * this will cause previous value to be collected. This has to |
| 5011 | * happen after the real putenv() call because the old value |
| 5012 | * was still accessible until then. */ |
| 5013 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5014 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5015 | /* really not much we can do; just leak */ |
| 5016 | PyErr_Clear(); |
| 5017 | } |
| 5018 | else { |
| 5019 | Py_DECREF(newstr); |
| 5020 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5021 | |
| 5022 | #if defined(PYOS_OS2) |
| 5023 | } |
| 5024 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5025 | Py_INCREF(Py_None); |
| 5026 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5027 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5028 | #endif /* putenv */ |
| 5029 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5030 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5031 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5032 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5033 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5034 | |
| 5035 | static PyObject * |
| 5036 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5037 | { |
| 5038 | char *s1; |
| 5039 | |
| 5040 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5041 | return NULL; |
| 5042 | |
| 5043 | unsetenv(s1); |
| 5044 | |
| 5045 | /* Remove the key from posix_putenv_garbage; |
| 5046 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5047 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5048 | * old value was still accessible until then. |
| 5049 | */ |
| 5050 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5051 | PyTuple_GET_ITEM(args, 0))) { |
| 5052 | /* really not much we can do; just leak */ |
| 5053 | PyErr_Clear(); |
| 5054 | } |
| 5055 | |
| 5056 | Py_INCREF(Py_None); |
| 5057 | return Py_None; |
| 5058 | } |
| 5059 | #endif /* unsetenv */ |
| 5060 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5061 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5062 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5063 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5064 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5065 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5066 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5067 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5068 | { |
| 5069 | int code; |
| 5070 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5071 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5072 | return NULL; |
| 5073 | message = strerror(code); |
| 5074 | if (message == NULL) { |
| 5075 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5076 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5077 | return NULL; |
| 5078 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5079 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5080 | } |
| 5081 | #endif /* strerror */ |
| 5082 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5083 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5084 | #ifdef HAVE_SYS_WAIT_H |
| 5085 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5086 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5087 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5088 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5089 | 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] | 5090 | |
| 5091 | static PyObject * |
| 5092 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5093 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5094 | WAIT_TYPE status; |
| 5095 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5096 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5097 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5098 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5099 | |
| 5100 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5101 | } |
| 5102 | #endif /* WCOREDUMP */ |
| 5103 | |
| 5104 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5105 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5106 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5107 | 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] | 5108 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5109 | |
| 5110 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5111 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5112 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5113 | WAIT_TYPE status; |
| 5114 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5115 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5116 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5117 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5118 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5119 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5120 | } |
| 5121 | #endif /* WIFCONTINUED */ |
| 5122 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5123 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5124 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5125 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5126 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5127 | |
| 5128 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5129 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5130 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5131 | WAIT_TYPE status; |
| 5132 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5133 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5134 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5135 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5136 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5137 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5138 | } |
| 5139 | #endif /* WIFSTOPPED */ |
| 5140 | |
| 5141 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5142 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5143 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5144 | 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] | 5145 | |
| 5146 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5147 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5148 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5149 | WAIT_TYPE status; |
| 5150 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5151 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5152 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5153 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5154 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5155 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5156 | } |
| 5157 | #endif /* WIFSIGNALED */ |
| 5158 | |
| 5159 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5160 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5161 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5162 | 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] | 5163 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5164 | |
| 5165 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5166 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5167 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5168 | WAIT_TYPE status; |
| 5169 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5170 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5171 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5172 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5173 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5174 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5175 | } |
| 5176 | #endif /* WIFEXITED */ |
| 5177 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5178 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5179 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5180 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5181 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5182 | |
| 5183 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5184 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5185 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5186 | WAIT_TYPE status; |
| 5187 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5188 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5189 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5190 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5191 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5192 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5193 | } |
| 5194 | #endif /* WEXITSTATUS */ |
| 5195 | |
| 5196 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5197 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5198 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5199 | 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] | 5200 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5201 | |
| 5202 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5203 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5204 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5205 | WAIT_TYPE status; |
| 5206 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5207 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5208 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5209 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5210 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5211 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5212 | } |
| 5213 | #endif /* WTERMSIG */ |
| 5214 | |
| 5215 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5216 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5217 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5218 | Return the signal that stopped the process that provided\n\ |
| 5219 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5220 | |
| 5221 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5222 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5223 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5224 | WAIT_TYPE status; |
| 5225 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5226 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5227 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5228 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5229 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5230 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5231 | } |
| 5232 | #endif /* WSTOPSIG */ |
| 5233 | |
| 5234 | #endif /* HAVE_SYS_WAIT_H */ |
| 5235 | |
| 5236 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5237 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5238 | #ifdef _SCO_DS |
| 5239 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5240 | needed definitions in sys/statvfs.h */ |
| 5241 | #define _SVID3 |
| 5242 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5243 | #include <sys/statvfs.h> |
| 5244 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5245 | static PyObject* |
| 5246 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5247 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5248 | if (v == NULL) |
| 5249 | return NULL; |
| 5250 | |
| 5251 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5252 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5253 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5254 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5255 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5256 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5257 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5258 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5259 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5260 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5261 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5262 | #else |
| 5263 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5264 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5265 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5266 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5267 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5268 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5269 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5270 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5271 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5272 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5273 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5274 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5275 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5276 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5277 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5278 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5279 | #endif |
| 5280 | |
| 5281 | return v; |
| 5282 | } |
| 5283 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5284 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5285 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5286 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5287 | |
| 5288 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5289 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5290 | { |
| 5291 | int fd, res; |
| 5292 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5293 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5294 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5295 | return NULL; |
| 5296 | Py_BEGIN_ALLOW_THREADS |
| 5297 | res = fstatvfs(fd, &st); |
| 5298 | Py_END_ALLOW_THREADS |
| 5299 | if (res != 0) |
| 5300 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5301 | |
| 5302 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5303 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5304 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5305 | |
| 5306 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5307 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5308 | #include <sys/statvfs.h> |
| 5309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5310 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5311 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5312 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5313 | |
| 5314 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5315 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5316 | { |
| 5317 | char *path; |
| 5318 | int res; |
| 5319 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5320 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5321 | return NULL; |
| 5322 | Py_BEGIN_ALLOW_THREADS |
| 5323 | res = statvfs(path, &st); |
| 5324 | Py_END_ALLOW_THREADS |
| 5325 | if (res != 0) |
| 5326 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5327 | |
| 5328 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5329 | } |
| 5330 | #endif /* HAVE_STATVFS */ |
| 5331 | |
| 5332 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5333 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5334 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5335 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5336 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 5337 | The directory and a prefix may be specified as strings; they may be omitted\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5338 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5339 | |
| 5340 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5341 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5342 | { |
| 5343 | PyObject *result = NULL; |
| 5344 | char *dir = NULL; |
| 5345 | char *pfx = NULL; |
| 5346 | char *name; |
| 5347 | |
| 5348 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5349 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5350 | |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 5351 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 5352 | "tempnam is a potential security risk to your program", |
| 5353 | 1) < 0) |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5354 | return NULL; |
| 5355 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5356 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5357 | name = _tempnam(dir, pfx); |
| 5358 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5359 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5360 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5361 | if (name == NULL) |
| 5362 | return PyErr_NoMemory(); |
| 5363 | result = PyString_FromString(name); |
| 5364 | free(name); |
| 5365 | return result; |
| 5366 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5367 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5368 | |
| 5369 | |
| 5370 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5371 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5372 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5373 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5374 | |
| 5375 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5376 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5377 | { |
| 5378 | FILE *fp; |
| 5379 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5380 | fp = tmpfile(); |
| 5381 | if (fp == NULL) |
| 5382 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 5383 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5384 | } |
| 5385 | #endif |
| 5386 | |
| 5387 | |
| 5388 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5389 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5390 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5391 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5392 | |
| 5393 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5394 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5395 | { |
| 5396 | char buffer[L_tmpnam]; |
| 5397 | char *name; |
| 5398 | |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 5399 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 5400 | "tmpnam is a potential security risk to your program", |
| 5401 | 1) < 0) |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5402 | return NULL; |
| 5403 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5404 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5405 | name = tmpnam_r(buffer); |
| 5406 | #else |
| 5407 | name = tmpnam(buffer); |
| 5408 | #endif |
| 5409 | if (name == NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5410 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5411 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5412 | "unexpected NULL from tmpnam_r" |
| 5413 | #else |
| 5414 | "unexpected NULL from tmpnam" |
| 5415 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5416 | ); |
| 5417 | PyErr_SetObject(PyExc_OSError, err); |
| 5418 | Py_XDECREF(err); |
| 5419 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5420 | } |
| 5421 | return PyString_FromString(buffer); |
| 5422 | } |
| 5423 | #endif |
| 5424 | |
| 5425 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5426 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5427 | * It maps strings representing configuration variable names to |
| 5428 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5429 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5430 | * rarely-used constants. There are three separate tables that use |
| 5431 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5432 | * |
| 5433 | * This code is always included, even if none of the interfaces that |
| 5434 | * need it are included. The #if hackery needed to avoid it would be |
| 5435 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5436 | */ |
| 5437 | struct constdef { |
| 5438 | char *name; |
| 5439 | long value; |
| 5440 | }; |
| 5441 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5442 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5443 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5444 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5445 | { |
| 5446 | if (PyInt_Check(arg)) { |
| 5447 | *valuep = PyInt_AS_LONG(arg); |
| 5448 | return 1; |
| 5449 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5450 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5451 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5452 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5453 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5454 | size_t hi = tablesize; |
| 5455 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5456 | const char *confname; |
| 5457 | Py_ssize_t namelen; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5458 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5459 | PyErr_SetString(PyExc_TypeError, |
| 5460 | "configuration names must be strings or integers"); |
| 5461 | return 0; |
| 5462 | } |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5463 | confname = PyUnicode_AsString(arg); |
| 5464 | if (confname == NULL) |
| 5465 | return 0; |
| 5466 | namelen = strlen(confname); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5467 | while (lo < hi) { |
| 5468 | mid = (lo + hi) / 2; |
| 5469 | cmp = strcmp(confname, table[mid].name); |
| 5470 | if (cmp < 0) |
| 5471 | hi = mid; |
| 5472 | else if (cmp > 0) |
| 5473 | lo = mid + 1; |
| 5474 | else { |
| 5475 | *valuep = table[mid].value; |
| 5476 | return 1; |
| 5477 | } |
| 5478 | } |
| 5479 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5480 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5481 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | |
| 5485 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5486 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5487 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5488 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5489 | #endif |
| 5490 | #ifdef _PC_ABI_ASYNC_IO |
| 5491 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5492 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5493 | #ifdef _PC_ASYNC_IO |
| 5494 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5495 | #endif |
| 5496 | #ifdef _PC_CHOWN_RESTRICTED |
| 5497 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5498 | #endif |
| 5499 | #ifdef _PC_FILESIZEBITS |
| 5500 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5501 | #endif |
| 5502 | #ifdef _PC_LAST |
| 5503 | {"PC_LAST", _PC_LAST}, |
| 5504 | #endif |
| 5505 | #ifdef _PC_LINK_MAX |
| 5506 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5507 | #endif |
| 5508 | #ifdef _PC_MAX_CANON |
| 5509 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5510 | #endif |
| 5511 | #ifdef _PC_MAX_INPUT |
| 5512 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5513 | #endif |
| 5514 | #ifdef _PC_NAME_MAX |
| 5515 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5516 | #endif |
| 5517 | #ifdef _PC_NO_TRUNC |
| 5518 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5519 | #endif |
| 5520 | #ifdef _PC_PATH_MAX |
| 5521 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5522 | #endif |
| 5523 | #ifdef _PC_PIPE_BUF |
| 5524 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5525 | #endif |
| 5526 | #ifdef _PC_PRIO_IO |
| 5527 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5528 | #endif |
| 5529 | #ifdef _PC_SOCK_MAXBUF |
| 5530 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5531 | #endif |
| 5532 | #ifdef _PC_SYNC_IO |
| 5533 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5534 | #endif |
| 5535 | #ifdef _PC_VDISABLE |
| 5536 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5537 | #endif |
| 5538 | }; |
| 5539 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5540 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5541 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5542 | { |
| 5543 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5544 | sizeof(posix_constants_pathconf) |
| 5545 | / sizeof(struct constdef)); |
| 5546 | } |
| 5547 | #endif |
| 5548 | |
| 5549 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5550 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5551 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5552 | 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] | 5553 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5554 | |
| 5555 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5556 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5557 | { |
| 5558 | PyObject *result = NULL; |
| 5559 | int name, fd; |
| 5560 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5561 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5562 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5563 | long limit; |
| 5564 | |
| 5565 | errno = 0; |
| 5566 | limit = fpathconf(fd, name); |
| 5567 | if (limit == -1 && errno != 0) |
| 5568 | posix_error(); |
| 5569 | else |
| 5570 | result = PyInt_FromLong(limit); |
| 5571 | } |
| 5572 | return result; |
| 5573 | } |
| 5574 | #endif |
| 5575 | |
| 5576 | |
| 5577 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5578 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5579 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5580 | 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] | 5581 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5582 | |
| 5583 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5584 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5585 | { |
| 5586 | PyObject *result = NULL; |
| 5587 | int name; |
| 5588 | char *path; |
| 5589 | |
| 5590 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5591 | conv_path_confname, &name)) { |
| 5592 | long limit; |
| 5593 | |
| 5594 | errno = 0; |
| 5595 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5596 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5597 | if (errno == EINVAL) |
| 5598 | /* could be a path or name problem */ |
| 5599 | posix_error(); |
| 5600 | else |
| 5601 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5602 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5603 | else |
| 5604 | result = PyInt_FromLong(limit); |
| 5605 | } |
| 5606 | return result; |
| 5607 | } |
| 5608 | #endif |
| 5609 | |
| 5610 | #ifdef HAVE_CONFSTR |
| 5611 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5612 | #ifdef _CS_ARCHITECTURE |
| 5613 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5614 | #endif |
| 5615 | #ifdef _CS_HOSTNAME |
| 5616 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5617 | #endif |
| 5618 | #ifdef _CS_HW_PROVIDER |
| 5619 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5620 | #endif |
| 5621 | #ifdef _CS_HW_SERIAL |
| 5622 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5623 | #endif |
| 5624 | #ifdef _CS_INITTAB_NAME |
| 5625 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5626 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5627 | #ifdef _CS_LFS64_CFLAGS |
| 5628 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5629 | #endif |
| 5630 | #ifdef _CS_LFS64_LDFLAGS |
| 5631 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5632 | #endif |
| 5633 | #ifdef _CS_LFS64_LIBS |
| 5634 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5635 | #endif |
| 5636 | #ifdef _CS_LFS64_LINTFLAGS |
| 5637 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5638 | #endif |
| 5639 | #ifdef _CS_LFS_CFLAGS |
| 5640 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5641 | #endif |
| 5642 | #ifdef _CS_LFS_LDFLAGS |
| 5643 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5644 | #endif |
| 5645 | #ifdef _CS_LFS_LIBS |
| 5646 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5647 | #endif |
| 5648 | #ifdef _CS_LFS_LINTFLAGS |
| 5649 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5650 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5651 | #ifdef _CS_MACHINE |
| 5652 | {"CS_MACHINE", _CS_MACHINE}, |
| 5653 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5654 | #ifdef _CS_PATH |
| 5655 | {"CS_PATH", _CS_PATH}, |
| 5656 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5657 | #ifdef _CS_RELEASE |
| 5658 | {"CS_RELEASE", _CS_RELEASE}, |
| 5659 | #endif |
| 5660 | #ifdef _CS_SRPC_DOMAIN |
| 5661 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5662 | #endif |
| 5663 | #ifdef _CS_SYSNAME |
| 5664 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5665 | #endif |
| 5666 | #ifdef _CS_VERSION |
| 5667 | {"CS_VERSION", _CS_VERSION}, |
| 5668 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5669 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5670 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5671 | #endif |
| 5672 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5673 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5674 | #endif |
| 5675 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5676 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5677 | #endif |
| 5678 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5679 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5680 | #endif |
| 5681 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5682 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5683 | #endif |
| 5684 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5685 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5686 | #endif |
| 5687 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5688 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5689 | #endif |
| 5690 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5691 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5692 | #endif |
| 5693 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5694 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5695 | #endif |
| 5696 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5697 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5698 | #endif |
| 5699 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5700 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5701 | #endif |
| 5702 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5703 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5704 | #endif |
| 5705 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5706 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5707 | #endif |
| 5708 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5709 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5710 | #endif |
| 5711 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5712 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5713 | #endif |
| 5714 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5715 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5716 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5717 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5718 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5719 | #endif |
| 5720 | #ifdef _MIPS_CS_BASE |
| 5721 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5722 | #endif |
| 5723 | #ifdef _MIPS_CS_HOSTID |
| 5724 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5725 | #endif |
| 5726 | #ifdef _MIPS_CS_HW_NAME |
| 5727 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5728 | #endif |
| 5729 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5730 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5731 | #endif |
| 5732 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5733 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5734 | #endif |
| 5735 | #ifdef _MIPS_CS_OSREL_MIN |
| 5736 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5737 | #endif |
| 5738 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5739 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5740 | #endif |
| 5741 | #ifdef _MIPS_CS_OS_NAME |
| 5742 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5743 | #endif |
| 5744 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5745 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5746 | #endif |
| 5747 | #ifdef _MIPS_CS_PROCESSORS |
| 5748 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5749 | #endif |
| 5750 | #ifdef _MIPS_CS_SERIAL |
| 5751 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5752 | #endif |
| 5753 | #ifdef _MIPS_CS_VENDOR |
| 5754 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5755 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5756 | }; |
| 5757 | |
| 5758 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5759 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5760 | { |
| 5761 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5762 | sizeof(posix_constants_confstr) |
| 5763 | / sizeof(struct constdef)); |
| 5764 | } |
| 5765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5766 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5767 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5768 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5769 | |
| 5770 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5771 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5772 | { |
| 5773 | PyObject *result = NULL; |
| 5774 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5775 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5776 | |
| 5777 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5778 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5779 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5780 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5781 | len = confstr(name, buffer, sizeof(buffer)); |
| 5782 | if (len == 0) { |
| 5783 | if (errno) { |
| 5784 | posix_error(); |
| 5785 | } |
| 5786 | else { |
| 5787 | result = Py_None; |
| 5788 | Py_INCREF(Py_None); |
| 5789 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5790 | } |
| 5791 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5792 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5793 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5794 | if (result != NULL) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5795 | confstr(name, PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5796 | } |
| 5797 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5798 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5799 | } |
| 5800 | } |
| 5801 | return result; |
| 5802 | } |
| 5803 | #endif |
| 5804 | |
| 5805 | |
| 5806 | #ifdef HAVE_SYSCONF |
| 5807 | static struct constdef posix_constants_sysconf[] = { |
| 5808 | #ifdef _SC_2_CHAR_TERM |
| 5809 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5810 | #endif |
| 5811 | #ifdef _SC_2_C_BIND |
| 5812 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5813 | #endif |
| 5814 | #ifdef _SC_2_C_DEV |
| 5815 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5816 | #endif |
| 5817 | #ifdef _SC_2_C_VERSION |
| 5818 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5819 | #endif |
| 5820 | #ifdef _SC_2_FORT_DEV |
| 5821 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5822 | #endif |
| 5823 | #ifdef _SC_2_FORT_RUN |
| 5824 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5825 | #endif |
| 5826 | #ifdef _SC_2_LOCALEDEF |
| 5827 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5828 | #endif |
| 5829 | #ifdef _SC_2_SW_DEV |
| 5830 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5831 | #endif |
| 5832 | #ifdef _SC_2_UPE |
| 5833 | {"SC_2_UPE", _SC_2_UPE}, |
| 5834 | #endif |
| 5835 | #ifdef _SC_2_VERSION |
| 5836 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5837 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5838 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5839 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5840 | #endif |
| 5841 | #ifdef _SC_ACL |
| 5842 | {"SC_ACL", _SC_ACL}, |
| 5843 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5844 | #ifdef _SC_AIO_LISTIO_MAX |
| 5845 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5846 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5847 | #ifdef _SC_AIO_MAX |
| 5848 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5849 | #endif |
| 5850 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5851 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5852 | #endif |
| 5853 | #ifdef _SC_ARG_MAX |
| 5854 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5855 | #endif |
| 5856 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5857 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5858 | #endif |
| 5859 | #ifdef _SC_ATEXIT_MAX |
| 5860 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5861 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5862 | #ifdef _SC_AUDIT |
| 5863 | {"SC_AUDIT", _SC_AUDIT}, |
| 5864 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5865 | #ifdef _SC_AVPHYS_PAGES |
| 5866 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5867 | #endif |
| 5868 | #ifdef _SC_BC_BASE_MAX |
| 5869 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5870 | #endif |
| 5871 | #ifdef _SC_BC_DIM_MAX |
| 5872 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5873 | #endif |
| 5874 | #ifdef _SC_BC_SCALE_MAX |
| 5875 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5876 | #endif |
| 5877 | #ifdef _SC_BC_STRING_MAX |
| 5878 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5879 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5880 | #ifdef _SC_CAP |
| 5881 | {"SC_CAP", _SC_CAP}, |
| 5882 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5883 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5884 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5885 | #endif |
| 5886 | #ifdef _SC_CHAR_BIT |
| 5887 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5888 | #endif |
| 5889 | #ifdef _SC_CHAR_MAX |
| 5890 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5891 | #endif |
| 5892 | #ifdef _SC_CHAR_MIN |
| 5893 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5894 | #endif |
| 5895 | #ifdef _SC_CHILD_MAX |
| 5896 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5897 | #endif |
| 5898 | #ifdef _SC_CLK_TCK |
| 5899 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 5900 | #endif |
| 5901 | #ifdef _SC_COHER_BLKSZ |
| 5902 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 5903 | #endif |
| 5904 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 5905 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 5906 | #endif |
| 5907 | #ifdef _SC_DCACHE_ASSOC |
| 5908 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5909 | #endif |
| 5910 | #ifdef _SC_DCACHE_BLKSZ |
| 5911 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5912 | #endif |
| 5913 | #ifdef _SC_DCACHE_LINESZ |
| 5914 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 5915 | #endif |
| 5916 | #ifdef _SC_DCACHE_SZ |
| 5917 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5918 | #endif |
| 5919 | #ifdef _SC_DCACHE_TBLKSZ |
| 5920 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5921 | #endif |
| 5922 | #ifdef _SC_DELAYTIMER_MAX |
| 5923 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5924 | #endif |
| 5925 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5926 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5927 | #endif |
| 5928 | #ifdef _SC_EXPR_NEST_MAX |
| 5929 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5930 | #endif |
| 5931 | #ifdef _SC_FSYNC |
| 5932 | {"SC_FSYNC", _SC_FSYNC}, |
| 5933 | #endif |
| 5934 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5935 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5936 | #endif |
| 5937 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5938 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5939 | #endif |
| 5940 | #ifdef _SC_ICACHE_ASSOC |
| 5941 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5942 | #endif |
| 5943 | #ifdef _SC_ICACHE_BLKSZ |
| 5944 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5945 | #endif |
| 5946 | #ifdef _SC_ICACHE_LINESZ |
| 5947 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5948 | #endif |
| 5949 | #ifdef _SC_ICACHE_SZ |
| 5950 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5951 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5952 | #ifdef _SC_INF |
| 5953 | {"SC_INF", _SC_INF}, |
| 5954 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5955 | #ifdef _SC_INT_MAX |
| 5956 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5957 | #endif |
| 5958 | #ifdef _SC_INT_MIN |
| 5959 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5960 | #endif |
| 5961 | #ifdef _SC_IOV_MAX |
| 5962 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5963 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5964 | #ifdef _SC_IP_SECOPTS |
| 5965 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5966 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5967 | #ifdef _SC_JOB_CONTROL |
| 5968 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5969 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5970 | #ifdef _SC_KERN_POINTERS |
| 5971 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5972 | #endif |
| 5973 | #ifdef _SC_KERN_SIM |
| 5974 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5975 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5976 | #ifdef _SC_LINE_MAX |
| 5977 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5978 | #endif |
| 5979 | #ifdef _SC_LOGIN_NAME_MAX |
| 5980 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5981 | #endif |
| 5982 | #ifdef _SC_LOGNAME_MAX |
| 5983 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5984 | #endif |
| 5985 | #ifdef _SC_LONG_BIT |
| 5986 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5987 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5988 | #ifdef _SC_MAC |
| 5989 | {"SC_MAC", _SC_MAC}, |
| 5990 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5991 | #ifdef _SC_MAPPED_FILES |
| 5992 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5993 | #endif |
| 5994 | #ifdef _SC_MAXPID |
| 5995 | {"SC_MAXPID", _SC_MAXPID}, |
| 5996 | #endif |
| 5997 | #ifdef _SC_MB_LEN_MAX |
| 5998 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5999 | #endif |
| 6000 | #ifdef _SC_MEMLOCK |
| 6001 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6002 | #endif |
| 6003 | #ifdef _SC_MEMLOCK_RANGE |
| 6004 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6005 | #endif |
| 6006 | #ifdef _SC_MEMORY_PROTECTION |
| 6007 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6008 | #endif |
| 6009 | #ifdef _SC_MESSAGE_PASSING |
| 6010 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6011 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6012 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6013 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6014 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6015 | #ifdef _SC_MQ_OPEN_MAX |
| 6016 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6017 | #endif |
| 6018 | #ifdef _SC_MQ_PRIO_MAX |
| 6019 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6020 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6021 | #ifdef _SC_NACLS_MAX |
| 6022 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6023 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6024 | #ifdef _SC_NGROUPS_MAX |
| 6025 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6026 | #endif |
| 6027 | #ifdef _SC_NL_ARGMAX |
| 6028 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6029 | #endif |
| 6030 | #ifdef _SC_NL_LANGMAX |
| 6031 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6032 | #endif |
| 6033 | #ifdef _SC_NL_MSGMAX |
| 6034 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6035 | #endif |
| 6036 | #ifdef _SC_NL_NMAX |
| 6037 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6038 | #endif |
| 6039 | #ifdef _SC_NL_SETMAX |
| 6040 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6041 | #endif |
| 6042 | #ifdef _SC_NL_TEXTMAX |
| 6043 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6044 | #endif |
| 6045 | #ifdef _SC_NPROCESSORS_CONF |
| 6046 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6047 | #endif |
| 6048 | #ifdef _SC_NPROCESSORS_ONLN |
| 6049 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6050 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6051 | #ifdef _SC_NPROC_CONF |
| 6052 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6053 | #endif |
| 6054 | #ifdef _SC_NPROC_ONLN |
| 6055 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6056 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6057 | #ifdef _SC_NZERO |
| 6058 | {"SC_NZERO", _SC_NZERO}, |
| 6059 | #endif |
| 6060 | #ifdef _SC_OPEN_MAX |
| 6061 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6062 | #endif |
| 6063 | #ifdef _SC_PAGESIZE |
| 6064 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6065 | #endif |
| 6066 | #ifdef _SC_PAGE_SIZE |
| 6067 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6068 | #endif |
| 6069 | #ifdef _SC_PASS_MAX |
| 6070 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6071 | #endif |
| 6072 | #ifdef _SC_PHYS_PAGES |
| 6073 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6074 | #endif |
| 6075 | #ifdef _SC_PII |
| 6076 | {"SC_PII", _SC_PII}, |
| 6077 | #endif |
| 6078 | #ifdef _SC_PII_INTERNET |
| 6079 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6080 | #endif |
| 6081 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6082 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6083 | #endif |
| 6084 | #ifdef _SC_PII_INTERNET_STREAM |
| 6085 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6086 | #endif |
| 6087 | #ifdef _SC_PII_OSI |
| 6088 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6089 | #endif |
| 6090 | #ifdef _SC_PII_OSI_CLTS |
| 6091 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6092 | #endif |
| 6093 | #ifdef _SC_PII_OSI_COTS |
| 6094 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6095 | #endif |
| 6096 | #ifdef _SC_PII_OSI_M |
| 6097 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6098 | #endif |
| 6099 | #ifdef _SC_PII_SOCKET |
| 6100 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6101 | #endif |
| 6102 | #ifdef _SC_PII_XTI |
| 6103 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6104 | #endif |
| 6105 | #ifdef _SC_POLL |
| 6106 | {"SC_POLL", _SC_POLL}, |
| 6107 | #endif |
| 6108 | #ifdef _SC_PRIORITIZED_IO |
| 6109 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6110 | #endif |
| 6111 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6112 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6113 | #endif |
| 6114 | #ifdef _SC_REALTIME_SIGNALS |
| 6115 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6116 | #endif |
| 6117 | #ifdef _SC_RE_DUP_MAX |
| 6118 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6119 | #endif |
| 6120 | #ifdef _SC_RTSIG_MAX |
| 6121 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6122 | #endif |
| 6123 | #ifdef _SC_SAVED_IDS |
| 6124 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6125 | #endif |
| 6126 | #ifdef _SC_SCHAR_MAX |
| 6127 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6128 | #endif |
| 6129 | #ifdef _SC_SCHAR_MIN |
| 6130 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6131 | #endif |
| 6132 | #ifdef _SC_SELECT |
| 6133 | {"SC_SELECT", _SC_SELECT}, |
| 6134 | #endif |
| 6135 | #ifdef _SC_SEMAPHORES |
| 6136 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6137 | #endif |
| 6138 | #ifdef _SC_SEM_NSEMS_MAX |
| 6139 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6140 | #endif |
| 6141 | #ifdef _SC_SEM_VALUE_MAX |
| 6142 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6143 | #endif |
| 6144 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6145 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6146 | #endif |
| 6147 | #ifdef _SC_SHRT_MAX |
| 6148 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6149 | #endif |
| 6150 | #ifdef _SC_SHRT_MIN |
| 6151 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6152 | #endif |
| 6153 | #ifdef _SC_SIGQUEUE_MAX |
| 6154 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6155 | #endif |
| 6156 | #ifdef _SC_SIGRT_MAX |
| 6157 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6158 | #endif |
| 6159 | #ifdef _SC_SIGRT_MIN |
| 6160 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6161 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6162 | #ifdef _SC_SOFTPOWER |
| 6163 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6164 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6165 | #ifdef _SC_SPLIT_CACHE |
| 6166 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6167 | #endif |
| 6168 | #ifdef _SC_SSIZE_MAX |
| 6169 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6170 | #endif |
| 6171 | #ifdef _SC_STACK_PROT |
| 6172 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6173 | #endif |
| 6174 | #ifdef _SC_STREAM_MAX |
| 6175 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6176 | #endif |
| 6177 | #ifdef _SC_SYNCHRONIZED_IO |
| 6178 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6179 | #endif |
| 6180 | #ifdef _SC_THREADS |
| 6181 | {"SC_THREADS", _SC_THREADS}, |
| 6182 | #endif |
| 6183 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6184 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6185 | #endif |
| 6186 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6187 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6188 | #endif |
| 6189 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6190 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6191 | #endif |
| 6192 | #ifdef _SC_THREAD_KEYS_MAX |
| 6193 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6194 | #endif |
| 6195 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6196 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6197 | #endif |
| 6198 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6199 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6200 | #endif |
| 6201 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6202 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6203 | #endif |
| 6204 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6205 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6206 | #endif |
| 6207 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6208 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6209 | #endif |
| 6210 | #ifdef _SC_THREAD_STACK_MIN |
| 6211 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6212 | #endif |
| 6213 | #ifdef _SC_THREAD_THREADS_MAX |
| 6214 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6215 | #endif |
| 6216 | #ifdef _SC_TIMERS |
| 6217 | {"SC_TIMERS", _SC_TIMERS}, |
| 6218 | #endif |
| 6219 | #ifdef _SC_TIMER_MAX |
| 6220 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6221 | #endif |
| 6222 | #ifdef _SC_TTY_NAME_MAX |
| 6223 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6224 | #endif |
| 6225 | #ifdef _SC_TZNAME_MAX |
| 6226 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6227 | #endif |
| 6228 | #ifdef _SC_T_IOV_MAX |
| 6229 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6230 | #endif |
| 6231 | #ifdef _SC_UCHAR_MAX |
| 6232 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6233 | #endif |
| 6234 | #ifdef _SC_UINT_MAX |
| 6235 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6236 | #endif |
| 6237 | #ifdef _SC_UIO_MAXIOV |
| 6238 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6239 | #endif |
| 6240 | #ifdef _SC_ULONG_MAX |
| 6241 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6242 | #endif |
| 6243 | #ifdef _SC_USHRT_MAX |
| 6244 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6245 | #endif |
| 6246 | #ifdef _SC_VERSION |
| 6247 | {"SC_VERSION", _SC_VERSION}, |
| 6248 | #endif |
| 6249 | #ifdef _SC_WORD_BIT |
| 6250 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6251 | #endif |
| 6252 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6253 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6254 | #endif |
| 6255 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6256 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6257 | #endif |
| 6258 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6259 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6260 | #endif |
| 6261 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6262 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6263 | #endif |
| 6264 | #ifdef _SC_XOPEN_CRYPT |
| 6265 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6266 | #endif |
| 6267 | #ifdef _SC_XOPEN_ENH_I18N |
| 6268 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6269 | #endif |
| 6270 | #ifdef _SC_XOPEN_LEGACY |
| 6271 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6272 | #endif |
| 6273 | #ifdef _SC_XOPEN_REALTIME |
| 6274 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6275 | #endif |
| 6276 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6277 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6278 | #endif |
| 6279 | #ifdef _SC_XOPEN_SHM |
| 6280 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6281 | #endif |
| 6282 | #ifdef _SC_XOPEN_UNIX |
| 6283 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6284 | #endif |
| 6285 | #ifdef _SC_XOPEN_VERSION |
| 6286 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6287 | #endif |
| 6288 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6289 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6290 | #endif |
| 6291 | #ifdef _SC_XOPEN_XPG2 |
| 6292 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6293 | #endif |
| 6294 | #ifdef _SC_XOPEN_XPG3 |
| 6295 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6296 | #endif |
| 6297 | #ifdef _SC_XOPEN_XPG4 |
| 6298 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6299 | #endif |
| 6300 | }; |
| 6301 | |
| 6302 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6303 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6304 | { |
| 6305 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6306 | sizeof(posix_constants_sysconf) |
| 6307 | / sizeof(struct constdef)); |
| 6308 | } |
| 6309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6310 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6311 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6312 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6313 | |
| 6314 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6315 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6316 | { |
| 6317 | PyObject *result = NULL; |
| 6318 | int name; |
| 6319 | |
| 6320 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6321 | int value; |
| 6322 | |
| 6323 | errno = 0; |
| 6324 | value = sysconf(name); |
| 6325 | if (value == -1 && errno != 0) |
| 6326 | posix_error(); |
| 6327 | else |
| 6328 | result = PyInt_FromLong(value); |
| 6329 | } |
| 6330 | return result; |
| 6331 | } |
| 6332 | #endif |
| 6333 | |
| 6334 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6335 | /* This code is used to ensure that the tables of configuration value names |
| 6336 | * are in sorted order as required by conv_confname(), and also to build the |
| 6337 | * the exported dictionaries that are used to publish information about the |
| 6338 | * names available on the host platform. |
| 6339 | * |
| 6340 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6341 | * when used, even for platforms we're not able to test on. It also makes |
| 6342 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6343 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6344 | |
| 6345 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6346 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6347 | { |
| 6348 | const struct constdef *c1 = |
| 6349 | (const struct constdef *) v1; |
| 6350 | const struct constdef *c2 = |
| 6351 | (const struct constdef *) v2; |
| 6352 | |
| 6353 | return strcmp(c1->name, c2->name); |
| 6354 | } |
| 6355 | |
| 6356 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6357 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6358 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6359 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6360 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6361 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6362 | |
| 6363 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6364 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6365 | if (d == NULL) |
| 6366 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6367 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6368 | for (i=0; i < tablesize; ++i) { |
| 6369 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6370 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6371 | Py_XDECREF(o); |
| 6372 | Py_DECREF(d); |
| 6373 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6374 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6375 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6376 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6377 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6378 | } |
| 6379 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6380 | /* Return -1 on failure, 0 on success. */ |
| 6381 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6382 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6383 | { |
| 6384 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6385 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6386 | sizeof(posix_constants_pathconf) |
| 6387 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6388 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6389 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6390 | #endif |
| 6391 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6392 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6393 | sizeof(posix_constants_confstr) |
| 6394 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6395 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6396 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6397 | #endif |
| 6398 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6399 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6400 | sizeof(posix_constants_sysconf) |
| 6401 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6402 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6403 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6404 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6405 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6406 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6407 | |
| 6408 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6409 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6410 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6411 | 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] | 6412 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6413 | |
| 6414 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6415 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6416 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6417 | abort(); |
| 6418 | /*NOTREACHED*/ |
| 6419 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6420 | return NULL; |
| 6421 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6422 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6423 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6424 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6425 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6426 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6427 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6428 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6429 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6430 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6431 | application (if any) its extension is associated.\n\ |
| 6432 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6433 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6434 | \n\ |
| 6435 | startfile returns as soon as the associated application is launched.\n\ |
| 6436 | There is no option to wait for the application to close, and no way\n\ |
| 6437 | to retrieve the application's exit status.\n\ |
| 6438 | \n\ |
| 6439 | The filepath is relative to the current directory. If you want to use\n\ |
| 6440 | 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] | 6441 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6442 | |
| 6443 | static PyObject * |
| 6444 | win32_startfile(PyObject *self, PyObject *args) |
| 6445 | { |
| 6446 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6447 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6448 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6449 | #ifdef Py_WIN_WIDE_FILENAMES |
| 6450 | if (unicode_file_names()) { |
| 6451 | PyObject *unipath, *woperation = NULL; |
| 6452 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6453 | &unipath, &operation)) { |
| 6454 | PyErr_Clear(); |
| 6455 | goto normal; |
| 6456 | } |
| 6457 | |
| 6458 | |
| 6459 | if (operation) { |
| 6460 | woperation = PyUnicode_DecodeASCII(operation, |
| 6461 | strlen(operation), NULL); |
| 6462 | if (!woperation) { |
| 6463 | PyErr_Clear(); |
| 6464 | operation = NULL; |
| 6465 | goto normal; |
| 6466 | } |
| 6467 | } |
| 6468 | |
| 6469 | Py_BEGIN_ALLOW_THREADS |
| 6470 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6471 | PyUnicode_AS_UNICODE(unipath), |
| 6472 | NULL, NULL, SW_SHOWNORMAL); |
| 6473 | Py_END_ALLOW_THREADS |
| 6474 | |
| 6475 | Py_XDECREF(woperation); |
| 6476 | if (rc <= (HINSTANCE)32) { |
| 6477 | PyObject *errval = win32_error_unicode("startfile", |
| 6478 | PyUnicode_AS_UNICODE(unipath)); |
| 6479 | return errval; |
| 6480 | } |
| 6481 | Py_INCREF(Py_None); |
| 6482 | return Py_None; |
| 6483 | } |
| 6484 | #endif |
| 6485 | |
| 6486 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6487 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 6488 | Py_FileSystemDefaultEncoding, &filepath, |
| 6489 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6490 | return NULL; |
| 6491 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6492 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6493 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6494 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6495 | if (rc <= (HINSTANCE)32) { |
| 6496 | PyObject *errval = win32_error("startfile", filepath); |
| 6497 | PyMem_Free(filepath); |
| 6498 | return errval; |
| 6499 | } |
| 6500 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6501 | Py_INCREF(Py_None); |
| 6502 | return Py_None; |
| 6503 | } |
| 6504 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6505 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6506 | #ifdef HAVE_GETLOADAVG |
| 6507 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6508 | "getloadavg() -> (float, float, float)\n\n\ |
| 6509 | Return the number of processes in the system run queue averaged over\n\ |
| 6510 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6511 | was unobtainable"); |
| 6512 | |
| 6513 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6514 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6515 | { |
| 6516 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6517 | if (getloadavg(loadavg, 3)!=3) { |
| 6518 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6519 | return NULL; |
| 6520 | } else |
| 6521 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6522 | } |
| 6523 | #endif |
| 6524 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6525 | #ifdef MS_WINDOWS |
| 6526 | |
| 6527 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6528 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6529 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6530 | |
| 6531 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6532 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6533 | DWORD dwFlags ); |
| 6534 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6535 | BYTE *pbBuffer ); |
| 6536 | |
| 6537 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 6538 | static HCRYPTPROV hCryptProv = 0; |
| 6539 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6540 | static PyObject* |
| 6541 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6542 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6543 | int howMany; |
| 6544 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6545 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6546 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6547 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6548 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6549 | if (howMany < 0) |
| 6550 | return PyErr_Format(PyExc_ValueError, |
| 6551 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6552 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6553 | if (hCryptProv == 0) { |
| 6554 | HINSTANCE hAdvAPI32 = NULL; |
| 6555 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6556 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6557 | /* Obtain handle to the DLL containing CryptoAPI |
| 6558 | This should not fail */ |
| 6559 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6560 | if(hAdvAPI32 == NULL) |
| 6561 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6562 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6563 | /* Obtain pointers to the CryptoAPI functions |
| 6564 | This will fail on some early versions of Win95 */ |
| 6565 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6566 | hAdvAPI32, |
| 6567 | "CryptAcquireContextA"); |
| 6568 | if (pCryptAcquireContext == NULL) |
| 6569 | return PyErr_Format(PyExc_NotImplementedError, |
| 6570 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6571 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6572 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6573 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6574 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6575 | return PyErr_Format(PyExc_NotImplementedError, |
| 6576 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6577 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6578 | /* Acquire context */ |
| 6579 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6580 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6581 | return win32_error("CryptAcquireContext", NULL); |
| 6582 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6583 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6584 | /* Allocate bytes */ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6585 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6586 | if (result != NULL) { |
| 6587 | /* Get random data */ |
| 6588 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6589 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6590 | Py_DECREF(result); |
| 6591 | return win32_error("CryptGenRandom", NULL); |
| 6592 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6593 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6594 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6595 | } |
| 6596 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6597 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6598 | PyDoc_STRVAR(device_encoding__doc__, |
| 6599 | "device_encoding(fd) -> str\n\n\ |
| 6600 | Return a string describing the encoding of the device\n\ |
| 6601 | if the output is a terminal; else return None."); |
| 6602 | |
| 6603 | static PyObject * |
| 6604 | device_encoding(PyObject *self, PyObject *args) |
| 6605 | { |
| 6606 | int fd; |
| 6607 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6608 | return NULL; |
| 6609 | if (!isatty(fd)) { |
| 6610 | Py_INCREF(Py_None); |
| 6611 | return Py_None; |
| 6612 | } |
| 6613 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6614 | if (fd == 0) { |
| 6615 | char buf[100]; |
| 6616 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6617 | return PyUnicode_FromString(buf); |
| 6618 | } |
| 6619 | if (fd == 1 || fd == 2) { |
| 6620 | char buf[100]; |
| 6621 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6622 | return PyUnicode_FromString(buf); |
| 6623 | } |
| 6624 | #elif defined(CODESET) |
| 6625 | { |
| 6626 | char *codeset = nl_langinfo(CODESET); |
| 6627 | if (codeset) |
| 6628 | return PyUnicode_FromString(codeset); |
| 6629 | } |
| 6630 | #endif |
| 6631 | Py_INCREF(Py_None); |
| 6632 | return Py_None; |
| 6633 | } |
| 6634 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6635 | #ifdef __VMS |
| 6636 | /* Use openssl random routine */ |
| 6637 | #include <openssl/rand.h> |
| 6638 | PyDoc_STRVAR(vms_urandom__doc__, |
| 6639 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6640 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6641 | |
| 6642 | static PyObject* |
| 6643 | vms_urandom(PyObject *self, PyObject *args) |
| 6644 | { |
| 6645 | int howMany; |
| 6646 | PyObject* result; |
| 6647 | |
| 6648 | /* Read arguments */ |
| 6649 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 6650 | return NULL; |
| 6651 | if (howMany < 0) |
| 6652 | return PyErr_Format(PyExc_ValueError, |
| 6653 | "negative argument not allowed"); |
| 6654 | |
| 6655 | /* Allocate bytes */ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6656 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6657 | if (result != NULL) { |
| 6658 | /* Get random data */ |
| 6659 | if (RAND_pseudo_bytes((unsigned char*) |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6660 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6661 | howMany) < 0) { |
| 6662 | Py_DECREF(result); |
| 6663 | return PyErr_Format(PyExc_ValueError, |
| 6664 | "RAND_pseudo_bytes"); |
| 6665 | } |
| 6666 | } |
| 6667 | return result; |
| 6668 | } |
| 6669 | #endif |
| 6670 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6671 | static PyMethodDef posix_methods[] = { |
| 6672 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6673 | #ifdef HAVE_TTYNAME |
| 6674 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6675 | #endif |
| 6676 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6677 | #ifdef HAVE_CHFLAGS |
| 6678 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 6679 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6680 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6681 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6682 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6683 | #endif /* HAVE_CHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6684 | #ifdef HAVE_LCHFLAGS |
| 6685 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 6686 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6687 | #ifdef HAVE_LCHOWN |
| 6688 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6689 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6690 | #ifdef HAVE_CHROOT |
| 6691 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6692 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6693 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6694 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6695 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6696 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6697 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6698 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6699 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6700 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6701 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6702 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6703 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6704 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6705 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6706 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6707 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6708 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6709 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6710 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6711 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6712 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6713 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6714 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6715 | {"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] | 6716 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6717 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6718 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6719 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6720 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6721 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6722 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6723 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6724 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6725 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6726 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6727 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6728 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6729 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6730 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6731 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6732 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6733 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6734 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6735 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6736 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6737 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6738 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6739 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6740 | #if defined(PYOS_OS2) |
| 6741 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 6742 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 6743 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6744 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6745 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6746 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6747 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6748 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6749 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6750 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6751 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6752 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6753 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6754 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6755 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6756 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6757 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6758 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6759 | #endif /* HAVE_GETEGID */ |
| 6760 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6761 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6762 | #endif /* HAVE_GETEUID */ |
| 6763 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6764 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6765 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6766 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6767 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6768 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6769 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6770 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6771 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6772 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6773 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6774 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6775 | #endif /* HAVE_GETPPID */ |
| 6776 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6777 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6778 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6779 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6780 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6781 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6782 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6783 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6784 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6785 | #ifdef HAVE_KILLPG |
| 6786 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6787 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6788 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6789 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6790 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6791 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6792 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6793 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6794 | #ifdef HAVE_SETEUID |
| 6795 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6796 | #endif /* HAVE_SETEUID */ |
| 6797 | #ifdef HAVE_SETEGID |
| 6798 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6799 | #endif /* HAVE_SETEGID */ |
| 6800 | #ifdef HAVE_SETREUID |
| 6801 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6802 | #endif /* HAVE_SETREUID */ |
| 6803 | #ifdef HAVE_SETREGID |
| 6804 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6805 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6806 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6807 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6808 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6809 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6810 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6811 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6812 | #ifdef HAVE_GETPGID |
| 6813 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 6814 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6815 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6816 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6817 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6818 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6819 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6820 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6821 | #ifdef HAVE_WAIT3 |
| 6822 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 6823 | #endif /* HAVE_WAIT3 */ |
| 6824 | #ifdef HAVE_WAIT4 |
| 6825 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 6826 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6827 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6828 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6829 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6830 | #ifdef HAVE_GETSID |
| 6831 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 6832 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6833 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6834 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6835 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6836 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6837 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6838 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6839 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6840 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6841 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6842 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6843 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6844 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6845 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6846 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6847 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6848 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6849 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6850 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6851 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6852 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6853 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6854 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6855 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6856 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6857 | #endif |
| 6858 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6859 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6860 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6861 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6862 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 6863 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6864 | #ifdef HAVE_DEVICE_MACROS |
| 6865 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 6866 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 6867 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 6868 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6869 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6870 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6871 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6872 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6873 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6874 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6875 | #ifdef HAVE_UNSETENV |
| 6876 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6877 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6878 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6879 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6880 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6881 | #ifdef HAVE_FCHDIR |
| 6882 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 6883 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6884 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6885 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6886 | #endif |
| 6887 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6888 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6889 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6890 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6891 | #ifdef WCOREDUMP |
| 6892 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 6893 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6894 | #ifdef WIFCONTINUED |
| 6895 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 6896 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6897 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6898 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6899 | #endif /* WIFSTOPPED */ |
| 6900 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6901 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6902 | #endif /* WIFSIGNALED */ |
| 6903 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6904 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6905 | #endif /* WIFEXITED */ |
| 6906 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6907 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6908 | #endif /* WEXITSTATUS */ |
| 6909 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6910 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6911 | #endif /* WTERMSIG */ |
| 6912 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6913 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6914 | #endif /* WSTOPSIG */ |
| 6915 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6916 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6917 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6918 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6919 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6920 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6921 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 6922 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6923 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6924 | #endif |
| 6925 | #ifdef HAVE_TEMPNAM |
| 6926 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 6927 | #endif |
| 6928 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6929 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6930 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6931 | #ifdef HAVE_CONFSTR |
| 6932 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 6933 | #endif |
| 6934 | #ifdef HAVE_SYSCONF |
| 6935 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 6936 | #endif |
| 6937 | #ifdef HAVE_FPATHCONF |
| 6938 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 6939 | #endif |
| 6940 | #ifdef HAVE_PATHCONF |
| 6941 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 6942 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6943 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6944 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 6945 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 6946 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6947 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6948 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6949 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6950 | #ifdef MS_WINDOWS |
| 6951 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 6952 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6953 | #ifdef __VMS |
| 6954 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 6955 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6956 | {NULL, NULL} /* Sentinel */ |
| 6957 | }; |
| 6958 | |
| 6959 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6960 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6961 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6962 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6963 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6964 | } |
| 6965 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6966 | #if defined(PYOS_OS2) |
| 6967 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6968 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6969 | { |
| 6970 | APIRET rc; |
| 6971 | ULONG values[QSV_MAX+1]; |
| 6972 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 6973 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6974 | |
| 6975 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 6976 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6977 | Py_END_ALLOW_THREADS |
| 6978 | |
| 6979 | if (rc != NO_ERROR) { |
| 6980 | os2_error(rc); |
| 6981 | return -1; |
| 6982 | } |
| 6983 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6984 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 6985 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 6986 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 6987 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 6988 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 6989 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 6990 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6991 | |
| 6992 | switch (values[QSV_VERSION_MINOR]) { |
| 6993 | case 0: ver = "2.00"; break; |
| 6994 | case 10: ver = "2.10"; break; |
| 6995 | case 11: ver = "2.11"; break; |
| 6996 | case 30: ver = "3.00"; break; |
| 6997 | case 40: ver = "4.00"; break; |
| 6998 | case 50: ver = "5.00"; break; |
| 6999 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7000 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7001 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7002 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7003 | ver = &tmp[0]; |
| 7004 | } |
| 7005 | |
| 7006 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7007 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7008 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7009 | |
| 7010 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7011 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7012 | tmp[1] = ':'; |
| 7013 | tmp[2] = '\0'; |
| 7014 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7015 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7016 | } |
| 7017 | #endif |
| 7018 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7019 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7020 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7021 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7022 | #ifdef F_OK |
| 7023 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7024 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7025 | #ifdef R_OK |
| 7026 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7027 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7028 | #ifdef W_OK |
| 7029 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7030 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7031 | #ifdef X_OK |
| 7032 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7033 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7034 | #ifdef NGROUPS_MAX |
| 7035 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7036 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7037 | #ifdef TMP_MAX |
| 7038 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7039 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7040 | #ifdef WCONTINUED |
| 7041 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7042 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7043 | #ifdef WNOHANG |
| 7044 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7045 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7046 | #ifdef WUNTRACED |
| 7047 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7048 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7049 | #ifdef O_RDONLY |
| 7050 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7051 | #endif |
| 7052 | #ifdef O_WRONLY |
| 7053 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7054 | #endif |
| 7055 | #ifdef O_RDWR |
| 7056 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7057 | #endif |
| 7058 | #ifdef O_NDELAY |
| 7059 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7060 | #endif |
| 7061 | #ifdef O_NONBLOCK |
| 7062 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7063 | #endif |
| 7064 | #ifdef O_APPEND |
| 7065 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7066 | #endif |
| 7067 | #ifdef O_DSYNC |
| 7068 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7069 | #endif |
| 7070 | #ifdef O_RSYNC |
| 7071 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7072 | #endif |
| 7073 | #ifdef O_SYNC |
| 7074 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7075 | #endif |
| 7076 | #ifdef O_NOCTTY |
| 7077 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7078 | #endif |
| 7079 | #ifdef O_CREAT |
| 7080 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7081 | #endif |
| 7082 | #ifdef O_EXCL |
| 7083 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7084 | #endif |
| 7085 | #ifdef O_TRUNC |
| 7086 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7087 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7088 | #ifdef O_BINARY |
| 7089 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7090 | #endif |
| 7091 | #ifdef O_TEXT |
| 7092 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7093 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7094 | #ifdef O_LARGEFILE |
| 7095 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7096 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7097 | #ifdef O_SHLOCK |
| 7098 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7099 | #endif |
| 7100 | #ifdef O_EXLOCK |
| 7101 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7102 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7103 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7104 | /* MS Windows */ |
| 7105 | #ifdef O_NOINHERIT |
| 7106 | /* Don't inherit in child processes. */ |
| 7107 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7108 | #endif |
| 7109 | #ifdef _O_SHORT_LIVED |
| 7110 | /* Optimize for short life (keep in memory). */ |
| 7111 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7112 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7113 | #endif |
| 7114 | #ifdef O_TEMPORARY |
| 7115 | /* Automatically delete when last handle is closed. */ |
| 7116 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7117 | #endif |
| 7118 | #ifdef O_RANDOM |
| 7119 | /* Optimize for random access. */ |
| 7120 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7121 | #endif |
| 7122 | #ifdef O_SEQUENTIAL |
| 7123 | /* Optimize for sequential access. */ |
| 7124 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7125 | #endif |
| 7126 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7127 | /* GNU extensions. */ |
| 7128 | #ifdef O_DIRECT |
| 7129 | /* Direct disk access. */ |
| 7130 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7131 | #endif |
| 7132 | #ifdef O_DIRECTORY |
| 7133 | /* Must be a directory. */ |
| 7134 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7135 | #endif |
| 7136 | #ifdef O_NOFOLLOW |
| 7137 | /* Do not follow links. */ |
| 7138 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7139 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7140 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7141 | /* These come from sysexits.h */ |
| 7142 | #ifdef EX_OK |
| 7143 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7144 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7145 | #ifdef EX_USAGE |
| 7146 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7147 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7148 | #ifdef EX_DATAERR |
| 7149 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7150 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7151 | #ifdef EX_NOINPUT |
| 7152 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7153 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7154 | #ifdef EX_NOUSER |
| 7155 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7156 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7157 | #ifdef EX_NOHOST |
| 7158 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7159 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7160 | #ifdef EX_UNAVAILABLE |
| 7161 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7162 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7163 | #ifdef EX_SOFTWARE |
| 7164 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7165 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7166 | #ifdef EX_OSERR |
| 7167 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7168 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7169 | #ifdef EX_OSFILE |
| 7170 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7171 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7172 | #ifdef EX_CANTCREAT |
| 7173 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7174 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7175 | #ifdef EX_IOERR |
| 7176 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7177 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7178 | #ifdef EX_TEMPFAIL |
| 7179 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7180 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7181 | #ifdef EX_PROTOCOL |
| 7182 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7183 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7184 | #ifdef EX_NOPERM |
| 7185 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7186 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7187 | #ifdef EX_CONFIG |
| 7188 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7189 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7190 | #ifdef EX_NOTFOUND |
| 7191 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7192 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7193 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7194 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7195 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7196 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7197 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7198 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7199 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7200 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7201 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7202 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7203 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7204 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7205 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7206 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7207 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7208 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7209 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7210 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7211 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7212 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7213 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7214 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7215 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7216 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7217 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7218 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7219 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7220 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7221 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7222 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7223 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7224 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7225 | #if defined(PYOS_OS2) |
| 7226 | if (insertvalues(d)) return -1; |
| 7227 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7228 | return 0; |
| 7229 | } |
| 7230 | |
| 7231 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7232 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7233 | #define INITFUNC initnt |
| 7234 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7235 | |
| 7236 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7237 | #define INITFUNC initos2 |
| 7238 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7239 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7240 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7241 | #define INITFUNC initposix |
| 7242 | #define MODNAME "posix" |
| 7243 | #endif |
| 7244 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7245 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7246 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7247 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7248 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7249 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7250 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7251 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7252 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7253 | if (m == NULL) |
| 7254 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7255 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7256 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7257 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7258 | Py_XINCREF(v); |
| 7259 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7260 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7261 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7262 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7263 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7264 | return; |
| 7265 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7266 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7267 | return; |
| 7268 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7269 | Py_INCREF(PyExc_OSError); |
| 7270 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7271 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7272 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7273 | if (posix_putenv_garbage == NULL) |
| 7274 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7275 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7276 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7277 | if (!initialized) { |
| 7278 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7279 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7280 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7281 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7282 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7283 | structseq_new = StatResultType.tp_new; |
| 7284 | StatResultType.tp_new = statresult_new; |
| 7285 | |
| 7286 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7287 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 7288 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7289 | Py_INCREF((PyObject*) &StatResultType); |
| 7290 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7291 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7292 | PyModule_AddObject(m, "statvfs_result", |
| 7293 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7294 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7295 | |
| 7296 | #ifdef __APPLE__ |
| 7297 | /* |
| 7298 | * Step 2 of weak-linking support on Mac OS X. |
| 7299 | * |
| 7300 | * The code below removes functions that are not available on the |
| 7301 | * currently active platform. |
| 7302 | * |
| 7303 | * This block allow one to use a python binary that was build on |
| 7304 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7305 | * OSX 10.4. |
| 7306 | */ |
| 7307 | #ifdef HAVE_FSTATVFS |
| 7308 | if (fstatvfs == NULL) { |
| 7309 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 7310 | return; |
| 7311 | } |
| 7312 | } |
| 7313 | #endif /* HAVE_FSTATVFS */ |
| 7314 | |
| 7315 | #ifdef HAVE_STATVFS |
| 7316 | if (statvfs == NULL) { |
| 7317 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 7318 | return; |
| 7319 | } |
| 7320 | } |
| 7321 | #endif /* HAVE_STATVFS */ |
| 7322 | |
| 7323 | # ifdef HAVE_LCHOWN |
| 7324 | if (lchown == NULL) { |
| 7325 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 7326 | return; |
| 7327 | } |
| 7328 | } |
| 7329 | #endif /* HAVE_LCHOWN */ |
| 7330 | |
| 7331 | |
| 7332 | #endif /* __APPLE__ */ |
| 7333 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7334 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7335 | |
| 7336 | #ifdef __cplusplus |
| 7337 | } |
| 7338 | #endif |